Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include <cctype> 00003 #include "Car.h" 00004 #include "TextLCD.h" 00005 #include "signal_wrapper.h" 00006 00007 Serial pc(USBTX, USBRX); // The connection to USB 00008 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2 ); // rs, e, d4-d7 00009 00010 // define any needed global variables 00011 // ... 00012 // ... 00013 #define MAX_CAR_SPEED 15 00014 #define MIN_CAR_SPEED 5 00015 00016 // This function present the "prompt" string to the user before accepting 00017 // an integer back from the Serial connection. If the user enters anything 00018 // other than a digit (e.g. return key, letters), the currently built number 00019 // is returned 00020 int read_int(char* prompt) { 00021 int number = 0; 00022 00023 pc.printf(prompt); 00024 00025 char input; 00026 while(1) { 00027 input = pc.getc(); 00028 pc.putc(input); 00029 00030 if( std::isdigit(input) ) { 00031 number = (number * 10) + (input-'0'); 00032 } else { 00033 pc.putc(input); 00034 break; 00035 } 00036 } 00037 00038 return number; 00039 } 00040 00041 void handle_input(int* car_speed) 00042 { 00043 int valid_reading = 0; 00044 while( !valid_reading ) 00045 { 00046 *car_speed = read_int("car_speed:"); 00047 if( *car_speed >= MIN_CAR_SPEED && *car_speed <= MAX_CAR_SPEED ) 00048 { 00049 valid_reading = 1; 00050 printf("\r\n"); 00051 } 00052 else 00053 { 00054 printf("\r\n"); 00055 printf("invalid input ... range: %d -> %d\r\n", MIN_CAR_SPEED, MAX_CAR_SPEED); 00056 } 00057 00058 } 00059 } 00060 00061 // The main thread of the program. This runs the main program, accepting user 00062 // input, creating cars, displaying state of cars, and handling coordination that 00063 // may be needed. Timing statistics are also presented. 00064 int main() 00065 { 00066 // ------------------------------------------------------------------------------ 00067 // The following three variables are used for timing statistics, do not modify them 00068 Timer stopwatch; // A timer to keep track of how long the updates take, for statistics purposes 00069 int numberCycles = 0; 00070 int totalUpdateTime = 0; 00071 // ------------------------------------------------------------------------------ 00072 // prepare screen for input 00073 printf("\033[2J"); 00074 printf("\r\n"); 00075 00076 00077 00078 Car car(0); // Create a car. This call will need to be modified with any additional parameters you add 00079 stopwatch.start(); // Start out stats timer 00080 00081 while (true) { // Run the simulation indefinitely 00082 00083 int carspeed =0; 00084 // Prompt the user for an initial speed and ensure it is valid 00085 handle_input(&carspeed); 00086 // Start the car / let the car know its initial speed 00087 car.reset(carspeed); 00088 lcd.cls(); 00089 // ---------------------------------------------------------------------- 00090 // Timing statistics logic, do not modify 00091 stopwatch.reset(); 00092 // ---------------------------------------------------------------------- 00093 00094 do { // Run the simulation until the car exits the road 00095 ; 00096 // wait some time before sending signal 00097 while(stopwatch.read_ms() < TICK) 00098 { 00099 // do nothing I guess 00100 } 00101 00102 //signal car it can update 00103 send_signal( CAR_SIGNAL ); 00104 00105 // wait for car to update 00106 wait_for_signal( CAR_UPDATE_MAIN_SIGNAL ); 00107 00108 // ------------------------------------------------------------------ 00109 // Timing statistics logic, do not modify 00110 totalUpdateTime += stopwatch.read_ms(); 00111 numberCycles++; 00112 stopwatch.reset(); 00113 // ------------------------------------------------------------------ 00114 lcd.cls(); 00115 // Display the state of the car to the LCD 00116 lcd.printf( "%d -> %d \n",car.speed,car.position ); 00117 00118 } while (car.is_simulating()); // Modify this condition to stop the simulation when the car exits the road 00119 // ---------------------------------------------------------------------- 00120 // Timing statistics printout, do not modify 00121 pc.printf("Resetting...\r\n"); 00122 pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0)); 00123 totalUpdateTime = 0; 00124 numberCycles = 0; 00125 // ---------------------------------------------------------------------- 00126 } 00127 }
Generated on Thu Jul 14 2022 17:18:57 by
1.7.2