microwave simulator

Dependencies:   C12832_lcd LM75B mbed

Revision:
1:fac01d40d819
Parent:
0:2f825861a3a2
Child:
2:9f6bdaddefff
diff -r 2f825861a3a2 -r fac01d40d819 main.cpp
--- a/main.cpp	Fri Oct 18 20:41:08 2013 +0000
+++ b/main.cpp	Fri Oct 18 21:50:14 2013 +0000
@@ -5,13 +5,16 @@
 #include "keypad.h"
 
 // uncomment to allow DEBUG
-#define DEBUG
+//#define DEBUG
 
 #define BUTTON_LEFT     (p13)
 #define BUTTON_RIGHT    (p16)
 #define BUTTON_START    (p15)
 #define BUTTON_STOP     (p12)
 #define BUTTON_DOOR     (p14)
+#define TEMP_SCL        (p27)
+#define TEMP_SDA        (p28)
+#define ALARM           (p26)
 
 #define DOOR_CLOSED     (0)
 #define DOOR_OPEN       (1)
@@ -28,8 +31,6 @@
 #define MAX_TICKER_TIME         (60)
 #define WDOG_TIME               (1)
 
-#define WHILE_LOOP_INTERVAL_MS  (5)     // msec
-
 #define TEMP_INCREMENT          (10.0f) // degC
 
 typedef enum
@@ -64,7 +65,7 @@
                BUTTON_START, press_start,
                BUTTON_STOP, press_stop,
                BUTTON_DOOR, press_door );
-LM75B tmp(p28,p27);                         // temperature sensor
+LM75B tmp(TEMP_SDA,TEMP_SCL);              // temperature sensor
 
 
 // output peripherals
@@ -74,7 +75,7 @@
 #ifdef DEBUG
   DigitalOut debugLED(LED4);        // general debug LED
 #endif /* DEBUG */
-PwmOut alarm(p26);  
+PwmOut alarm(ALARM);                // alarm
 
 // Timers
 Ticker wdog;            // 1sec watchdog
@@ -90,25 +91,25 @@
 volatile unsigned char start_requested;
 volatile unsigned char stop_requested;
 volatile unsigned char temp_ticker;
+unsigned char reset_wdog;
 #ifdef DEBUG
   unsigned long ulErrorCode = 0UL;
 #endif /* DEBUG */
 
 int main()
 {
-    unsigned char while_loop_counter = 0;
+    unsigned int while_loop_counter = 0;
     
     init_microwave();
     
     while(1)
     {
-        wait_ms(WHILE_LOOP_INTERVAL_MS);
         while_loop_counter++;
         
         microwave_sm(); // execute state machine
         
         // do not need to update LCD as often
-        if ( while_loop_counter & 0x20 )
+        if ( while_loop_counter & 0x4000 )
         {
             update_output();
         }
@@ -123,6 +124,7 @@
     opStatus = STATUS_IDLE;
     start_requested = FALSE;
     stop_requested = FALSE;
+    reset_wdog = FALSE;
 }
 
 void press_left(void)
@@ -200,7 +202,7 @@
         // reset wdog
         if ( ssCookingTime )
         {
-            wdog.attach(wdog_update, 1);
+            reset_wdog = TRUE;
         }
         
         if ( temp_ticker )
@@ -215,9 +217,9 @@
     switch ( opStatus )
     {
     case STATUS_IDLE:
-        fTemp = tmp.read();
         if ( ssCookingTime && !ucDoorStatus && start_requested )
         {
+            fTemp = tmp.read();
             start_requested = FALSE;
             wdog.attach(wdog_update, 1);
             temp_ticker = (ssCookingTime > MAX_TICKER_TIME) ? MAX_TICKER_TIME : ssCookingTime;
@@ -254,11 +256,16 @@
             {
                 opStatus = STATUS_DONE;
             }
-            else if ( !temp_ticker )
+            if ( !temp_ticker )
             {
                 fTemp += TEMP_INCREMENT;
                 temp_ticker = (ssCookingTime > MAX_TICKER_TIME) ? MAX_TICKER_TIME : ssCookingTime;
             }
+            if ( reset_wdog )
+            {
+                wdog.attach(wdog_update, 1);
+                reset_wdog = FALSE;
+            }
         }
         break;
         
@@ -281,6 +288,7 @@
 void update_output(void)
 {
     static unsigned char toggle_alarm = 0;
+    static unsigned char toggle_leds = 0;
     static etSTATUS previousState = (etSTATUS) 0xFF;
     
     // clear the screen only when we transition states to prevent flicker
@@ -306,8 +314,12 @@
         lcd.printf("Temp: %.2f degC\n", fTemp);
         
         // update LEDs
-        cooking_status = !cooking_status;
-        carousel_status = !carousel_status;
+        toggle_leds++;
+        if ( toggle_leds & 0x10 )
+        {
+            cooking_status = !cooking_status;
+            carousel_status = !carousel_status;
+        }
         
         // alarm should be off in this state
         toggle_alarm = 0;
@@ -321,7 +333,7 @@
 
         // sound alarm
         toggle_alarm++;
-        if ( toggle_alarm & 8 )
+        if ( toggle_alarm & 0x10 )
         {
             alarm.period(1.0/5000);
             alarm = 0.5;