Zoltan Hudak
/
Clock_Hello
Demo for the Clock library (real time clock driven by a Ticker).
Diff: main.cpp
- Revision:
- 2:0fcd1b86588b
- Parent:
- 1:0e2956eaae94
- Child:
- 3:0a77d653f8a6
--- a/main.cpp Sat Oct 24 11:20:09 2015 +0000 +++ b/main.cpp Sun Nov 29 19:51:16 2015 +0000 @@ -1,21 +1,22 @@ /** * Demonstrates how to use the Clock library * - * Note: It's a software implemented Real Time Clock. No external hardware (like DS1307 or DS3231 or etc.) is needed. + * Note: It's a software implemented Real Time Clock. + * No external hardware (like DS1307 or DS3231 or etc.) is needed. * */ #include "mbed.h" #include "Clock.h" -Serial serial(USBTX, USBRX); +Serial pc(USBTX, USBRX); -Clock rtc; // Create an instance of Clock class (set to 00:00:00 January 1, 1970) +Clock rtc; // Create an instance of Clock class (set to 00:00:00 1 January, 1970) -// Create some alarm times +// Create some alarms 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 +time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); volatile bool ticked = false; // tick flag @@ -31,69 +32,71 @@ int main() { - rtc.attach(onClockTick); // attach a handler function to the rtc's tick event + 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); // year, month (1 stands for Jan etc.), day of month, hour, minute, second + rtc.set(2015, 3, 24, 11, 35, 45); 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 here + // or update digital display etc. + // ... + // In this demo we just print some info .. // - // Draw (update) clock hands on a graphical display - // or show some time related information .. - // - serial.printf("==================================================\r\n"); + pc.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)); + pc.printf("Time as seconds since January 1, 1970 = %d\r\n", time); + pc.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); + pc.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()); + 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()); // // Perform periodical tasks: // if(rtc.sec() % 10 == 0) { - serial.printf("\r\n Called each 10 seconds\r\n\r\n"); + pc.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"); + pc.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"); + pc.printf("\r\n Called each 5 minutes\r\n"); if(rtc.min() == 0) - serial.printf("\r\n Called each hour\r\n"); + pc.printf("\r\n Called each hour\r\n"); if(rtc.hour() == 0) - serial.printf("\r\n Called at midnight\r\n"); + pc.printf("\r\n Called at midnight\r\n"); } // // Trigger alarms: // if(rtc.time() == alarm1) { - serial.printf("\r\n Alarm1 triggered!\r\n\r\n"); + pc.printf("\r\n Alarm1 triggered!\r\n\r\n"); } if(rtc.time() == alarm2) { - serial.printf("\r\n Alarm2 triggered!\r\n\r\n"); + pc.printf("\r\n Alarm2 triggered!\r\n\r\n"); } } }