Bhakti Kulkarni / Mbed 2 deprecated RTOS_Alarm_Clock

Dependencies:   C12832_lcd DebounceInterrupts mbed-rtos mbed USBDevice

Files at this revision

API Documentation at this revision

Comitter:
bhakti08
Date:
Tue Mar 25 20:11:11 2014 +0000
Parent:
1:aa410246cf19
Commit message:
Final Version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Mar 25 01:50:16 2014 +0000
+++ b/main.cpp	Tue Mar 25 20:11:11 2014 +0000
@@ -1,3 +1,14 @@
+/*****************************************************************************/
+/*      Main program for the implementation of RTOS_ALARM Clock              */
+/*Function: To sound an alarm when the actual time is equal to alarm time    */
+/*Features: USB access to set the time                                       */
+/*          User can set the time with the joystick                          */
+/*          Audio Visual alarm available                                     */
+/*          Alarm turns OFF automaticaaly after 1 min if not turned OFF      */
+/*Author:   Bhakti Kulkarni                                                  */
+/*Date:     03/25/2014                                                       */
+/*****************************************************************************/
+
 #include "mbed.h"
 #include "C12832_lcd.h"
 #include "DebouncedIn.h"
@@ -14,26 +25,31 @@
 #define MAX_SECS 60
 
 C12832_LCD lcd;
-//Serial pc(USBTX,USBRX);
 USBSerial serial;
 
-DebouncedInterrupt increase(p12);
-DebouncedInterrupt select(p14);
-Mutex M_lcd;
-DebouncedIn stop_alarm(p16);
-Ticker update_time;
-DigitalOut wake_up(LED2);
-PwmOut speaker(p26);
+DebouncedInterrupt increase(p12);                     //Switch to increase time
+DebouncedInterrupt select(p14);         //Switch to toggle between hrs and mins
+Mutex M_lcd;                            //Mutex for LCD
+DebouncedIn stop_alarm(p16);            //Switch to stop the alarm
+Ticker update_time;                     //Ticker to update actual time
+DigitalOut wake_up(LED2);               //Visual alarm LED
+PwmOut speaker(p26);                    //Speaker for audible alarm
 
-
+/*Global variables for mins and hrs*/
 int hrs;
 int mins;
 int hrs_set = MAX_HRS;
 int mins_set = MAX_MINS;
 int sec_set = 0;
-bool selection= HR;
-bool set_time_sig;
+bool selection= HR;            //Signal to indicate the hrs/mins to be changed
 
+/*****************************************************************************/
+/*                     ISR for increasing the time                           */
+/*Function: This routine checks the value of selection signal.               */
+/*          If selection value - HR it updates hrs and if selection = MIN    */
+/*          it updates the minute value. It also checks for the maximum      */
+/*          value of hrs and mins and reset appropriate variable             */
+/*****************************************************************************/
 void increase_time()
 {
     if (selection == HR)
@@ -56,15 +72,25 @@
         }
      }
 }
+/*****************************************************************************/
 
+/*****************************************************************************/
+/*                          ISR to update selection                          */
+/*Function: Allows the user to toggle between the hours and minutes          */
+/*****************************************************************************/
 void select_time()
 {
     selection = !selection;
 }
+/*****************************************************************************/
 
+/*****************************************************************************/
+/*                        Thread to update LCD                               */
+/*Function: This thread updates the LCD with the actual and the alarm time   */
+/*****************************************************************************/
 void update_lcd(void const *args)
 {
-    M_lcd.lock();
+    M_lcd.lock();                                       //Mutex to lock the LCD
     lcd.cls();
     lcd.locate(0,20);
     while(1) {
@@ -77,8 +103,14 @@
         Thread::wait(25);
     }
 }
+/*****************************************************************************/
 
-
+/*****************************************************************************/
+/*                   Ticker function to update the actual time               */
+/*This function is called every second. It increments no. of seconds         */
+/*Also checks for max secs, mins and hours and accordingly adjusts other     */
+/*variables.                                                                 */
+/*****************************************************************************/
 void time_update()
 {
     sec_set++;
@@ -99,7 +131,37 @@
         sec_set = 0;
     }
 }
-void alarm_on();
+/*****************************************************************************/
+
+/*****************************************************************************/
+/*                        Function to sound the alarm                        */
+/*This function is called when the alarm time = actual time                  */
+/*It sounds the alarm till alarm_stop button is pressed | 1 min is ellapsed  */
+/*The variable frequency and beat rate of the PWM decide the tone of alarm   */
+/*****************************************************************************/
+void alarm_on()
+{
+    float frequency[]={659,554,659,554,440,494,554,587,494,659,554,440};
+    float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
+    while (!stop_alarm && !(hrs_set == hrs && mins_set == mins+DURATION)) {
+        wake_up = 1;
+        for (int i=0;i<=11;i++) {
+            speaker.period(1/(2*frequency[i])); // set PWM period
+            speaker=0.5; // set duty cycle
+            wait(0.4*beat[i]); // hold for beat period
+        }
+    }
+    wake_up =0;                                       //Turn OFF visual alarm
+    speaker = 0.0;                                     //Turn OFF audio alarm
+}
+/*****************************************************************************/
+
+/*****************************************************************************/
+/*                    Thread for time Comparison                             */
+/*This thread comapres the actual and the alarm time and calls the alarm     */
+/*function when both the timings are equal. It also reset the alarm time     */
+/*after the alarm is turned OFF(either by user or after a minute is ellapsed)*/
+/*****************************************************************************/
 void sound_alarm(void const *args)
 {
     while (true){
@@ -112,24 +174,15 @@
     Thread::wait(50);
     }
 }
-
+/*****************************************************************************/
 
-void alarm_on()
-{
-    float frequency[]={659,554,659,554,440,494,554,587,494,659,554,440};
-    float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
-    while (!stop_alarm && !(hrs_set == hrs && mins_set == mins+DURATION)) {
-        wake_up = 1;
-        for (int i=0;i<=11;i++) {
-            speaker.period(1/(2*frequency[i])); // set PWM period
-            speaker=0.5; // set duty cycle
-            wait(0.4*beat[i]); // hold for beat period
-        }
-    }
-    wake_up =0;
-    speaker = 0.0;
-}
-
+/*****************************************************************************/
+/*                               Main Thread                                 */
+/*Initializes all the threads and the USB inerface.                          */
+/*This thread makes a blocking call to set the time. In this application     */
+/*blocking call is used since the actual time is necessary parameter for the */
+/*alarm clock. Without the time alarm clock has no function to do.           */
+/*****************************************************************************/
 int main() {
     char ch;
     increase.attach(&increase_time);
@@ -143,17 +196,17 @@
         serial.scanf("%d",&hrs_set);
         if (hrs_set > MAX_HRS)
             serial.printf("Enter Valid hours\r\n");
-    }while (hrs_set > MAX_HRS);
+    }while (hrs_set > MAX_HRS);              //error checking for hours entered
     serial.printf("Set mins\r\n");
     do
     {
         serial.scanf("%d",&mins_set);
         if (mins_set > MAX_MINS)
             serial.printf("Enter Valid minutes\r\n");
-    }while (mins_set > MAX_MINS);
+    }while (mins_set > MAX_MINS);          //error checking for minutes entered
     update_time.attach(&time_update,1);
     while(1)
     {
-        Thread::wait(10000);
+        Thread::wait(1000);
     }
 }