Zoltan Hudak
/
Clock_Hello
Demo for the Clock library (real time clock driven by a Ticker).
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /** 00002 * Demo for the Clock library <https://developer.mbed.org/users/hudakz/code/Clock/> 00003 * 00004 * Note: Software implemented Real Time Clock driven by a Ticker. 00005 * No external hardware (like DS1307 or DS3231 or etc.) is needed. 00006 * 00007 */ 00008 00009 #include "mbed.h" 00010 #include "Clock.h" 00011 00012 Serial pc(USBTX, USBRX); 00013 00014 00015 Clock rtc; // Create an instance of Clock class (set to 00:00:00 January 1, 1970) 00016 00017 // Create alarms as needed 00018 time_t alarm1 = Clock::asTime(2015, 3, 24, 11, 36, 15); // year, month (1 stands for Jan etc.), day of month, hour, minute, second 00019 time_t alarm2 = Clock::asTime(2015, 3, 24, 11, 37, 30); 00020 00021 volatile bool ticked = false; // tick flag 00022 00023 /** 00024 * @brief Clock tick ISR - event handler (called once a second) 00025 * @note Keep it as short as possible. 00026 * @param 00027 * @retval 00028 */ 00029 void onClockTick(void) { 00030 ticked = true; 00031 } 00032 00033 /** 00034 * @brief Main 00035 * @note 00036 * @param 00037 * @retval 00038 */ 00039 int main() { 00040 time_t rawtime; 00041 struct tm* p_tm; 00042 00043 rtc.attach(onClockTick); // attach a handler function to the rtc's tick event 00044 00045 // 00046 // Set rtc for instance to 2015, March, 24th, 11 hours, 35 minutes, 45 seconds 00047 // 00048 rtc.set(2015, 3, 24, 11, 35, 45); 00049 //rawtime = time(NULL); // if you like to call C library time function 00050 rawtime = rtc.time(); // if you like to call Clock function 00051 00052 p_tm = gmtime(&rawtime); 00053 printf("Current time around the World:\r\n"); 00054 printf("Phoenix, AZ (U.S.) : %d:%d\r\n", (p_tm->tm_hour -7) % 24, p_tm->tm_min); 00055 printf("Reykjavik (Iceland) : %d:%d\r\n", (p_tm->tm_hour + 0) % 24, p_tm->tm_min); 00056 printf("Beijing (China) : %d:%d\r\n", (p_tm->tm_hour + 8) % 24, p_tm->tm_min); 00057 00058 00059 00060 while (true) { 00061 if(ticked == true) { 00062 ticked = false; // clear the flag for next use in ISR 00063 00064 // You draw clock hands on a graphical display 00065 // or update/refresh digital display or etc. 00066 // ... 00067 // In this demo we just display some info on the PC's serial terminal .. 00068 // 00069 pc.printf("==================================================\r\n"); 00070 //rawtime = time(NULL); // if you like you can call C library time function 00071 rawtime = rtc.time(); // or Clock's function 00072 pc.printf("Time as seconds since January 1, 1970 = %d\r\n", rawtime); 00073 pc.printf("Time as a basic string = %s", ctime(&rawtime)); 00074 00075 // 00076 // Use custom format: 00077 // 00078 char buffer[32]; 00079 strftime(buffer, 32, "%I:%M %p", localtime(&rawtime)); 00080 pc.printf("Time as a custom formatted string = %s\r\n", buffer); 00081 00082 // 00083 // Create your own format: 00084 // 00085 pc.printf("Date: %.4d-%.2d-%.2d\r\n", rtc.year(), rtc.mon(), rtc.mday()); 00086 pc.printf("Time: %.2d:%.2d:%.2d\r\n", rtc.hour(), rtc.min(), rtc.sec()); 00087 00088 pc.printf("------------------------------------\r\n"); 00089 00090 // 00091 // Perform periodical tasks: 00092 // 00093 if(rtc.sec() % 10 == 0) { 00094 pc.printf("Called once a 10 seconds\r\n"); 00095 } 00096 00097 if(rtc.sec() == 0) { 00098 pc.printf("Called once a minute.\r\n"); 00099 00100 if(rtc.min() % 5 == 0) 00101 pc.printf("Called once a 5 minutes\r\n"); 00102 00103 if(rtc.min() == 0) { 00104 pc.printf("Called once an hour\r\n"); 00105 00106 if(rtc.hour() % 3 == 0) 00107 pc.printf("Called once a 3 hours\r\n"); 00108 00109 if(rtc.hour() == 0) { 00110 pc.printf("Called at midnight\r\n"); 00111 00112 if(rtc.wday() == 3) 00113 pc.printf("Called on Wednesday at midnight\r\n"); 00114 } 00115 } 00116 } 00117 00118 // 00119 // Trigger the alarms: 00120 // 00121 if(rtc.time() == alarm1) { 00122 pc.printf("Alarm1 triggered!\r\n"); 00123 } 00124 00125 if(rtc.time() == alarm2) { 00126 pc.printf("Alarm2 triggered!\r\n"); 00127 } 00128 00129 pc.printf("------------------------------------\r\n"); 00130 } 00131 } 00132 }
Generated on Wed Jul 13 2022 01:42:24 by 1.7.2