Time adjustment and display. Adjustment via Com line and Display on LCD.

Dependencies:   mbed TextLCD CheckRTC

Revision:
3:aff4a05d7a03
Parent:
2:0e3642d50dcc
diff -r 0e3642d50dcc -r aff4a05d7a03 main.cpp
--- a/main.cpp	Fri Oct 03 10:58:59 2014 +0000
+++ b/main.cpp	Sat Nov 01 02:25:31 2014 +0000
@@ -5,7 +5,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created:  March     27th, 2010
- *      Revised:  October    3rd, 2014
+ *      Revised:  November   1st, 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
@@ -14,44 +14,170 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+//#define USE_TEXT_LCD
+
 //  Include ---------------------------------------------------------------------------------------
 #include "mbed.h"
+#ifdef USE_TEXT_LCD
 #include "TextLCD.h"
+#endif
+#include "CheckRTC.h"
 
 //  Object ----------------------------------------------------------------------------------------
 Serial pc(USBTX, USBRX);
 DigitalOut myled1(LED1);                                // Assign LED1 output port
+#ifdef USE_TEXT_LCD
 TextLCD lcd(p22, p21, p8, p7, p6, p5,TextLCD::LCD40x2);  // rs, e, d4-d7
+#endif
 
 //  RAM -------------------------------------------------------------------------------------------
 
 //  ROM / Constant data ---------------------------------------------------------------------------
 
 //  Function prototypes ---------------------------------------------------------------------------
+void msg_hlp (void);
+void chk_and_set_time(char *ptr);
+void put_rn (void);
+void put_r (void);
+int xatoi (char **str, int32_t *res);
+void get_line (char *buff, int len);
 
 //  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 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
+#ifdef USE_TEXT_LCD
+#define L_CLS(x)                lcd.cls(x) 
+#define L_LOC(x,y)              lcd.locate(0, 0);
+#define L_PRINTF(...)           lcd.printf(__VA_ARGS__)
+#else
+#define L_CLS(x)                {;} 
+#define L_LOC(x,y)              {;}
+#define L_PRINTF(...)           {;}
+#endif
+
+#define STYLE1
+//#define STYLE2
 //#define STYLE_COM
 
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
+//  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;
+
+    CheckRTC();
+    L_CLS();
+    L_LOC(0, 0);
+    L_LOC(0, 0);   // 1st line top
+    //          1234567890123456789012345678901234567890
+    L_PRINTF("  Waiting for time adjustment via com ");
+    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();
+    PRINTF("e.g. >t 14 11 1 8 5 15<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;
+                    L_CLS();
+                    L_LOC(0, 0);   // 1st line top
+                    L_PRINTF("It is %d sec since Jan.1,1970\n", seconds);
+                    L_LOC(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
+                    L_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;
+        }
+    }
+}
+
 //  Put \r\n
-void put_rn ( void )
+void put_rn (void)
 {
     PUTC('\r');
     PUTC('\n');
 }
 
 //  Put \r
-void put_r ( void )
+void put_r (void)
 {
     PUTC('\r');
 }
@@ -157,22 +283,22 @@
     char buf[40];
 
     if (xatoi(&ptr, &p1)) {
-        t.tm_year		= (uint8_t)p1 + 100;
+        t.tm_year       = (uint8_t)p1 + 100;
         PRINTF("Year:%d ",p1);
         xatoi( &ptr, &p1 );
-        t.tm_mon		= (uint8_t)p1 - 1;
+        t.tm_mon        = (uint8_t)p1 - 1;
         PRINTF("Month:%d ",p1);
         xatoi( &ptr, &p1 );
-        t.tm_mday		= (uint8_t)p1;
+        t.tm_mday       = (uint8_t)p1;
         PRINTF("Day:%d ",p1);
         xatoi( &ptr, &p1 );
-        t.tm_hour		= (uint8_t)p1;
+        t.tm_hour       = (uint8_t)p1;
         PRINTF("Hour:%d ",p1);
         xatoi( &ptr, &p1 );
-        t.tm_min    	= (uint8_t)p1;
+        t.tm_min        = (uint8_t)p1;
         PRINTF("Min:%d ",p1);
         xatoi( &ptr, &p1 );
-        t.tm_sec 		= (uint8_t)p1;
+        t.tm_sec        = (uint8_t)p1;
         PRINTF("Sec: %d",p1);
         put_rn();
         seconds = mktime(&t);
@@ -183,105 +309,3 @@
     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;
-
-    lcd.cls();
-    lcd.locate(0, 0);
-    lcd.locate(0, 0);   // 1st line top
-    //          1234567890123456789012345678901234567890
-    lcd.printf("  Waiting for time adjustment via com ");
-    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();
-    PRINTF("e.g. >t 14 10 3 20 5 15<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;
-        }
-    }
-}
\ No newline at end of file