CIS441 Proj MS 2b

Dependencies:   TextLCD MQTT

Revision:
4:ef8866873df5
Parent:
3:aa2778d92301
Child:
6:e59641c4562c
--- a/main.cpp	Mon Nov 11 01:33:28 2019 +0000
+++ b/main.cpp	Wed Dec 11 21:13:57 2019 +0000
@@ -7,7 +7,6 @@
 #include "Road.h"
 #include "AccCar.h"
 #include "TextLCD.h"
-#include "Intersection.h" 
 
 TextLCD lcd(p15, p16, p17, p18, p19, p20);
 
@@ -18,82 +17,47 @@
     Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
     int numberCycles = 0;
     int totalUpdateTime = 0;
-    int road1wait = 0;
-    int road2wait = 0;
     // ------------------------------------------------------------------------------
 
     int time = 0;
 
-    Intersection intersection; 
-    Road* road1 = new Road(&intersection, 1);
-    Road* road2 = new Road(&intersection, 2); 
+    Road road1;
 
     stopwatch.start();
     stopwatch.reset();
     
+    int return_cars[MAX_CARS];
     
     do {
-        intersection.preChecks(); 
-        if (intersection.queue[0] > -1) {
-            if (intersection.queue[0] < 5) {
-                road1wait++;
-            } else {
-                road2wait++;
-            }
-        }
-        if (intersection.queue[1] > -1) {
-            if (intersection.queue[1] < 5) {
-                road1wait++;
-            } else {
-                road2wait++;
-            }
-        }
+        Road::ready(0);
         
-        int new_cars1 = road1->try_enter_car(time);
-        int new_cars2 = road2->try_enter_car(time); 
+        int new_cars = road1.try_enter_car(time);
         
-        road1->let_cars_update();
-        road2->let_cars_update(); 
-        road1->wait_for_car_update();
-        road2->wait_for_car_update(); 
+        road1.let_cars_update();
+        road1.wait_for_car_update();
         
         
         printf("\r\nRoad 1 Update %d\r\n", time);
-        road1->print_status();
-        printf("\r\nRoad 2 Update %d\r\n", time);
-        road2->print_status(); 
-    
-    lcd.cls(); 
-    if(new_cars1 == -1 && (intersection.intersection_car == -1 || intersection.intersection_car > 4)){
-            lcd.printf("x, x\n");
+        road1.print_status();
+        
+    if(new_cars == -1 && road1.intersection_car == -1){
+            lcd.printf("x, x");
     }
-    else if(new_cars1 != -1 && (intersection.intersection_car == -1 || intersection.intersection_car > 4)){
-        lcd.printf("%d, x\n", new_cars1);
+    else if(new_cars != -1 && road1.intersection_car == -1){
+        lcd.printf("%d, x", new_cars);
     }
-    else if(new_cars1 == -1 && !(intersection.intersection_car == -1 || intersection.intersection_car > 4)){
-        lcd.printf("x, %d\n", intersection.intersection_car);
+    else if(new_cars == -1 && road1.intersection_car != -1){
+        lcd.printf("x, %d", road1.intersection_car);
     }
     else{
-        lcd.printf("%d, %d\n", new_cars1, intersection.intersection_car);
-    }
-    
-    if(new_cars2 == -1 && intersection.intersection_car < 5){
-        lcd.printf("x, x");
-    }
-    else if(new_cars2 != -1 && intersection.intersection_car < 5){
-        lcd.printf("%d, x", new_cars2);
-    }
-    else if(new_cars2 == -1 && intersection.intersection_car >= 5){
-        lcd.printf("x, %d", intersection.intersection_car);
-    }
-    else{
-        lcd.printf("%d, %d", new_cars2, intersection.intersection_car);
+        lcd.printf("%d, %d", new_cars, road1.intersection_car);
     }
 
-    printf("\r\n");
-    road1->check_exit_cars();  
-    road2->check_exit_cars(); 
-    time++;
+
+        printf("\r\n");
+    road1.check_exit_cars(return_cars);
+        
+        time++;
         
         // ------------------------------------------------------------------
         // Timing statistics logic, do not modify
@@ -101,14 +65,16 @@
         numberCycles++;
         stopwatch.reset();
         // ------------------------------------------------------------------
-                
-    } while( road1->active_cars > 0x00 || road2->active_cars > 0x00);
+        
+        lcd.cls();
+        
+        Communication::publish_road_ready();
+        while(Road::ready(-1) == 0);       
+    } while( road1.active_cars > 0x00);
     
             // ----------------------------------------------------------------------
     // Timing statistics printout, do not modify
     printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
-    printf("Average road1 intersection wait: %fs \r\n", (road1wait*1.0)/5.0);
-    printf("Average road2 intersection wait: %fs \r\n", (road2wait*1.0)/5.0);
     totalUpdateTime = 0;
     numberCycles = 0;
     // ----------------------------------------------------------------------