// lightsys.cpp - TRAFFIC LIGHT SYSTEM SIMULATOR // // MODULE INDEX // NAME CONTENTS // ColourStr Colour string // Display Set traffic light colours // main Main line // // 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 //----------------------------------------------------------------------------- // GLOBAL DATA static Colour northState; // Current traffic light states static Colour eastState; static Colour southState; static Colour westState; static int tickCnt; // Tick count static int northWait; // Vehicle waiting flags static int eastWait; static int southWait; static int westWait; //----------------------------------------------------------------------------- // COLOUR STRING char * ColourStr ( Colour colour) // Colour to convert to string { char *str; // String value switch (colour) { case RED: str = "R"; break; case AMBER: str = "A"; break; case GREEN: str = "G"; break; default: str = "?"; break; } return str; } //----------------------------------------------------------------------------- // SET TRAFFIC LIGHT COLOURS void Display ( Colour northColour, // New traffic light settings Colour eastColour, Colour southColour, Colour westColour) { printf ("%3d\n%d %d %d %d %s %s %s %s ", tickCnt, northWait, eastWait, southWait, westWait, ColourStr(northColour), ColourStr(eastColour), ColourStr(southColour), ColourStr(westColour) ); tickCnt = 0; northState = northColour; eastState = eastColour; southState = southColour; westState = westColour; } //----------------------------------------------------------------------------- // MAIN LINE int main () { int northNewWait; // Vehicle waiting flags int eastNewWait; int southNewWait; int westNewWait; northWait = 0; eastWait = 0; southWait = 0; westWait = 0; srand48 (2031960); InitApp (); for (;;) { // Set the new waiting states northNewWait = northWait; eastNewWait = eastWait; southNewWait = southWait; westNewWait = westWait; if (northState == GREEN || northState == AMBER) northNewWait = 0; else if (lrand48() % 120 == 7) northNewWait = 1; if (eastState == GREEN || eastState == AMBER) eastNewWait = 0; else if (lrand48() % 120 == 7) eastNewWait = 1; if (southState == GREEN || southState == AMBER) southNewWait = 0; else if (lrand48() % 120 == 7) southNewWait = 1; if (westState == GREEN || westState == AMBER) westNewWait = 0; else if (lrand48() % 120 == 7) westNewWait = 1; // If the waiting states have changed, notify the // application. if ( northNewWait != northWait || eastNewWait != eastWait || southNewWait != southWait || westNewWait != westWait ) { northWait = northNewWait; eastWait = eastNewWait; southWait = southNewWait; westWait = westNewWait; VehicleWaiting (northWait, eastWait, southWait, westWait); } // Increment the tick count tickCnt ++ ; // Process a clock tick Tick (); } return 0; }