homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Revision:
114:fbe9ef1630c1
Parent:
113:8111b9cd15ce
Child:
115:6390d9b9be07
--- a/main.cpp	Thu Sep 12 21:29:49 2013 +0000
+++ b/main.cpp	Thu Sep 12 21:44:27 2013 +0000
@@ -336,20 +336,29 @@
 //---        
           case  FSM_IDLE :                      // IDLE.
           {
-          led0 = 0;
             if (dFSMstate != dFSMstateLast)     // if just entered state.
-            {      
+            {     
+                                                // live read of temperature,
+                                                // allow total time adjustments,
+                                                // clear total time.
+             
               queueTemperatureControl.put((int *) TEMP_READ,1);   
               queueSuppressTimeAdjust.put((int*) 0,1);
               queueClearTotalTime.put    ((int*) 1,1);
             }
             
+                                                // set global LCD data.
             giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
             giLCD.dRemainingTime = 0;           // suppress remaining time display.
-//            giLCD.fCelsius       = 0;
-            
+
+                                                // remaining time loaded with total time.
             giRemainingTime.cControl = RT_PRELOAD;                                           
             dFSMstateLast = dFSMstate;          // determine next state.
+            
+                                                // to FSM_COOK if the start button was
+                                                // pressed, the door is closed, and the
+                                                // remaining time is more than zero.
+            
             if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_COOK;
             break;
           }
@@ -358,22 +367,34 @@
           {
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
+                                                // start decrementing remaining time.
+                                                // temperature synthesizer is now
+                                                // calculating the temperature,
+                                                // user can no longer adjust total time.
               giRemainingTime.cControl = RT_DECREMENT;
               queueTemperatureControl.put((int *) TEMP_CALC,1);
               queueSuppressTimeAdjust.put((int*) 1,1);
             }    
             
+                                                // update global LCD data.
             giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
             giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
- //           giLCD.fCelsius       = 0;            
+            
             
             
                                                 
             dFSMstateLast = dFSMstate;          // determine next state.
+            
+                                                // pause on door-open if remaining time.
+            
             if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE; 
             else
+            
+                                                // stop button -> idle state.
             if (dButtonStop) dFSMstate = FSM_IDLE;
             else
+            
+                                                // beeper state once countdown complete.
             if (dRemainingTime <= 0) dFSMstate = FSM_DONE;
             break;
           }
@@ -383,20 +404,29 @@
               
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
+                                                // suspend synthesized temperature calculation.
               queueTemperatureControl.put((int *) TEMP_FREEZE,1);
             }
             
-            
+                                                // update LCD globals.
             giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
             giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
- //           giLCD.fCelsius       = 0;              
             
             
-            giRemainingTime.cControl = RT_PAUSE;
-                                                // determine next state.
-            dFSMstateLast = dFSMstate;
+                                               
+            giRemainingTime.cControl = RT_PAUSE;// pause remaining time countdown.
+                                                
+            dFSMstateLast = dFSMstate;          // determine next state.
+            
+            
+                                                // continue if user hits start button,
+                                                // and the door is closed, and there
+                                                // is some remaining time.
+            
             if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_CONTINUE;
             else
+            
+                                                // to idle if user hits stop button.
             if (dButtonStop) dFSMstate = FSM_IDLE; 
             break;
           }
@@ -407,22 +437,29 @@
               
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
+                                                // continue calculating synthetic temperature.
               queueTemperatureControl.put((int *) TEMP_CALC,1);
             }
             
-            
+                                                // update LCD globals.
             giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
             giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
-   //         giLCD.fCelsius       = 0;              
-            
+        
             
+                                                // continue decrementing time.
             giRemainingTime.cControl = RT_DECREMENT;
-                                                // determine next state.
-            dFSMstateLast = dFSMstate;
+                                                
+            dFSMstateLast = dFSMstate;          // determine next state.
+            
+                                                // beep state when no more time.
+            
             if (dRemainingTime <= 0) dFSMstate = FSM_DONE;
             else
+                                                // user stop causes jump to idle.
             if (dButtonStop) dFSMstate = FSM_IDLE; 
             else
+                                                // back to pause if door open,
+                                                // and there's remaining time.
             if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE;
             break;
           }          
@@ -431,33 +468,24 @@
 //---             
           case  FSM_DONE :                      // DONE.
           {
-            led0 = 1;
-            led3 = 0;
             if (dFSMstate != dFSMstateLast)     // if just entered state.
             { 
-
+                                                // freeze synthetic temperature.
               queueTemperatureControl.put((int *) TEMP_FREEZE,1);
-
-              
-              giRemainingTime.cBeepEnable = 1;
+              giRemainingTime.cBeepEnable = 1;  // initiate beep.
               beepTimer.reset();                // clear the beep timer.
             }
            
-            
+                                                // update LCD globals.
             giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
             giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
-//            giLCD.fCelsius       = 0;              
-            
-            
+            giRemainingTime.cControl = RT_CLEAR;// clear remaining time. (needed?)
+                                                
+            dFSMstateLast = dFSMstate;          // determine next state.
             
-            giRemainingTime.cControl = RT_CLEAR;
-                                                // determine next state.
-            dFSMstateLast = dFSMstate;
+                                                // to idle once beeper is done.
             if (beepTimer.read() >= BEEPTIME) {dFSMstate = FSM_IDLE; giRemainingTime.cBeepEnable = 0;}
-            else dFSMstate = FSM_DONE; 
-            
-            led3 = 1;
-            
+            else dFSMstate = FSM_DONE;          // (else stay here).
             break;
           }       
 //---           
@@ -473,32 +501,26 @@
                                                 // interpret as integer, not pointer.
           dButtonState = (int) queueEvent.value.p; 
           
-
-          
           if (dButtonState == MSG_START)
           {
-            dButtonStart = 1;
+            dButtonStart = 1;                   // copy-in start info.
             dButtonStop  = 0;
-            
-                                                // if the door is open, ignore
-                                                // and cancel 'start' state.
-            if (dDoorOpen) dButtonStart = 0;
+            if (dDoorOpen) dButtonStart = 0;    // cancel start if door open.
           }
           if (dButtonState == MSG_STOP)
           {
-            dButtonStart = 0;
+            dButtonStart = 0;                   // copy-in stop state.
             dButtonStop  = 1;
-            queueClearTotalTime.put    ((int*) 1,1);
+            queueClearTotalTime.put((int*) 1,1);// clear total time on stop.
           }    
           
-          if (dButtonState == MSG_OPEN)
-          {
-                                                // if the door opens, clear current 'start' state.
+          if (dButtonState == MSG_OPEN)         // if the door opens, clear current 'start' state.
+          {                       
             dDoorOpen    = 1;
             dButtonStart = 0;
           }       
           
-          if (dButtonState == MSG_CLOSED)
+          if (dButtonState == MSG_CLOSED)       // detect door closed.
           {
             dDoorOpen = 0;
           }               
@@ -508,15 +530,11 @@
         {
           dButtonStart   = 0;
           dButtonStop    = 0;
-//        dDoorOpen      = 0;   
-        
-        
         }
  
                                                 // fetch from global scope.
         dRemainingTime = giRemainingTime.dRemainingTime;
 
-                      // pipeline variable.
         Thread::wait(THREAD_5_WAITmS);          // multitasking. 
       }                                         // thread loop.
     }                                           // threadCookStateFSM.