Charles Tritt
/
21_ConsoleIO_v5
Use this project as a starting point for simple IO testing in Mbed. This is the initial version.
Diff: main.cpp
- Revision:
- 114:1cfad1babb55
- Parent:
- 113:cc5beacdad5a
- Child:
- 115:f22cbbc92bae
--- a/main.cpp Mon Oct 11 12:45:46 2021 +0000 +++ b/main.cpp Tue Oct 12 21:48:12 2021 +0000 @@ -1,41 +1,36 @@ /* -Project: 21_GlobalEx_v5 +Project: 21_ScopeEx1_v5 File: main.cpp This simple program demonstrates that C passes arguments by value. - - Uses VT-100 escape sequences to prevent scrolling. See - http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html and/or + + Uses VT-100 escape sequences to prevent scrolling. See + http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html and/or https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797. - + Written by: Dr. C. S. Tritt; Last revised 10/11/21 (v. 1.1) */ #include "mbed.h" -int myFunc(int x, int y); // Function declaration required. +int main() +{ + const int DELAY = 2000000;; // mS wait time. + const char ESC = 27; // Define escape character for escape sequence. + printf("%c[2J%c[H", ESC, ESC); // ANSI/VT100 clear screen/home. -// Arbitrary factor. Unitless. Move into main to break project. -const float myFactor = 3.3f; // Suffix f indicates float (default is double). + int count1 = 1; // Declared in outer block -int main() { - const int DELAY = 2000;; // mS wait time. - const char ESC=27; // Define escape character for escape sequence. - while(true) { // Main forever loop. - printf("%c[2J%c[H", ESC, ESC); // ANSI/VT100 clear screen/home. - printf("In function demo main...\n"); - int a = 5; // Create and initialize a. - printf("a = %d\n", a); // Display a. - int b = 6; // Create and initialize b. - printf("b = %d\n", b); // Display b. - printf("About to call my function.\n"); - int c = myFunc(a, b); // Call my function. - printf("Function has returned.\n"); - printf("c = %d\n", c); // Display b. + do { // Do while construct may be new to you! + int count2 = 0; // Declared & initialized in inner block + ++count2; // Ineffective due to line above! + printf("count1 = %d count2 = %d\n", count1, count2); + // Note that count1 is incremented in while test! + } while (++count1 <= 5); // count2 no longer exists after loop exit. + + printf("count1 = %d\n", count1); + return 0; + + while(true) { // Main forever loop. ThisThread::sleep_for(DELAY); // Pause } -} - -int myFunc(int x, int y) { // Function definition. Often in separate files! - int z = (x + y)/myFactor; - return z; // Explicit return is required in C/C++. } \ No newline at end of file