![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
Time adjustment and display. Adjustment via Com line and Display on LCD.
Dependencies: mbed TextLCD CheckRTC
Diff: main.cpp
- Revision:
- 1:af3fc8a8eb57
- Parent:
- 0:b3d6027d4ef2
- Child:
- 2:0e3642d50dcc
--- a/main.cpp Sat Mar 27 12:31:34 2010 +0000 +++ b/main.cpp Fri Oct 03 10:43:13 2014 +0000 @@ -1,100 +1,285 @@ -// -// RTC Test Program -// Kenji Arai / JH1PJL -// March 27th,2010 Started -// March 27th,2010 -// +/* + * mbed Application program + * + * Copyright (c) 2010-2014 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: March 27th, 2010 + * Revised: October 3rd, 2014 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// Include --------------------------------------------------------------------------------------- #include "mbed.h" #include "TextLCD.h" +// Object ---------------------------------------------------------------------------------------- +Serial pc(USBTX, USBRX); +DigitalOut myled1(LED1); // Assign LED1 output port +TextLCD lcd(p22, p21, p8, p7, p6, p5,TextLCD::LCD40x2); // rs, e, d4-d7 + +// RAM ------------------------------------------------------------------------------------------- + +// ROM / Constant data --------------------------------------------------------------------------- + +// Function prototypes --------------------------------------------------------------------------- + +// Definition ------------------------------------------------------------------------------------ +#define BAUD(x) pc.baud(x) +#define GETC(x) pc.getc(x) +#define PUTC(x) pc.putc(x) +#define PRINTF(...) pc.printf(__VA_ARGS__) +#define READABLE(x) pc.readable(x) + //#define STYLE1 #define STYLE2 - -Serial pc(USBTX, USBRX); -DigitalOut myled1(LED1); // Assign LED1 output port -TextLCD lcd(p22, p28, p27, p26, p25, p24, p23, 40, 2); // rs,rw,e,d0,d1,d2,d3,40char's x 2 lines +//#define STYLE_COM -int hex( char c ) +//------------------------------------------------------------------------------------------------- +// Control Program +//------------------------------------------------------------------------------------------------- +// Put \r\n +void put_rn ( void ) { - if( c<= '/' ) return( 0 ); - if( ( c -= '0' ) <= 9 || 10 <= ( c -= 'A' - '0' - 10 ) && c <= 15 ){ - return( (int)c ); - } - return( 0 ); + PUTC('\r'); + PUTC('\n'); +} + +// Put \r +void put_r ( void ) +{ + PUTC('\r'); } -int conv( void ) +// Change string -> number +int xatoi (char **str, int32_t *res) +{ + unsigned long val; + unsigned char c, radix, s = 0; + + while ((c = **str) == ' ') { + (*str)++; + } + if (c == '-') { + s = 1; + c = *(++(*str)); + } + if (c == '0') { + c = *(++(*str)); + if (c <= ' ') { + *res = 0; + return 1; + } + if (c == 'x') { + radix = 16; + c = *(++(*str)); + } else { + if (c == 'b') { + radix = 2; + c = *(++(*str)); + } else { + if ((c >= '0')&&(c <= '9')) { + radix = 8; + } else { + return 0; + } + } + } + } else { + if ((c < '1')||(c > '9')) { + return 0; + } + radix = 10; + } + val = 0; + while (c > ' ') { + if (c >= 'a') { + c -= 0x20; + } + c -= '0'; + if (c >= 17) { + c -= 7; + if (c <= 9) { + return 0; + } + } + if (c >= radix) { + return 0; + } + val = val * radix + c; + c = *(++(*str)); + } + if (s) { + val = -val; + } + *res = val; + return 1; +} + +// Get key input data +void get_line (char *buff, int len) { char c; - int d; - - c = pc.getc(); - pc.putc(c); - d = hex(c); - c = pc.getc(); - pc.putc(c); - d = d * 10 + hex(c); - return d; + int idx = 0; + + for (;;) { + c = GETC(); + if (c == '\r') { + buff[idx++] = c; + break; + } + if ((c == '\b') && idx) { + idx--; + PUTC(c); + PUTC(' '); + PUTC(c); + } + if (((uint8_t)c >= ' ') && (idx < len - 1)) { + buff[idx++] = c; + PUTC(c); + } + } + buff[idx] = 0; + PUTC('\n'); } -int main() { - char c; +// RTC related subroutines +void chk_and_set_time(char *ptr) +{ + int32_t p1; + struct tm t; + time_t seconds; + char buf[40]; + + if (xatoi(&ptr, &p1)) { + t.tm_year = (uint8_t)p1 + 100; + PRINTF("Year:%d ",p1); + xatoi( &ptr, &p1 ); + t.tm_mon = (uint8_t)p1 - 1; + PRINTF("Month:%d ",p1); + xatoi( &ptr, &p1 ); + t.tm_mday = (uint8_t)p1; + PRINTF("Day:%d ",p1); + xatoi( &ptr, &p1 ); + t.tm_hour = (uint8_t)p1; + PRINTF("Hour:%d ",p1); + xatoi( &ptr, &p1 ); + t.tm_min = (uint8_t)p1; + PRINTF("Min:%d ",p1); + xatoi( &ptr, &p1 ); + t.tm_sec = (uint8_t)p1; + PRINTF("Sec: %d",p1); + put_rn(); + seconds = mktime(&t); + set_time(seconds); + } + seconds = time(NULL); + strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds)); + PRINTF("Time: %s", buf); + put_rn(); +} + +// Help Massage +void msg_hlp (void) +{ + PRINTF("t - Check and set RTC"); + put_rn(); + PRINTF("/ - Show time every second (Esc -> hit any key)"); + put_rn(); + PRINTF("? - Help"); + put_rn(); +} + +int main() +{ + char *ptr; + char linebuf[64]; char buf[40]; time_t seconds; - struct tm t; lcd.cls(); lcd.locate(0, 0); lcd.locate(0, 0); // 1st line top - // 1234567890123456789012345678901234567890 + // 1234567890123456789012345678901234567890 lcd.printf(" Waiting for time adjustment via com "); - printf("\r\n"); - while (1){ - while (!pc.readable()){ - seconds = time(NULL); - printf("Current time is"); - strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d) ", localtime(&seconds)); - printf(" = %s Is this okay? [Enter Y/y or N/n]\r", buf); - } - c = getchar(); - if (c == 'N' || c == 'n'){ - printf("\r\nPlese enter Year 20YY (only 2 digits)\r\n]"); - t.tm_year = 100 + conv(); - printf("\r\nPlese enter Month MM (only 2 digits)\r\n]"); - t.tm_mon = conv(); - printf("\r\nPlese enter Day DD (only 2 digits)\r\n]"); - t.tm_mday = conv(); - printf("\r\nPlese enter Hour HH (only 2 digits)\r\n]"); - t.tm_hour = conv(); - printf("\r\nPlese enter Miniut MM (only 2 digits)\r\n]"); - t.tm_min = conv(); - printf("\r\nPlese enter Second SS (only 2 digits)\r\n]"); - t.tm_sec = conv(); - printf("\r\n"); - seconds = mktime(&t); - set_time(seconds); - } else { - break; + put_rn(); + seconds = time(NULL); + PRINTF("Current time is"); + put_rn(); +#ifdef STYLE_COM + PRINTF("Time: %s", ctime(&seconds)); +#else + strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); + PRINTF("Time: %s", buf); +#endif + put_rn(); + PRINTF("Is it correct time?"); + put_rn(); + PRINTF("YES -> please enter '/'"); + put_rn(); + PRINTF("NO -> please enter t yy mm dd hh mm ss <ret>"); + put_rn(); + for (;;) { + put_r(); + PUTC('>'); + ptr = linebuf; + get_line(ptr, sizeof(linebuf)); + switch (*ptr++) { + //--------------------------------------------------------------------------------- + // check and set RTC + //--------------------------------------------------------------------------------- + case 't' : + put_r(); + chk_and_set_time(ptr); + break; + //--------------------------------------------------------------------------------- + // check and set RTC + //--------------------------------------------------------------------------------- + case '/' : + put_r(); + PRINTF("Current Time -> Plese see LCD also"); + put_rn(); + while (1) { + if (READABLE()) { + break; + } + while ( seconds == time(NULL)) ; + seconds = time(NULL); + myled1 = !myled1; + lcd.cls(); + lcd.locate(0, 0); // 1st line top + lcd.printf("It is %d sec since Jan.1,1970\n", seconds); + lcd.locate(0, 1); // 2nd line top +#ifdef STYLE1 + // 27 Mar 2010 13:24:00 + strftime(buf,40, "%x %X ", localtime(&seconds)); +#endif +#ifdef STYLE2 + // 13:24:00 PM (2010/03/27) + strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); +#endif + lcd.printf("Time = %s", buf); +#ifdef STYLE_COM + PRINTF("Time: %s", ctime(&seconds)); +#else + PRINTF("Time: %s", buf); +#endif + put_rn(); + } + break; + //--------------------------------------------------------------------------------- + // check and set RTC + //--------------------------------------------------------------------------------- + case '?' : + default : + put_r(); + msg_hlp(); + break; } } - printf("\r\n Current Time -> Plese see LCD \r\n"); - for(;;){ - while ( seconds == time(NULL)) ; - seconds = time(NULL); - myled1 = !myled1; - lcd.cls(); - lcd.locate(0, 0); // 1st line top - lcd.printf("It is %d sec since Jan.1,1970\n", seconds); - lcd.locate(0, 1); // 2nd line top - #ifdef STYLE1 - // 27 Mar 2010 13:24:00 - strftime(buf,40, "%x %X ", localtime(&seconds)); - #endif - #ifdef STYLE2 - // 13:24:00 PM (2010/03/27) - strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); - #endif - lcd.printf("Time = %s", buf); - //printf("Time = %s\r", buf); - } } \ No newline at end of file