CIS441 Proj MS 2b

Dependencies:   TextLCD MQTT

main.cpp

Committer:
mwgold
Date:
2019-12-11
Revision:
4:ef8866873df5
Parent:
3:aa2778d92301
Child:
6:e59641c4562c

File content as of revision 4:ef8866873df5:

/* 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() {
    // ------------------------------------------------------------------------------
    // 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);
        
        int new_cars = road1.try_enter_car(time);
        
        road1.let_cars_update();
        road1.wait_for_car_update();
        
        
        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();
        
        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));
    totalUpdateTime = 0;
    numberCycles = 0;
    // ----------------------------------------------------------------------
}