// lightapp.cpp - TRAFFIC LIGHT APPLICATION // // MODULE INDEX // NAME CONTENTS // InitApp Initiate application // Tick Process clock tick // VehicleWaiting Process change in vehicle waiting status // // MAINTENANCE HISTORY // DATE PROGRAMMER AND DETAILS // 09-11-02 JS Original // //----------------------------------------------------------------------------- #include // Standard input/output #include // String manipulation #include // Standard library #include // Error codes #include "lightsys.h" // Traffic light system declarations //----------------------------------------------------------------------------- // TRAFFIC LIGHT CYCLE STRUCTURE struct Cycle { int cyclePeriod; // Cycle period Colour cycleNorthColour; // North facing colour Colour cycleEastColour; // East facing colour Colour cycleSouthColour; // South facing colour Colour cycleWestColour; // West facing colour }; //----------------------------------------------------------------------------- // CYCLE TABLE Cycle cycleArr[] = { {30, GREEN, RED, RED, RED}, {5, AMBER, RED, RED, RED}, {60, RED, GREEN, RED, RED}, {5, RED, AMBER, RED, RED}, {40, RED, RED, GREEN, RED}, {5, RED, RED, AMBER, RED}, {50, RED, RED, RED, GREEN}, {5, RED, RED, RED, AMBER} }; #define CYCLECNT (sizeof(cycleArr)/sizeof(cycleArr[0])) //----------------------------------------------------------------------------- // GLOBAL DATA int phase; // Current phase int ticksRem; // Ticks remaining in the current phase int northWait; // Vehicles waiting on the north branch int eastWait; // Vehicles waiting on the east branch int southWait; // Vehicles waiting on the south branch int westWait; // Vehicles waiting on the west branch //----------------------------------------------------------------------------- // INITIATE APPLICATION void InitApp () { phase = 0; ticksRem = cycleArr[phase].cyclePeriod; northWait = 0; eastWait = 0; southWait = 0; westWait = 0; Display ( cycleArr[phase].cycleNorthColour, cycleArr[phase].cycleEastColour, cycleArr[phase].cycleSouthColour, cycleArr[phase].cycleWestColour ); } //----------------------------------------------------------------------------- // PROCESS CLOCK TICK void Tick () { // Decrement ticks remaining in the current cycle. // If no more ticks remain, move to the next phase. ticksRem -- ; if (ticksRem == 0) { // Select the next phase phase ++ ; if (phase >= CYCLECNT) phase = 0; // If the new phase involves a light turning green // and no cars are waiting on that branch, but cars // are waiting on a branch somewhere, skip the next // two phases. while ( ( northWait || eastWait || southWait || westWait ) && ( ( ! northWait && cycleArr[phase].cycleNorthColour==GREEN ) || ( ! eastWait && cycleArr[phase].cycleEastColour==GREEN ) || ( ! southWait && cycleArr[phase].cycleSouthColour==GREEN ) || ( ! westWait && cycleArr[phase].cycleWestColour==GREEN ) ) ) { phase ++ ; if (phase >= CYCLECNT) phase = 0; phase ++ ; if (phase >= CYCLECNT) phase = 0; } // Set the ticks remaining in the new phase ticksRem = cycleArr[phase].cyclePeriod; // Display the new traffic light settings Display ( cycleArr[phase].cycleNorthColour, cycleArr[phase].cycleEastColour, cycleArr[phase].cycleSouthColour, cycleArr[phase].cycleWestColour ); } } //----------------------------------------------------------------------------- // PROCESS CHANGE IN VEHICLE WAITING STATUS void VehicleWaiting ( int northNewWait, // Vehicles waiting on the north branch int eastNewWait, // Vehicles waiting on the east branch int southNewWait, // Vehicles waiting on the south branch int westNewWait) // Vehicles waiting on the west branch { northWait = northNewWait; eastWait = eastNewWait; southWait = southNewWait; westWait = westNewWait; }