Mike Moore / Mbed 2 deprecated RTOS_HW_07

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

main.cpp

Committer:
gatedClock
Date:
2013-09-10
Revision:
55:17f3354da63a
Parent:
54:b0e7352d2516
Child:
56:18cff6eb91db

File content as of revision 55:17f3354da63a:

/*----------------------------------------------//------------------------------
    student   : m-moore
    class     : rtos
    directory : RTOS_HW_07
    file      : main.cpp
----description---------------------------------//------------------------------
-----includes-----------------------------------//----------------------------*/
    #include "mbed.h"                           // mbed class.
    #include "rtos.h"                           // rtos class.
    #include "LM75B.h"                          // thermometer class.
    #include "C12832_lcd.h"                     // LCD  class.
//---defines------------------------------------//------------------------------
    #define LCD1 lcd.locate(0, 0);              // LCD line 1.
    #define LCD2 lcd.locate(0,11);              // LCD line 2.
    #define LCD3 lcd.locate(0,22);              // LCD line 3.
       

    #define DEBOUNCE          0.16              // debounce pause duration in S.
    #define uS_TIMEOUT      100                 // Timer uS timeout.
    #define LBSIG             1                 // left button signal code.
    #define PIPEuS         1000                 // pipeline clock period.
    #define SLOWCLOCKuS  500000                 // slow-clock period.
    #define TEMPCLOCKS        1                 // temperature-measuring period in S.
    #define PIPEDATASIZE      8                 // dimension of tPipeData.
    #define THREAD_1_WAITmS 400                 // thread 1 wait in mS.
    #define THREAD_2_WAITmS 400                 // thread 2 wait in mS.
    #define THREAD_3_WAITmS 400                 // thread 3 wait in mS.
    #define THREAD_4_WAITmS 400                 // thread 4 wait in mS.
    #define HB_MODULO      1024                 // heartbeat modulo divisor.
    
    #define MSG_INC_TIME   0x01
    #define MSG_DEC_TIME   0x02
//--global_definitions--------------------------//------------------------------
    struct tButtons                             // button ISR updates.
    {
      char cLeftButton;                         // cooktime +60S.
      char cRightButton;                        // cooktime -60S.
      char cTopButton;                          // start cook.
      char cBottomButton;                       // stop cook.
      char cDoorOpen;                           // door open. 
    };
    
    Queue<int ,1> queueModTotalTime;            // message to modify total time.

//--global_variables----------------------------//------------------------------ 
    char     gcSignalWaitEnable;                // 1 to wait on a signal.
    char     gcSlowClock;                       // slow-clock signal.
    char     gcInitializePipeline;              // 1 to initialize pipeline state.
    float    gfCelsius;                         // from thermometer.
    float    gfLCDcelsius;                      // feed into LCD.
    int      gdLCDtotalCookTimeSec;             // feed into LCD.
    int      gdCookTimeRemainingSec;            // feed into LCD.
    tButtons gtButtons;                         // ISR button updates.
//--global_instances----------------------------//------------------------------ 
    Serial      pc(USBTX, USBRX);               // PuTTY terminal communication.
    LM75B        temperature(p28,p27);          // on-board thermometer.   
    C12832_LCD   lcd;                           // LCD object.
    DigitalOut   led0(LED4);                    // magnetron.
    DigitalOut   led1(LED3);
    DigitalOut   led2(LED2);
    DigitalOut   led3(LED1);
    
    InterruptIn  iJoyStickUp    (p15);          // joystick up rising edge.
    InterruptIn  iJoyStickDown  (p12);          // joystick down rising edge.
    InterruptIn  iJoyStickLeft  (p13);          // joystick left rising edge.
    InterruptIn  iJoyStickRight (p16);          // joystick right rising edge.
    InterruptIn  iJoyStickCenter(p14);          // 1 if joystick middle pressed.

    Ticker       tickerButtonStateManager;      // manage the button states.
    Ticker       tickerSlowClock;               // generate a ~1Hz clock.

//-------prototypes-----------------------------//------------------------------

    void slowClock();                           // 1Hz or thereabouts.

    void initialization();                      // initialize settings.
    
    void ISRleftButtonRising();                 // cook-time increase.
    void ISRleftButtonFalling();                // button-release debounce.
    void ISRrightButtonRising();                // cook-time decrease.
    void ISRrightButtonFalling();               // button-release debounce.
    void ISRtopButtonRising();                  // cook start.
    void ISRtopButtonFalling();                 // button-release debounce.
    void ISRbottomButtonRising();               // cook stop.
    void ISRbottomButtonFalling();              // button-release debounce.
    void ISRcenterButtonRising();               // door state toggle.
    void ISRcenterButtonFalling();              // button-release debounce.
    void disableSignalWaiting();                // break from signal waiting.
    
    void threadButtonStateManager(void const *args);
    void threadTotalTimeControl(void const *args);
    
    void temperatureThread(void const *args);   // temperature measurement.
    void LCDthread        (void const *args);   // LCD display thread.
//==============================================//==============================
    int main(void) 
    {
      char cLeftButtonState;                    // 1 means button was pressed.
    
    
    
      iJoyStickLeft.rise (&ISRleftButtonRising);
      iJoyStickLeft.fall (&ISRleftButtonFalling);
      
      iJoyStickRight.rise(&ISRrightButtonRising);
      iJoyStickRight.fall(&ISRrightButtonFalling);
   
      iJoyStickUp.rise   (&ISRtopButtonRising);
      iJoyStickUp.fall   (&ISRtopButtonFalling);      
      
      iJoyStickDown.rise (&ISRbottomButtonRising);
      iJoyStickDown.fall (&ISRbottomButtonFalling);
      
      iJoyStickCenter.rise(&ISRcenterButtonRising);
      iJoyStickCenter.fall(&ISRcenterButtonFalling);
      
      gcInitializePipeline = 1;                 // tell pipeline to initialize.
      
      initialization();                         // initialize variables.
      gcSlowClock = 0;
      led1 = 0;
      gtButtons.cDoorOpen = 0;                  // initialize with door closed.
 
      tickerSlowClock.attach_us(&slowClock         ,SLOWCLOCKuS);
 
      
      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);


      
 
      
      while(1)
      {
        Thread::wait(10000);
      
      
      
 
      }
    }     
/*----------------------------------------------//----------------------------*/
                                                // process buttons, generate messages.
    void threadButtonStateManager(void const *args)         
    {
      static int dMessage;                      // message.
      
      while(1)                                  // thread loop.
      {
      
//---                                           // TOTAL TIME CONTROL.
      
      if (gtButtons.cLeftButton)                // total time increment button.
      {
        dMessage = MSG_INC_TIME;                // set message.
        queueModTotalTime.put((int *) dMessage);        // tell total time to increment.
        
        pc.printf("\n\r sending %d\n\r",dMessage);
        
        gtButtons.cLeftButton = 0;              // clear the button state.
      }
      
      if (gtButtons.cRightButton)               // total time increment button.
      {
        dMessage = MSG_DEC_TIME;                // set message.
        queueModTotalTime.put((int *) dMessage);        // tell total time to decrement.
        
        pc.printf("\n\r sending %d\n\r",dMessage);
        
        gtButtons.cRightButton = 0;             // clear the button state.
      }
      
        Thread::wait(THREAD_4_WAITmS);          // multitasking.
      }                                         // thread loop.
    
    }                                           // threadButtonStateManager.
/*----------------------------------------------//----------------------------*/
//  the incoming messages are mutually-exclusive.
                                                // total time controller.
    void threadTotalTimeControl(void const *args)           
    {
      static int   dTotalTime = 0;              // total time variable.
      
             int   pdGet;                       // message data pointer.
             
         osEvent   queueEvent;                  // queue event.
      
      while(1)                                  // thread loop.
      {
      
        queueEvent = queueModTotalTime.get(1);  // get message.
        if (queueEvent.status == osEventMessage)
        {
          pdGet = (int) queueEvent.value.p;   // drain the queue.
          
          pc.printf("\n\r getting %d\n\r",pdGet);
          
                                                // increment total time.
          if (pdGet == MSG_INC_TIME) dTotalTime += 60; 
          
                                                // decrement total time.
          if (pdGet == MSG_DEC_TIME) dTotalTime -= 60;
        }
     
        if (dTotalTime > 180) dTotalTime = 180; // saturate.
        if (dTotalTime <   0) dTotalTime =   0; // saturate.
        
        gdLCDtotalCookTimeSec = dTotalTime;     // transmit to LCD.
      
      
        Thread::wait(THREAD_3_WAITmS);          // multitasking. 
      }                                         // thread loop.
    }                                           // threadTotalTimeControl.
/*----------------------------------------------//----------------------------*/
    void slowClock(void)                        // 1Hz or thereabouts.
    {
      gcSlowClock = !gcSlowClock;               // toggle clock.
    }
/*----------------------------------------------//----------------------------*/
    void initialization(void)                   // program initializations.
    {
      gcSignalWaitEnable = 1;
    }
/*----------------------------------------------//----------------------------*/
    void ISRleftButtonRising(void)              // cooktime plus 60s.
    {
      __disable_irq();                          // debounce start.
      
      gtButtons.cLeftButton = 1;                // detect left button.
      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRleftButtonFalling(void)             // button-release debounce.
    {
      __disable_irq();                          // debounce start.

      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRrightButtonRising(void)             // cooktime -60s.
    {
      __disable_irq();                          // debounce start.

      gtButtons.cRightButton = 1;               // detect right button.
      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRrightButtonFalling(void)            // button-release debounce.
    {
      __disable_irq();                          // debounce start.
      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRtopButtonRising(void)               // cook start.
    {
      __disable_irq();                          // debounce start.

      gtButtons.cTopButton = 1;                 // detect top button.
      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRtopButtonFalling(void)              // button-release debounce.
    {
      __disable_irq();                          // debounce start.

      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/

//  front-end control of magnetron off.
//  due to physical danger, the magnetron is turned off immediately
//  upon off-button press, in the interrupt that it generates.

    void ISRbottomButtonRising(void)            // cook stop.
    {
      __disable_irq();                          // debounce start.
      
      led0 = 0;                                 // magnetron off.

      gtButtons.cBottomButton = 1;              // detect bottom button.
      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRbottomButtonFalling(void)           // button-release debounce.
    {
      __disable_irq();                          // debounce start.


      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
//  front-end control of magnetron off.
//  due to physical danger, the magnetron is turned off immediately
//  upon detection of an open door.

    void ISRcenterButtonRising(void)            // toggle door state.
    {
      __disable_irq();                          // debounce start.
      
      if (gtButtons.cDoorOpen)                  // calculate door state.
      gtButtons.cDoorOpen = 0;
      else
      gtButtons.cDoorOpen = 1;
      
      if (gtButtons.cDoorOpen) led0 = 0;        // magnetron off.

      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void ISRcenterButtonFalling(void)           // button-release debounce.
    {
      __disable_irq();                          // debounce start.


      
      wait(DEBOUNCE);                           // debounce time.
      
      __enable_irq();                           // debounce done. 
    } 
/*----------------------------------------------//----------------------------*/
    void temperatureThread(void const *args)    // temperature measurement.
    {
      while(1)                                  // thread loop.
      {
        gfCelsius = temperature.read();         // physical measurement.
      
        Thread::wait(THREAD_1_WAITmS);          // multitasking. 
      }                                         // thread loop.
    }                                           // temperatureThread.
/*----------------------------------------------//----------------------------*/
    void LCDthread(void const *args)            // LCD display thread.
    {
      while(1)                                  // thread loop.
      {
        lcd.cls();                              // clear the display.
      
        LCD1;                                   // line 1.
        lcd.printf(" total   cook time: %d",gdLCDtotalCookTimeSec);
      
        LCD2;                                   // line 2.
        lcd.printf(" remaing cook time: %d",gdCookTimeRemainingSec);
    
        LCD3;                                   // line 3.
        lcd.printf(" temperature      : %5.3f",gfLCDcelsius);
      
        Thread::wait(THREAD_2_WAITmS);          // multitasking.
      }                                         // thread loop.
    }                                           // LCDthread.
/*----------------------------------------------//----------------------------*/