Whitworth-EN173-2016
/
SmartRise_MBED
Alarm Clock
Fork of SmartRise_MBED by
TimeControl.cpp@6:73866b51e0b7, 2016-01-26 (annotated)
- Committer:
- asloop18
- Date:
- Tue Jan 26 01:14:35 2016 +0000
- Revision:
- 6:73866b51e0b7
- Parent:
- 4:33f3750fe70a
- Child:
- 7:5412a6d7ef6d
time functions isolated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asloop18 | 4:33f3750fe70a | 1 | #include "mbed.h" |
asloop18 | 4:33f3750fe70a | 2 | #include "TextLCD.h" |
asloop18 | 3:51b248042aa0 | 3 | |
asloop18 | 4:33f3750fe70a | 4 | //IO |
asloop18 | 4:33f3750fe70a | 5 | TextLCD lcd(p27,p28,p23,p24,p25,p26); //RS E d1,d2,d3,d4 |
asloop18 | 4:33f3750fe70a | 6 | DigitalIn HrAdjustUp(p6); |
asloop18 | 4:33f3750fe70a | 7 | DigitalIn HrAdjustDown(p7); |
asloop18 | 4:33f3750fe70a | 8 | DigitalIn MinAdjust(p8); //buttons for setting time and alarm |
asloop18 | 6:73866b51e0b7 | 9 | |
asloop18 | 4:33f3750fe70a | 10 | DigitalOut arm(p12); |
asloop18 | 3:51b248042aa0 | 11 | |
asloop18 | 4:33f3750fe70a | 12 | struct tm* t; |
asloop18 | 4:33f3750fe70a | 13 | int Ahh,AHH,Amm; |
asloop18 | 4:33f3750fe70a | 14 | int HH,mm,hh; //HH 24hr || hh 12hr |
asloop18 | 4:33f3750fe70a | 15 | char d,ad; //am0/pm1 |
asloop18 | 4:33f3750fe70a | 16 | Timer set; |
asloop18 | 4:33f3750fe70a | 17 | |
asloop18 | 4:33f3750fe70a | 18 | void time_init(void){ //starting time and timers |
asloop18 | 4:33f3750fe70a | 19 | set.start(); |
asloop18 | 6:73866b51e0b7 | 20 | AHH=12;Amm=0; |
asloop18 | 4:33f3750fe70a | 21 | } |
asloop18 | 4:33f3750fe70a | 22 | |
asloop18 | 6:73866b51e0b7 | 23 | void timeDisplay(void){ //main time management function and display |
asloop18 | 6:73866b51e0b7 | 24 | time_t rawTime; |
asloop18 | 4:33f3750fe70a | 25 | time(&rawTime); |
asloop18 | 4:33f3750fe70a | 26 | t = localtime(&rawTime); |
asloop18 | 4:33f3750fe70a | 27 | HH=t->tm_hour; mm=t->tm_min; //updating local hr and min var |
asloop18 | 4:33f3750fe70a | 28 | |
asloop18 | 6:73866b51e0b7 | 29 | //TIME format |
asloop18 | 4:33f3750fe70a | 30 | if(HH>12)hh=HH-12; else hh=HH+1; //convert 24 to 12 hr |
asloop18 | 4:33f3750fe70a | 31 | if(HH>=12) d='P'; else d='A'; //update am/pm |
asloop18 | 6:73866b51e0b7 | 32 | //Alarm format |
asloop18 | 4:33f3750fe70a | 33 | if(AHH>12)Ahh=AHH-12; else Ahh=AHH+1; //convert 24 to 12 hr |
asloop18 | 4:33f3750fe70a | 34 | if(AHH>=12) ad='P'; else ad='A'; |
asloop18 | 6:73866b51e0b7 | 35 | |
asloop18 | 4:33f3750fe70a | 36 | lcd.printf(" %2d/%.2d %2d:%.2d%cM Alarm-%2I:%.2I%cM " , t->tm_mon+1,t->tm_mday,hh,t->tm_min,d,Ahh,Amm,ad); //Print date(month/day), time |
asloop18 | 6:73866b51e0b7 | 37 | } |
asloop18 | 4:33f3750fe70a | 38 | |
asloop18 | 3:51b248042aa0 | 39 | |
asloop18 | 3:51b248042aa0 | 40 | |
asloop18 | 4:33f3750fe70a | 41 | void alarmProg(void){ //Alarm set/adjust function to be called in main, when alarm set switch is enabled |
asloop18 | 4:33f3750fe70a | 42 | if(set.read()>0.3){ |
asloop18 | 4:33f3750fe70a | 43 | if(HrAdjustUp==1) {AHH=AHH+1;set.reset();} |
asloop18 | 4:33f3750fe70a | 44 | if(HrAdjustDown==1) {AHH=AHH-1;set.reset();} |
asloop18 | 4:33f3750fe70a | 45 | if(MinAdjust==1) {Amm=Amm+1;set.reset();} |
asloop18 | 4:33f3750fe70a | 46 | if(AHH>23) AHH=0; if(AHH<0) AHH=23; |
asloop18 | 4:33f3750fe70a | 47 | if(AHH>=12) ad='P'; else ad='A'; |
asloop18 | 4:33f3750fe70a | 48 | if(Amm>=60) Amm=0; |
asloop18 | 4:33f3750fe70a | 49 | } |
asloop18 | 4:33f3750fe70a | 50 | } |
asloop18 | 3:51b248042aa0 | 51 | |
asloop18 | 6:73866b51e0b7 | 52 | void timeProg(void){ |
asloop18 | 6:73866b51e0b7 | 53 | time_t seconds = time(NULL); |
asloop18 | 6:73866b51e0b7 | 54 | if(set.read()>0.3){ |
asloop18 | 6:73866b51e0b7 | 55 | if(HrAdjustUp==1){ |
asloop18 | 6:73866b51e0b7 | 56 | set_time(seconds+60*60); |
asloop18 | 6:73866b51e0b7 | 57 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 58 | } |
asloop18 | 6:73866b51e0b7 | 59 | if(HrAdjustDown==1){ |
asloop18 | 6:73866b51e0b7 | 60 | set_time(seconds-60*60); |
asloop18 | 6:73866b51e0b7 | 61 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 62 | } |
asloop18 | 6:73866b51e0b7 | 63 | if(MinAdjust==1){ |
asloop18 | 6:73866b51e0b7 | 64 | set_time(seconds+60); |
asloop18 | 6:73866b51e0b7 | 65 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 66 | } |
asloop18 | 6:73866b51e0b7 | 67 | } |
asloop18 | 6:73866b51e0b7 | 68 | } |
asloop18 | 3:51b248042aa0 | 69 |