Whitworth-EN173-2016
/
SmartRise_MBED
Alarm Clock
Fork of SmartRise_MBED by
TimeControl.cpp@13:2ba69c3dc08a, 2016-01-27 (annotated)
- Committer:
- pstephens18
- Date:
- Wed Jan 27 04:59:13 2016 +0000
- Revision:
- 13:2ba69c3dc08a
- Parent:
- 12:f42b74f76630
Mission Accomplished
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 |
pstephens18 | 12:f42b74f76630 | 9 | DigitalIn AlarmAdjust2(p9); |
asloop18 | 6:73866b51e0b7 | 10 | |
asloop18 | 4:33f3750fe70a | 11 | DigitalOut arm(p12); |
pstephens18 | 12:f42b74f76630 | 12 | // 1454284799 is the end of Febuary |
asloop18 | 3:51b248042aa0 | 13 | |
asloop18 | 4:33f3750fe70a | 14 | struct tm* t; |
asloop18 | 4:33f3750fe70a | 15 | int Ahh,AHH,Amm; |
asloop18 | 4:33f3750fe70a | 16 | int HH,mm,hh; //HH 24hr || hh 12hr |
asloop18 | 4:33f3750fe70a | 17 | char d,ad; //am0/pm1 |
asloop18 | 4:33f3750fe70a | 18 | Timer set; |
asloop18 | 4:33f3750fe70a | 19 | |
asloop18 | 4:33f3750fe70a | 20 | void time_init(void){ //starting time and timers |
asloop18 | 4:33f3750fe70a | 21 | set.start(); |
pstephens18 | 13:2ba69c3dc08a | 22 | set_time(1454284799+10); |
asloop18 | 6:73866b51e0b7 | 23 | AHH=12;Amm=0; |
asloop18 | 4:33f3750fe70a | 24 | } |
asloop18 | 4:33f3750fe70a | 25 | |
asloop18 | 6:73866b51e0b7 | 26 | void timeDisplay(void){ //main time management function and display |
asloop18 | 6:73866b51e0b7 | 27 | time_t rawTime; |
asloop18 | 4:33f3750fe70a | 28 | time(&rawTime); |
asloop18 | 4:33f3750fe70a | 29 | t = localtime(&rawTime); |
asloop18 | 4:33f3750fe70a | 30 | HH=t->tm_hour; mm=t->tm_min; //updating local hr and min var |
asloop18 | 4:33f3750fe70a | 31 | |
asloop18 | 6:73866b51e0b7 | 32 | //TIME format |
pstephens18 | 12:f42b74f76630 | 33 | if(HH>12)hh=HH-12; else hh=HH+0.1; // I don't know why this makes it work but it does |
pstephens18 | 12:f42b74f76630 | 34 | if(HH ==0){hh = HH+12.1;} |
pstephens18 | 12:f42b74f76630 | 35 | //convert 24 to 12 hr |
asloop18 | 4:33f3750fe70a | 36 | if(HH>=12) d='P'; else d='A'; //update am/pm |
asloop18 | 6:73866b51e0b7 | 37 | //Alarm format |
asloop18 | 9:63098da649d1 | 38 | if(AHH>12)Ahh=AHH-12; else Ahh=AHH; //convert 24 to 12 hr |
asloop18 | 4:33f3750fe70a | 39 | if(AHH>=12) ad='P'; else ad='A'; |
asloop18 | 6:73866b51e0b7 | 40 | |
pstephens18 | 12:f42b74f76630 | 41 | 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 | 42 | } |
asloop18 | 4:33f3750fe70a | 43 | |
asloop18 | 7:5412a6d7ef6d | 44 | char checkAlarm(void){ |
asloop18 | 9:63098da649d1 | 45 | if(HH==AHH&&mm==Amm&&d==ad) return 1; |
asloop18 | 7:5412a6d7ef6d | 46 | else return 0; |
asloop18 | 7:5412a6d7ef6d | 47 | } |
asloop18 | 3:51b248042aa0 | 48 | |
asloop18 | 4:33f3750fe70a | 49 | void alarmProg(void){ //Alarm set/adjust function to be called in main, when alarm set switch is enabled |
asloop18 | 4:33f3750fe70a | 50 | if(set.read()>0.3){ |
asloop18 | 4:33f3750fe70a | 51 | if(HrAdjustUp==1) {AHH=AHH+1;set.reset();} |
asloop18 | 4:33f3750fe70a | 52 | if(HrAdjustDown==1) {AHH=AHH-1;set.reset();} |
asloop18 | 4:33f3750fe70a | 53 | if(MinAdjust==1) {Amm=Amm+1;set.reset();} |
asloop18 | 4:33f3750fe70a | 54 | if(AHH>23) AHH=0; if(AHH<0) AHH=23; |
asloop18 | 4:33f3750fe70a | 55 | if(AHH>=12) ad='P'; else ad='A'; |
asloop18 | 4:33f3750fe70a | 56 | if(Amm>=60) Amm=0; |
asloop18 | 4:33f3750fe70a | 57 | } |
asloop18 | 4:33f3750fe70a | 58 | } |
asloop18 | 3:51b248042aa0 | 59 | |
asloop18 | 6:73866b51e0b7 | 60 | void timeProg(void){ |
asloop18 | 6:73866b51e0b7 | 61 | time_t seconds = time(NULL); |
asloop18 | 6:73866b51e0b7 | 62 | if(set.read()>0.3){ |
pstephens18 | 12:f42b74f76630 | 63 | if(HrAdjustUp==1 && AlarmAdjust2 ==1){ // Adjust Day Forward |
pstephens18 | 12:f42b74f76630 | 64 | set_time(seconds+86400); |
pstephens18 | 12:f42b74f76630 | 65 | set.reset(); |
pstephens18 | 12:f42b74f76630 | 66 | } |
pstephens18 | 12:f42b74f76630 | 67 | else if(HrAdjustUp==1){ |
asloop18 | 6:73866b51e0b7 | 68 | set_time(seconds+60*60); |
asloop18 | 6:73866b51e0b7 | 69 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 70 | } |
pstephens18 | 12:f42b74f76630 | 71 | if(HrAdjustDown==1 && AlarmAdjust2 ==1){ // Adjust Day Backward |
pstephens18 | 12:f42b74f76630 | 72 | set_time(seconds- 86400); |
pstephens18 | 12:f42b74f76630 | 73 | set.reset(); |
pstephens18 | 12:f42b74f76630 | 74 | } |
pstephens18 | 12:f42b74f76630 | 75 | else if(HrAdjustDown==1){ |
asloop18 | 6:73866b51e0b7 | 76 | set_time(seconds-60*60); |
asloop18 | 6:73866b51e0b7 | 77 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 78 | } |
pstephens18 | 12:f42b74f76630 | 79 | else if(MinAdjust==1){ |
asloop18 | 6:73866b51e0b7 | 80 | set_time(seconds+60); |
asloop18 | 6:73866b51e0b7 | 81 | set.reset(); |
asloop18 | 6:73866b51e0b7 | 82 | } |
asloop18 | 6:73866b51e0b7 | 83 | } |
asloop18 | 6:73866b51e0b7 | 84 | } |
asloop18 | 3:51b248042aa0 | 85 |