New example. Initial version.

Committer:
CSTritt
Date:
Sun Oct 17 02:15:18 2021 +0000
Revision:
116:8990686eedf5
Parent:
115:6ba84689e2c9
Child:
117:e072f162cbce
Initial version. New in 2021.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 107:61b9c99a4e27 1 /*
CSTritt 116:8990686eedf5 2 Project: 21_SerialPass_v5
CSTritt 109:b061f9830736 3 File: main.cpp
CSTritt 111:956b1c606b66 4
CSTritt 116:8990686eedf5 5 Demonstrates how to pass a Serial objectto a function by reference. Also
CSTritt 116:8990686eedf5 6 introduces C strings and uses VT-100 escape sequences to prevent scrolling.
CSTritt 116:8990686eedf5 7 See...
CSTritt 114:1cfad1babb55 8
CSTritt 116:8990686eedf5 9 * C Strings: https://www.tutorialspoint.com/cprogramming/c_strings.htm.
CSTritt 116:8990686eedf5 10 * String literal escape sequences (this is a different meaning than the VT-100
CSTritt 116:8990686eedf5 11 terminal escape sequences): https://en.cppreference.com/w/cpp/language/escape.
CSTritt 116:8990686eedf5 12 * VT-100 Escape sequences: http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html
CSTritt 116:8990686eedf5 13 * ANSI Escape Sequences:
CSTritt 116:8990686eedf5 14 https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
CSTritt 116:8990686eedf5 15 * ANSI Color Codes, etc.
CSTritt 116:8990686eedf5 16 https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html.
CSTritt 114:1cfad1babb55 17
CSTritt 116:8990686eedf5 18 Calls
CSTritt 116:8990686eedf5 19
CSTritt 116:8990686eedf5 20 clrTerm
CSTritt 116:8990686eedf5 21 whiteText
CSTritt 116:8990686eedf5 22 blueText
CSTritt 116:8990686eedf5 23
CSTritt 116:8990686eedf5 24 Written by: Dr. C. S. Tritt; Last revised 10/16/21 (v. 1.0)
CSTritt 107:61b9c99a4e27 25 */
Jonathan Austin 0:2757d7abb7d9 26 #include "mbed.h"
CSTritt 115:6ba84689e2c9 27 #include "myFuncs.h"
CSTritt 115:6ba84689e2c9 28 // Construct a USER_BUTTON digital input.
CSTritt 115:6ba84689e2c9 29 DigitalIn myButton(USER_BUTTON);
CSTritt 116:8990686eedf5 30 // Construct a timer object. Not yet used. May use later.
CSTritt 115:6ba84689e2c9 31 Timer myTimer;
CSTritt 115:6ba84689e2c9 32 // Construct a transmit only serial connection over our USB.
CSTritt 115:6ba84689e2c9 33 Serial pc(USBTX, NC, 9600);
CSTritt 108:eee3167b25b4 34
CSTritt 114:1cfad1babb55 35 int main()
CSTritt 114:1cfad1babb55 36 {
CSTritt 116:8990686eedf5 37 clrTerm(pc); // Clear the terminal at startup.
CSTritt 116:8990686eedf5 38 whiteText(pc); // Change text to white.
CSTritt 116:8990686eedf5 39 pc.printf("Welcome to SerialPass.\n");
CSTritt 116:8990686eedf5 40 pc.printf("Press the ");
CSTritt 116:8990686eedf5 41 blueText(pc); // Change text to blue. There are better approaches to this.
CSTritt 116:8990686eedf5 42 pc.printf("blue User Button");
CSTritt 116:8990686eedf5 43 whiteText(pc); // Change text to white.
CSTritt 116:8990686eedf5 44 pc.printf(" to clear this and continue...\n");
CSTritt 116:8990686eedf5 45 // Wait here for button to be pressed (making it low, false).
CSTritt 116:8990686eedf5 46 while (myButton) {
CSTritt 116:8990686eedf5 47 ThisThread::sleep_for(10); // Yield time to other threads.
CSTritt 116:8990686eedf5 48 }
CSTritt 116:8990686eedf5 49 clrTerm(pc); // Clear the terminal.
CSTritt 116:8990686eedf5 50 // This is the better approach. Define some char array constants containing
CSTritt 116:8990686eedf5 51 // the escape codes. The \x1B inserts the 1B_16 (escape) characters into the
CSTritt 116:8990686eedf5 52 // char vectors (really "C strings" because they are "null terminated"
CSTritt 116:8990686eedf5 53 // (because they were created using the double quotes). Use the strings in
CSTritt 116:8990686eedf5 54 // printf statements to send the control text to the terminal. There are
CSTritt 116:8990686eedf5 55 // probably even betters ways to do this, but this is good enough for now.
CSTritt 116:8990686eedf5 56 const char red[] = "\x1B[31m";
CSTritt 116:8990686eedf5 57 const char white[] = "\x1B[37m";
CSTritt 116:8990686eedf5 58 const char blue[] = "\x1B[34m";
CSTritt 116:8990686eedf5 59 pc.printf("The screen should clear and this text appear in...\n");
CSTritt 116:8990686eedf5 60 // Note finish with white to leave terminal in a typical state.
CSTritt 116:8990686eedf5 61 pc.printf("%sred, %swhite, and %sblue%s.\n", red, white, blue, white);
CSTritt 114:1cfad1babb55 62 while(true) { // Main forever loop.
CSTritt 115:6ba84689e2c9 63 ThisThread::sleep_for(300000); // Sleep for 5 minutes, repeatedly.
CSTritt 108:eee3167b25b4 64 }
CSTritt 113:cc5beacdad5a 65 }