Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // These are the libraries you are allowed to use to write your solution. Do not add any
- // additional libraries as the auto-tester will be locked down to the following:
- #include "pch.h"
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <cstdlib>
- #include <string.h>
- #include <windows.h>
- #include <cmath>
- // Do NOT Add or remove any #include statements to this project!!
- // All library functions required should be covered by the above
- // include list. Do not add a *.h file for this project as all your
- // code should be included in this file.
- using namespace std; // Please do not remove this line
- // optional: your choice if you want to use/modify these definitions for MAX-MIN-Range
- const double MAXRANGE = 255;
- const double MINRANGE = 0;
- // optional: your choice if you want to write class method and function prototypes here.
- // They should be defined above the OUSB and TrafficLight Class definitions
- // if they are going to be used by the classes themselves as all code must
- // contained in this file (ie. no separate *.h files for autotester)
- class OUSB // Do NOT change class name
- {
- private:
- // data inputs
- unsigned short PORTA;
- unsigned short PORTB; // private, used to hold the current VAL of PortB, can only be accessed by OUSB::Public members
- unsigned short PORTC;
- unsigned short PORTD;
- unsigned short runOUSBcommand(string *command);
- public:
- OUSB()
- {
- PORTA, PORTB, PORTC, PORTD = 0;
- }
- // Constructor: Remember to define this else it won't compile
- // Interface functions (These can be changed....)
- // functions for class
- void WritePortB(unsigned short newValue); // class method
- void writePORTD(unsigned short newValue);
- unsigned short readPORTA();
- unsigned short ReadPortB();
- unsigned short readPORTC();
- unsigned short readPORTD();
- // Values
- unsigned short PASS; // whatever we pass to the function -- // class member
- // constructors
- // you may choose to implement operator overloaded functions for read and write functions for portb
- // void operator<<(const unsigned short &val); // write portb
- // void operator>>(unsigned short &val); // read portb
- };
- // Traffic Light class
- class TrafficLight // Do NOT change class name
- {
- private:
- bool redLamp;
- bool yellowLamp;
- bool greenLamp;
- public:
- TrafficLight()
- {
- redLamp, yellowLamp, greenLamp = 0;
- }
- // Constructor: Remember to define this else it won't compile
- // Interface functions (These can be changed....)
- string lightOn(string val);
- bool isREDon(string x) // Accessor function
- {
- if (x == "R")
- {
- redLamp = 1;
- return true;
- }
- else
- {
- redLamp = 0;
- return false;
- }
- }
- // string yellowOn();
- bool isYELLOWon(string x) // Accessor function
- {
- if (x == "Y")
- {
- yellowLamp = 1;
- return true;
- }
- else
- {
- yellowLamp = 0;
- return false;
- }
- }
- //string greenOn();
- bool isGREENon(string x) // Accessor function
- {
- if (x == "G")
- {
- greenLamp = 1;
- return true;
- }
- else
- {
- greenLamp = 0;
- return false;
- }
- }
- string changeTrafficLightState(string state);
- // vars
- };
- // write all class methods and stand alone functions here below this line
- // CLASS OUSB
- unsigned short OUSB::runOUSBcommand(string *command)
- {
- FILE *fpipe;
- char line[250] = {}; // size of Line should be smaller than command
- fpipe = (FILE*)_popen(command->c_str(), "r"); // attempt to open pipe and execute a command
- if (fpipe != NULL) // check that the pipe opened correctly
- {
- while (fgets(line, sizeof(line), fpipe))
- { // do nothing here, or print out debug data
- // if (Debug) cout << "DEBUG :" << line; // print out OUSB data for debug purposes
- }
- _pclose(fpipe); // close pipe
- }
- //else cout << "Error, problems with pipe!\n";
- // do something with the value returned by the OUSB board, eg:
- //unsigned short portb = (unsigned short)atoi(line); // convert from char array to unsigned short
- unsigned short TEMP = (unsigned short)atoi(line);
- return TEMP; // value for command
- }
- void OUSB::WritePortB(unsigned short PASS)
- {
- char LED[10] = {};
- _itoa_s(PASS, LED, 10); // convert int to string
- string command = "ousb -r io PORTB "; // command to write 20 to OUSB board
- command.append(LED);
- //strcat_s(command, LED); // slap em together
- PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- }
- void OUSB::writePORTD(unsigned short PASS)
- {
- char LED[10] = {};
- _itoa_s(PASS, LED, 10); // convert int to string
- string command = "ousb -r io PORTD "; // command to write 20 to OUSB board
- command.append(LED);
- //strcat_s(command, LED); // slap em together
- PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- }
- unsigned short OUSB::ReadPortB()
- {
- string command = "ousb -r io PORTB";
- PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- return PORTB;
- }
- unsigned short OUSB::readPORTA()
- {
- string command = "ousb -r io PORTA";
- PORTA = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- return PORTA;
- }
- unsigned short OUSB::readPORTC()
- {
- string command = "ousb -r io PORTC";
- PORTC = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- return PORTC;
- }
- unsigned short OUSB::readPORTD()
- {
- string command = "ousb -r io PORTD";
- PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
- return PORTD;
- }
- // CLASS Traffic LX
- string TrafficLight::lightOn(string val)
- {
- if (isGREENon(val) == true)
- {
- val = { "8" };
- return val;
- }
- else if (isYELLOWon(val) == true)
- {
- val = { "6 32" }; // 1 for now, will change later
- return val;
- }
- else if (isREDon(val) == true)
- {
- val = { "1 16" }; // 1 for now, will change later
- return val;
- }
- return val;
- }
- string TrafficLight::changeTrafficLightState(string state)
- {
- if (state == "R")
- {
- state = "G";
- return state;
- }
- if (state == "G")
- {
- state = "Y";
- return state;
- }
- if (state == "Y")
- {
- state = "R";
- return state;
- }
- return state;
- }
- int main(int argc, char *argv[])
- {
- //--- When no parameters MUST print id string in CSV format.
- //if (argc == 1) // no parameters print this line.
- //{
- // cout << "1224567,s1224567@student.rmit.edu.au,FirstName_LastName"
- // << endl;
- // // Any errors in the line above, or the naming of the file
- // // will result in 10% being deducted from your mark.
- // return(0);
- //}
- //--- START YOUR CODE HERE.
- int input_num = atoi(argv[2]); // number input
- if (argc != 3) // too many inputs
- {
- cout << "Error - Too Many Inputs" << endl;
- return 0;
- }
- if ((argv[1] == "R") || (argv[1] == "G") || (argv[1] == "Y"))
- {
- // do nothing
- }
- else // incorrect
- {
- cout << "Wrong Input Argument" << endl;
- return 0;
- }
- if ((input_num < 0) || (input_num > 50)) // out of range
- {
- cout << "Input Number out of range" << endl;
- return 0;
- }
- string start = argv[1]; // R, G, Y starting position
- string val = {};
- string PB = {};
- string PD1 = {};
- string PD2 = {};
- int transitions = atoi(argv[2]); // number of transitions
- // traffic lx states
- int RLB = 0; // PortB
- int YLB = 0;
- int GLB = 0;
- int RLD = 0; // PortD
- int YLD = 0;
- int GLD = 0;
- OUSB state;
- int Current_stateB = 0;
- int Current_stateD = 0;
- TrafficLight light;
- for (int i = 0; i < transitions; i++)
- {
- // read traffixlx state
- if (start == "R")
- {
- val = light.lightOn(start);
- PB = val[0];
- PD1 = val[2];
- PD2 = val[3];
- PD1.append(PD2);
- RLB = stoi(PB); //make red
- RLD = stoi(PD1); //make red
- Current_stateB = state.ReadPortB();
- Current_stateD = state.readPORTD();
- Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
- RLB = RLB | Current_stateB; // inserts RLB into lower 4.
- Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
- RLD = RLD | Current_stateD; // inserts RLD into lower 4.
- state.WritePortB(RLB); // Write new Value
- state.writePORTD(RLD); // Write new Value
- start = light.changeTrafficLightState(start); //start = G
- }
- else if (start == "G")
- {
- val = light.lightOn(start);
- PB = val[0];
- GLB = stoi(PB); //make green
- Current_stateB = state.ReadPortB();
- Current_stateD = state.readPORTD();
- Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
- GLB = GLB | Current_stateB; // inserts GLX into lower 4.
- GLD = Current_stateD & 207; // bitshift: clears bottom 4 bits
- state.writePORTD(GLD);
- state.WritePortB(GLB); // Write new Value
- start = light.changeTrafficLightState(start); //start = Y
- }
- else if (start == "Y")
- {
- val = light.lightOn(start);
- PB = val[0];
- PD1 = val[2];
- PD2 = val[3];
- PD1.append(PD2);
- YLB = stoi(PB); //make yellow
- YLD = stoi(PD1); //make yellow
- Current_stateB = state.ReadPortB();
- Current_stateD = state.readPORTD();
- Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
- YLB = YLB | Current_stateB; // inserts RLB into lower 4.
- Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
- YLD = YLD | Current_stateD; // inserts RLD into lower 4.
- state.WritePortB(YLB); // Write new Value
- state.writePORTD(YLD); // Write new Value
- start = light.changeTrafficLightState(start); //start = R
- }
- }
- return(0); // The convention is to return Zero to signal NO ERRORS, if you change it
- // the AutoTester will assume you have made some major error.
- // Leave it as zero.
- }
- // do NOT put any functions/code after this closing bracket, if you use functions add them before main()
- // End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement