Time adjustment and display. Adjustment via Com line and Display on LCD.
Dependencies: mbed TextLCD CheckRTC
main.cpp
00001 /* 00002 * mbed Application program 00003 * 00004 * Copyright (c) 2010-2014 Kenji Arai / JH1PJL 00005 * http://www.page.sannet.ne.jp/kenjia/index.html 00006 * http://mbed.org/users/kenjiArai/ 00007 * Created: March 27th, 2010 00008 * Revised: November 1st, 2014 00009 * 00010 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00011 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00012 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00013 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00014 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00015 */ 00016 00017 //#define USE_TEXT_LCD 00018 00019 // Include --------------------------------------------------------------------------------------- 00020 #include "mbed.h" 00021 #ifdef USE_TEXT_LCD 00022 #include "TextLCD.h" 00023 #endif 00024 #include "CheckRTC.h" 00025 00026 // Object ---------------------------------------------------------------------------------------- 00027 Serial pc(USBTX, USBRX); 00028 DigitalOut myled1(LED1); // Assign LED1 output port 00029 #ifdef USE_TEXT_LCD 00030 TextLCD lcd(p22, p21, p8, p7, p6, p5,TextLCD::LCD40x2); // rs, e, d4-d7 00031 #endif 00032 00033 // RAM ------------------------------------------------------------------------------------------- 00034 00035 // ROM / Constant data --------------------------------------------------------------------------- 00036 00037 // Function prototypes --------------------------------------------------------------------------- 00038 void msg_hlp (void); 00039 void chk_and_set_time(char *ptr); 00040 void put_rn (void); 00041 void put_r (void); 00042 int xatoi (char **str, int32_t *res); 00043 void get_line (char *buff, int len); 00044 00045 // Definition ------------------------------------------------------------------------------------ 00046 #define BAUD(x) pc.baud(x) 00047 #define GETC(x) pc.getc(x) 00048 #define PUTC(x) pc.putc(x) 00049 #define PRINTF(...) pc.printf(__VA_ARGS__) 00050 #define READABLE(x) pc.readable(x) 00051 00052 #ifdef USE_TEXT_LCD 00053 #define L_CLS(x) lcd.cls(x) 00054 #define L_LOC(x,y) lcd.locate(0, 0); 00055 #define L_PRINTF(...) lcd.printf(__VA_ARGS__) 00056 #else 00057 #define L_CLS(x) {;} 00058 #define L_LOC(x,y) {;} 00059 #define L_PRINTF(...) {;} 00060 #endif 00061 00062 #define STYLE1 00063 //#define STYLE2 00064 //#define STYLE_COM 00065 00066 //------------------------------------------------------------------------------------------------- 00067 // Control Program 00068 //------------------------------------------------------------------------------------------------- 00069 // Help Massage 00070 void msg_hlp (void) 00071 { 00072 PRINTF("t - Check and set RTC"); 00073 put_rn(); 00074 PRINTF("/ - Show time every second (Esc -> hit any key)"); 00075 put_rn(); 00076 PRINTF("? - Help"); 00077 put_rn(); 00078 } 00079 00080 int main() 00081 { 00082 char *ptr; 00083 char linebuf[64]; 00084 char buf[40]; 00085 time_t seconds; 00086 00087 CheckRTC(); 00088 L_CLS(); 00089 L_LOC(0, 0); 00090 L_LOC(0, 0); // 1st line top 00091 // 1234567890123456789012345678901234567890 00092 L_PRINTF(" Waiting for time adjustment via com "); 00093 put_rn(); 00094 seconds = time(NULL); 00095 PRINTF("Current time is"); 00096 put_rn(); 00097 #ifdef STYLE_COM 00098 PRINTF("Time: %s", ctime(&seconds)); 00099 #else 00100 strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); 00101 PRINTF("Time: %s", buf); 00102 #endif 00103 put_rn(); 00104 PRINTF("Is it correct time?"); 00105 put_rn(); 00106 PRINTF("YES -> please enter '/'"); 00107 put_rn(); 00108 PRINTF("NO -> please enter t yy mm dd hh mm ss <ret>"); 00109 put_rn(); 00110 PRINTF("e.g. >t 14 11 1 8 5 15<ret>"); 00111 put_rn(); 00112 for (;;) { 00113 put_r(); 00114 PUTC('>'); 00115 ptr = linebuf; 00116 get_line(ptr, sizeof(linebuf)); 00117 switch (*ptr++) { 00118 //--------------------------------------------------------------------------------- 00119 // check and set RTC 00120 //--------------------------------------------------------------------------------- 00121 case 't' : 00122 put_r(); 00123 chk_and_set_time(ptr); 00124 break; 00125 //--------------------------------------------------------------------------------- 00126 // check and set RTC 00127 //--------------------------------------------------------------------------------- 00128 case '/' : 00129 put_r(); 00130 PRINTF("Current Time -> Plese see LCD also"); 00131 put_rn(); 00132 while (1) { 00133 if (READABLE()) { 00134 break; 00135 } 00136 while ( seconds == time(NULL)) ; 00137 seconds = time(NULL); 00138 myled1 = !myled1; 00139 L_CLS(); 00140 L_LOC(0, 0); // 1st line top 00141 L_PRINTF("It is %d sec since Jan.1,1970\n", seconds); 00142 L_LOC(0, 1); // 2nd line top 00143 #ifdef STYLE1 00144 // 27 Mar 2010 13:24:00 00145 strftime(buf,40, "%x %X ", localtime(&seconds)); 00146 #endif 00147 #ifdef STYLE2 00148 // 13:24:00 PM (2010/03/27) 00149 strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); 00150 #endif 00151 L_PRINTF("Time = %s", buf); 00152 #ifdef STYLE_COM 00153 PRINTF("Time: %s", ctime(&seconds)); 00154 #else 00155 PRINTF("Time: %s", buf); 00156 #endif 00157 put_rn(); 00158 } 00159 break; 00160 //--------------------------------------------------------------------------------- 00161 // check and set RTC 00162 //--------------------------------------------------------------------------------- 00163 case '?' : 00164 default : 00165 put_r(); 00166 msg_hlp(); 00167 break; 00168 } 00169 } 00170 } 00171 00172 // Put \r\n 00173 void put_rn (void) 00174 { 00175 PUTC('\r'); 00176 PUTC('\n'); 00177 } 00178 00179 // Put \r 00180 void put_r (void) 00181 { 00182 PUTC('\r'); 00183 } 00184 00185 // Change string -> number 00186 int xatoi (char **str, int32_t *res) 00187 { 00188 unsigned long val; 00189 unsigned char c, radix, s = 0; 00190 00191 while ((c = **str) == ' ') { 00192 (*str)++; 00193 } 00194 if (c == '-') { 00195 s = 1; 00196 c = *(++(*str)); 00197 } 00198 if (c == '0') { 00199 c = *(++(*str)); 00200 if (c <= ' ') { 00201 *res = 0; 00202 return 1; 00203 } 00204 if (c == 'x') { 00205 radix = 16; 00206 c = *(++(*str)); 00207 } else { 00208 if (c == 'b') { 00209 radix = 2; 00210 c = *(++(*str)); 00211 } else { 00212 if ((c >= '0')&&(c <= '9')) { 00213 radix = 8; 00214 } else { 00215 return 0; 00216 } 00217 } 00218 } 00219 } else { 00220 if ((c < '1')||(c > '9')) { 00221 return 0; 00222 } 00223 radix = 10; 00224 } 00225 val = 0; 00226 while (c > ' ') { 00227 if (c >= 'a') { 00228 c -= 0x20; 00229 } 00230 c -= '0'; 00231 if (c >= 17) { 00232 c -= 7; 00233 if (c <= 9) { 00234 return 0; 00235 } 00236 } 00237 if (c >= radix) { 00238 return 0; 00239 } 00240 val = val * radix + c; 00241 c = *(++(*str)); 00242 } 00243 if (s) { 00244 val = -val; 00245 } 00246 *res = val; 00247 return 1; 00248 } 00249 00250 // Get key input data 00251 void get_line (char *buff, int len) 00252 { 00253 char c; 00254 int idx = 0; 00255 00256 for (;;) { 00257 c = GETC(); 00258 if (c == '\r') { 00259 buff[idx++] = c; 00260 break; 00261 } 00262 if ((c == '\b') && idx) { 00263 idx--; 00264 PUTC(c); 00265 PUTC(' '); 00266 PUTC(c); 00267 } 00268 if (((uint8_t)c >= ' ') && (idx < len - 1)) { 00269 buff[idx++] = c; 00270 PUTC(c); 00271 } 00272 } 00273 buff[idx] = 0; 00274 PUTC('\n'); 00275 } 00276 00277 // RTC related subroutines 00278 void chk_and_set_time(char *ptr) 00279 { 00280 int32_t p1; 00281 struct tm t; 00282 time_t seconds; 00283 char buf[40]; 00284 00285 if (xatoi(&ptr, &p1)) { 00286 t.tm_year = (uint8_t)p1 + 100; 00287 PRINTF("Year:%d ",p1); 00288 xatoi( &ptr, &p1 ); 00289 t.tm_mon = (uint8_t)p1 - 1; 00290 PRINTF("Month:%d ",p1); 00291 xatoi( &ptr, &p1 ); 00292 t.tm_mday = (uint8_t)p1; 00293 PRINTF("Day:%d ",p1); 00294 xatoi( &ptr, &p1 ); 00295 t.tm_hour = (uint8_t)p1; 00296 PRINTF("Hour:%d ",p1); 00297 xatoi( &ptr, &p1 ); 00298 t.tm_min = (uint8_t)p1; 00299 PRINTF("Min:%d ",p1); 00300 xatoi( &ptr, &p1 ); 00301 t.tm_sec = (uint8_t)p1; 00302 PRINTF("Sec: %d",p1); 00303 put_rn(); 00304 seconds = mktime(&t); 00305 set_time(seconds); 00306 } 00307 seconds = time(NULL); 00308 strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds)); 00309 PRINTF("Time: %s", buf); 00310 put_rn(); 00311 }
Generated on Thu Jul 14 2022 19:49:47 by 1.7.2