Charles Tritt
/
21_SerialPass_v5
New example. Initial version.
main.cpp@117:e072f162cbce, 2021-10-17 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Oct 17 13:54:15 2021 +0000
- Revision:
- 117:e072f162cbce
- Parent:
- 116:8990686eedf5
Cleaned up comments.
Who changed what in which revision?
User | Revision | Line number | New 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 | 117:e072f162cbce | 5 | This project demonstrates how to pass a Serial object to functions by reference. |
CSTritt | 117:e072f162cbce | 6 | It also introduces multi-file projects and C strings. It uses VT-100 escape |
CSTritt | 117:e072f162cbce | 7 | sequences to prevent scrolling and change text color. |
CSTritt | 117:e072f162cbce | 8 | |
CSTritt | 117:e072f162cbce | 9 | The function approach to sending terminal control commands is awkward. It was |
CSTritt | 117:e072f162cbce | 10 | used to demonstrate the use of multiple functions each in their own file and |
CSTritt | 117:e072f162cbce | 11 | custom header files. The string constant approach used after the button is |
CSTritt | 117:e072f162cbce | 12 | better. It involves defining char array constants containing the escape codes. |
CSTritt | 117:e072f162cbce | 13 | The \x1B inserts the 1B_16 (escape) characters into the char vectors (really |
CSTritt | 117:e072f162cbce | 14 | "C strings" because they are "null terminated" (since they were created using |
CSTritt | 117:e072f162cbce | 15 | double quotes). Use the strings in printf statements to send the control text |
CSTritt | 117:e072f162cbce | 16 | to the terminal. There are probably even betters ways to do this, but this is |
CSTritt | 117:e072f162cbce | 17 | good enough for now. |
CSTritt | 117:e072f162cbce | 18 | |
CSTritt | 116:8990686eedf5 | 19 | See... |
CSTritt | 114:1cfad1babb55 | 20 | |
CSTritt | 116:8990686eedf5 | 21 | * C Strings: https://www.tutorialspoint.com/cprogramming/c_strings.htm. |
CSTritt | 116:8990686eedf5 | 22 | * String literal escape sequences (this is a different meaning than the VT-100 |
CSTritt | 116:8990686eedf5 | 23 | terminal escape sequences): https://en.cppreference.com/w/cpp/language/escape. |
CSTritt | 116:8990686eedf5 | 24 | * VT-100 Escape sequences: http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html |
CSTritt | 116:8990686eedf5 | 25 | * ANSI Escape Sequences: |
CSTritt | 116:8990686eedf5 | 26 | https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797. |
CSTritt | 116:8990686eedf5 | 27 | * ANSI Color Codes, etc. |
CSTritt | 116:8990686eedf5 | 28 | https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html. |
CSTritt | 114:1cfad1babb55 | 29 | |
CSTritt | 116:8990686eedf5 | 30 | Calls |
CSTritt | 116:8990686eedf5 | 31 | |
CSTritt | 117:e072f162cbce | 32 | clrTerm - Clears terminal and places cursor at home position. |
CSTritt | 117:e072f162cbce | 33 | whiteText - Makes text white. Ackward to use. Done for demo. |
CSTritt | 117:e072f162cbce | 34 | blueText - Makes text blue. Awkward to use. Done for demo. |
CSTritt | 116:8990686eedf5 | 35 | |
CSTritt | 117:e072f162cbce | 36 | Written by: Dr. C. S. Tritt; Last revised 10/17/21 (v. 1.0) |
CSTritt | 107:61b9c99a4e27 | 37 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 38 | #include "mbed.h" |
CSTritt | 115:6ba84689e2c9 | 39 | #include "myFuncs.h" |
CSTritt | 117:e072f162cbce | 40 | |
CSTritt | 115:6ba84689e2c9 | 41 | // Construct a USER_BUTTON digital input. |
CSTritt | 115:6ba84689e2c9 | 42 | DigitalIn myButton(USER_BUTTON); |
CSTritt | 117:e072f162cbce | 43 | // Construct a Serial object for output only. |
CSTritt | 115:6ba84689e2c9 | 44 | Serial pc(USBTX, NC, 9600); |
CSTritt | 108:eee3167b25b4 | 45 | |
CSTritt | 114:1cfad1babb55 | 46 | int main() |
CSTritt | 114:1cfad1babb55 | 47 | { |
CSTritt | 116:8990686eedf5 | 48 | clrTerm(pc); // Clear the terminal at startup. |
CSTritt | 117:e072f162cbce | 49 | whiteText(pc); // Change text to white (likely the default). |
CSTritt | 116:8990686eedf5 | 50 | pc.printf("Welcome to SerialPass.\n"); |
CSTritt | 117:e072f162cbce | 51 | // This is awkward but demonstrates the use of multiple function files. |
CSTritt | 116:8990686eedf5 | 52 | pc.printf("Press the "); |
CSTritt | 116:8990686eedf5 | 53 | blueText(pc); // Change text to blue. There are better approaches to this. |
CSTritt | 116:8990686eedf5 | 54 | pc.printf("blue User Button"); |
CSTritt | 116:8990686eedf5 | 55 | whiteText(pc); // Change text to white. |
CSTritt | 116:8990686eedf5 | 56 | pc.printf(" to clear this and continue...\n"); |
CSTritt | 117:e072f162cbce | 57 | |
CSTritt | 116:8990686eedf5 | 58 | // Wait here for button to be pressed (making it low, false). |
CSTritt | 116:8990686eedf5 | 59 | while (myButton) { |
CSTritt | 117:e072f162cbce | 60 | ThisThread::sleep_for(10); // Yield some time to other threads. |
CSTritt | 116:8990686eedf5 | 61 | } |
CSTritt | 117:e072f162cbce | 62 | // Display the most button text. |
CSTritt | 116:8990686eedf5 | 63 | clrTerm(pc); // Clear the terminal. |
CSTritt | 116:8990686eedf5 | 64 | const char red[] = "\x1B[31m"; |
CSTritt | 116:8990686eedf5 | 65 | const char white[] = "\x1B[37m"; |
CSTritt | 116:8990686eedf5 | 66 | const char blue[] = "\x1B[34m"; |
CSTritt | 116:8990686eedf5 | 67 | pc.printf("The screen should clear and this text appear in...\n"); |
CSTritt | 117:e072f162cbce | 68 | // Note finish with white to leave terminal in the expected state. |
CSTritt | 116:8990686eedf5 | 69 | pc.printf("%sred, %swhite, and %sblue%s.\n", red, white, blue, white); |
CSTritt | 114:1cfad1babb55 | 70 | while(true) { // Main forever loop. |
CSTritt | 115:6ba84689e2c9 | 71 | ThisThread::sleep_for(300000); // Sleep for 5 minutes, repeatedly. |
CSTritt | 108:eee3167b25b4 | 72 | } |
CSTritt | 113:cc5beacdad5a | 73 | } |