Several examples run on only mbed-os5.13.0 (not 5.14.0)
Dependencies: BD_SD_DISCO_F769NI BSP_DISCO_F769NI LCD_DISCO_F769NI TS_DISCO_F769NI USBHost_F769NI
z_example/1_rtc.cpp@3:35ac9ee7d2d6, 2019-08-07 (annotated)
- Committer:
- kenjiArai
- Date:
- Wed Aug 07 05:39:01 2019 +0000
- Revision:
- 3:35ac9ee7d2d6
- Child:
- 4:0f4affc00183
1st trial revision (Not finalized yet)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 3:35ac9ee7d2d6 | 1 | /* |
kenjiArai | 3:35ac9ee7d2d6 | 2 | * Mbed Application program / check RTC (Real Time Clock) |
kenjiArai | 3:35ac9ee7d2d6 | 3 | * |
kenjiArai | 3:35ac9ee7d2d6 | 4 | * Copyright (c) 2017,'18,'19 Kenji Arai / JH1PJL |
kenjiArai | 3:35ac9ee7d2d6 | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 3:35ac9ee7d2d6 | 6 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 3:35ac9ee7d2d6 | 7 | * Created: August 11th, 2017 |
kenjiArai | 3:35ac9ee7d2d6 | 8 | * Revised: July 30th, 2019 |
kenjiArai | 3:35ac9ee7d2d6 | 9 | */ |
kenjiArai | 3:35ac9ee7d2d6 | 10 | |
kenjiArai | 3:35ac9ee7d2d6 | 11 | #include "select_program.h" |
kenjiArai | 3:35ac9ee7d2d6 | 12 | //#define EXAMPLE_1_CHECK_RTC |
kenjiArai | 3:35ac9ee7d2d6 | 13 | #ifdef EXAMPLE_1_CHECK_RTC |
kenjiArai | 3:35ac9ee7d2d6 | 14 | |
kenjiArai | 3:35ac9ee7d2d6 | 15 | // Include -------------------------------------------------------------------- |
kenjiArai | 3:35ac9ee7d2d6 | 16 | #include "mbed.h" |
kenjiArai | 3:35ac9ee7d2d6 | 17 | |
kenjiArai | 3:35ac9ee7d2d6 | 18 | // Definition ----------------------------------------------------------------- |
kenjiArai | 3:35ac9ee7d2d6 | 19 | |
kenjiArai | 3:35ac9ee7d2d6 | 20 | // Constructor ---------------------------------------------------------------- |
kenjiArai | 3:35ac9ee7d2d6 | 21 | DigitalIn userSW(BUTTON1); |
kenjiArai | 3:35ac9ee7d2d6 | 22 | DigitalOut myled(LED1); // Indicate the sampling period |
kenjiArai | 3:35ac9ee7d2d6 | 23 | Serial pc(USBTX, USBRX, 115200); |
kenjiArai | 3:35ac9ee7d2d6 | 24 | |
kenjiArai | 3:35ac9ee7d2d6 | 25 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 3:35ac9ee7d2d6 | 26 | |
kenjiArai | 3:35ac9ee7d2d6 | 27 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 3:35ac9ee7d2d6 | 28 | const char * msg0 = "Is a time correct? If no, please hit any key. "; |
kenjiArai | 3:35ac9ee7d2d6 | 29 | const char * msg1 = "<Push USER SW then enter sleep mode> "; |
kenjiArai | 3:35ac9ee7d2d6 | 30 | const char * msg2 = "\r\nEnter Standby Mode, please push RESET to wake-up\r\n"; |
kenjiArai | 3:35ac9ee7d2d6 | 31 | |
kenjiArai | 3:35ac9ee7d2d6 | 32 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 3:35ac9ee7d2d6 | 33 | extern void time_enter_mode(void); |
kenjiArai | 3:35ac9ee7d2d6 | 34 | static void goto_standby(void); |
kenjiArai | 3:35ac9ee7d2d6 | 35 | #if 0 |
kenjiArai | 3:35ac9ee7d2d6 | 36 | static void time_enter_mode(void); |
kenjiArai | 3:35ac9ee7d2d6 | 37 | static void chk_and_set_time(char *ptr); |
kenjiArai | 3:35ac9ee7d2d6 | 38 | static int xatoi (char **str, unsigned long *res); |
kenjiArai | 3:35ac9ee7d2d6 | 39 | static void get_line (char *buff, int len); |
kenjiArai | 3:35ac9ee7d2d6 | 40 | #endif |
kenjiArai | 3:35ac9ee7d2d6 | 41 | //------------------------------------------------------------------------------ |
kenjiArai | 3:35ac9ee7d2d6 | 42 | // Control Program |
kenjiArai | 3:35ac9ee7d2d6 | 43 | //------------------------------------------------------------------------------ |
kenjiArai | 3:35ac9ee7d2d6 | 44 | int main() |
kenjiArai | 3:35ac9ee7d2d6 | 45 | { |
kenjiArai | 3:35ac9ee7d2d6 | 46 | char buf[64]; // data buffer for text |
kenjiArai | 3:35ac9ee7d2d6 | 47 | time_t seconds; |
kenjiArai | 3:35ac9ee7d2d6 | 48 | uint8_t wait_counter = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 49 | |
kenjiArai | 3:35ac9ee7d2d6 | 50 | pc.printf("\x1b[2J\x1b[H %s\r\n %s %s (UTC)\r\n", |
kenjiArai | 3:35ac9ee7d2d6 | 51 | __FILE__, __DATE__, __TIME__); |
kenjiArai | 3:35ac9ee7d2d6 | 52 | myled = !myled; |
kenjiArai | 3:35ac9ee7d2d6 | 53 | ThisThread::sleep_for(1000); |
kenjiArai | 3:35ac9ee7d2d6 | 54 | myled = !myled; |
kenjiArai | 3:35ac9ee7d2d6 | 55 | ThisThread::sleep_for(1000); |
kenjiArai | 3:35ac9ee7d2d6 | 56 | while(1) { |
kenjiArai | 3:35ac9ee7d2d6 | 57 | seconds = time(NULL); |
kenjiArai | 3:35ac9ee7d2d6 | 58 | strftime(buf, 50, " %B %d,'%y, %H:%M:%S,", localtime(&seconds)); |
kenjiArai | 3:35ac9ee7d2d6 | 59 | pc.printf("[Time] %s", buf); |
kenjiArai | 3:35ac9ee7d2d6 | 60 | pc.printf(" sec=%d\r\n", seconds); |
kenjiArai | 3:35ac9ee7d2d6 | 61 | pc.printf("%s", msg0); |
kenjiArai | 3:35ac9ee7d2d6 | 62 | pc.printf("%s\r", msg1); |
kenjiArai | 3:35ac9ee7d2d6 | 63 | wait_counter = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 64 | while (seconds == time(NULL)) { |
kenjiArai | 3:35ac9ee7d2d6 | 65 | if (pc.readable() == 1) { |
kenjiArai | 3:35ac9ee7d2d6 | 66 | buf[0] = pc.getc(); // dummy read |
kenjiArai | 3:35ac9ee7d2d6 | 67 | time_enter_mode(); |
kenjiArai | 3:35ac9ee7d2d6 | 68 | } |
kenjiArai | 3:35ac9ee7d2d6 | 69 | if (userSW == 1) { |
kenjiArai | 3:35ac9ee7d2d6 | 70 | pc.printf("%s", msg2); |
kenjiArai | 3:35ac9ee7d2d6 | 71 | ThisThread::sleep_for(1000); |
kenjiArai | 3:35ac9ee7d2d6 | 72 | myled = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 73 | goto_standby(); |
kenjiArai | 3:35ac9ee7d2d6 | 74 | } |
kenjiArai | 3:35ac9ee7d2d6 | 75 | ThisThread::sleep_for(50); |
kenjiArai | 3:35ac9ee7d2d6 | 76 | if (++wait_counter > (2000 / 50)) { |
kenjiArai | 3:35ac9ee7d2d6 | 77 | break; |
kenjiArai | 3:35ac9ee7d2d6 | 78 | } |
kenjiArai | 3:35ac9ee7d2d6 | 79 | } |
kenjiArai | 3:35ac9ee7d2d6 | 80 | uint8_t n = strlen(msg0) + strlen(msg1); |
kenjiArai | 3:35ac9ee7d2d6 | 81 | for (uint8_t i = 0; i < n; i++) { |
kenjiArai | 3:35ac9ee7d2d6 | 82 | pc.putc(' '); |
kenjiArai | 3:35ac9ee7d2d6 | 83 | } |
kenjiArai | 3:35ac9ee7d2d6 | 84 | pc.printf(" \r"); // Not use '\n' |
kenjiArai | 3:35ac9ee7d2d6 | 85 | myled = !myled; |
kenjiArai | 3:35ac9ee7d2d6 | 86 | } |
kenjiArai | 3:35ac9ee7d2d6 | 87 | } |
kenjiArai | 3:35ac9ee7d2d6 | 88 | |
kenjiArai | 3:35ac9ee7d2d6 | 89 | void goto_standby(void) |
kenjiArai | 3:35ac9ee7d2d6 | 90 | { |
kenjiArai | 3:35ac9ee7d2d6 | 91 | ThisThread::sleep_for(0x7fffffff); // sleep 24 days |
kenjiArai | 3:35ac9ee7d2d6 | 92 | } |
kenjiArai | 3:35ac9ee7d2d6 | 93 | |
kenjiArai | 3:35ac9ee7d2d6 | 94 | #if 0 |
kenjiArai | 3:35ac9ee7d2d6 | 95 | void time_enter_mode(void) |
kenjiArai | 3:35ac9ee7d2d6 | 96 | { |
kenjiArai | 3:35ac9ee7d2d6 | 97 | char *ptr; |
kenjiArai | 3:35ac9ee7d2d6 | 98 | char linebuf[64]; |
kenjiArai | 3:35ac9ee7d2d6 | 99 | |
kenjiArai | 3:35ac9ee7d2d6 | 100 | pc.printf("\r\nSet time into RTC\r\n"); |
kenjiArai | 3:35ac9ee7d2d6 | 101 | pc.printf(" e.g. >19 7 15 10 11 12 -> July 15th,'19, 10:11:12\r\n"); |
kenjiArai | 3:35ac9ee7d2d6 | 102 | pc.printf(" If time is fine, just hit enter\r\n"); |
kenjiArai | 3:35ac9ee7d2d6 | 103 | pc.putc('>'); |
kenjiArai | 3:35ac9ee7d2d6 | 104 | ptr = linebuf; |
kenjiArai | 3:35ac9ee7d2d6 | 105 | get_line(ptr, sizeof(linebuf)); |
kenjiArai | 3:35ac9ee7d2d6 | 106 | pc.printf("\r"); |
kenjiArai | 3:35ac9ee7d2d6 | 107 | chk_and_set_time(ptr); |
kenjiArai | 3:35ac9ee7d2d6 | 108 | } |
kenjiArai | 3:35ac9ee7d2d6 | 109 | |
kenjiArai | 3:35ac9ee7d2d6 | 110 | // Get key input data |
kenjiArai | 3:35ac9ee7d2d6 | 111 | void get_line (char *buff, int len) |
kenjiArai | 3:35ac9ee7d2d6 | 112 | { |
kenjiArai | 3:35ac9ee7d2d6 | 113 | char c; |
kenjiArai | 3:35ac9ee7d2d6 | 114 | int idx = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 115 | |
kenjiArai | 3:35ac9ee7d2d6 | 116 | for (;;) { |
kenjiArai | 3:35ac9ee7d2d6 | 117 | c = pc.getc(); |
kenjiArai | 3:35ac9ee7d2d6 | 118 | if (c == '\r') { |
kenjiArai | 3:35ac9ee7d2d6 | 119 | buff[idx++] = c; |
kenjiArai | 3:35ac9ee7d2d6 | 120 | break; |
kenjiArai | 3:35ac9ee7d2d6 | 121 | } |
kenjiArai | 3:35ac9ee7d2d6 | 122 | if ((c == '\b') && idx) { |
kenjiArai | 3:35ac9ee7d2d6 | 123 | idx--; |
kenjiArai | 3:35ac9ee7d2d6 | 124 | pc.putc(c); |
kenjiArai | 3:35ac9ee7d2d6 | 125 | pc.putc(' '); |
kenjiArai | 3:35ac9ee7d2d6 | 126 | pc.putc(c); |
kenjiArai | 3:35ac9ee7d2d6 | 127 | } |
kenjiArai | 3:35ac9ee7d2d6 | 128 | if (((uint8_t)c >= ' ') && (idx < len - 1)) { |
kenjiArai | 3:35ac9ee7d2d6 | 129 | buff[idx++] = c; |
kenjiArai | 3:35ac9ee7d2d6 | 130 | pc.putc(c); |
kenjiArai | 3:35ac9ee7d2d6 | 131 | } |
kenjiArai | 3:35ac9ee7d2d6 | 132 | } |
kenjiArai | 3:35ac9ee7d2d6 | 133 | buff[idx] = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 134 | pc.putc('\n'); |
kenjiArai | 3:35ac9ee7d2d6 | 135 | } |
kenjiArai | 3:35ac9ee7d2d6 | 136 | |
kenjiArai | 3:35ac9ee7d2d6 | 137 | void chk_and_set_time(char *ptr) |
kenjiArai | 3:35ac9ee7d2d6 | 138 | { |
kenjiArai | 3:35ac9ee7d2d6 | 139 | unsigned long p1; |
kenjiArai | 3:35ac9ee7d2d6 | 140 | struct tm t; |
kenjiArai | 3:35ac9ee7d2d6 | 141 | time_t seconds; |
kenjiArai | 3:35ac9ee7d2d6 | 142 | |
kenjiArai | 3:35ac9ee7d2d6 | 143 | if (xatoi(&ptr, &p1)) { |
kenjiArai | 3:35ac9ee7d2d6 | 144 | t.tm_year = (uint8_t)p1 + 100; |
kenjiArai | 3:35ac9ee7d2d6 | 145 | pc.printf("Year:%d ",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 146 | xatoi( &ptr, &p1 ); |
kenjiArai | 3:35ac9ee7d2d6 | 147 | t.tm_mon = (uint8_t)p1 - 1; |
kenjiArai | 3:35ac9ee7d2d6 | 148 | pc.printf("Month:%d ",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 149 | xatoi( &ptr, &p1 ); |
kenjiArai | 3:35ac9ee7d2d6 | 150 | t.tm_mday = (uint8_t)p1; |
kenjiArai | 3:35ac9ee7d2d6 | 151 | pc.printf("Day:%d ",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 152 | xatoi( &ptr, &p1 ); |
kenjiArai | 3:35ac9ee7d2d6 | 153 | t.tm_hour = (uint8_t)p1; |
kenjiArai | 3:35ac9ee7d2d6 | 154 | pc.printf("Hour:%d ",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 155 | xatoi( &ptr, &p1 ); |
kenjiArai | 3:35ac9ee7d2d6 | 156 | t.tm_min = (uint8_t)p1; |
kenjiArai | 3:35ac9ee7d2d6 | 157 | pc.printf("Min:%d ",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 158 | xatoi( &ptr, &p1 ); |
kenjiArai | 3:35ac9ee7d2d6 | 159 | t.tm_sec = (uint8_t)p1; |
kenjiArai | 3:35ac9ee7d2d6 | 160 | pc.printf("Sec:%d \r\n",(int)p1); |
kenjiArai | 3:35ac9ee7d2d6 | 161 | } else { |
kenjiArai | 3:35ac9ee7d2d6 | 162 | return; |
kenjiArai | 3:35ac9ee7d2d6 | 163 | } |
kenjiArai | 3:35ac9ee7d2d6 | 164 | seconds = mktime(&t); |
kenjiArai | 3:35ac9ee7d2d6 | 165 | set_time(seconds); |
kenjiArai | 3:35ac9ee7d2d6 | 166 | pc.printf( |
kenjiArai | 3:35ac9ee7d2d6 | 167 | "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", |
kenjiArai | 3:35ac9ee7d2d6 | 168 | t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec |
kenjiArai | 3:35ac9ee7d2d6 | 169 | ); |
kenjiArai | 3:35ac9ee7d2d6 | 170 | } |
kenjiArai | 3:35ac9ee7d2d6 | 171 | |
kenjiArai | 3:35ac9ee7d2d6 | 172 | // Change string -> integer |
kenjiArai | 3:35ac9ee7d2d6 | 173 | int xatoi (char **str, unsigned long *res) |
kenjiArai | 3:35ac9ee7d2d6 | 174 | { |
kenjiArai | 3:35ac9ee7d2d6 | 175 | unsigned long val; |
kenjiArai | 3:35ac9ee7d2d6 | 176 | unsigned char c, radix, s = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 177 | |
kenjiArai | 3:35ac9ee7d2d6 | 178 | while ((c = **str) == ' ') { |
kenjiArai | 3:35ac9ee7d2d6 | 179 | (*str)++; |
kenjiArai | 3:35ac9ee7d2d6 | 180 | } |
kenjiArai | 3:35ac9ee7d2d6 | 181 | if (c == '-') { |
kenjiArai | 3:35ac9ee7d2d6 | 182 | s = 1; |
kenjiArai | 3:35ac9ee7d2d6 | 183 | c = *(++(*str)); |
kenjiArai | 3:35ac9ee7d2d6 | 184 | } |
kenjiArai | 3:35ac9ee7d2d6 | 185 | if (c == '0') { |
kenjiArai | 3:35ac9ee7d2d6 | 186 | c = *(++(*str)); |
kenjiArai | 3:35ac9ee7d2d6 | 187 | if (c <= ' ') { |
kenjiArai | 3:35ac9ee7d2d6 | 188 | *res = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 189 | return 1; |
kenjiArai | 3:35ac9ee7d2d6 | 190 | } |
kenjiArai | 3:35ac9ee7d2d6 | 191 | if (c == 'x') { |
kenjiArai | 3:35ac9ee7d2d6 | 192 | radix = 16; |
kenjiArai | 3:35ac9ee7d2d6 | 193 | c = *(++(*str)); |
kenjiArai | 3:35ac9ee7d2d6 | 194 | } else { |
kenjiArai | 3:35ac9ee7d2d6 | 195 | if (c == 'b') { |
kenjiArai | 3:35ac9ee7d2d6 | 196 | radix = 2; |
kenjiArai | 3:35ac9ee7d2d6 | 197 | c = *(++(*str)); |
kenjiArai | 3:35ac9ee7d2d6 | 198 | } else { |
kenjiArai | 3:35ac9ee7d2d6 | 199 | if ((c >= '0')&&(c <= '9')) { |
kenjiArai | 3:35ac9ee7d2d6 | 200 | radix = 8; |
kenjiArai | 3:35ac9ee7d2d6 | 201 | } else { |
kenjiArai | 3:35ac9ee7d2d6 | 202 | return 0; |
kenjiArai | 3:35ac9ee7d2d6 | 203 | } |
kenjiArai | 3:35ac9ee7d2d6 | 204 | } |
kenjiArai | 3:35ac9ee7d2d6 | 205 | } |
kenjiArai | 3:35ac9ee7d2d6 | 206 | } else { |
kenjiArai | 3:35ac9ee7d2d6 | 207 | if ((c < '1')||(c > '9')) { |
kenjiArai | 3:35ac9ee7d2d6 | 208 | return 0; |
kenjiArai | 3:35ac9ee7d2d6 | 209 | } |
kenjiArai | 3:35ac9ee7d2d6 | 210 | radix = 10; |
kenjiArai | 3:35ac9ee7d2d6 | 211 | } |
kenjiArai | 3:35ac9ee7d2d6 | 212 | val = 0; |
kenjiArai | 3:35ac9ee7d2d6 | 213 | while (c > ' ') { |
kenjiArai | 3:35ac9ee7d2d6 | 214 | if (c >= 'a') { |
kenjiArai | 3:35ac9ee7d2d6 | 215 | c -= 0x20; |
kenjiArai | 3:35ac9ee7d2d6 | 216 | } |
kenjiArai | 3:35ac9ee7d2d6 | 217 | c -= '0'; |
kenjiArai | 3:35ac9ee7d2d6 | 218 | if (c >= 17) { |
kenjiArai | 3:35ac9ee7d2d6 | 219 | c -= 7; |
kenjiArai | 3:35ac9ee7d2d6 | 220 | if (c <= 9) { |
kenjiArai | 3:35ac9ee7d2d6 | 221 | return 0; |
kenjiArai | 3:35ac9ee7d2d6 | 222 | } |
kenjiArai | 3:35ac9ee7d2d6 | 223 | } |
kenjiArai | 3:35ac9ee7d2d6 | 224 | if (c >= radix) { |
kenjiArai | 3:35ac9ee7d2d6 | 225 | return 0; |
kenjiArai | 3:35ac9ee7d2d6 | 226 | } |
kenjiArai | 3:35ac9ee7d2d6 | 227 | val = val * radix + c; |
kenjiArai | 3:35ac9ee7d2d6 | 228 | c = *(++(*str)); |
kenjiArai | 3:35ac9ee7d2d6 | 229 | } |
kenjiArai | 3:35ac9ee7d2d6 | 230 | if (s) { |
kenjiArai | 3:35ac9ee7d2d6 | 231 | val = -val; |
kenjiArai | 3:35ac9ee7d2d6 | 232 | } |
kenjiArai | 3:35ac9ee7d2d6 | 233 | *res = val; |
kenjiArai | 3:35ac9ee7d2d6 | 234 | return 1; |
kenjiArai | 3:35ac9ee7d2d6 | 235 | } |
kenjiArai | 3:35ac9ee7d2d6 | 236 | #endif |
kenjiArai | 3:35ac9ee7d2d6 | 237 | |
kenjiArai | 3:35ac9ee7d2d6 | 238 | #endif |