
Demo for the Clock library (real time clock driven by a Ticker).
Revision 5:dee2286a6b76, committed 2019-01-15
- Comitter:
- hudakz
- Date:
- Tue Jan 15 14:47:50 2019 +0000
- Parent:
- 4:abbab29cf5c1
- Commit message:
- updated
Changed in this revision
--- a/Clock.lib Sat Apr 02 11:17:48 2016 +0000 +++ b/Clock.lib Tue Jan 15 14:47:50 2019 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/hudakz/code/Clock/#bbe7d7474acd +http://developer.mbed.org/users/hudakz/code/Clock/#7edabed68b0f
--- a/main.cpp Sat Apr 02 11:17:48 2016 +0000 +++ b/main.cpp Tue Jan 15 14:47:50 2019 +0000 @@ -1,11 +1,11 @@ /** * Demo for the Clock library <https://developer.mbed.org/users/hudakz/code/Clock/> - * - * Note: Software implemented Real Time Clock driven by a Ticker. + * + * Note: Software implemented Real Time Clock driven by a Ticker. * No external hardware (like DS1307 or DS3231 or etc.) is needed. - * + * */ - + #include "mbed.h" #include "Clock.h" @@ -16,12 +16,12 @@ // Create alarms as needed time_t alarm1 = Clock::asTime(2015, 3, 24, 11, 36, 15); // year, month (1 stands for Jan etc.), day of month, hour, minute, second -time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); +time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); volatile bool ticked = false; // tick flag /** - * @brief Clock tick event handler (called once a second) + * @brief Clock tick ISR - event handler (called once a second) * @note Keep it as short as possible. * @param * @retval @@ -32,86 +32,101 @@ /** * @brief Main - * @note + * @note * @param * @retval */ int main() { - rtc.attach(&onClockTick); // attach a handler function to the rtc's tick event + time_t rawtime; + struct tm* p_tm; + rtc.attach(onClockTick); // attach a handler function to the rtc's tick event + // // Set rtc for instance to 2015, March, 24th, 11 hours, 35 minutes, 45 seconds // rtc.set(2015, 3, 24, 11, 35, 45); + //rawtime = time(NULL); // if you like to call C library time function + rawtime = rtc.time(); // if you like to call Clock function + + p_tm = gmtime(&rawtime); + printf("Current time around the World:\r\n"); + printf("Phoenix, AZ (U.S.) : %d:%d\r\n", (p_tm->tm_hour -7) % 24, p_tm->tm_min); + printf("Reykjavik (Iceland) : %d:%d\r\n", (p_tm->tm_hour + 0) % 24, p_tm->tm_min); + printf("Beijing (China) : %d:%d\r\n", (p_tm->tm_hour + 8) % 24, p_tm->tm_min); + + while (true) { if(ticked == true) { - ticked = false; // clear the flag for next use - - // In your application you can - // draw clock hands on a graphical display - // or update digital display or etc. + ticked = false; // clear the flag for next use in ISR + + // You draw clock hands on a graphical display + // or update/refresh digital display or etc. // ... - // In this demo we just display some info on the connected PC's terminal .. + // In this demo we just display some info on the PC's serial terminal .. // - pc.printf("==================================================\n"); - //time_t time = time(NULL); // you can call C library time function if you like - time_t time = rtc.time(); // or Clock function - pc.printf("Time as seconds since January 1, 1970 = %d\n", time); - pc.printf("Time as a basic string = %s\n", ctime(&time)); - + pc.printf("==================================================\r\n"); + //rawtime = time(NULL); // if you like you can call C library time function + rawtime = rtc.time(); // or Clock's function + pc.printf("Time as seconds since January 1, 1970 = %d\r\n", rawtime); + pc.printf("Time as a basic string = %s", ctime(&rawtime)); + // // Use custom format: // char buffer[32]; - strftime(buffer, 32, "%I:%M %p", localtime(&time)); - pc.printf("Time as a custom formatted string = %s\n", buffer); - + strftime(buffer, 32, "%I:%M %p", localtime(&rawtime)); + pc.printf("Time as a custom formatted string = %s\r\n", buffer); + // // Create your own format: // - pc.printf("Date: %.4d-%.2d-%.2d\n", rtc.year(), rtc.mon(), rtc.mday()); - pc.printf("Time: %.2d:%.2d:%.2d\n", rtc.hour(), rtc.min(), rtc.sec()); + pc.printf("Date: %.4d-%.2d-%.2d\r\n", rtc.year(), rtc.mon(), rtc.mday()); + pc.printf("Time: %.2d:%.2d:%.2d\r\n", rtc.hour(), rtc.min(), rtc.sec()); + pc.printf("------------------------------------\r\n"); + // // Perform periodical tasks: // if(rtc.sec() % 10 == 0) { - pc.printf("Called once per 10 seconds\n"); + pc.printf("Called once a 10 seconds\r\n"); } - + if(rtc.sec() == 0) { - pc.printf("Called once per minute.\n"); - + pc.printf("Called once a minute.\r\n"); + if(rtc.min() % 5 == 0) - pc.printf("Called once per 5 minutes\n"); - + pc.printf("Called once a 5 minutes\r\n"); + if(rtc.min() == 0) { - pc.printf("\Called once per hour\n"); - + pc.printf("Called once an hour\r\n"); + if(rtc.hour() % 3 == 0) - pc.printf("Called once per 3 hours\n"); - + pc.printf("Called once a 3 hours\r\n"); + if(rtc.hour() == 0) { - pc.printf("Called at midnight\n"); - + pc.printf("Called at midnight\r\n"); + if(rtc.wday() == 3) - pc.printf("Called on Wednesday at midnight\n"); + pc.printf("Called on Wednesday at midnight\r\n"); } } } - + // // Trigger the alarms: // if(rtc.time() == alarm1) { - pc.printf("Alarm1 triggered!\n"); + pc.printf("Alarm1 triggered!\r\n"); } - + if(rtc.time() == alarm2) { - pc.printf("Alarm2 triggered!\n"); + pc.printf("Alarm2 triggered!\r\n"); } + + pc.printf("------------------------------------\r\n"); } } -} - +} \ No newline at end of file
--- a/mbed.bld Sat Apr 02 11:17:48 2016 +0000 +++ b/mbed.bld Tue Jan 15 14:47:50 2019 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file