Kenji Arai
/
ePD_4R2inch_test_program_on_nRF52
test program for 4.2inch e-Paper
Embed:
(wiki syntax)
Show/hide line numbers
set_RTC.cpp
00001 /* 00002 * Set time into RTC 00003 * 00004 * Copyright (c) 2019 Kenji Arai / JH1PJL 00005 * http://www.page.sannet.ne.jp/kenjia/index.html 00006 * http://mbed.org/users/kenjiArai/ 00007 * Created: April 27th, 2019 00008 * Revised: August 23rd, 2019 00009 */ 00010 00011 #include "mbed.h" 00012 00013 extern Serial pc; 00014 00015 static void chk_and_set_time(char *ptr); 00016 static int32_t xatoi (char **str, int32_t *res); 00017 static void get_line (char *buff, int len); 00018 00019 void time_enter_mode(void) 00020 { 00021 char *ptr; 00022 char linebuf[64]; 00023 00024 pc.printf("\r\nSet time into RTC\r\n"); 00025 pc.printf(" e.g. >19 8 23 10 11 12 -> August 23rd,'19, 10:11:12\r\n"); 00026 pc.printf(" If time is fine, just hit enter\r\n"); 00027 pc.putc('>'); 00028 ptr = linebuf; 00029 get_line(ptr, sizeof(linebuf)); 00030 pc.printf("\r"); 00031 chk_and_set_time(ptr); 00032 } 00033 00034 static void get_line (char *buff, int len) 00035 { 00036 char c; 00037 uint32_t idx = 0; 00038 00039 while(true) { 00040 c = pc.getc(); 00041 if (c == '\r') { 00042 buff[idx++] = c; 00043 break; 00044 } 00045 if ((c == '\b') && idx) { 00046 idx--; 00047 pc.putc(c); 00048 pc.putc(' '); 00049 pc.putc(c); 00050 } 00051 if (((uint8_t)c >= ' ') && (idx < len - 1)) { 00052 buff[idx++] = c; 00053 pc.putc(c); 00054 } 00055 } 00056 buff[idx] = 0; 00057 pc.puts("\r\n"); 00058 } 00059 00060 static void chk_and_set_time(char *ptr) 00061 { 00062 int32_t p1; 00063 struct tm t; 00064 time_t seconds; 00065 00066 if (xatoi(&ptr, &p1)) { 00067 t.tm_year = (uint8_t)p1 + 100; 00068 pc.printf("Year:%d ",p1); 00069 xatoi( &ptr, &p1 ); 00070 t.tm_mon = (uint8_t)p1 - 1; 00071 pc.printf("Month:%d ",p1); 00072 xatoi( &ptr, &p1 ); 00073 t.tm_mday = (uint8_t)p1; 00074 pc.printf("Day:%d ",p1); 00075 xatoi( &ptr, &p1 ); 00076 t.tm_hour = (uint8_t)p1; 00077 pc.printf("Hour:%d ",p1); 00078 xatoi( &ptr, &p1 ); 00079 t.tm_min = (uint8_t)p1; 00080 pc.printf("Min:%d ",p1); 00081 xatoi( &ptr, &p1 ); 00082 t.tm_sec = (uint8_t)p1; 00083 pc.printf("Sec: %d \r\n",p1); 00084 } else { 00085 return; 00086 } 00087 seconds = mktime(&t); 00088 set_time(seconds); 00089 pc.printf( 00090 "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", 00091 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec 00092 ); 00093 } 00094 00095 static int32_t xatoi (char **str, int32_t *res) 00096 { 00097 int32_t val; 00098 uint8_t c, radix, s = 0; 00099 00100 while ((c = **str) == ' ') { 00101 (*str)++; 00102 } 00103 if (c == '-') { 00104 s = 1; 00105 c = *(++(*str)); 00106 } 00107 if (c == '0') { 00108 c = *(++(*str)); 00109 if (c <= ' ') { 00110 *res = 0; 00111 return 1; 00112 } 00113 if (c == 'x') { 00114 radix = 16; 00115 c = *(++(*str)); 00116 } else { 00117 if (c == 'b') { 00118 radix = 2; 00119 c = *(++(*str)); 00120 } else { 00121 if ((c >= '0')&&(c <= '9')) { 00122 radix = 8; 00123 } else { 00124 return 0; 00125 } 00126 } 00127 } 00128 } else { 00129 if ((c < '1')||(c > '9')) { 00130 return 0; 00131 } 00132 radix = 10; 00133 } 00134 val = 0; 00135 while (c > ' ') { 00136 if (c >= 'a') { 00137 c -= 0x20; 00138 } 00139 c -= '0'; 00140 if (c >= 17) { 00141 c -= 7; 00142 if (c <= 9) { 00143 return 0; 00144 } 00145 } 00146 if (c >= radix) { 00147 return 0; 00148 } 00149 val = val * radix + c; 00150 c = *(++(*str)); 00151 } 00152 if (s) { 00153 val = -val; 00154 } 00155 *res = val; 00156 return 1; 00157 }
Generated on Thu Jul 14 2022 15:24:22 by 1.7.2