![](/media/cache/profiles/Headshot_Fall2023_Square.jpg.50x50_q85.jpg)
New example. Initial version.
Diff: main.cpp
- Revision:
- 116:8990686eedf5
- Parent:
- 115:6ba84689e2c9
- Child:
- 117:e072f162cbce
--- 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. }