Matthew Goldsmith / Mbed OS cis441projMS1a

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 "Road.h"
00008 #include "AccCar.h"
00009 #include "TextLCD.h"
00010 #include "Intersection.h" 
00011 
00012 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00013 
00014 // main() runs in its own thread in the OS
00015 int main() {
00016     // ------------------------------------------------------------------------------
00017     // The following three variables are used for timing statistics, do not modify them
00018     Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
00019     int numberCycles = 0;
00020     int totalUpdateTime = 0;
00021     int road1wait = 0;
00022     int road2wait = 0;
00023     // ------------------------------------------------------------------------------
00024 
00025     int time = 0;
00026 
00027     Intersection intersection; 
00028     Road* road1 = new Road(&intersection, 1);
00029     Road* road2 = new Road(&intersection, 2); 
00030 
00031     stopwatch.start();
00032     stopwatch.reset();
00033     
00034     
00035     do {
00036         intersection.preChecks(); 
00037         if (intersection.queue[0] > -1) {
00038             if (intersection.queue[0] < 5) {
00039                 road1wait++;
00040             } else {
00041                 road2wait++;
00042             }
00043         }
00044         if (intersection.queue[1] > -1) {
00045             if (intersection.queue[1] < 5) {
00046                 road1wait++;
00047             } else {
00048                 road2wait++;
00049             }
00050         }
00051         
00052         int new_cars1 = road1->try_enter_car(time);
00053         int new_cars2 = road2->try_enter_car(time); 
00054         
00055         road1->let_cars_update();
00056         road2->let_cars_update(); 
00057         road1->wait_for_car_update();
00058         road2->wait_for_car_update(); 
00059         
00060         
00061         printf("\r\nRoad 1 Update %d\r\n", time);
00062         road1->print_status();
00063         printf("\r\nRoad 2 Update %d\r\n", time);
00064         road2->print_status(); 
00065     
00066     lcd.cls(); 
00067     if(new_cars1 == -1 && (intersection.intersection_car == -1 || intersection.intersection_car > 4)){
00068             lcd.printf("x, x\n");
00069     }
00070     else if(new_cars1 != -1 && (intersection.intersection_car == -1 || intersection.intersection_car > 4)){
00071         lcd.printf("%d, x\n", new_cars1);
00072     }
00073     else if(new_cars1 == -1 && !(intersection.intersection_car == -1 || intersection.intersection_car > 4)){
00074         lcd.printf("x, %d\n", intersection.intersection_car);
00075     }
00076     else{
00077         lcd.printf("%d, %d\n", new_cars1, intersection.intersection_car);
00078     }
00079     
00080     if(new_cars2 == -1 && intersection.intersection_car < 5){
00081         lcd.printf("x, x");
00082     }
00083     else if(new_cars2 != -1 && intersection.intersection_car < 5){
00084         lcd.printf("%d, x", new_cars2);
00085     }
00086     else if(new_cars2 == -1 && intersection.intersection_car >= 5){
00087         lcd.printf("x, %d", intersection.intersection_car);
00088     }
00089     else{
00090         lcd.printf("%d, %d", new_cars2, intersection.intersection_car);
00091     }
00092 
00093     printf("\r\n");
00094     road1->check_exit_cars();  
00095     road2->check_exit_cars(); 
00096     time++;
00097         
00098         // ------------------------------------------------------------------
00099         // Timing statistics logic, do not modify
00100         totalUpdateTime += stopwatch.read_ms();
00101         numberCycles++;
00102         stopwatch.reset();
00103         // ------------------------------------------------------------------
00104                 
00105     } while( road1->active_cars > 0x00 || road2->active_cars > 0x00);
00106     
00107             // ----------------------------------------------------------------------
00108     // Timing statistics printout, do not modify
00109     printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
00110     printf("Average road1 intersection wait: %fs \r\n", (road1wait*1.0)/5.0);
00111     printf("Average road2 intersection wait: %fs \r\n", (road2wait*1.0)/5.0);
00112     totalUpdateTime = 0;
00113     numberCycles = 0;
00114     // ----------------------------------------------------------------------
00115 }