This program prompts the user for the current date and time and sets the Nucleo real time clock (RTC) accordingly. It demonstrates the use of C data and time functions (defined in time.h, which appears to be included by mbed). It also demonstrates reading keyboard input with echo and extracting numeric values from strings.

Fork of time_HelloWorld by mbed_example

Files at this revision

API Documentation at this revision

Comitter:
CSTritt
Date:
Wed Nov 01 15:53:48 2017 +0000
Parent:
5:9e9f57a3f839
Commit message:
Fixed pass 0 problem in wait interval read loop. Cleaned up output (printfs) and comments.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 9e9f57a3f839 -r ac560c8872cd main.cpp
--- a/main.cpp	Tue Oct 31 18:44:04 2017 +0000
+++ b/main.cpp	Wed Nov 01 15:53:48 2017 +0000
@@ -2,7 +2,7 @@
   Project: time_SetTime
   File: main.cpp
   Modified by: Dr. C. S. Tritt
-  Last modified: 10/31/17 (v. 1.0)
+  Last modified: 10/31/17 (v. 1.1)
 
   This program prompts the user for the current date and time and sets the
   Nucleo real time clock (RTC) accordingly. It demonstrates the use of C data
@@ -10,6 +10,11 @@
   It also demonstrates reading keyboard input with echo and extracting numeric 
   values from strings.
   
+  Versions
+  
+  1.1 - Fixed pass 0 error in time interval loop (switched from while to do 
+  while).
+  
   Notes & Limitations
   
   Entered date and time values are not validated.
@@ -43,7 +48,7 @@
 
     // Get current date from user...
     char cdString[9]; // Current date string. Allow room for LF/null.
-    printf("Enter current date (MM-DD-YY format):\n");
+    pc.printf("Enter current date (MM-DD-YY format):\n");
     int i = 0; // Array (character location) index. Reused below.
     while (i < 9) {
         if (pc.readable()) { // A charactr is waiting...
@@ -53,15 +58,15 @@
         }
     }
     cdString[8] = (char) 0; // Manually insert null character over LF.
-    printf("cdString: %s\n", cdString); // Display string.
+    pc.printf("cdString: %s\n", cdString); // Display string.
     int month, day, year; // Ints to hold month, day and year.
     sscanf(cdString, "%2d-%2d-%2d", &month, &day, &year);
     year = year + 2000; // Convert year to correct style.
-    printf("Month: %d, Day: %d, Year: %d\n", month, day, year);
+    pc.printf("Month: %d, Day: %d, Year: %d\n", month, day, year);
 
     // Get current time from user...
     char tString[9]; // Current time string. Allow room for LF/null.
-    printf("Enter current 24-hour time (hh:mm:ss format):\n");
+    pc.printf("Enter current 24-hour time (hh:mm:ss format):\n");
     i = 0; // Reuse index.
     while (i < 9) {
         if (pc.readable()) { // // A charactr is waiting...
@@ -71,28 +76,29 @@
         }
     }
     tString[8] = (char) 0; // Manually insert null character over LF.
-    pc.putc((char) 10); // Manually output newline.
-    printf("tString: %s\n", tString); // Display string.
+    pc.printf("tString: %s\n", tString); // Display string.
     int hour, min, sec; // Ints to hold hours, minutes and seconds.
     sscanf(tString, "%2d:%2d:%2d", &hour, &min, &sec);
-    printf("Hours: %d, Minutes: %d, Seconds: %d\n", hour, min, sec);
+    pc.printf("Hours: %d, Minutes: %d, Seconds: %d\n", hour, min, sec);
     
     // Get wait between screen updates interval in seconds...
     // Allow for short (1 and 2 character entries).
     char wString[4] = {"   "}; // Wait interval string.
     i = 0; // Reuse index.
-    printf("Enter update interval in seconds (999 max.):\n");
-    while (i < 3 && wString[i-1] != (char) 10) { // Read 3 chars or to LF.
+    pc.printf("Enter update interval in seconds (999 max.):\n");
+    do { // Read 3 chars or to LF.
         if (pc.readable()) { // // A charactr is waiting...
             wString[i] = (char) pc.getc(); // Read a character.
             pc.putc(wString[i]); // Echo the character.
             i++; // Increment i as each character is read.
         }
-    }
+    } while (i < 4 && wString[i-1] != (char) 10);
+    // The following line works regardless of # of digits entered!
     wString[i - 1] = (char) 0; // Manually insert null character over LF.
-    printf("wString: %s\n", wString); // Display string.
+    pc.printf("wString: %s\n", wString); // Display string.
     int waitTime; // Time to wait between screen updates.
     sscanf(wString, "%d", &waitTime);
+    pc.printf("Wait interval: %d\n", waitTime);
     
     // Fill standard library tm structure, hours, minutes & seconds required!
     userTime_tm.tm_year = year - 1900; // Convert to expected basis.
@@ -104,19 +110,19 @@
 
     userTime_tt = mktime (&userTime_tm); // call mktime to create time_t type.
 
-    set_time(userTime_tt);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+    set_time(userTime_tt);  // Set RTC time to entered time.
 
     while (true) { // Loop forever.
         time_t curTime_tt = time(NULL); // Get the current time as a time type.
 
         // Send output to PC...
-        printf("Time as seconds since January 1, 1970 = %d\n", curTime_tt);
-        printf("Current time in...\n");
+        pc.printf("Time as seconds since January 1, 1970 = %d\n", curTime_tt);
+        pc.printf("Current time in...\n");
         // The ctime string ends with a newline.
-        printf("...basic string format = %s", ctime(&curTime_tt));
+        pc.printf("...basic string format = %s", ctime(&curTime_tt));
         char tBuffer[32];
         strftime(tBuffer, 32, "%I:%M %p", localtime(&curTime_tt));
-        printf("...custom string format = %s\n\n", tBuffer);
+        pc.printf("...custom string format = %s\n\n", tBuffer);
 
         wait(waitTime); // Wait between updates.
     }