
Demo for the Clock library (real time clock driven by a Ticker).
main.cpp@0:81acb52fed21, 2015-04-30 (annotated)
- Committer:
- hudakz
- Date:
- Thu Apr 30 10:31:45 2015 +0000
- Revision:
- 0:81acb52fed21
- Child:
- 1:0e2956eaae94
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:81acb52fed21 | 1 | /** |
hudakz | 0:81acb52fed21 | 2 | * Demonstrates how to use the Clock library |
hudakz | 0:81acb52fed21 | 3 | * |
hudakz | 0:81acb52fed21 | 4 | * Note: It's a soft Real Time Clock. No external hardware is needed. |
hudakz | 0:81acb52fed21 | 5 | * |
hudakz | 0:81acb52fed21 | 6 | */ |
hudakz | 0:81acb52fed21 | 7 | |
hudakz | 0:81acb52fed21 | 8 | #include "mbed.h" |
hudakz | 0:81acb52fed21 | 9 | #include "Clock.h" |
hudakz | 0:81acb52fed21 | 10 | |
hudakz | 0:81acb52fed21 | 11 | Serial serial(USBTX, USBRX); |
hudakz | 0:81acb52fed21 | 12 | |
hudakz | 0:81acb52fed21 | 13 | |
hudakz | 0:81acb52fed21 | 14 | Clock rtc; // Create Clock instance (set to 00:00:00 January 1, 1970) |
hudakz | 0:81acb52fed21 | 15 | |
hudakz | 0:81acb52fed21 | 16 | // Create some alarm times |
hudakz | 0:81acb52fed21 | 17 | time_t alarm1 = Clock::asTime(2015, 3, 24, 11, 36, 15); // year, month, day of month, hour, minute, second |
hudakz | 0:81acb52fed21 | 18 | time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); // year, month, day of month, hour, minute, second |
hudakz | 0:81acb52fed21 | 19 | |
hudakz | 0:81acb52fed21 | 20 | // Create flags to support performing time consuming tasks in main() (in case you have such tasks) |
hudakz | 0:81acb52fed21 | 21 | bool minute_tick_flag = false; |
hudakz | 0:81acb52fed21 | 22 | |
hudakz | 0:81acb52fed21 | 23 | /** |
hudakz | 0:81acb52fed21 | 24 | * @brief Clock tick event handler (called once a second) |
hudakz | 0:81acb52fed21 | 25 | * @note Make sure that it takes less than 1 second to execute |
hudakz | 0:81acb52fed21 | 26 | * this function. Otherwise the rtc will fall behind! |
hudakz | 0:81acb52fed21 | 27 | * One technique is to set some flags here and |
hudakz | 0:81acb52fed21 | 28 | * then test them in the while loop of main(). |
hudakz | 0:81acb52fed21 | 29 | * See minute_tick_flag. |
hudakz | 0:81acb52fed21 | 30 | * @param |
hudakz | 0:81acb52fed21 | 31 | * @retval |
hudakz | 0:81acb52fed21 | 32 | */ |
hudakz | 0:81acb52fed21 | 33 | void onRtcTick(void) { |
hudakz | 0:81acb52fed21 | 34 | serial.printf("==================================================\r\n"); |
hudakz | 0:81acb52fed21 | 35 | // time_t time = time(NULL); // you can call C time function if you like |
hudakz | 0:81acb52fed21 | 36 | time_t time = rtc.time(); // or rtc equivalent |
hudakz | 0:81acb52fed21 | 37 | serial.printf("Time as seconds since January 1, 1970 = %d\r\n", time); |
hudakz | 0:81acb52fed21 | 38 | serial.printf("Time as a basic string = %s\r\n", ctime(&time)); |
hudakz | 0:81acb52fed21 | 39 | |
hudakz | 0:81acb52fed21 | 40 | // You can use also custom format. |
hudakz | 0:81acb52fed21 | 41 | char buffer[32]; |
hudakz | 0:81acb52fed21 | 42 | strftime(buffer, 32, "%I:%M %p", localtime(&time)); |
hudakz | 0:81acb52fed21 | 43 | serial.printf("Time as a custom formatted string = %s\r\n", buffer); |
hudakz | 0:81acb52fed21 | 44 | // Or design your own: |
hudakz | 0:81acb52fed21 | 45 | serial.printf("Date: %.4d-%.2d-%.2d\r\n", rtc.year(), rtc.mon(), rtc.mday()); |
hudakz | 0:81acb52fed21 | 46 | serial.printf("Time: %.2d:%.2d:%.2d\r\n", rtc.hour(), rtc.min(), rtc.sec()); |
hudakz | 0:81acb52fed21 | 47 | |
hudakz | 0:81acb52fed21 | 48 | // But if it takes too much time (more than 1s) to execute this Interrupt Service Routine |
hudakz | 0:81acb52fed21 | 49 | // then the RTC will fall behind! |
hudakz | 0:81acb52fed21 | 50 | // That's why we shall keep this routine as short as possible. |
hudakz | 0:81acb52fed21 | 51 | // For time consuming tasks we better set flags here and after testing them in the main() |
hudakz | 0:81acb52fed21 | 52 | // perform the task there. For an example have a look at the minute_tick_flag. |
hudakz | 0:81acb52fed21 | 53 | |
hudakz | 0:81acb52fed21 | 54 | // |
hudakz | 0:81acb52fed21 | 55 | // Execute periodical tasks |
hudakz | 0:81acb52fed21 | 56 | // |
hudakz | 0:81acb52fed21 | 57 | if(rtc.sec() % 10 == 0) { |
hudakz | 0:81acb52fed21 | 58 | serial.printf("\r\n Called each 10 seconds\r\n\r\n"); |
hudakz | 0:81acb52fed21 | 59 | } |
hudakz | 0:81acb52fed21 | 60 | |
hudakz | 0:81acb52fed21 | 61 | if(rtc.sec() == 0) { |
hudakz | 0:81acb52fed21 | 62 | serial.printf("\r\n Called each minute.\r\n\r\n"); |
hudakz | 0:81acb52fed21 | 63 | |
hudakz | 0:81acb52fed21 | 64 | if(rtc.min() % 5 == 0) |
hudakz | 0:81acb52fed21 | 65 | serial.printf("\r\n Called each 5 minutes\r\n"); |
hudakz | 0:81acb52fed21 | 66 | |
hudakz | 0:81acb52fed21 | 67 | if(rtc.min() == 0) |
hudakz | 0:81acb52fed21 | 68 | serial.printf("\r\n Called each hour\r\n"); |
hudakz | 0:81acb52fed21 | 69 | |
hudakz | 0:81acb52fed21 | 70 | if(rtc.hour() == 0) |
hudakz | 0:81acb52fed21 | 71 | serial.printf("\r\n Called at midnight\r\n"); |
hudakz | 0:81acb52fed21 | 72 | |
hudakz | 0:81acb52fed21 | 73 | minute_tick_flag = true; // set the flag here (to be tested and reset in main()) |
hudakz | 0:81acb52fed21 | 74 | } |
hudakz | 0:81acb52fed21 | 75 | |
hudakz | 0:81acb52fed21 | 76 | // |
hudakz | 0:81acb52fed21 | 77 | // Trigger alarms |
hudakz | 0:81acb52fed21 | 78 | // |
hudakz | 0:81acb52fed21 | 79 | if(rtc.time() == alarm1) { |
hudakz | 0:81acb52fed21 | 80 | serial.printf("\r\n Alarm1 triggered!\r\n\r\n"); |
hudakz | 0:81acb52fed21 | 81 | } |
hudakz | 0:81acb52fed21 | 82 | |
hudakz | 0:81acb52fed21 | 83 | if(rtc.time() == alarm2) { |
hudakz | 0:81acb52fed21 | 84 | serial.printf("\r\n Alarm2 triggered!\r\n\r\n"); |
hudakz | 0:81acb52fed21 | 85 | } |
hudakz | 0:81acb52fed21 | 86 | } |
hudakz | 0:81acb52fed21 | 87 | |
hudakz | 0:81acb52fed21 | 88 | int main() { |
hudakz | 0:81acb52fed21 | 89 | rtc.attach(onRtcTick); // attach a handler function to the rtc tick event |
hudakz | 0:81acb52fed21 | 90 | |
hudakz | 0:81acb52fed21 | 91 | // |
hudakz | 0:81acb52fed21 | 92 | // Set time. For instance 2015, March, 24th, 11 hours, 35 minutes, 45 seconds |
hudakz | 0:81acb52fed21 | 93 | // |
hudakz | 0:81acb52fed21 | 94 | rtc.set(2015, 3, 24, 11, 35, 45); // year, month (1 stands for Jan etc.), day of month, hour, minute, second |
hudakz | 0:81acb52fed21 | 95 | |
hudakz | 0:81acb52fed21 | 96 | while (true) { |
hudakz | 0:81acb52fed21 | 97 | if(minute_tick_flag == true) { |
hudakz | 0:81acb52fed21 | 98 | minute_tick_flag = false; // clear the flag for next use |
hudakz | 0:81acb52fed21 | 99 | // Perform time consuming tasks at defined times! |
hudakz | 0:81acb52fed21 | 100 | serial.printf("Performing some time consuming tasks in the main() ...\r\n\r\n"); |
hudakz | 0:81acb52fed21 | 101 | // ... |
hudakz | 0:81acb52fed21 | 102 | } |
hudakz | 0:81acb52fed21 | 103 | } |
hudakz | 0:81acb52fed21 | 104 | } |