István Cserny / Mbed 2 deprecated Lab04_rtc_time

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
cspista
Date:
Thu Nov 11 14:43:16 2021 +0000
Parent:
5:0c6401d671c6
Commit message:
Final version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 0c6401d671c6 -r ffcfca43c247 main.cpp
--- a/main.cpp	Tue Oct 23 12:08:10 2018 +0200
+++ b/main.cpp	Thu Nov 11 14:43:16 2021 +0000
@@ -1,34 +1,87 @@
-/* mbed Example Program
- * Copyright (c) 2006-2014 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
 #include "mbed.h"
+Serial pc(USBTX,USBRX);     //UART0 via OpenSDA
+DigitalOut myled(LED1);
+Ticker myticker;
+time_t mytime;
+volatile uint8_t myflag = 0;
+#define DATE_20200222_222222    1582377742      // 2020/2/22 22:22:22 
 
-int main() {
-    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+void processSerialCommand();
+
+void setflag(void)
+{
+    myflag = 1;
+}
 
-    while (true) {
-        time_t seconds = time(NULL);
-        
-        printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
-        
-        printf("Time as a basic string = %s", ctime(&seconds));
-
-        char buffer[32];
-        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
-        printf("Time as a custom formatted string = %s", buffer);
-        
-        wait(1);
+int main()
+{   pc.baud(115200);
+    time_t seconds;
+    seconds = time(NULL);
+    if (seconds < DATE_20200222_222222) {
+      set_time(DATE_20200222_222222);
+    }  
+    myticker.attach(&setflag,5);  
+    while(1) {
+        if(pc.readable()) {        
+            processSerialCommand();          
+        }
+        if(myflag) {
+            mytime = time(NULL);
+            pc.printf("RTC time: %s\r\n",ctime(&mytime));
+            myflag = 0;
+        }
     }
 }
+
+void processSerialCommand()
+{
+    char c = pc.getc();
+    switch(c) {
+        case 'T':
+            // Command to set RTC time
+            // Command format: TYYMMDDHHMMSS
+            // Example: 2012 Oct 21 1:23pm is T121021132300
+            struct tm tme;
+            time_t newTime;
+
+            // Parse incomming 12 ASCII charaters into time_t
+            // no error checking for numeric values in YYMDDHHMMSS fields, so be careful!
+            c = pc.getc();
+            tme.tm_year = c - '0';
+            c = pc.getc();
+            tme.tm_year = 10*tme.tm_year;
+            tme.tm_year += c-'0';
+            tme.tm_year += 100;             //Years are counted from 1900!
+            c = pc.getc();
+            tme.tm_mon = c - '0';
+            c = pc.getc();
+            tme.tm_mon = 10*tme.tm_mon;
+            tme.tm_mon += c-'0'-1;          //corrected by -1 due to a stupid error
+            c = pc.getc();
+            tme.tm_mday = c - '0';
+            c = pc.getc();
+            tme.tm_mday = 10*tme.tm_mday;
+            tme.tm_mday += c-'0';
+            c = pc.getc();
+            tme.tm_hour = c - '0';
+            c = pc.getc();
+            tme.tm_hour = 10*tme.tm_hour;
+            tme.tm_hour += c-'0';
+            c = pc.getc();
+            tme.tm_min = c - '0';
+            c = pc.getc();
+            tme.tm_min = 10*tme.tm_min;
+            tme.tm_min += c-'0';
+            c = pc.getc();
+            tme.tm_sec = c - '0';
+            c = pc.getc();
+            tme.tm_sec = 10*tme.tm_sec;
+            tme.tm_sec += c-'0';
+            newTime = mktime(&tme);
+            set_time(newTime);
+            pc.printf("RTC set to: %s\r\n",ctime(&newTime));
+    }
+    while(pc.readable()) {
+        pc.getc();    // clear serial buffer
+    }
+}
diff -r 0c6401d671c6 -r ffcfca43c247 mbed-os.lib
--- a/mbed-os.lib	Tue Oct 23 12:08:10 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/ARMmbed/mbed-os/#c9e63f14085f5751ff5ead79a7c0382d50a813a2
diff -r 0c6401d671c6 -r ffcfca43c247 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 11 14:43:16 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file