Display RTC on LCD. User is also able to update RTC from console.

Dependencies:   TextLCD mbed

Revision:
1:1b17dcd80a7d
Parent:
0:bad75bd13618
--- a/main.cpp	Fri Feb 21 10:00:46 2014 +0000
+++ b/main.cpp	Sun Jul 03 18:23:11 2016 +0000
@@ -1,26 +1,128 @@
+// Includes ---------------------------------------------------------------------------------------
 #include "mbed.h"
+#include "TextLCD.h"
+
+// Objects ----------------------------------------------------------------------------------------
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7, 16char, 2 lines
+Ticker ticker;
+time_t currentTime;
 
-DigitalOut myled(LED1);
+const int UPDATE_FREQ = 1; // sec
+char lcdBuffer[32]; // buffer for LCD (32=2x16)
 
+// Function prototypes ---------------------------------------------------------------------------
+void displayOnLcd();
+void updateRealTimeClock(char *buffer);
+void getLineFromSerial(char *keyBuffer, int bufferLength);
+void displayMessageOnConsole();
+
+// Main  -------------------------------------------------------------------------------------
 int main() {
     
-    printf("RTC example\n"); 
-    set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
-    printf("Date and time are set.\n");
-
-    while(1) {
-
-        time_t seconds = time(NULL);
+    char lineBuffer[32];
+    char *pointer;
+    pointer = lineBuffer;
+    
+    // update time for every UPDATE_FREQ sec
+    ticker.attach(&displayOnLcd, UPDATE_FREQ);
 
-        //printf("Time as seconds since January 1, 1970 = %d\n", seconds);
+    // 
+    for(;;) {
+        // show initial message on console
+        displayMessageOnConsole();
         
-        printf("Time as a basic string = %s", ctime(&seconds));
-
-        //char buffer[32];
-        //strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
-        //printf("Time as a custom formatted string = %s", buffer);
-
-        myled = !myled;      
-        wait(1);
+        // get input from console
+        getLineFromSerial(pointer, sizeof(lineBuffer));
+        
+        // update RTC based on input value
+        updateRealTimeClock(pointer);        
     }
 }
+
+// Functions -------------------------------------------------------------------------------------
+// Display Current Time on LCD
+void displayOnLcd() {
+        myled = !myled;
+
+    currentTime = time(NULL);
+    strftime(lcdBuffer, 32, "%Y/%m/%d  %H:%M:%S", localtime(&currentTime));
+
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("RTC:%s", lcdBuffer);
+
+    myled = !myled;      
+};
+
+// Update RTC
+void updateRealTimeClock(char *buffer) {
+    char *tp;
+    char *timeArray[6];
+    int arrayIndex;
+    struct tm struct_time;
+    
+    // extract number from string
+    arrayIndex = 0;
+    tp = strtok( buffer, " /:-" );
+    timeArray[arrayIndex++] = tp;
+    printf("%d ", atoi(tp));
+    while ( tp != NULL && arrayIndex < 6 ) {
+        tp = strtok( NULL," /:-" );
+        timeArray[arrayIndex++] = tp;
+        if ( tp != NULL ) {
+            printf("%d ", atoi(tp));
+        }
+    }
+    printf("\r\n");
+    
+    // store number into time struct   
+    struct_time.tm_year = atoi(timeArray[0]) - 1900;
+    struct_time.tm_mon  = atoi(timeArray[1]) - 1;
+    struct_time.tm_mday = atoi(timeArray[2]);
+    struct_time.tm_hour = atoi(timeArray[3]);
+    struct_time.tm_min  = atoi(timeArray[4]);
+    struct_time.tm_sec  = atoi(timeArray[5]);
+
+    currentTime = mktime(&struct_time);
+    set_time(currentTime);
+}
+
+// Read & display input data from console
+void getLineFromSerial(char *keyBuffer, int bufferLength)
+{
+    char c;
+    int index = 0;
+ 
+    for (;;) {
+        // break if keyBuffer is full
+        if (index >= bufferLength) {
+            break;
+        }
+
+        // read input
+        c = pc.getc();
+        pc.putc(c);
+        
+        // break if end
+        if (c == '\r') {
+            keyBuffer[index++] = c;
+            printf("\n");
+            break;
+        }
+
+        // store in keyBuffer
+        keyBuffer[index++] = c;        
+    }
+}
+
+// display message and ask user to input time
+void displayMessageOnConsole() {
+    currentTime = time(NULL);
+    strftime(lcdBuffer, 32, "%Y/%m/%d %H:%M:%S", localtime(&currentTime));
+
+    printf("Current Time:%s\r\n", lcdBuffer);
+    printf("Enter new Time (YYYY/mm/dd HH:MM:SS)\r\n");
+}
+