diff main.c @ 310:0a64532c1de1

Fill out more main.c structure Add onewire files
author Matt Johnston <matt@ucc.asn.au>
date Wed, 09 May 2012 00:22:28 +0800
parents 49e83333e546
children 011160e5b1ce
line wrap: on
line diff
--- a/main.c	Tue May 08 22:51:38 2012 +0800
+++ b/main.c	Wed May 09 00:22:28 2012 +0800
@@ -21,7 +21,9 @@
 
 // 1 second. we have 1024 prescaler, 32768 crystal.
 #define SLEEP_COMPARE 32
-#define SECONDS_WAKE 60
+#define MEASURE_WAKE 60
+
+#define COMMS_WAKE 3600
 
 #define BAUD 9600
 #define UBRR ((F_CPU)/16/(BAUD)-1)
@@ -33,12 +35,18 @@
 static uint8_t n_measurements;
 static uint8_t measurements[500];
 
+// boolean flags
 static uint8_t need_measurement;
+static uint8_t need_comms;
+static uint8_t comms_done;
 
 static uint8_t readpos;
 static char readbuf[30];
 
 static uint8_t sec_count;
+static uint16_t bt_count;
+
+static void deep_sleep();
 
 static void 
 uart_init(unsigned int ubrr)
@@ -82,6 +90,7 @@
 static void
 cmd_btoff()
 {
+	comms_done = 1;
 }
 
 static void
@@ -127,12 +136,19 @@
 
 ISR(TIMER2_COMPA_vect)
 {
-    sec_count ++;
-    if (sec_count == SECONDS_WAKE)
+    measure_count ++;
+	comms_count ++;
+    if (measure_count == MEASURE_WAKE)
     {
-        sec_count = 0;
+        measure_count = 0;
         need_measurement = 1;
     }
+
+	if (comms_count == COMMS_WAKE)
+	{
+		comms_count = 0;
+		need_comms = 1;
+	}
 }
 
 DWORD get_fattime (void)
@@ -152,11 +168,42 @@
 }
 
 static void
+idle_sleep()
+{
+    set_sleep_mode(SLEEP_MODE_IDLE);
+    sleep_mode();
+}
+
+static void
 do_measurement()
 {
     need_measurement = 0;
 }
 
+static void
+do_comms()
+{
+	need_comms = 0;
+
+	// turn on bluetooth
+	// turn on serial (interrupts)
+	
+	// write sd card here? same 3.3v regulator...
+	
+	comms_done = 0;
+	for (;;)
+	{
+		if (comms_done)
+		{
+			break;
+		}
+		idle_sleep();
+	}
+
+	// turn off bluetooth
+	// turn off serial (interrupts)
+}
+
 int main(void)
 {
     uart_init(UBRR);
@@ -177,7 +224,16 @@
         if (need_measurement)
         {
             do_measurement();
+			continue;
         }
+
+        if (need_comms)
+        {
+            do_comms();
+			continue;
+        }
+
+		deep_sleep();
     }
     return 0;   /* never reached */
 }