homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Files at this revision

API Documentation at this revision

Comitter:
gatedClock
Date:
Thu Sep 12 15:02:17 2013 +0000
Parent:
87:147e2b08fae6
Child:
89:290c96cd027f
Commit message:
countdown neatening.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Sep 12 14:46:50 2013 +0000
+++ b/main.cpp	Thu Sep 12 15:02:17 2013 +0000
@@ -58,6 +58,7 @@
     #define RT_CLEAR       0x04                 // remaining-time set to zero.
     
     #define GRANULARITY    0x400                // 1-second countdown ticker granularity.
+    #define MAXSECONDS       180                // maximum microwave-on time.
     
     #define DEBUG1                              // debug preprocessor control.
 //--global_definitions--------------------------//------------------------------
@@ -646,34 +647,45 @@
     }                                           // LCDthread.
 /*----------------------------------------------//----------------------------*/
 
+//  cook remaining time countdown counter.
+//  possibly due to a bug in Ticker
+//  http://mbed.org/questions/1563/Mbed-Tickerfunction-hangs-system-from-re/
+//  I've been unable to detach/attach this routine in order to reset its phase
+//  when I tried it at a 1s resolution.  In order to provide the human perception
+//  of an immediate restart, I've increased the ticker frequency by the factor
+//  'GRANULARITY' and likewise divide that factor out when this routine
+//  promotes the remaining time to the global variable.
+
     void tickCookRemainingTime(void)            // cook-cycle countdown.
     {
       static int dRemainingTime = 0;            // remaining time in seconds.
-      
-
+             int dMaximum;                      // MAXSECONDS * GRANULARITY.
+             
+      dMaximum = MAXSECONDS * GRANULARITY;      // precalculate.
       
       switch (giRemainingTime.cControl)         // control processing.
       {
+      
         case RT_PRELOAD   :                     // preload with total time.
         {
-          dRemainingTime = giRemainingTime.dTotalTime * GRANULARITY + (GRANULARITY - 1);
-          if (dRemainingTime > 180 * GRANULARITY) dRemainingTime = 180 * GRANULARITY;
-          if (dRemainingTime <   0) dRemainingTime =   0;
+                                                // the 'GRANULARITY - 1' factor
+                                                // compensates for integer division
+                                                // dropping the right-of-decimal result,
+                                                // that occuring at the bottom of this
+                                                // routine.
+          dRemainingTime = (giRemainingTime.dTotalTime * GRANULARITY) + (GRANULARITY - 1);
           break;
         }
+        
         case RT_DECREMENT :                     // count-down.
         {
           dRemainingTime--;
-          if (dRemainingTime > 180 * GRANULARITY) dRemainingTime = 180 * GRANULARITY;
-          if (dRemainingTime <   0) dRemainingTime =   0;
           break;
         }
       
         case RT_PAUSE    :                      // suspend countdown.
         {
           dRemainingTime = dRemainingTime;
-          if (dRemainingTime > 180 * GRANULARITY) dRemainingTime = 180 * GRANULARITY;
-          if (dRemainingTime <   0) dRemainingTime =   0;
           break;
         }
         
@@ -686,11 +698,13 @@
       
         default          :                      // saturate, just in case.
         {
-          if (dRemainingTime > 180 * GRANULARITY) dRemainingTime = 180 * GRANULARITY;
-          if (dRemainingTime <   0) dRemainingTime =   0;        
         }
       }                                         // control processing.
       
+                                                // saturate value.
+      if (dRemainingTime >  dMaximum) dRemainingTime = dMaximum;
+      if (dRemainingTime <         0) dRemainingTime = 0;
+      
                                                 // promote to global scope.
       giRemainingTime.dRemainingTime = dRemainingTime/GRANULARITY;