homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Revision:
75:c2894d531f42
Parent:
74:4debb8f2e21d
Child:
76:74c454c9d75b
--- a/main.cpp	Wed Sep 11 23:15:52 2013 +0000
+++ b/main.cpp	Thu Sep 12 00:42:54 2013 +0000
@@ -169,9 +169,8 @@
  
       tickerSlowClock.attach_us(&slowClock         ,SLOWCLOCKuS);
       
-                                                // count-down by one second.
-      pc.printf("\n\r starting cookRemainingTime");
-      tickerCookCountdown.attach(&tickCookRemainingTime,1); 
+
+       
  
       
       Thread thread_1(temperatureThread       ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
@@ -187,7 +186,7 @@
       
       while(1)
       {
-        pc.printf("\n\r gdDiagTotalTime %d",gdDiagTotalTime);
+ //       pc.printf("\n\r gdDiagTotalTime %d",gdDiagTotalTime);
  
         Thread::wait(1000);
       
@@ -288,6 +287,7 @@
           
           dRC = queueSetRemainingTime.put((int *) dTotalTime,1);
           pc.printf("\n\r just queueSetRemainingTime.put %d, dRC = %d",dTotalTime,dRC);
+          giRemainingTime.dTotalTime = dTotalTime;
           
       
         }
@@ -321,45 +321,90 @@
       static int   dFSMstate     = FSM_IDLE;    // state of this FSM.    
       static int   dFSMstateLast = FSM_IDLE;    // previous FSM state.
       static int   dButtonState;                // received button state.
-      static int   dRemainingTime;              // received remaining time.
+      static int   dRemainingTime = 0;          // received remaining time.
       
       static int   dButtonStart   = 0;
       static int   dButtonStop    = 0;
       static int   dDoorOpen      = 0;
-      static int   dTimeRemaining = 0;
+
       
       osEvent   queueEvent;                     // queue event.
+/*
+    struct tRemainingTime                       // remaining time related.
+    {
+      char cControl;                            // countdown control.
+      int  dTotalTime;                          // initialize to this.
+      int  dRemainingTime;                      // the countdown value.
+    }
+
+giRemainingTime.
+
+    #define RT_PRELOAD     0x01                 // remaining-time preload.
+    #define RT_DECREMENT   0x02                 // remaining-time decrement.
+    #define RT_PAUSE       0x03                 // remaining-time don't change.
+    #define RT_CLEAR       0x04                 // remaining-time set to zero.
+
+*/
+      
       
       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);      
+      
         switch (dFSMstate)                      // cook-mode state machine.
         {
           case  FSM_IDLE :                      // IDLE.
           {
-          
-            if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_COOK;
-            if (dFSMstate != dFSMstateLast) { }
+            if (dFSMstate != dFSMstateLast)     // if just entered state.
+            { 
+            }
+            giRemainingTime.cControl = RT_CLEAR;
+                                                // determine next state.
+            dFSMstateLast = dFSMstate;
+            if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_COOK;
             break;
           }
            case  FSM_COOK :                     // COOK.
           {
-          
-            if (dDoorOpen & dTimeRemaining) dFSMstate = FSM_PAUSE;    
-            if (dFSMstate != dFSMstateLast) { }     
+            if (dFSMstate != dFSMstateLast)     // if just entered state.
+            { 
+              led3 = 1;
+              tickerCookCountdown.detach();     // initialize countdown.
+              giRemainingTime.cControl = RT_PRELOAD;
+              tickCookRemainingTime();
+              tickerCookCountdown.attach(&tickCookRemainingTime,1);
+              giRemainingTime.cControl = RT_DECREMENT;
+            }    
+                                                // determine next state.
+            dFSMstateLast = dFSMstate;
+            if (dDoorOpen & dRemainingTime) dFSMstate = FSM_PAUSE; 
             break;
           }
           case  FSM_PAUSE :                     // PAUSE.
           {
-            if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_DONE;
-            if (dButtonStop) dFSMstate = FSM_COOK;   
-            if (dFSMstate != dFSMstateLast) { }
+              
+            if (dFSMstate != dFSMstateLast)     // if just entered state.
+            { 
+            }
+            giRemainingTime.cControl = RT_PAUSE;
+                                                // determine next state.
+            dFSMstateLast = dFSMstate;
+            if (dButtonStart & !dDoorOpen & dRemainingTime) dFSMstate = FSM_DONE;
+            else
+            if (dButtonStop) dFSMstate = FSM_COOK; 
             break;
           }
           case  FSM_DONE :                      // DONE.
           {
+            
+            if (dFSMstate != dFSMstateLast)     // if just entered state.
+            { 
+            }
+            giRemainingTime.cControl = RT_CLEAR;
+                                                // determine next state.
+            dFSMstateLast = dFSMstate;
             dFSMstate = FSM_IDLE; 
-            if (dFSMstate != dFSMstateLast) { }
             break;
           }       
         
@@ -367,17 +412,15 @@
         
         }
         
-      
-   
-      
      
-        queueEvent = queueUpdateFSM.get(1);     // get message.
+    
+        queueEvent = queueUpdateFSM.get(1);     // threadButtonStateManager
         if (queueEvent.status == osEventMessage)// update state variable.  
         {
                                                 // interpret as integer, not pointer.
           dButtonState = (int) queueEvent.value.p; 
           
-          pc.printf("\n dButtonState = %d",dButtonState);
+
           
           if (dButtonState == MSG_START)
           {
@@ -405,23 +448,11 @@
           }               
                 
         }
-                                                // get message.
-        queueEvent = queueUpdateRemainingTime.get(1);     
-        if (queueEvent.status == osEventMessage)// update state variable.  
-        {
-          led3 = !led3;
-                                                // interpret as integer, not pointer.
-          dRemainingTime = (int) queueEvent.value.p; 
+ 
+                                                // fetch from global scope.
+        dRemainingTime = giRemainingTime.dRemainingTime;
 
-        }        
-        
-        
-
-      
-      
-      
-      
-        dFSMstateLast = dFSMstate;              // pipeline variable.
+                      // pipeline variable.
         Thread::wait(THREAD_5_WAITmS);          // multitasking. 
       }                                         // thread loop.
     }                                           // threadCookStateFSM.
@@ -606,13 +637,16 @@
     {
       static int dRemainingTime = 0;            // remaining time in seconds.
       
-      switch (giRemainingTime.cControl)
+      led0 = 1;
+      
+      switch (giRemainingTime.cControl)         // control processing.
       {
         case RT_PRELOAD   :                     // preload with total time.
         {
           dRemainingTime = giRemainingTime.dTotalTime;
           if (dRemainingTime > 180) dRemainingTime = 180;
           if (dRemainingTime <   0) dRemainingTime =   0;
+          led1 = 1;
           break;
         }
         case RT_DECREMENT :                     // count-down.
@@ -620,6 +654,7 @@
           dRemainingTime--;
           if (dRemainingTime > 180) dRemainingTime = 180;
           if (dRemainingTime <   0) dRemainingTime =   0;
+          led2 = 1;
           break;
         }
       
@@ -642,9 +677,10 @@
           if (dRemainingTime > 180) dRemainingTime = 180;
           if (dRemainingTime <   0) dRemainingTime =   0;        
         }
-      }
+      }                                         // control processing.
       
-
+                                                // promote to global scope.
+      giRemainingTime.dRemainingTime = dRemainingTime;
       
     }                                           // cookRemainingTime.
 /*----------------------------------------------//----------------------------*/