CIS441 Proj MS 2b

Dependencies:   TextLCD MQTT

main.cpp

Committer:
kchen7
Date:
2019-12-13
Revision:
12:ade22f4681af
Parent:
11:2ab3397b6e30
Child:
13:2c12f60bd10a

File content as of revision 12:ade22f4681af:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "Road.h"
#include "AccCar.h"
#include "TextLCD.h"

//TextLCD lcd(p15, p16, p17, p18, p19, p20);

// main() runs in its own thread in the OS
int main() {
    Communication::init();
    wait(1);
    printf("starting simulation/r/n");
    // ------------------------------------------------------------------------------
    // The following three variables are used for timing statistics, do not modify them
    Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
    int numberCycles = 0;
    int totalUpdateTime = 0;
    // ------------------------------------------------------------------------------

    int time = 0;

    Road road1;

    stopwatch.start();
    stopwatch.reset();
    
    int return_cars[MAX_CARS];
    
    do {
        Road::ready(0);
        Communication::yield(); 

        int new_cars = road1.try_enter_car(time);
        road1.let_cars_update();
        printf("main.cpp: update flags raised\n");
                
        road1.wait_for_car_update();
        printf("main.cpp: finished updating\n"); 
                
        printf("\r\nRoad 1 Update %d\r\n", time);
        road1.print_status();
        
//        if(new_cars == -1 && road1.intersection_car == -1){
//                lcd.printf("x, x");
//        }
//        else if(new_cars != -1 && road1.intersection_car == -1){
//            lcd.printf("%d, x", new_cars);
//        }
//        else if(new_cars == -1 && road1.intersection_car != -1){
//            lcd.printf("x, %d", road1.intersection_car);
//        }
//        else{
//            lcd.printf("%d, %d", new_cars, road1.intersection_car);
//        }

        printf("\r\n");
        road1.check_exit_cars(return_cars);

        time++;
        
        // ------------------------------------------------------------------
        // Timing statistics logic, do not modify
        totalUpdateTime += stopwatch.read_ms();
        numberCycles++;
        stopwatch.reset();
        // ------------------------------------------------------------------
        
//        lcd.cls();

        road1.publish_car_info();
        printf("successful publish: car info\r\n");
        Communication::publish_road_ready();
        
        Communication::yield(); 
        while(Road::ready(-1) == 0) {
            Communication::yield(); 
        }
        printf("----------------------\r\n"); 
    } 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));
    totalUpdateTime = 0;
    numberCycles = 0;
    // ----------------------------------------------------------------------
}