Ebrahim Atya
/
grpass_clock
Group assignment alarm clock
main.cpp
- Committer:
- mmlhein
- Date:
- 2019-05-23
- Revision:
- 8:c8421fc56e8f
- Parent:
- 7:b9d3bc940d2c
- Child:
- 9:b53b3bb0166a
File content as of revision 8:c8421fc56e8f:
#include "mbed.h" #include "TextLCD.h" #include <string> using namespace std; 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); InterruptIn down(p12); 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; string alarm_string, clock_string; void cursor_left() { if(state_init) { if (col >0 && col <13) if (col == 12) col = 10; else if (col ==10) col = 7; else if (col ==7) col = 4; else if (col == 4) { col =3; row =1; } else if (col ==3 && row == 1) { col = 12; row = 0; } else col = 4; else col = 4; } else if(state_menu){ if(col == 3){ col = 9; } else if(col == 9){ col = 3; } } else if(state_alarm){ if (col == 12) col = 10; else if (col ==10) col = 7; else if (col ==7) col = 4; else if (col == 4) { col =3; row =1; } else if (col ==3 && row == 1) { col = 9; row = 1; } else if( col == 9 && row ==1) { col = 12; row = 0; } else col = 4; }else col = 4; pc.printf("Cursor left: %i,%i\r\n", col, row); } void cursor_right() { if(state_init) { if (col >0 && col <13) if (col == 12){ col =3; row =1; } else if (col ==10) col = 12; else if (col ==7) col = 10; else if (col == 4) { col = 7; } else if (col ==3 && row == 1) { col = 4; row = 0; } else col = 4; else col = 4; }else if(state_menu){ if(col == 3){ col = 9; } else if(col == 9){ col = 3; } }else if(state_alarm){ if (col >0 && col <13) if (col == 12){ col =9; row =1; } else if (col ==10) col = 12; else if (col ==7) col = 10; else if (col == 4) { col = 7; } else if (col ==3 && row == 1) { col = 4; row = 0; } else if(col == 9 && row == 1){ col = 3; row = 1; } else col = 4; }else col = 4; pc.printf("Cursor right: %i,%i\r\n", col, row); } void chg_time_up(){ if(state_init){ if (col ==10){ r_sec = r_sec +1; if (r_sec > 59) r_sec = 0; } else if (col ==7){ r_min = r_min + 1; if (r_min > 59) r_min = 0; } else if (col ==4){ r_hour = r_hour + 1; if (r_hour > 12) r_hour = 1;} else if (col == 12){ if(r_apm==0) r_apm=1; else r_apm=0; } else pc.printf("lmfao"); pc.printf("\nUP: %d:%d:%d %d\r\n", r_hour, r_min, r_sec, r_apm); } else if(state_alarm) { if (col ==10){ alarm_sec = alarm_sec +1; if (alarm_sec > 59) alarm_sec = 0; } else if (col ==7){ alarm_min = alarm_min + 1; if (alarm_min > 59) alarm_min = 0; } else if (col ==4){ alarm_hour = alarm_hour + 1; if (alarm_hour > 12) alarm_hour = 1;} else if (col == 12){ if(alarm_apm==0) alarm_apm=1; else alarm_apm=0; } else pc.printf("lmfao"); pc.printf("\nUP: %d:%d:%d %d\r\n", alarm_hour, alarm_min, alarm_sec, alarm_apm); } } void chg_time_down() { if(state_init){ if (col ==10){ r_sec = r_sec - 1; if (r_sec < 0) r_sec = 59; } else if (col ==7){ r_min = r_min - 1; if (r_min < 0) r_min = 59;} else if (col ==4){ r_hour = r_hour - 1; if (r_hour < 1) r_hour = 12; } else if (col == 12){ if(r_apm==0) r_apm=1; else r_apm=0; } else pc.printf("lmfao"); } else if(state_alarm){ if (col ==10){ alarm_sec = alarm_sec - 1; if (alarm_sec < 0) alarm_sec = 59; } else if (col ==7){ alarm_min = alarm_min - 1; if (alarm_min < 0) alarm_min = 59;} else if (col ==4){ alarm_hour = alarm_hour - 1; if (alarm_hour < 1) alarm_hour = 12; } else if (col == 12){ if(alarm_apm==0) alarm_apm=1; else alarm_apm=0; } else pc.printf("lmfao"); } pc.printf("\nDOWN: %d:%d:%d\r\n", r_hour, r_min, r_sec); } void show_menu(){ if(alarm_up){ lcd.locate(2,1); lcd.printf("*"); }else { lcd.locate(2,1); lcd.printf(" "); } lcd.locate(3,1); 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(){ //we basically, comparing 2 strings. alarm string and the clock strings if match we ring it if(clock_string == alarm_string){ pc.printf("%s %s : same\r\n", alarm_string, clock_string); blinker_cursor.detach(); alarm_sound(); } } void blink_cursor(){ lcd.locate(col, row); lcd.printf("_"); } void blink_num() { if (r_hour > 9){ lcd.locate(3,0); lcd.printf("%d:", r_hour); } else{ lcd.locate(3,0); lcd.printf("0%d:", r_hour); } if (r_min > 9){ lcd.locate(6,0); lcd.printf("%d:", r_min); } else{ lcd.locate(6,0); lcd.printf("0%d:", r_min); } if (r_sec > 9){ lcd.locate(9,0); lcd.printf("%d", r_sec); } else{ lcd.locate(9,0); lcd.printf("0%d", r_sec); } lcd.locate(12,0); if (r_apm ==0) lcd.printf("AM"); else lcd.printf("PM"); lcd.locate(3,1); lcd.printf("Done"); return; } void alarm_disp(){ lcd.cls(); if (alarm_hour > 9){ lcd.locate(3,0); lcd.printf("%d:", alarm_hour); } else{ lcd.locate(3,0); lcd.printf("0%d:", alarm_hour); } if (alarm_min > 9){ lcd.locate(6,0); lcd.printf("%d:", alarm_min); } else{ lcd.locate(6,0); lcd.printf("0%d:", alarm_min); } if (alarm_sec > 9){ lcd.locate(9,0); lcd.printf("%d", alarm_sec); } else{ lcd.locate(9,0); lcd.printf("0%d", alarm_sec); } lcd.locate(12,0); if (alarm_apm ==0) lcd.printf("AM"); else lcd.printf("PM"); lcd.locate(3,1); lcd.printf("Off"); lcd.locate(9,1); lcd.printf("On"); return; } 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) { wait(1); 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) {//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; } } } } 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_numer.detach(); //Calculate the unix timestamp //1h : 3600, 1m: 60, 1s: 1 if(r_apm==0){// AM if(r_hour ==12) epoch = (0 *3600) + (r_min *60) +(r_sec); else epoch = (r_hour *3600) + (r_min *60) +(r_sec); } else if (r_apm ==1){//PM if(r_hour ==12) epoch = (r_hour *3600) + (r_min *60) +(r_sec); else epoch = 43200+(r_hour *3600) + (r_min *60) +(r_sec); } pc.printf("%d\r\n", epoch); set_time(epoch); state_init = false; break; } } } } void view_clock() { lcd.locate(3,0); time_t seconds = time(NULL); char buffer[32]; strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds)); lcd.printf("%s", buffer); show_menu(); clock_string = buffer; } void main_menu(){ state_menu = true; blinker_cursor.attach(&blink_cursor, 0.1); while(1){ wait(1); if(col == 3 && row == 1){ if(joycenter){ clock_viewer.detach(); setup_alarm(); clock_viewer.attach(&view_clock,1); } } else if(col == 9 && row ==1){ if(joycenter){ clock_viewer.detach(); setup_init(); clock_viewer.attach(&view_clock,1); } } } } int main(){ // Initial value row =0 ; col=0 ; alarm_hour=12; alarm_min=0; alarm_sec=10; alarm_apm=0; r_hour=12 ; r_min=0 ; r_sec = 0; r_apm = 0; //0-AM, 1-PM epoch = 0; // 12:00:00 AM // Change the cursor value. left.rise(&cursor_left); right.rise(&cursor_right); up.rise(&chg_time_up); down.rise(&chg_time_down); /*Run setup init -> show main menu |_ setup Alarm | |_Off alarm | |_On alarm |_ Change time */ setup_init(); clock_viewer.attach(&view_clock,1); lcd.cls(); main_menu(); }