Ebrahim Atya
/
grpass_clock
Group assignment alarm clock
main.cpp
- Committer:
- mmlhein
- Date:
- 2019-05-23
- Revision:
- 6:167533262100
- Parent:
- 5:fc218b8312eb
- Child:
- 7:b9d3bc940d2c
File content as of revision 6:167533262100:
#include "mbed.h" #include "TextLCD.h" #include <string> using namespace std; TextLCD lcd(p19, p20, p21, p22, p23, p24); Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer, check_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 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"); } //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"); // } 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); } 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 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){ blinker_numer.detach(); state_alarm = false; state_menu = true; alarm_up = false; check_alarm.detach(); break; } } else if(col == 9 && row == 1){ if(joycenter){ alarm_up = true; blinker_numer.detach(); state_alarm = false; state_menu = true; check_alarm.attach(&alarm_chk,1); 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; } } } void setup_init(){ state_init = true; 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 //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", 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){ lcd.cls(); lcd.locate(3,1); lcd.printf("OK"); } } } } int main(){ // Initial value row =0 ; col=0 ; alarm_hour=12; alarm_min=0; alarm_sec=0; 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(); set_time(epoch); clock_viewer.attach(&view_clock,1); lcd.cls(); main_menu(); }