homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Revision:
76:74c454c9d75b
Parent:
75:c2894d531f42
Child:
77:73e4fd83642f
--- a/main.cpp	Thu Sep 12 00:42:54 2013 +0000
+++ b/main.cpp	Thu Sep 12 02:18:59 2013 +0000
@@ -7,6 +7,7 @@
     gotchyas
     1. using pc.printf inside a ticker routine will freeze the routine.
     2. using queues (get, put) will not work within a ticker routine.
+    3. Ticker has a bug.  http://mbed.org/questions/1563/Mbed-Tickerfunction-hangs-system-from-re/
 -----includes-----------------------------------//----------------------------*/
     #include "mbed.h"                           // mbed class.
     #include "rtos.h"                           // rtos class.
@@ -29,7 +30,7 @@
     #define THREAD_2_WAITmS  20                 // LCD thread wait.
     #define THREAD_3_WAITmS  80                 // thread 3 wait in mS.
     #define THREAD_4_WAITmS   1                 // thread 4 wait in mS.
-    #define THREAD_5_WAITmS 400                 // thread 5 wait in mS.
+    #define THREAD_5_WAITmS 500                 // FSM thread wait.
     #define HB_MODULO      1024                 // heartbeat modulo divisor.
     
     #define MSG_INC_TIME   0x01
@@ -105,7 +106,7 @@
 
     Ticker       tickerButtonStateManager;      // manage the button states.
     Ticker       tickerSlowClock;               // generate a ~1Hz clock.
-    Ticker       tickerCookCountdown;           // remaining cook time.
+    
     
  //   Timer        timerFSMdone;                  // duration of FSM 'done' state.
 
@@ -326,6 +327,9 @@
       static int   dButtonStart   = 0;
       static int   dButtonStop    = 0;
       static int   dDoorOpen      = 0;
+      static char  cAttached      = 0;          // track ticker attachment.
+      
+      Ticker       tickerCookCountdown;           // remaining cook time.
 
       
       osEvent   queueEvent;                     // queue event.
@@ -350,19 +354,36 @@
       while(1)                                  // thread loop.
       {
       
-pc.printf("\n\r FSM state, stateLast: %d %d- start, open, remaining, total %d %d %d %d",dFSMstate,dFSMstateLast,dButtonStart,dDoorOpen,dRemainingTime,giRemainingTime.dTotalTime);      
+pc.printf("\n\rFSM state, stateLast: %d %d- start, open, remaining, total %d %d %d %d",dFSMstate,dFSMstateLast,dButtonStart,dDoorOpen,dRemainingTime,giRemainingTime.dTotalTime);      
       
         switch (dFSMstate)                      // cook-mode state machine.
         {
           case  FSM_IDLE :                      // IDLE.
           {
+pc.printf("\n\r 01");
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
+pc.printf("\n\r 02");
+              if (cAttached)
+              {
+                tickerCookCountdown.detach();     // immediate countdown clear.
+                cAttached = 0;
+//              tickerCookCountdown.remove_function(&tickCookRemainingTime);
+
+              }
+pc.printf("\n\r 03");
+              giRemainingTime.cControl = RT_CLEAR;
+pc.printf("\n\r 04");
+              tickCookRemainingTime();      
+pc.printf("\n\r 05");      
             }
+pc.printf("\n\r 06");
             giRemainingTime.cControl = RT_CLEAR;
-                                                // determine next state.
-            dFSMstateLast = dFSMstate;
+pc.printf("\n\r 07");                                               
+            dFSMstateLast = dFSMstate;          // determine next state.
+pc.printf("\n\r 08");
             if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_COOK;
+pc.printf("\n\r 09");
             break;
           }
            case  FSM_COOK :                     // COOK.
@@ -370,15 +391,24 @@
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
               led3 = 1;
-              tickerCookCountdown.detach();     // initialize countdown.
+              if (cAttached)
+              {
+                tickerCookCountdown.detach();     // initialize countdown.
+                cAttached = 0;
+              }
               giRemainingTime.cControl = RT_PRELOAD;
               tickCookRemainingTime();
-              tickerCookCountdown.attach(&tickCookRemainingTime,1);
+              if (!cAttached)
+              {
+                tickerCookCountdown.attach(&tickCookRemainingTime,1);
+                cAttached = 1;
+              }
               giRemainingTime.cControl = RT_DECREMENT;
             }    
-                                                // determine next state.
-            dFSMstateLast = dFSMstate;
+                                                
+            dFSMstateLast = dFSMstate;          // determine next state.
             if (dDoorOpen & dRemainingTime) dFSMstate = FSM_PAUSE; 
+            if (dButtonStop) dFSMstate = FSM_IDLE;
             break;
           }
           case  FSM_PAUSE :                     // PAUSE.
@@ -424,26 +454,26 @@
           
           if (dButtonState == MSG_START)
           {
-            pc.printf("\n\r message start.");
+//            pc.printf("\n\r message start.");
             dButtonStart = 1;
             dButtonStop  = 0;
           }
           if (dButtonState == MSG_STOP)
           {
-            pc.printf("\n\r message stop.");
+ //           pc.printf("\n\r message stop.");
             dButtonStart = 0;
             dButtonStop  = 1;
           }    
           
           if (dButtonState == MSG_OPEN)
           {
-            pc.printf("\n\r message open.");
+//            pc.printf("\n\r message open.");
             dDoorOpen = 1;
           }       
           
           if (dButtonState == MSG_CLOSED)
           {
-            pc.printf("\n\r message closed.");
+//            pc.printf("\n\r message closed.");
             dDoorOpen = 0;
           }               
                 
@@ -580,7 +610,7 @@
                                                 // below else the anti-blink
                                                 // code won't work.
         dLCDtotalCookTimeSec  = gdLCDtotalCookTimeSec;
-        dCookTimeRemainingSec = gdCookTimeRemainingSec;
+        dCookTimeRemainingSec = giRemainingTime.dRemainingTime;
         fLCDcelsius           = gfLCDcelsius;
         
 
@@ -637,7 +667,7 @@
     {
       static int dRemainingTime = 0;            // remaining time in seconds.
       
-      led0 = 1;
+
       
       switch (giRemainingTime.cControl)         // control processing.
       {
@@ -646,7 +676,6 @@
           dRemainingTime = giRemainingTime.dTotalTime;
           if (dRemainingTime > 180) dRemainingTime = 180;
           if (dRemainingTime <   0) dRemainingTime =   0;
-          led1 = 1;
           break;
         }
         case RT_DECREMENT :                     // count-down.
@@ -654,7 +683,6 @@
           dRemainingTime--;
           if (dRemainingTime > 180) dRemainingTime = 180;
           if (dRemainingTime <   0) dRemainingTime =   0;
-          led2 = 1;
           break;
         }
       
@@ -668,7 +696,8 @@
         
         case RT_CLEAR    :                      // clear countdown.
         {
-          dRemainingTime = 0;               
+          dRemainingTime = 0;      
+          led3 = 1;         
           break;
         }