Charles Tritt
/
21_ConsoleIO_v5
Use this project as a starting point for simple IO testing in Mbed. This is the initial version.
main.cpp@111:956b1c606b66, 2021-10-11 (annotated)
- Committer:
- CSTritt
- Date:
- Mon Oct 11 12:15:03 2021 +0000
- Revision:
- 111:956b1c606b66
- Parent:
- 110:22b71c32c5e1
- Child:
- 112:c7c727d92f2a
Older program renamed and updated to Mbed v. 5. Note: this program uses ASNSI VT-100 escape codes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:61b9c99a4e27 | 1 | /* |
CSTritt | 111:956b1c606b66 | 2 | Project: 21_FuncEx_v5 |
CSTritt | 109:b061f9830736 | 3 | File: main.cpp |
CSTritt | 111:956b1c606b66 | 4 | |
CSTritt | 111:956b1c606b66 | 5 | This simple program demonstrates that C passes arguments by value. |
CSTritt | 110:22b71c32c5e1 | 6 | |
CSTritt | 111:956b1c606b66 | 7 | See http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html and/or |
CSTritt | 111:956b1c606b66 | 8 | https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 for escape |
CSTritt | 111:956b1c606b66 | 9 | sequences. |
CSTritt | 110:22b71c32c5e1 | 10 | |
CSTritt | 111:956b1c606b66 | 11 | Written by: Dr. C. S. Tritt; Last revised 10/11/21 (v. 1.2) |
CSTritt | 107:61b9c99a4e27 | 12 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 13 | #include "mbed.h" |
CSTritt | 108:eee3167b25b4 | 14 | |
CSTritt | 111:956b1c606b66 | 15 | // Function definitions are typically in .h files are more commonly that are |
CSTritt | 111:956b1c606b66 | 16 | // included here. |
CSTritt | 111:956b1c606b66 | 17 | int myFunc(int x, int y); // Function declaration required. |
CSTritt | 111:956b1c606b66 | 18 | |
CSTritt | 111:956b1c606b66 | 19 | Serial pc(USBTX, USBRX, 9600); // Create Serial object. |
CSTritt | 111:956b1c606b66 | 20 | |
CSTritt | 111:956b1c606b66 | 21 | const char ESC=27; // ESC character for ANSI escape sequences. |
CSTritt | 111:956b1c606b66 | 22 | const int DELAY = 2000; // Loop delay in mS. |
CSTritt | 111:956b1c606b66 | 23 | |
CSTritt | 111:956b1c606b66 | 24 | int main() { |
CSTritt | 111:956b1c606b66 | 25 | while(true) { |
CSTritt | 111:956b1c606b66 | 26 | // Clear screen & move cursor to upper left ANSI/VT100 sequence. |
CSTritt | 111:956b1c606b66 | 27 | pc.printf("%c[2J", ESC); pc.printf("%c[H", ESC); |
CSTritt | 111:956b1c606b66 | 28 | // Start of display code. |
CSTritt | 111:956b1c606b66 | 29 | pc.printf("In function demo main...\n"); |
CSTritt | 111:956b1c606b66 | 30 | int a = 5; // Create and initialize a. |
CSTritt | 111:956b1c606b66 | 31 | pc.printf("a = %d\n", a); // Display a. |
CSTritt | 111:956b1c606b66 | 32 | int b = 6; // Create and initialize b. |
CSTritt | 111:956b1c606b66 | 33 | pc.printf("b = %d\n", b); // Display b. |
CSTritt | 111:956b1c606b66 | 34 | pc.printf("About to call my function.\n"); |
CSTritt | 111:956b1c606b66 | 35 | int c = myFunc(a, b); // Call my function. |
CSTritt | 111:956b1c606b66 | 36 | pc.printf("Function has returned.\n"); |
CSTritt | 111:956b1c606b66 | 37 | pc.printf("a = %d\n", a); // Redisplay a. |
CSTritt | 111:956b1c606b66 | 38 | pc.printf("b = %d\n", b); // Redisplay b. |
CSTritt | 111:956b1c606b66 | 39 | pc.printf("c = %d\n", c); // Display b. |
CSTritt | 111:956b1c606b66 | 40 | ThisThread::sleep_for(DELAY); // Pause for DELAY mS. |
CSTritt | 108:eee3167b25b4 | 41 | } |
CSTritt | 111:956b1c606b66 | 42 | } |
CSTritt | 111:956b1c606b66 | 43 | |
CSTritt | 111:956b1c606b66 | 44 | int myFunc(int x, int y) { // Function definition. Often in separate files! |
CSTritt | 111:956b1c606b66 | 45 | int z = x + y; |
CSTritt | 111:956b1c606b66 | 46 | // Changing parameter values here won't change argument values in main! |
CSTritt | 111:956b1c606b66 | 47 | x = 50; |
CSTritt | 111:956b1c606b66 | 48 | pc.printf("x = %d\n", x); // Generally don't put I/O in regular functions. |
CSTritt | 111:956b1c606b66 | 49 | y = 100; |
CSTritt | 111:956b1c606b66 | 50 | pc.printf("y = %d\n", y); // Generally don't put I/O in regular functions. |
CSTritt | 111:956b1c606b66 | 51 | return z; // Explicit return is required in C/C++. |
CSTritt | 111:956b1c606b66 | 52 | } |