Charles Tritt / Mbed OS 21_SerialPass_v5

Files at this revision

API Documentation at this revision

Comitter:
CSTritt
Date:
Sun Oct 17 02:15:18 2021 +0000
Parent:
115:6ba84689e2c9
Child:
117:e072f162cbce
Commit message:
Initial version. New in 2021.

Changed in this revision

blueText.cpp Show annotated file Show diff for this revision Revisions of this file
clrTerm.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
myFuncs.h Show annotated file Show diff for this revision Revisions of this file
whiteText.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blueText.cpp	Sun Oct 17 02:15:18 2021 +0000
@@ -0,0 +1,21 @@
+/* File: blueText.cpp
+
+This function uses ANSI/VT100 escapse sequences to change text to blue. Serial
+object must be passed by reference (since it is physically unique and can't be
+copied). The call will look normal as does the usage of pc here.
+
+Created by Dr. C. S. Tritt
+Last revised: 10/16/21 (v. 1.0)
+*/
+
+// Need for mbed use.
+#include "mbed.h"
+
+// Including the myFuncs.h header here will catch mismatches between the 
+// declaration and the definition.
+#include "myFuncs.h"
+
+void blueText(Serial & pc){
+    const char ESC = 27; // Define escape character for escape sequence.
+    pc.printf("%c[34m", ESC); // ANSI/VT100 blue text.
+}
\ No newline at end of file
--- a/clrTerm.cpp	Wed Oct 13 13:08:42 2021 +0000
+++ b/clrTerm.cpp	Sun Oct 17 02:15:18 2021 +0000
@@ -1,13 +1,21 @@
 /* File: clrTerm.cpp
 
-This function uses ANSI/VT100 escapse sequences to clear the terminal.
+This function uses ANSI/VT100 escapse sequences to clear the terminal. Serial
+object must be passed by reference (since it is physically unique and can't be
+copied). The call will look normal (clrTerm(pc)) as does the usage of pc here.
 
 Created by Dr. C. S. Tritt
-Last revised: 10/13/21 (v. 1.0)
+Last revised: 10/16/21 (v. 1.0)
 */
+
+// Need for mbed use.
 #include "mbed.h"
 
-void clrTerm(Serial *termPort){
+// Including the myFuncs.h header here will catch mismatches between the 
+// declaration and the definition.
+#include "myFuncs.h"
+
+void clrTerm(Serial & pc){
     const char ESC = 27; // Define escape character for escape sequence.
-    termPort->printf("%c[2J%c[H", ESC, ESC); // ANSI/VT100 clear screen/home.
+    pc.printf("%c[2J%c[H", ESC, ESC); // ANSI/VT100 clear screen/home.
 }
\ No newline at end of file
--- a/main.cpp	Wed Oct 13 13:08:42 2021 +0000
+++ b/main.cpp	Sun Oct 17 02:15:18 2021 +0000
@@ -1,35 +1,64 @@
 /*
-Project: 21_TimeSense_v5
+Project: 21_SerialPass_v5
 File: main.cpp
 
- A simple time sense game.
+Demonstrates how to pass a Serial objectto a function by reference. Also 
+introduces C strings and uses VT-100 escape sequences to prevent scrolling. 
+See...
 
- Uses VT-100 escape sequences to prevent scrolling. See
- http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html and/or
- https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
+* C Strings: https://www.tutorialspoint.com/cprogramming/c_strings.htm.
+* String literal escape sequences (this is a different meaning than the VT-100
+  terminal escape sequences): https://en.cppreference.com/w/cpp/language/escape.
+* VT-100 Escape sequences:  http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html
+* ANSI Escape Sequences:
+  https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
+* ANSI Color Codes, etc.
+  https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html.
 
- Written by: Dr. C. S. Tritt; Last revised 10/13/21 (v. 1.1)
+Calls
+
+clrTerm
+whiteText
+blueText
+
+ Written by: Dr. C. S. Tritt; Last revised 10/16/21 (v. 1.0)
 */
 #include "mbed.h"
 #include "myFuncs.h"
 // Construct a USER_BUTTON digital input.
 DigitalIn myButton(USER_BUTTON);
- // Construct a timer object.
+ // Construct a timer object. Not yet used. May use later.
 Timer myTimer;
  // Construct a transmit only serial connection over our USB.
 Serial pc(USBTX, NC, 9600);
 
 int main()
 {
-    const int TARGET = 5000; // Target time in mS.
-    const int TRIALS = 3; // Number of trials (cycles).
-    int rSum = 0; // Running sum for average in mS.
-
-    clrTerm(&pc); // Clear the terminal.
-    pc.printf("Hold button down for %f seconds.\n", 
-        static_cast<float>(TARGET)/1000.f);
-
-
+    clrTerm(pc); // Clear the terminal at startup.
+    whiteText(pc); // Change text to white.
+    pc.printf("Welcome to SerialPass.\n");
+    pc.printf("Press the ");
+    blueText(pc); // Change text to blue. There are better approaches to this.
+    pc.printf("blue User Button");
+    whiteText(pc); // Change text to white.
+    pc.printf(" to clear this and continue...\n");
+    // Wait here for button to be pressed (making it low, false).
+    while (myButton) {
+        ThisThread::sleep_for(10); // Yield time to other threads.
+    }
+    clrTerm(pc); // Clear the terminal.
+    // This is the better approach. Define some char array constants containing
+    // the escape codes. The \x1B inserts the 1B_16 (escape) characters into the
+    // char vectors (really "C strings" because they are "null terminated"  
+    // (because they were created using the double quotes). Use the strings in
+    // printf statements to send the control text to the terminal. There are 
+    // probably even betters ways to do this, but this is good enough for now.
+    const char red[] = "\x1B[31m";
+    const char white[] = "\x1B[37m";
+    const char blue[] = "\x1B[34m";     
+    pc.printf("The screen should clear and this text appear in...\n");
+    // Note finish with white to leave terminal in a typical state.
+    pc.printf("%sred, %swhite, and %sblue%s.\n", red, white, blue, white);
     while(true) {  // Main forever loop.
         ThisThread::sleep_for(300000); // Sleep for 5 minutes, repeatedly.
     }
--- a/myFuncs.h	Wed Oct 13 13:08:42 2021 +0000
+++ b/myFuncs.h	Sun Oct 17 02:15:18 2021 +0000
@@ -1,9 +1,9 @@
 /* File: myFuncs.h
 
-This file declares my utility functions.
+This file declares my utility functions. Headers must match definitions.
 
 Created by Dr. C. S. Tritt
-Last reviseed: 10/13/21 (v. 1.0
+Last reviseed: 10/16/21 (v. 1.0)
 */
 
 // The next two lines and final endif prevents multiple inclusion.
@@ -11,7 +11,8 @@
 #define MY_FUNCS
 
 // Function declarations...
-void clrTerm(Serial *);
-
+void clrTerm(Serial & pc);
+void blueText(Serial & pc);
+void whiteText(Serial & pc);
 // Ends if. Don't put comments in pre-processor lines.
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/whiteText.cpp	Sun Oct 17 02:15:18 2021 +0000
@@ -0,0 +1,22 @@
+/* File: whiteText.cpp
+
+This function uses ANSI/VT100 escapse sequences to change text to blue. Serial
+object must be passed by reference (since it is physically unique and can't be
+copied). The call will look normal as does the usage of pc here.
+
+Created by Dr. C. S. Tritt
+Last revised: 10/16/21 (v. 1.0)
+*/
+
+// Need for mbed use.
+#include "mbed.h"
+
+// Including the myFuncs.h header here will catch mismatches between the
+// declaration and the definition.
+#include "myFuncs.h"
+
+void whiteText(Serial & pc)
+{
+    const char ESC = 27; // Define escape character for escape sequence.
+    pc.printf("%c[37m", ESC); // ANSI/VT100 white text.
+}
\ No newline at end of file