Emanuel Kuflik / Mbed OS HW05

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include <cctype>
00008 #include "Car.h"
00009 #include "AccCar.h"
00010 #include "TextLCD.h"
00011 #include "Road.h"
00012 
00013 Serial pc(USBTX, USBRX);
00014 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00015 
00016 #define ROADLENGTH 100
00017 
00018 // Read the max number of services to perform from pc input
00019 int read_int(char* prompt) {
00020     int maxService = 0;
00021     
00022     pc.printf(prompt);
00023     
00024     char input;
00025     while(1) {
00026         input = pc.getc();
00027         pc.putc(input);
00028         
00029         if( std::isdigit(input) ) {
00030             maxService = (maxService * 10) + (input-'0');   
00031         } else {
00032             pc.putc(input);
00033             break;   
00034         } 
00035     }
00036     
00037     return maxService;
00038 }
00039 
00040 // main() runs in its own thread in the OS
00041 int main()
00042 {   
00043     // ------------------------------------------------------------------------------
00044     // The following three variables are used for timing statistics, do not modify them
00045     Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
00046     int numberCycles = 0;
00047     int totalUpdateTime = 0;
00048     // ------------------------------------------------------------------------------
00049     
00050     Road road;    
00051     AccCar car1(1, &road, 0x01);
00052 //    AccCar car2(2, &road, 0x02);
00053 //    AccCar car3(3, &road, 0x03);
00054 //    AccCar car4(4, &road, 0x04);
00055 //    AccCar car5(5, &road, 0x05);
00056     
00057     road.add_acc_car(&car1);
00058     
00059     
00060     stopwatch.start();
00061     
00062 //    car2.set_forward_car(&car1);
00063 //    car3.set_forward_car(&car2);
00064 //    car4.set_forward_car(&car3);
00065 //    car5.set_forward_car(&car4);
00066     
00067     car1.reset();
00068 //    car2.reset();
00069 //    car3.reset();
00070 //    car4.reset();
00071 //    car5.reset();
00072     
00073     int timerForCar2 = rand() % 3 + 1;
00074     int timerForCar3 = rand() % 3 + 1;
00075     int timerForCar4 = rand() % 3 + 1;
00076     int timerForCar5 = rand() % 3 + 1;
00077 
00078     stopwatch.reset();
00079     
00080     do {
00081         
00082         road.let_cars_update();
00083         road.wait_for_car_update();
00084         // ------------------------------------------------------------------
00085         // Timing statistics logic, do not modify
00086         totalUpdateTime += stopwatch.read_ms();
00087         numberCycles++;
00088         stopwatch.reset();
00089         // ------------------------------------------------------------------
00090        // if (numberCycles == timerForCar2){
00091 //            road.add_acc_car(&car2);
00092 //        }
00093 //        if (numberCycles == (timerForCar2+timerForCar3)){
00094 //            road.add_acc_car(&car3);
00095 //        }
00096 //        if (numberCycles == (timerForCar2+timerForCar3+timerForCar4)){
00097 //            road.add_acc_car(&car4);
00098 //        }
00099         lcd.cls();
00100         pc.printf("1 %d -> %d\n", car1.position, car1.speed);
00101         //pc.printf("1 %d -> %d\n2 %d -> %d\n3 %d -> %d\n4 %d -> %d\n5 %d -> %d", car1.position, car1.speed, car2.position, car2.speed, car3.position, car3.speed, car4.position, car4.speed, car5.position, car5.speed);
00102         lcd.printf("1 %d -> %d\n", car1.position, car1.speed);
00103         
00104     } while (car1.position <= ROADLENGTH); 
00105     car1.stop();
00106 //    car2.stop();
00107 //    car3.stop();
00108 //    car4.stop();
00109 //    car5.stop();
00110     
00111             // ----------------------------------------------------------------------
00112     // Timing statistics printout, do not modify
00113     pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
00114     totalUpdateTime = 0;
00115     numberCycles = 0;
00116     // ----------------------------------------------------------------------
00117 }