Matthew Goldsmith / Mbed OS cis441projMS1a

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Intersection.cpp Source File

Intersection.cpp

00001 #include "Intersection.h"
00002 #include <stdlib.h>
00003 
00004 Intersection::Intersection() {
00005     intersection_car = -1;
00006     queue[0] = -1; 
00007     queue[1] = -1; 
00008     occupied = false; 
00009     road1Car = -1; 
00010     road2Car = -1; 
00011 }
00012 
00013 void Intersection::preChecks() {
00014     if (intersection_car == -1) {
00015         occupied = false;
00016     }
00017     
00018     if (road1Car > -1) {
00019         if (queue[0] == -1) {
00020             queue[0] = road1Car; 
00021         } else {
00022             queue[1] = road1Car; 
00023         }
00024         road1Car = -1; 
00025     } 
00026     
00027     if (road2Car > -1) {
00028         if (queue[0] == -1) {
00029             queue[0] = road2Car; 
00030         } else {
00031             queue[1] = road2Car; 
00032         }
00033         road2Car = -1; 
00034     } 
00035 }
00036 
00037 void Intersection::intendToEnter(int carId, int roadId) {
00038     if (roadId == 1) {
00039         road1Car = carId; 
00040     } else {
00041         road2Car = carId; 
00042     }
00043 }
00044 
00045 void Intersection::leaveIntersection() {
00046     this->intersection_car = -1;
00047 } 
00048 
00049 int Intersection::attemptEnterIntersection(int carId) {
00050     if (occupied || queue[0] != carId) {
00051         return 0; 
00052     } else {
00053          intersection_car = carId; 
00054          occupied = true; 
00055          queue[0] = queue[1]; 
00056          queue[1] = -1; 
00057          return 1; 
00058     }
00059 }
00060