Group assignment alarm clock

Dependencies:   mbed TextLCD

Revision:
8:c8421fc56e8f
Parent:
7:b9d3bc940d2c
Child:
9:b53b3bb0166a
--- a/main.cpp	Thu May 23 01:10:30 2019 +0000
+++ b/main.cpp	Thu May 23 05:52:25 2019 +0000
@@ -3,8 +3,8 @@
 #include <string> 
 using namespace std;
 
-TextLCD lcd(p19, p20, p21, p22, p23, p24);
-Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer, check_alarm;
+TextLCD lcd(p19, p20, p25, p22, p23, p24);
+Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer, check_alarm, sound_alarm;
 // Initially we set joystick to intterupt.
 InterruptIn joycenter(p14);
 InterruptIn up(p15);
@@ -12,6 +12,7 @@
 InterruptIn left(p13);
 InterruptIn right(p16);
 Serial pc(USBTX, USBRX); // tx, rx
+PwmOut buzzer(p21);
 
 int row, col, r_hour, r_min, r_sec, r_apm, epoch, alarm_hour, alarm_min, alarm_sec, alarm_apm;
 bool state_init, state_menu, state_alarm, alarm_up;
@@ -244,37 +245,33 @@
     lcd.printf("Alarm");
     lcd.locate(9,1);
     lcd.printf("Set");
-    
-    
 }
-
+void alarm_sound(){                                           
+    float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; //frequency array
+    float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2};             //beat array
+    lcd.cls();
+    lcd.printf("Press button to off alarm");
+    while(1){
+            for(int i=0; i<=11; i++) {
+            buzzer.period(1/(2*frequency[i]));// set PWM period
+            buzzer=0.8;// set duty cycle
+            wait(0.4*beat[i]);// hold for beat period          
+        }
+        if(joycenter){//Off the alarm
+            buzzer.period(0);
+            buzzer=0;
+            break;
+        }
+    }
+}
 //Function alarm_chk is in progress
 void alarm_chk(){
-//    char buffer_hour[32],buffer_min[32],buffer_sec[32],buffer_apm[32];
-//    char buff_ah[32], buff_am[32],buff_as[32],buff_apm[32];
-//    time_t seconds = time(NULL);
-//    strftime(buffer_hour, 32, "%I", localtime(&seconds));
-//    strftime(buffer_min, 32, "%M", localtime(&seconds));
-//    strftime(buffer_sec, 32, "%S", localtime(&seconds));
-//    strftime(buffer_apm, 32, "%p", localtime(&seconds));
-//    sprintf(buff_ah, "%d", alarm_hour);
-//    if (buff_ah == buffer_hour){
-//        pc.printf("same lol");
-//    }
-
     //we basically, comparing 2 strings. alarm string and the clock strings if match we ring it
-    if(clock_string == alarm_string)
+    if(clock_string == alarm_string){
         pc.printf("%s %s : same\r\n", alarm_string, clock_string);
-    else
-        pc.printf("%s %s : not same\r\n", alarm_string, clock_string);
-    
-    //if(int(buffer_hour) == alarm_hour && int (buffer_min) == alarm_min && int (buffer_sec) == alarm_sec && int(buffer_apm) == alarm_apm){
-//        pc.printf("Alarm ring");
-//    }
-    
-    
-//    pc.printf("%s,%d ,%d,%d,%d,%d,%d,%d", int(buffer_hour),alarm_hour,int (buffer_min),alarm_min,int (buffer_sec),alarm_sec,int(buffer_apm),alarm_apm);
-
+        blinker_cursor.detach();
+        alarm_sound();
+        }
 }
 
 void blink_cursor(){
@@ -355,41 +352,66 @@
     lcd.printf("On");
     return;
 }
-
-void setup_alarm(){
+void __set_alarm_string()
+{
+    char buffer_alarm[32];
+    alarm_string.clear();
+    if(alarm_hour <9) {
+        sprintf (buffer_alarm, "0%d:", alarm_hour);
+    } else {
+        sprintf (buffer_alarm, "%d:", alarm_hour);
+    }
+    alarm_string += buffer_alarm;
+    if (alarm_min <9) {
+        sprintf (buffer_alarm, "0%d:", alarm_min);
+    } else {
+        sprintf (buffer_alarm, "%d:", alarm_min);
+    }
+    alarm_string += buffer_alarm;
+    if (alarm_sec<9) {
+        sprintf (buffer_alarm, "0%d", alarm_sec);
+    } else {
+        sprintf (buffer_alarm, "%d", alarm_sec);
+    }
+    alarm_string += buffer_alarm;
+    if(alarm_apm ==0) {
+        sprintf (buffer_alarm, " %s", "AM");
+    } else {
+        sprintf (buffer_alarm, " %s", "PM");
+    }
+    alarm_string += buffer_alarm;
+}
+void setup_alarm()
+{
     state_alarm = true;
     state_menu = false;
     blinker_numer.attach(&alarm_disp,0.5);
-    
-    while(1){
+
+    while(1) {
         wait(1);
-        if(col == 3 && row ==1){
-            if(joycenter){
+        if(col == 3 && row ==1) {
+            if(joycenter) { //Turn off Alarm and back to main menu
                 blinker_numer.detach();
                 state_alarm = false;
                 state_menu = true;
                 alarm_up = false;
                 check_alarm.detach();
+                alarm_string.clear();
                 break;
-            
+
             }
-        } else if(col == 9 && row == 1){
-            if(joycenter){
+        } else if(col == 9 && row == 1) {//Turn on Alarm and back to main menu
+            if(joycenter) {
                 alarm_up = true;
                 blinker_numer.detach();
                 state_alarm = false;
                 state_menu = true;
                 check_alarm.attach(&alarm_chk,1);
+                __set_alarm_string();
                 break;
             }
-        char buffer_alarm[32];
-        if(alarm_apm==0){
-            sprintf (buffer_alarm, "%d:%d:%d AM", alarm_hour, alarm_min, alarm_sec);
-            }
-        else if (alarm_apm==1){
-            sprintf (buffer_alarm, "%d:%d:%d PM", alarm_hour, alarm_min, alarm_sec);
-            }
-        alarm_string=buffer_alarm;
+        
+
         }
     }
 }
@@ -397,15 +419,13 @@
 
 void setup_init(){
     state_init = true;
-    
-
+    lcd.cls();
     blinker_cursor.attach(&blink_cursor, 0.1);
     blinker_numer.attach(&blink_num, 0.5);
     while (1){
         // This is where, we select "Done", and we press joycenter to end the initial setup.
         if(col==3 && row==1){
             if(joycenter){
-                blinker_cursor.detach();
                 blinker_numer.detach();
                 
                 //Calculate the unix timestamp
@@ -423,7 +443,8 @@
                         epoch = 43200+(r_hour *3600) + (r_min *60) +(r_sec);
                     }
                
-                pc.printf("%d", epoch);
+                pc.printf("%d\r\n", epoch);
+                set_time(epoch);
                 state_init = false;
                 break;    
             }
@@ -454,12 +475,13 @@
                 clock_viewer.detach();
                 setup_alarm();
                 clock_viewer.attach(&view_clock,1);
+                
             }
         } else if(col == 9 && row ==1){
                 if(joycenter){
-                lcd.cls();
-                lcd.locate(3,1);
-                lcd.printf("OK");
+                clock_viewer.detach();
+                setup_init();
+                clock_viewer.attach(&view_clock,1);
             }
         }
     }
@@ -474,7 +496,7 @@
     col=0 ;
     alarm_hour=12;
     alarm_min=0;
-    alarm_sec=0; 
+    alarm_sec=10; 
     alarm_apm=0;
     r_hour=12 ;
     r_min=0 ;
@@ -496,7 +518,7 @@
                         |_ Change time
     */
     setup_init();
-    set_time(epoch);
+    
     clock_viewer.attach(&view_clock,1);
     lcd.cls();
     main_menu();