New example. Initial version.

main.cpp

Committer:
CSTritt
Date:
2021-10-17
Revision:
116:8990686eedf5
Parent:
115:6ba84689e2c9
Child:
117:e072f162cbce

File content as of revision 116:8990686eedf5:

/*
Project: 21_SerialPass_v5
File: main.cpp

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...

* 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.

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. Not yet used. May use later.
Timer myTimer;
 // Construct a transmit only serial connection over our USB.
Serial pc(USBTX, NC, 9600);

int main()
{
    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.
    }
}