homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Files at this revision

API Documentation at this revision

Comitter:
gatedClock
Date:
Wed Sep 11 20:44:18 2013 +0000
Parent:
68:cbebcfc948aa
Child:
70:7c0743c28b11
Commit message:
strange that cookRemainingTime is not running at all.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Sep 11 18:07:15 2013 +0000
+++ b/main.cpp	Wed Sep 11 20:44:18 2013 +0000
@@ -57,6 +57,7 @@
     Queue<int, 1> queueUpdateFSM;               // message to inform FSM.
     Queue<int, 1> queueUpdateRemainingTime;     // message to update remaining time.
     Queue<int, 1> queueSetRemainingTime;        // tell countdown it's start time.
+    Queue<int, 1> queueFSMnewState;             // latest FSM state.
 //--global_variables----------------------------//------------------------------ 
     char     gcSignalWaitEnable;                // 1 to wait on a signal.
     char     gcSlowClock;                       // slow-clock signal.
@@ -147,18 +148,19 @@
       
  //     timerFSMdone.start();                 // start 'done' timer.
  
-      tickerSlowClock.attach_us(&slowClock         ,SLOWCLOCKuS);
+ //     tickerSlowClock.attach_us(&slowClock         ,SLOWCLOCKuS);
       
                                                 // count-down by one second.
-      tickerCookCountdown.attach(&cookRemainingTime,1);  // causing trouble.
+      pc.printf("\n\r starting cookRemainingTime");
+      tickerCookCountdown.attach(&cookRemainingTime,1); 
  
       
-      Thread thread_1(temperatureThread       ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
-      Thread thread_2(LCDthread               ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
-      Thread thread_3(threadTotalTimeControl  ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
-      Thread thread_4(threadButtonStateManager,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); // was osPriorityIdle
-      Thread thread_5(threadCookStateFSM      ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
-
+ //     Thread thread_1(temperatureThread       ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
+//      Thread thread_2(LCDthread               ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
+//      Thread thread_3(threadTotalTimeControl  ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
+ //     Thread thread_4(threadButtonStateManager,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); // was osPriorityIdle
+ //     Thread thread_5(threadCookStateFSM      ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
+//
 //Thread thread_4(threadButtonStateManager);
 
 
@@ -195,7 +197,7 @@
       if (gtButtons.cLeftButton)                // total time increment button.
       {
         dMessage = MSG_INC_TIME;                // set message.
-        queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
+        queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer.
         gtButtons.cLeftButton = 0;              // clear the button state.
 //        pc.printf("\n\r time increment button.");
       }
@@ -203,7 +205,7 @@
       if (gtButtons.cRightButton)               // total time decrement button.
       {
         dMessage = MSG_DEC_TIME;                // set message.
-        queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
+        queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer.
         gtButtons.cRightButton = 0;             // clear the button state.
  //       pc.printf("\n\r time decrement button.");
       }
@@ -213,21 +215,21 @@
       if (gtButtons.cTopButton)                 // start-cook button.             
       {
         dMessage = MSG_START;                   // set message.
-        queueUpdateFSM.put((int *) dMessage);   // pretend it's a pointer.
+        queueUpdateFSM.put((int *) dMessage,1);   // pretend it's a pointer.
         gtButtons.cTopButton = 0;               // clear the button state.
       }
      
       if (gtButtons.cBottomButton)              // stop-cook button.
       {
         dMessage = MSG_STOP;                    // set message.
-        queueUpdateFSM.put((int *) dMessage);   // pretend it's a pointer.
+        queueUpdateFSM.put((int *) dMessage,1);   // pretend it's a pointer.
         gtButtons.cBottomButton = 0;            // clear the button state.
       }
   
       if (gtButtons.cCenterButton)              // door-state-toggle.
       {
         dMessage = gtButtons.cDoorOpen;         // determined in ISR.
-        queueUpdateFSM.put((int *) dMessage);   // pretend it's a pointer.
+        queueUpdateFSM.put((int *) dMessage,1);   // pretend it's a pointer.
         gtButtons.cCenterButton = 0;            // clear the button state.      
       }
 //---     
@@ -262,7 +264,7 @@
           if (dTotalTime > 180) dTotalTime = 180; 
           if (dTotalTime <   0) dTotalTime =   0;         
           
-          queueSetRemainingTime.put((int *) dTotalTime);
+          queueSetRemainingTime.put((int *) dTotalTime,1);
           
       
         }
@@ -293,7 +295,8 @@
 */
     void threadCookStateFSM(void const *args)   // cook-cycle FSM.
     {
-      static int   dFSMstate = FSM_IDLE;        // state of this FSM.    
+      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.
       
@@ -309,7 +312,7 @@
       
         if (dFSMstate == FSM_IDLE)              // idle state.
         {
-          pc.printf("\n\r FSM_IDLE");
+//          pc.printf("\n\r FSM_IDLE");
           if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_COOK;
         }
         else
@@ -333,6 +336,14 @@
           dFSMstate = FSM_IDLE;                 // end of beep.
         }      
       
+                                            
+        if (dFSMstate != dFSMstateLast)         // if just changed state.
+        {
+                                                // tell cookRemainingTime the new state.
+          queueFSMnewState.put((int* )dFSMstate,1);
+        
+        
+        }
       
      
         queueEvent = queueUpdateFSM.get(1);     // get message.
@@ -341,24 +352,30 @@
                                                 // interpret as integer, not pointer.
           dButtonState = (int) queueEvent.value.p; 
           
+          pc.printf("\n dButtonState = %d",dButtonState);
+          
           if (dButtonState == MSG_START)
           {
+            pc.printf("\n\r message start.");
             dButtonStart = 1;
             dButtonStop  = 0;
           }
           if (dButtonState == MSG_STOP)
           {
+            pc.printf("\n\r message stop.");
             dButtonStart = 0;
             dButtonStop  = 1;
           }    
           
           if (dButtonState == MSG_OPEN)
           {
+            pc.printf("\n\r message open.");
             dDoorOpen = 1;
           }       
           
           if (dButtonState == MSG_CLOSED)
           {
+            pc.printf("\n\r message closed.");
             dDoorOpen = 0;
           }               
                 
@@ -369,6 +386,7 @@
         {
                                                 // interpret as integer, not pointer.
           dRemainingTime = (int) queueEvent.value.p; 
+
         }        
         
         
@@ -377,7 +395,7 @@
       
       
       
-      
+        dFSMstateLast = dFSMstate;              // pipeline variable.
         Thread::wait(THREAD_5_WAITmS);          // multitasking. 
       }                                         // thread loop.
     }                                           // threadCookStateFSM.
@@ -537,23 +555,67 @@
 /*----------------------------------------------//----------------------------*/
     void cookRemainingTime(void)                // cook-cycle countdown.
     {
-    
+      static int dFSMstate      = FSM_IDLE;     // mirror FSM state.
+      static int dTotalTime     = 0;            // local copy, total time.
       static int dRemainingTime = 0;            // remaining time.
       osEvent   queueEvent;                     // queue event.
-   
-      dRemainingTime--;
+      
+      pc.printf("\n\r cookRemainingTime.");
+if (0) 
+{   
+ 
+                                                // count-down while FSM is in cook mode.
+      if (dFSMstate == FSM_COOK) dRemainingTime--;
 
+                                                // if done, including if stop button
+                                                // was presed.
+      if (dFSMstate == FSM_DONE) dRemainingTime = 0;
+       
+                                                // saturate remaining time.
       if (dRemainingTime < 0) dRemainingTime = 0;
+      
+                                                // send to FSM.
+      queueUpdateRemainingTime.put((int *) dRemainingTime,1);
 
       
-//      pc.printf("\n\r dRemainingTime = %d",dRemainingTime);
+      pc.printf("\n\r dRemainingTime = %d",dRemainingTime);
     
-                                                // get message.
+                                          
       queueEvent = queueSetRemainingTime.get(1);     
       if (queueEvent.status == osEventMessage)  // update state variable.  
       {
+       
                                                 // interpret as integer, not pointer.
-        dRemainingTime = (int) queueEvent.value.p; 
+        dTotalTime = (int) queueEvent.value.p; 
+        
+         pc.printf("\n\r dTotalTime = %d",dTotalTime);
       }        
+      
+      
+      
+      queueEvent = queueFSMnewState.get(1);     // get latest FSM state.
+      if (queueEvent.status == osEventMessage)  // if there is an FSM change.
+      {  
+        dFSMstate = (int) queueEvent.value.p;   // mirror the FSM state.
+        
+                                                
+        if (dFSMstate == FSM_COOK)              // if FSM just entered cook mode.
+        {
+          dRemainingTime = dTotalTime;          // rewind countdown.
+        }
+      
+      }
+ }     
     }
-/*----------------------------------------------//----------------------------*/
\ No newline at end of file
+/*----------------------------------------------//----------------------------*/
+
+
+
+
+
+
+
+
+
+
+