
Demo for the Clock library (real time clock driven by a Ticker).
Revision 1:0e2956eaae94, committed 2015-10-24
- Comitter:
- hudakz
- Date:
- Sat Oct 24 11:20:09 2015 +0000
- Parent:
- 0:81acb52fed21
- Child:
- 2:0fcd1b86588b
- Commit message:
- Updated.
Changed in this revision
Clock.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Clock.lib Thu Apr 30 10:31:45 2015 +0000 +++ b/Clock.lib Sat Oct 24 11:20:09 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/hudakz/code/Clock/#af43d5d263e7 +http://developer.mbed.org/users/hudakz/code/Clock/#0668893c1c6f
--- a/main.cpp Thu Apr 30 10:31:45 2015 +0000 +++ b/main.cpp Sat Oct 24 11:20:09 2015 +0000 @@ -1,7 +1,7 @@ /** * Demonstrates how to use the Clock library * - * Note: It's a soft Real Time Clock. No external hardware is needed. + * Note: It's a software implemented Real Time Clock. No external hardware (like DS1307 or DS3231 or etc.) is needed. * */ @@ -11,94 +11,90 @@ Serial serial(USBTX, USBRX); -Clock rtc; // Create Clock instance (set to 00:00:00 January 1, 1970) +Clock rtc; // Create an instance of Clock class (set to 00:00:00 January 1, 1970) // Create some alarm times -time_t alarm1 = Clock::asTime(2015, 3, 24, 11, 36, 15); // year, month, day of month, hour, minute, second -time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); // year, month, day of month, hour, minute, second +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); // year, month (1 stands for Jan etc.), day of month, hour, minute, second -// Create flags to support performing time consuming tasks in main() (in case you have such tasks) -bool minute_tick_flag = false; +volatile bool ticked = false; // tick flag /** * @brief Clock tick event handler (called once a second) - * @note Make sure that it takes less than 1 second to execute - * this function. Otherwise the rtc will fall behind! - * One technique is to set some flags here and - * then test them in the while loop of main(). - * See minute_tick_flag. + * @note Keep it as short as possible. * @param * @retval */ -void onRtcTick(void) { - serial.printf("==================================================\r\n"); -// time_t time = time(NULL); // you can call C time function if you like - time_t time = rtc.time(); // or rtc equivalent - serial.printf("Time as seconds since January 1, 1970 = %d\r\n", time); - serial.printf("Time as a basic string = %s\r\n", ctime(&time)); - - // You can use also custom format. - char buffer[32]; - strftime(buffer, 32, "%I:%M %p", localtime(&time)); - serial.printf("Time as a custom formatted string = %s\r\n", buffer); - // Or design your own: - serial.printf("Date: %.4d-%.2d-%.2d\r\n", rtc.year(), rtc.mon(), rtc.mday()); - serial.printf("Time: %.2d:%.2d:%.2d\r\n", rtc.hour(), rtc.min(), rtc.sec()); - - // But if it takes too much time (more than 1s) to execute this Interrupt Service Routine - // then the RTC will fall behind! - // That's why we shall keep this routine as short as possible. - // For time consuming tasks we better set flags here and after testing them in the main() - // perform the task there. For an example have a look at the minute_tick_flag. - - // - // Execute periodical tasks - // - if(rtc.sec() % 10 == 0) { - serial.printf("\r\n Called each 10 seconds\r\n\r\n"); - } - - if(rtc.sec() == 0) { - serial.printf("\r\n Called each minute.\r\n\r\n"); - - if(rtc.min() % 5 == 0) - serial.printf("\r\n Called each 5 minutes\r\n"); - - if(rtc.min() == 0) - serial.printf("\r\n Called each hour\r\n"); - - if(rtc.hour() == 0) - serial.printf("\r\n Called at midnight\r\n"); - - minute_tick_flag = true; // set the flag here (to be tested and reset in main()) - } - - // - // Trigger alarms - // - if(rtc.time() == alarm1) { - serial.printf("\r\n Alarm1 triggered!\r\n\r\n"); - } - - if(rtc.time() == alarm2) { - serial.printf("\r\n Alarm2 triggered!\r\n\r\n"); - } +void onClockTick(void) { + ticked = true; } + int main() { - rtc.attach(onRtcTick); // attach a handler function to the rtc tick event + rtc.attach(onClockTick); // attach a handler function to the rtc's tick event // - // Set time. For instance 2015, March, 24th, 11 hours, 35 minutes, 45 seconds + // Set rtc for instance to 2015, March, 24th, 11 hours, 35 minutes, 45 seconds // rtc.set(2015, 3, 24, 11, 35, 45); // year, month (1 stands for Jan etc.), day of month, hour, minute, second while (true) { - if(minute_tick_flag == true) { - minute_tick_flag = false; // clear the flag for next use - // Perform time consuming tasks at defined times! - serial.printf("Performing some time consuming tasks in the main() ...\r\n\r\n"); - // ... + if(ticked == true) { + ticked = false; // clear the flag for next use + + // + // Draw (update) clock hands on a graphical display + // or show some time related information .. + // + serial.printf("==================================================\r\n"); + //time_t time = time(NULL); // you can call C library time function if you like + time_t time = rtc.time(); // or Clock function + serial.printf("Time as seconds since January 1, 1970 = %d\r\n", time); + serial.printf("Time as a basic string = %s\r\n", ctime(&time)); + + // + // Use custom format: + // + char buffer[32]; + strftime(buffer, 32, "%I:%M %p", localtime(&time)); + serial.printf("Time as a custom formatted string = %s\r\n", buffer); + + // + // Design your own format: + // + serial.printf("Date: %.4d-%.2d-%.2d\r\n", rtc.year(), rtc.mon(), rtc.mday()); + serial.printf("Time: %.2d:%.2d:%.2d\r\n", rtc.hour(), rtc.min(), rtc.sec()); + + // + // Perform periodical tasks: + // + if(rtc.sec() % 10 == 0) { + serial.printf("\r\n Called each 10 seconds\r\n\r\n"); + } + + if(rtc.sec() == 0) { + serial.printf("\r\n Called each minute.\r\n\r\n"); + + if(rtc.min() % 5 == 0) + serial.printf("\r\n Called each 5 minutes\r\n"); + + if(rtc.min() == 0) + serial.printf("\r\n Called each hour\r\n"); + + if(rtc.hour() == 0) + serial.printf("\r\n Called at midnight\r\n"); + } + + // + // Trigger alarms: + // + if(rtc.time() == alarm1) { + serial.printf("\r\n Alarm1 triggered!\r\n\r\n"); + } + + if(rtc.time() == alarm2) { + serial.printf("\r\n Alarm2 triggered!\r\n\r\n"); + } } } }