finish homework2

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //#include "mbed.h"
00002 //#include <cctype>
00003 //#include "Car.h"
00004 //#include "AccCar.h"
00005 //#include "TextLCD.h"
00006 //#include "signal_wrapper.h"
00007 //
00008 //Serial pc(USBTX, USBRX);    // The connection to USB
00009 //TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2 ); // rs, e, d4-d7
00010 //
00011 //#define MIN_CAR_POSITION 20
00012 //#define MAX_CAR_POSITION 40
00013 //#define MAX_CAR_SPEED 15
00014 //#define MIN_CAR_SPEED 5
00015 //#define MAX_ACC_SPEED MAX_CAR_SPEED
00016 //#define MIN_ACC_SPEED MIN_CAR_SPEED
00017 //
00018 //// define any needed global variables
00019 //// ...
00020 //// ...
00021 //
00022 //
00023 //// This function present the "prompt" string to the user before accepting
00024 //// an integer back from the Serial connection. If the user enters anything
00025 //// other than a digit (e.g. return key, letters), the currently built number
00026 //// is returned
00027 //int read_int(char* prompt) {
00028 //    int number = 0;
00029 //    
00030 //    pc.printf(prompt);
00031 //    
00032 //    char input;
00033 //    while(1) {
00034 //        input = pc.getc();
00035 //        pc.putc(input);
00036 //        
00037 //        if( std::isdigit(input) ) {
00038 //            number = (number * 10) + (input-'0');   
00039 //        } else {
00040 //            pc.putc(input);
00041 //            break;   
00042 //        } 
00043 //    }
00044 //    
00045 //    return number;
00046 //}
00047 //
00048 // void get_valid_input(char* str, int* data, int max, int min)
00049 // {
00050 //    int valid_reading = 0;
00051 //    while( !valid_reading )
00052 //    {
00053 //        *data = read_int(str);   
00054 //        if( *data >= min && *data <= max )
00055 //        {
00056 //            valid_reading = 1;
00057 //        }
00058 //        else
00059 //        {
00060 //            printf("\r\n");
00061 //            printf("invalid input ... range: %d -> %d\r\n", min, max);
00062 //        }  
00063 //    }
00064 //    printf("\r\n");
00065 //}
00066 //
00067 //void handle_input(int* car_pos,int* car_speed,int* acc_speed)
00068 //{
00069 //    get_valid_input("car position: ",car_pos,MAX_CAR_POSITION,MIN_CAR_POSITION);
00070 //    get_valid_input("car_speed: ",car_speed,MAX_CAR_SPEED,MIN_CAR_SPEED);
00071 //    get_valid_input("acc_speed: ",acc_speed,MAX_ACC_SPEED,MIN_ACC_SPEED);
00072 //    return;
00073 //}
00074 //
00075 //// The main thread of the program. This runs the main program, accepting user
00076 //// input, creating cars, displaying state of cars, and handling coordination that
00077 //// may be needed. Timing statistics are also presented.
00078 //int main()
00079 //{   
00080 //    // ------------------------------------------------------------------------------
00081 //    // The following three variables are used for timing statistics, do not modify them
00082 //    Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
00083 //    int numberCycles = 0;
00084 //    int totalUpdateTime = 0;
00085 //    // ------------------------------------------------------------------------------
00086 //    
00087 //    // prepare serial monitor and make it look nice
00088 //    printf("\033[2J");
00089 //    printf("\r\n");
00090 //    
00091 //    
00092 //    Car car(0);         // Create a car. This call will need to be modified with any additional parameters you add
00093 //    AccCar acc_car(0);
00094 //    
00095 //    stopwatch.start();  // Start out stats timer
00096 //    
00097 //    while (true) {      // Run the simulation indefinitely
00098 //        
00099 //        int carspeed,carposition,acctarget;
00100 //        
00101 //        // get the input
00102 //        handle_input(&carposition,&carspeed,&acctarget);
00103 //
00104 //          //  clear LCD
00105 //          lcd.cls();
00106 //        
00107 //        // create acc and car threads based off user input
00108 //        car.reset(carspeed,carposition);
00109 //        acc_car.reset(acctarget);
00110 //        
00111 //        // ----------------------------------------------------------------------
00112 //        // Timing statistics logic, do not modify
00113 //        stopwatch.reset();
00114 //        // ----------------------------------------------------------------------
00115 //        do {    // Run the simulation until the car exits the road
00116 //;
00117 //            // wait some time before sending signal
00118 //            while(stopwatch.read_ms() < TICK)
00119 //            {
00120 //                // do nothing I guess
00121 //            }
00122 //            
00123 //            // signal car it can update
00124 //            send_signal( CAR_SIGNAL );
00125 //            
00126 //            // wait for car to update
00127 //            wait_for_signal( CAR_UPDATE_MAIN_SIGNAL );
00128 //            
00129 //            // update the ACC car
00130 //            send_signal( ACC_SIGNAL );
00131 //            
00132 //            // wait for the update to complete
00133 //            wait_for_signal( ACC_UPDATE_MAIN_SIGNAL );
00134 //
00135 //            // ------------------------------------------------------------------
00136 //            // Timing statistics logic, do not modify
00137 //            totalUpdateTime += stopwatch.read_ms();
00138 //            numberCycles++;
00139 //            stopwatch.reset();
00140 //            // ------------------------------------------------------------------
00141 //            lcd.cls();
00142 //            // Display the state of the car to the LCD
00143 //            lcd.printf( "Car: %d -> %d\n",car.speed,car.position );
00144 //            lcd.printf( "Acc: %d -> %d\n",acc_car.speed,acc_car.position );
00145 //            
00146 //        } while (acc_car.acc_is_simulating());     // Modify this condition to stop the simulation when the car exits the road
00147 //        
00148 //        // terminate any threads waiting for a signal
00149 //        send_signal( SIG_TERM );
00150 //        
00151 //        // ----------------------------------------------------------------------
00152 //        // Timing statistics printout, do not modify
00153 //        pc.printf("Resetting...\r\n");
00154 //        pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
00155 //        totalUpdateTime = 0;
00156 //        numberCycles = 0;
00157 //        // ----------------------------------------------------------------------
00158 //    }
00159 //}