Charles Tritt
/
21_SerialPass_v5
New example. Initial version.
Revision 117:e072f162cbce, committed 2021-10-17
- Comitter:
- CSTritt
- Date:
- Sun Oct 17 13:54:15 2021 +0000
- Parent:
- 116:8990686eedf5
- Commit message:
- Cleaned up comments.
Changed in this revision
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 |
--- a/main.cpp Sun Oct 17 02:15:18 2021 +0000 +++ b/main.cpp Sun Oct 17 13:54:15 2021 +0000 @@ -2,8 +2,20 @@ 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. +This project demonstrates how to pass a Serial object to functions by reference. +It also introduces multi-file projects and C strings. It uses VT-100 escape +sequences to prevent scrolling and change text color. + +The function approach to sending terminal control commands is awkward. It was +used to demonstrate the use of multiple functions each in their own file and +custom header files. The string constant approach used after the button is +better. It involves defining 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" (since they were created using +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. + See... * C Strings: https://www.tutorialspoint.com/cprogramming/c_strings.htm. @@ -17,47 +29,43 @@ Calls -clrTerm -whiteText -blueText +clrTerm - Clears terminal and places cursor at home position. +whiteText - Makes text white. Ackward to use. Done for demo. +blueText - Makes text blue. Awkward to use. Done for demo. - Written by: Dr. C. S. Tritt; Last revised 10/16/21 (v. 1.0) +Written by: Dr. C. S. Tritt; Last revised 10/17/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. +// Construct a Serial object for output only. Serial pc(USBTX, NC, 9600); int main() { clrTerm(pc); // Clear the terminal at startup. - whiteText(pc); // Change text to white. + whiteText(pc); // Change text to white (likely the default). pc.printf("Welcome to SerialPass.\n"); + // This is awkward but demonstrates the use of multiple function files. 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. + ThisThread::sleep_for(10); // Yield some time to other threads. } + // Display the most button text. 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. + // Note finish with white to leave terminal in the expected 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 Sun Oct 17 02:15:18 2021 +0000 +++ b/myFuncs.h Sun Oct 17 13:54:15 2021 +0000 @@ -1,12 +1,15 @@ /* File: myFuncs.h -This file declares my utility functions. Headers must match definitions. +This file declares my VT-100 utility functions. This headers must match +definitions. Created by Dr. C. S. Tritt Last reviseed: 10/16/21 (v. 1.0) */ -// The next two lines and final endif prevents multiple inclusion. +// The next two lines and final endif prevents multiple inclusion. An +// alternative to this is to use compiler dependent #pragma once that works in +// Visual Studio C/C++ but not GCC. #ifndef MY_FUNCS #define MY_FUNCS