changeset 351:2aed800aa2e9

merge
author Matt Johnston <matt@ucc.asn.au>
date Sat, 23 Jun 2012 23:37:36 +0800
parents e7f070855a22 (diff) 03322c38b4a9 (current diff)
children 99f8b97a9449 20f7161399a8 a1aa4176ca2c
files web/log.py
diffstat 3 files changed, 61 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Sat Jun 23 23:37:01 2012 +0800
+++ b/main.c	Sat Jun 23 23:37:36 2012 +0800
@@ -24,15 +24,15 @@
 // 1 second. we have 1024 prescaler, 32768 crystal.
 #define SLEEP_COMPARE 32
 // limited to uint16_t
-#define MEASURE_WAKE 20
+#define MEASURE_WAKE 5 // testing 
 
 #define VALUE_NOSENSOR -9000
 #define VALUE_BROKEN -8000
 
 // limited to uint16_t
-#define COMMS_WAKE 3600
+#define COMMS_WAKE 40 // XXX testing
 // limited to uint8_t
-#define WAKE_SECS 250 // XXX testing
+#define WAKE_SECS 30 // XXX testing
 
 #define BAUD 19200
 #define UBRR ((F_CPU)/8/(BAUD)-1)
@@ -126,10 +126,10 @@
     DDR_LED |= _BV(PIN_LED);
     DDR_SHDN |= _BV(PIN_SHDN);
 
-	// INT0 setup
-	EIMSK = _BV(INT0);
-	// set pullup
-	PORTD |= _BV(PD2);
+    // INT0 setup
+    EIMSK = _BV(INT0);
+    // set pullup
+    PORTD |= _BV(PD2);
 }
 
 static void
@@ -141,7 +141,7 @@
     }
     else
     {
-        //PORT_SHDN |= _BV(PIN_SHDN);
+        PORT_SHDN |= _BV(PIN_SHDN);
     }
 }
 
@@ -179,7 +179,7 @@
     UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
     //8N1
     UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
-	uart_enabled = 1;
+    uart_enabled = 1;
 }
 
 static void 
@@ -187,7 +187,7 @@
 {
     // Turn of interrupts and disable tx/rx
     UCSR0B = 0;
-	uart_enabled = 0;
+    uart_enabled = 0;
 
     // Power reduction register
     //PRR |= _BV(PRUSART0);
@@ -196,10 +196,10 @@
 int 
 uart_putchar(char c, FILE *stream)
 {
-	if (!uart_enabled)
-	{
-		return EOF;
-	}
+    if (!uart_enabled)
+    {
+        return EOF;
+    }
     // XXX could perhaps sleep in the loop for power.
     if (c == '\n')
     {
@@ -376,21 +376,21 @@
 static void
 cmd_add_all()
 {
-	uint8_t id[OW_ROMCODE_SIZE];
+    uint8_t id[OW_ROMCODE_SIZE];
     printf_P("Adding all\n");
     ow_reset();
-	for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
-	{
-		diff = ow_rom_search( diff, &id[0] );
-		if( diff == OW_PRESENCE_ERR ) {
-			printf_P( PSTR("No Sensor found\r") );
-			return;
-		}
-		
-		if( diff == OW_DATA_ERR ) {
-			printf_P( PSTR("Bus Error\r") );
-			return;
-		}
+    for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
+    {
+        diff = ow_rom_search( diff, &id[0] );
+        if( diff == OW_PRESENCE_ERR ) {
+            printf_P( PSTR("No Sensor found\r") );
+            return;
+        }
+        
+        if( diff == OW_DATA_ERR ) {
+            printf_P( PSTR("Bus Error\r") );
+            return;
+        }
         add_sensor(id);
     }
 }
@@ -464,21 +464,10 @@
 
 ISR(INT0_vect)
 {
-	need_comms = 1;
-    blink();
-    _delay_ms(100);
-    blink();
-    _delay_ms(100);
+    need_comms = 1;
     blink();
     _delay_ms(100);
     blink();
-    _delay_ms(100);
-    blink();
-    _delay_ms(100);
-    blink();
-    _delay_ms(100);
-    blink();
-    _delay_ms(100);
 }
 
 
@@ -512,14 +501,13 @@
 {
     TCNT2 = 0;
     measure_count ++;
-	comms_count ++;
+    comms_count ++;
 
     clock_epoch ++;
 
     if (comms_timeout != 0)
     {
-        // XXX testing
-        //comms_timeout--;
+        comms_timeout--;
     }
 
     if (measure_count >= MEASURE_WAKE)
@@ -528,11 +516,11 @@
         need_measurement = 1;
     }
 
-	if (comms_count >= COMMS_WAKE)
-	{
-		comms_count = 0;
-		need_comms = 1;
-	}
+    if (comms_count >= COMMS_WAKE)
+    {
+        comms_count = 0;
+        need_comms = 1;
+    }
 
 }
 
@@ -669,29 +657,31 @@
 static void
 do_comms()
 {
-	// turn on bluetooth
+    // turn on bluetooth
     set_aux_power(1);
     uart_on();
-	
-	// write sd card here? same 3.3v regulator...
-	
-	for (comms_timeout = WAKE_SECS; comms_timeout > 0;  )
-	{
+    
+    // write sd card here? same 3.3v regulator...
+    
+    for (comms_timeout = WAKE_SECS; comms_timeout > 0;  )
+    {
         if (need_measurement)
         {
             need_measurement = 0;
             do_measurement();
+            continue;
         }
 
         if (have_cmd)
         {
             have_cmd = 0;
             read_handler();
+            continue;
         }
 
         // wait for commands from the master
-		idle_sleep();
-	}
+        idle_sleep();
+    }
 
     uart_off();
     set_aux_power(0);
@@ -758,17 +748,17 @@
         {
             need_measurement = 0;
             do_measurement();
-			continue;
+            continue;
         }
 
         if (need_comms)
         {
             need_comms = 0;
             do_comms();
-			continue;
+            continue;
         }
 
-		deep_sleep();
+        deep_sleep();
         blink();
     }
 
--- a/server/ts.py	Sat Jun 23 23:37:01 2012 +0800
+++ b/server/ts.py	Sat Jun 23 23:37:36 2012 +0800
@@ -1,14 +1,14 @@
 #!/usr/bin/env python2.7
 
 BTADDR = "00:12:03:27:70:88"
-SLEEP_TIME = 180
+SLEEP_TIME = 5
 # time that the bluetooth takes to get going?
 EXTRA_WAKEUP = 0
 
 FETCH_TRIES = 3
 
 # avoid turning off the bluetooth etc.
-TESTING = True
+TESTING = False
 
 import sys
 # for wrt
@@ -97,7 +97,7 @@
 @retry()
 def turn_off(sock):
     if TESTING:
-        return None
+        return 99
     sock.send("btoff\n");
     # read newline
     l = readline(sock)
@@ -105,9 +105,11 @@
         print>>sys.stderr, "Bad response to btoff\n"
         return None
 
+    if not l.startswith('off:'):
+        print>>sys.stderr, "Bad response to btoff '%s'\n" % l
+        return None
     off, next_wake = l.rstrip().split(':')
-    if off != 'Off':
-        print>>sys.stderr, "Bad response to btoff '%s'\n" % l
+    print>>sys.stderr, "Next wake %s" % next_wake
 
     return int(next_wake)
 
@@ -167,7 +169,7 @@
         time.sleep(length)
 
 def main():
-	next_wake_time = 0
+    next_wake_time = 0
 
     while True:
         sock = None
@@ -181,15 +183,14 @@
             next_wake = None
             try:
                 next_wake_interval = do_comms(sock)
-                next_wake_time = time.now() + next_wake_interval
+                next_wake_time = time.time() + next_wake_interval
             except Exception, e:
                 print>>sys.stderr, "Error in do_comms:"
                 traceback.print_exc(file=sys.stderr)
-            if next_wake_time > time.now():
-                sleep_time = min(next_wake_time - time.now() - EXTRA_WAKEUP, sleep_time)
+            if next_wake_time > time.time():
+                sleep_time = min(next_wake_time - time.time() - EXTRA_WAKEUP, sleep_time)
 
-        if TESTING:
-            print "Sleeping for %d" % sleep_time
+        print "Sleeping for %d" % sleep_time
         sleep_for(sleep_time)
 
 if __name__ == '__main__':
--- a/web/log.py	Sat Jun 23 23:37:01 2012 +0800
+++ b/web/log.py	Sat Jun 23 23:37:36 2012 +0800
@@ -8,6 +8,7 @@
 import tempfile
 import time
 import syslog
+import sqlite3
 from colorsys import hls_to_rgb
 
 import config