Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile
Diff: Clock.cpp
- Revision:
- 2:c939d0501184
- Parent:
- 0:4e6ae21cbd31
diff -r c47a2f0816bb -r c939d0501184 Clock.cpp --- a/Clock.cpp Mon Dec 08 10:06:35 2014 +0000 +++ b/Clock.cpp Mon Dec 08 23:30:49 2014 +0000 @@ -1,12 +1,12 @@ #include "Clock.h" -bool Clock::isSet = false; Clock::Clock() : timezone(UTC) { - if (!isSet) { - isSet = true; - set_time(1388534400); - } + set_time(1388534400); +} + +void Clock::setAlarmThread(Thread *aThread) { + alarmThread = aThread; } /** @@ -40,6 +40,37 @@ return ntp.setTime("0.pool.ntp.org"); } +void Clock::setAlarm(int hour, int minute, int period) { + time_t currentTime = getTime(); + struct tm *timeinfo = localtime(¤tTime); + if (hour == 12) + hour = 0; + hour = ((period == AM) ? hour : (hour + 12)) % 24; + if (hour <= timeinfo->tm_hour && minute <= timeinfo->tm_hour) { + (timeinfo->tm_mday)++; + } + timeinfo->tm_hour = hour; + timeinfo->tm_min = minute; + timeinfo->tm_sec = 0; + time_t alarmTime = mktime(timeinfo); + alarmTicker.attach(this, &Clock::signalAlarm, difftime(alarmTime, currentTime)); + aSet = true; +} + +bool Clock::alarmSet() { + return aSet; +} + +void Clock::setTimer(int hours, int minutes) { + alarmTicker.attach(this, &Clock::signalAlarm, hours * 3600 + minutes * 60); + aSet = true; +} + +void Clock::deleteAlarm() { + alarmTicker.detach(); + aSet = false; +} + /** * Gets the system time in UTC and converts it according to the given timezone */ @@ -53,3 +84,11 @@ int Clock::getTimezone() { return timezone; } + +void Clock::signalAlarm() { + alarmThread->signal_set(0x1); + alarmTicker.detach(); + aSet = false; +} + +