Alarm Clock

Dependents:   IF-SmartClock

Committer:
takashikojo
Date:
Wed Jul 01 22:04:58 2015 +0000
Revision:
2:86a5b9235f16
Parent:
0:d69dc4537754
Child:
3:08b49402846d
add rotateOptions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takashikojo 0:d69dc4537754 1 #include "mbed.h"
takashikojo 0:d69dc4537754 2 #include "DigitalClock.h"
takashikojo 0:d69dc4537754 3 #include "AlarmClock.h"
takashikojo 0:d69dc4537754 4
takashikojo 0:d69dc4537754 5 AlarmClock::AlarmClock (PinName seg0, PinName seg1, PinName seg2, PinName seg3,
takashikojo 0:d69dc4537754 6 PinName seg4, PinName seg5, PinName seg6, PinName dot,
takashikojo 0:d69dc4537754 7 PinName digit0, PinName digit1, PinName digit2, PinName digit3,
takashikojo 0:d69dc4537754 8 PinName alarmB, PinName hourB, PinName minB, PinName toneP
takashikojo 0:d69dc4537754 9 ) :
takashikojo 0:d69dc4537754 10 DigitalClock(seg0, seg1, seg2, seg3, seg4, seg5, seg6, dot, digit0, digit1, digit2, digit3)
takashikojo 0:d69dc4537754 11 {
takashikojo 0:d69dc4537754 12 struct tm at ;
takashikojo 0:d69dc4537754 13 alarmB_stat = false ;
takashikojo 0:d69dc4537754 14 hourB_stat = false ;
takashikojo 0:d69dc4537754 15 minB_stat = false ;
takashikojo 0:d69dc4537754 16 alarmActive = false ;
takashikojo 0:d69dc4537754 17 newAlarmTime= false ;
takashikojo 0:d69dc4537754 18
takashikojo 0:d69dc4537754 19 alarmButton = new DigitalIn(alarmB) ;
takashikojo 0:d69dc4537754 20 hourButton = new DigitalIn(hourB) ;
takashikojo 0:d69dc4537754 21 minButton = new DigitalIn(minB) ;
takashikojo 0:d69dc4537754 22 tone = new PwmOut(toneP) ;
takashikojo 0:d69dc4537754 23
takashikojo 0:d69dc4537754 24 #define JST (9*60*60)
takashikojo 0:d69dc4537754 25 alarmTime = time(NULL) + JST ;
takashikojo 0:d69dc4537754 26 at = *localtime(&alarmTime) ;
takashikojo 0:d69dc4537754 27 alarmTime -= (at.tm_hour*60*60 + at.tm_min*60) ; /* Alarm time set to 00:00 */
takashikojo 0:d69dc4537754 28 }
takashikojo 0:d69dc4537754 29
takashikojo 0:d69dc4537754 30 AlarmClock::~AlarmClock(){ } ;
takashikojo 0:d69dc4537754 31 #define ALARM_RANGE 60
takashikojo 0:d69dc4537754 32 #define JST (9*60*60)
takashikojo 0:d69dc4537754 33 void AlarmClock::checkAlarmTime(void) {
takashikojo 0:d69dc4537754 34 time_t ctTime;
takashikojo 0:d69dc4537754 35 ctTime = time(NULL) + JST ;
takashikojo 0:d69dc4537754 36 struct tm ct ; struct tm at ;
takashikojo 0:d69dc4537754 37 //printf("alarmActive=%d, timeMatched=%d, ctTime=%d, alarmTime=%d\n", alarmActive, timeMatched, ctTime, alarmTime) ;
takashikojo 0:d69dc4537754 38 ct = *localtime(&ctTime) ;
takashikojo 0:d69dc4537754 39 at = *localtime(&alarmTime) ;
takashikojo 0:d69dc4537754 40 if((ct.tm_hour == at.tm_hour) && (ct.tm_min == at.tm_min))
takashikojo 0:d69dc4537754 41 timeMatched = true ;
takashikojo 0:d69dc4537754 42 else timeMatched = false ;
takashikojo 0:d69dc4537754 43 if(alarmActive && timeMatched)
takashikojo 0:d69dc4537754 44 alarmTone(true) ;
takashikojo 0:d69dc4537754 45 else alarmTone(false) ;
takashikojo 0:d69dc4537754 46 }
takashikojo 0:d69dc4537754 47
takashikojo 2:86a5b9235f16 48 static void rotateOptions(bool *alarm, bool *opt, int *count)
takashikojo 2:86a5b9235f16 49 {
takashikojo 2:86a5b9235f16 50 if ( !*alarm && !*opt){ *alarm = true ; }
takashikojo 2:86a5b9235f16 51 else if( *alarm && !*opt){ *opt = true ; *count = 0 ; }
takashikojo 2:86a5b9235f16 52 else if( *alarm && *opt){ *alarm = false ; }
takashikojo 2:86a5b9235f16 53 else if( !*alarm && *opt){ *opt = false ; }
takashikojo 2:86a5b9235f16 54 }
takashikojo 2:86a5b9235f16 55
takashikojo 0:d69dc4537754 56 void AlarmClock::checkAlarmButton() {
takashikojo 0:d69dc4537754 57 struct tm * alarmT ;
takashikojo 0:d69dc4537754 58
takashikojo 0:d69dc4537754 59 //printf("alarmB_stat=%d, alarmButton=%d", alarmB_stat, *alarmButton) ;
takashikojo 0:d69dc4537754 60 if(!alarmB_stat && !*alarmButton) {
takashikojo 0:d69dc4537754 61 stop() ; /* stop updating LED */
takashikojo 0:d69dc4537754 62 alarmT = localtime(&alarmTime);
takashikojo 0:d69dc4537754 63 //printf("Alarm Time = %d:%d\n", alarmT->tm_hour, alarmT->tm_min) ;
takashikojo 0:d69dc4537754 64 setLED(alarmT->tm_hour, alarmT->tm_min) ;
takashikojo 0:d69dc4537754 65 alarmB_stat = true ;
takashikojo 0:d69dc4537754 66 newAlarmTime = false ;
takashikojo 0:d69dc4537754 67 return ;
takashikojo 0:d69dc4537754 68 } else if(alarmB_stat && *alarmButton) {
takashikojo 0:d69dc4537754 69 start() ; /* show current time on LED */
takashikojo 0:d69dc4537754 70 alarmB_stat = false ;
takashikojo 2:86a5b9235f16 71 if(!newAlarmTime)rotateOptions(&alarmActive, &optionActive, &optionCount) ;
takashikojo 0:d69dc4537754 72 else alarmActive = true ;
takashikojo 0:d69dc4537754 73 //printf("alarmActive=%d\n", alarmActive) ;
takashikojo 2:86a5b9235f16 74 setDot(0, alarmActive) ; setDot(2, optionActive) ;
takashikojo 0:d69dc4537754 75 flashLED() ;
takashikojo 0:d69dc4537754 76 }
takashikojo 0:d69dc4537754 77 }
takashikojo 0:d69dc4537754 78
takashikojo 0:d69dc4537754 79 #define HOUR (60*60)
takashikojo 0:d69dc4537754 80 void AlarmClock::checkHourButton() {
takashikojo 0:d69dc4537754 81 struct tm * alarmT ;
takashikojo 0:d69dc4537754 82
takashikojo 0:d69dc4537754 83 //printf(" hourB_stat=%d, hourButton=%d", hourB_stat, hourButton->read()) ;
takashikojo 0:d69dc4537754 84 if((!hourB_stat && !*hourButton) && !*alarmButton) {
takashikojo 0:d69dc4537754 85 alarmTime += HOUR ;
takashikojo 0:d69dc4537754 86 alarmT = localtime(&alarmTime);
takashikojo 0:d69dc4537754 87 setLED(alarmT->tm_hour, alarmT->tm_min) ;
takashikojo 0:d69dc4537754 88 newAlarmTime = true ;
takashikojo 0:d69dc4537754 89 }
takashikojo 0:d69dc4537754 90 }
takashikojo 0:d69dc4537754 91
takashikojo 0:d69dc4537754 92 #define MIN 60
takashikojo 0:d69dc4537754 93 void AlarmClock::checkMinButton() {
takashikojo 0:d69dc4537754 94 struct tm * alarmT ;
takashikojo 0:d69dc4537754 95
takashikojo 0:d69dc4537754 96 //printf(" minB_stat=%d, minButton=%d\n", minB_stat, minButton->read()) ;
takashikojo 0:d69dc4537754 97 if((!minB_stat && !*minButton) && !*alarmButton) {
takashikojo 0:d69dc4537754 98 alarmTime += MIN ;
takashikojo 0:d69dc4537754 99 alarmT = localtime(&alarmTime);
takashikojo 0:d69dc4537754 100 setLED(alarmT->tm_hour, alarmT->tm_min) ;
takashikojo 0:d69dc4537754 101 newAlarmTime = true ;
takashikojo 0:d69dc4537754 102 }
takashikojo 0:d69dc4537754 103 }
takashikojo 0:d69dc4537754 104
takashikojo 0:d69dc4537754 105 void AlarmClock::checkButtons(void) {
takashikojo 0:d69dc4537754 106 if((alarmActive && timeMatched) && (!*alarmButton || !*hourButton || !*minButton)){
takashikojo 0:d69dc4537754 107 alarmActive = false ;
takashikojo 0:d69dc4537754 108 setDot(0, alarmActive) ;
takashikojo 0:d69dc4537754 109 }
takashikojo 0:d69dc4537754 110 if(alarmActive && timeMatched)
takashikojo 0:d69dc4537754 111 alarmTone(true) ;
takashikojo 0:d69dc4537754 112
takashikojo 0:d69dc4537754 113 checkAlarmButton() ;
takashikojo 0:d69dc4537754 114 checkHourButton() ;
takashikojo 0:d69dc4537754 115 checkMinButton() ;
takashikojo 0:d69dc4537754 116 }
takashikojo 0:d69dc4537754 117
takashikojo 0:d69dc4537754 118 void AlarmClock::alarmTone(bool t)
takashikojo 0:d69dc4537754 119 {
takashikojo 0:d69dc4537754 120 if(t) {
takashikojo 0:d69dc4537754 121 tone->write(0.5) ;
takashikojo 0:d69dc4537754 122 tone->period(0.002) ;
takashikojo 0:d69dc4537754 123 } else {
takashikojo 0:d69dc4537754 124 tone->write(0.0) ;
takashikojo 0:d69dc4537754 125 tone->period(0.0) ;
takashikojo 0:d69dc4537754 126 }
takashikojo 0:d69dc4537754 127 }
takashikojo 0:d69dc4537754 128
takashikojo 0:d69dc4537754 129 static int count ;
takashikojo 0:d69dc4537754 130 void AlarmClock::poll(void) {
takashikojo 0:d69dc4537754 131 checkButtons() ;
takashikojo 0:d69dc4537754 132 if((count++ % 10) == 0){
takashikojo 0:d69dc4537754 133 checkAlarmTime() ;
takashikojo 0:d69dc4537754 134 }
takashikojo 0:d69dc4537754 135 }