Advertisement
Guest User

Untitled

a guest
May 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 KB | None | 0 0
  1. // These are the libraries you are allowed to use to write your solution. Do not add any
  2. // additional libraries as the auto-tester will be locked down to the following:
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include <string.h>
  9. #include <windows.h>
  10. #include <cmath>    
  11. // Do NOT Add or remove any #include statements to this project!!
  12. // All library functions required should be covered by the above
  13. // include list. Do not add a *.h file for this project as all your
  14. // code should be included in this file.
  15.  
  16. using namespace std; // Please do not remove this line
  17.  
  18.                      // optional: your choice if you want to use/modify these definitions for MAX-MIN-Range
  19. const double MAXRANGE = 255;
  20. const double MINRANGE = 0;
  21.  
  22. // optional: your choice if you want to write class method and function prototypes here.
  23. //           They should be defined above the OUSB and TrafficLight Class definitions
  24. //           if they are going to be used by the classes themselves as all code must
  25. //           contained in this file (ie. no separate *.h files for autotester)
  26.  
  27.  
  28. class OUSB          // Do NOT change class name
  29. {
  30. private:
  31.     // data inputs
  32.     unsigned short PORTA;
  33.     unsigned short PORTB; // private, used to hold the current VAL of PortB, can only be accessed by OUSB::Public members
  34.     unsigned short PORTC;
  35.     unsigned short PORTD;
  36.  
  37.     unsigned short runOUSBcommand(string *command);
  38.  
  39. public:
  40.     OUSB()
  41.     {
  42.         PORTA, PORTB, PORTC, PORTD = 0;
  43.     }
  44.     // Constructor: Remember to define this else it won't compile
  45.  
  46.      // Interface functions (These can be changed....)
  47.     // functions for class
  48.     void WritePortB(unsigned short newValue); // class method
  49.     void writePORTD(unsigned short newValue);
  50.  
  51.  
  52.     unsigned short readPORTA();
  53.     unsigned short ReadPortB();
  54.     unsigned short readPORTC();
  55.     unsigned short readPORTD();
  56.    
  57.  
  58.     // Values
  59.  
  60.     unsigned short PASS; // whatever we pass to the function -- // class member
  61.  
  62.     // constructors
  63.  
  64.  
  65.  
  66.     // you may choose to implement operator overloaded functions for read and write functions for portb
  67.     // void operator<<(const unsigned short &val); // write portb
  68.     // void operator>>(unsigned short &val);       // read portb  
  69. };
  70.  
  71. // Traffic Light class
  72. class TrafficLight // Do NOT change class name
  73. {
  74. private:
  75.     bool redLamp;
  76.     bool yellowLamp;
  77.     bool greenLamp;
  78. public:
  79.     TrafficLight()
  80.     {
  81.         redLamp, yellowLamp, greenLamp = 0;
  82.     }
  83.     // Constructor: Remember to define this else it won't compile
  84.  
  85.     // Interface functions (These can be changed....)
  86.     string lightOn(string val);
  87.     bool isREDon(string x) // Accessor function  
  88.     {
  89.         if (x == "R")
  90.         {
  91.             redLamp = 1;
  92.             return true;
  93.         }
  94.         else
  95.         {
  96.             redLamp = 0;
  97.             return false;
  98.         }
  99.     }
  100.  
  101. //  string yellowOn();
  102.     bool isYELLOWon(string x) // Accessor function
  103.     {
  104.         if (x == "Y")
  105.         {
  106.             yellowLamp = 1;
  107.             return true;
  108.         }
  109.         else
  110.         {
  111.             yellowLamp = 0;
  112.             return false;
  113.         }
  114.     }
  115.  
  116.     //string greenOn();
  117.     bool isGREENon(string x) // Accessor function
  118.     {
  119.         if (x == "G")
  120.         {
  121.             greenLamp = 1;
  122.             return true;
  123.         }
  124.         else
  125.         {
  126.             greenLamp = 0;
  127.             return false;
  128.         }
  129.     }
  130.  
  131.     string changeTrafficLightState(string state);
  132.  
  133.  
  134.  
  135.     // vars
  136.  
  137. };
  138. // write all class methods and stand alone functions here below this line
  139.  
  140.  
  141.  
  142. // CLASS OUSB
  143.  
  144.  
  145. unsigned short OUSB::runOUSBcommand(string *command)
  146. {
  147.     FILE *fpipe;
  148.     char line[250] = {}; // size of Line should be smaller than command
  149.     fpipe = (FILE*)_popen(command->c_str(), "r");    // attempt to open pipe and execute a command
  150.     if (fpipe != NULL)                     // check that the pipe opened correctly
  151.     {
  152.         while (fgets(line, sizeof(line), fpipe))
  153.         {   // do nothing here, or print out debug data
  154.         //  if (Debug) cout << "DEBUG :" << line; // print out OUSB data for debug purposes
  155.         }
  156.         _pclose(fpipe);   // close pipe
  157.     }
  158.     //else cout << "Error, problems with pipe!\n";
  159.     // do something with the value returned by the OUSB board, eg:
  160.     //unsigned short portb = (unsigned short)atoi(line); // convert from char array to unsigned short
  161.     unsigned short TEMP = (unsigned short)atoi(line);
  162.     return TEMP; // value for command
  163. }
  164.  
  165. void OUSB::WritePortB(unsigned short PASS)
  166.  
  167. {
  168.     char LED[10] = {};
  169.     _itoa_s(PASS, LED, 10); // convert int to string
  170.     string command = "ousb -r io PORTB "; // command to write 20 to OUSB board
  171.     command.append(LED);
  172.     //strcat_s(command, LED); // slap em together
  173.  
  174.     PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
  175.    
  176.  
  177. }
  178.  
  179. void OUSB::writePORTD(unsigned short PASS)
  180.  
  181. {
  182.     char LED[10] = {};
  183.     _itoa_s(PASS, LED, 10); // convert int to string
  184.     string command = "ousb -r io PORTD "; // command to write 20 to OUSB board
  185.     command.append(LED);
  186.     //strcat_s(command, LED); // slap em together
  187.  
  188.     PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
  189.  
  190.  
  191. }
  192.  
  193. unsigned short OUSB::ReadPortB()
  194.  
  195. {
  196.     string command = "ousb -r io PORTB";
  197.  
  198.     PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  199.  
  200.     return PORTB;
  201. }
  202.  
  203. unsigned short OUSB::readPORTA()
  204.  
  205. {
  206.     string command = "ousb -r io PORTA";
  207.  
  208.     PORTA = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  209.  
  210.     return PORTA;
  211. }
  212.  
  213. unsigned short OUSB::readPORTC()
  214.  
  215. {
  216.     string command = "ousb -r io PORTC";
  217.  
  218.     PORTC = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  219.  
  220.     return PORTC;
  221. }
  222.  
  223. unsigned short OUSB::readPORTD()
  224.  
  225. {
  226.     string command = "ousb -r io PORTD";
  227.  
  228.     PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  229.  
  230.     return PORTD;
  231. }
  232.  
  233. // CLASS Traffic LX
  234.  
  235. string TrafficLight::lightOn(string val)
  236. {
  237.     if (isGREENon(val) == true)
  238.     {
  239.         val = { "8" };
  240.         return val;
  241.     }
  242.     else if (isYELLOWon(val) == true)
  243.     {
  244.         val = { "6 32" }; // 1 for now, will change later
  245.         return val;
  246.     }
  247.     else if (isREDon(val) == true)
  248.     {
  249.         val = { "1 16" }; // 1 for now, will change later
  250.         return val;
  251.     }
  252.  
  253.     return val;
  254. }
  255.  
  256.  
  257.  
  258. string TrafficLight::changeTrafficLightState(string state)
  259. {
  260.     if (state == "R")
  261.     {
  262.         state = "G";
  263.         return state;
  264.     }
  265.     if (state == "G")
  266.     {
  267.         state = "Y";
  268.         return state;
  269.     }
  270.     if (state == "Y")
  271.     {
  272.         state = "R";
  273.         return state;
  274.     }
  275.     return state;
  276. }
  277.  
  278.  
  279. int main(int argc, char *argv[])
  280. {
  281.     //--- When no parameters MUST print id string in CSV format.
  282.     //if (argc == 1)  // no parameters print this line.
  283.     //{
  284.     //  cout << "1224567,s1224567@student.rmit.edu.au,FirstName_LastName"
  285.     //      << endl;
  286.     //  // Any errors in the line above,  or the naming of the file
  287.     //  //  will result in 10% being deducted from your mark.
  288.     //  return(0);
  289.     //}
  290.  
  291.     //--- START YOUR CODE HERE.
  292.  
  293.     int input_num = atoi(argv[2]); // number input
  294.  
  295.     if (argc != 3) // too many inputs
  296.     {
  297.         cout << "Error - Too Many Inputs" << endl;
  298.         return 0;
  299.     }
  300.  
  301.     if ((argv[1] == "R") || (argv[1] == "G") || (argv[1] == "Y"))
  302.     {
  303.         // do nothing
  304.     }
  305.     else // incorrect
  306.     {
  307.         cout << "Wrong Input Argument" << endl;
  308.         return 0;
  309.     }
  310.  
  311.     if ((input_num < 0) || (input_num > 50)) // out of range
  312.     {
  313.         cout << "Input Number out of range" << endl;
  314.         return 0;
  315.     }
  316.    
  317.  
  318.     string start = argv[1]; // R, G, Y starting position
  319.     string val = {};
  320.     string PB = {};
  321.     string PD1 = {};
  322.     string PD2 = {};
  323.  
  324.     int transitions = atoi(argv[2]); // number of transitions
  325.  
  326.     // traffic lx states
  327.  
  328.     int RLB = 0; // PortB
  329.     int YLB = 0;
  330.     int GLB = 0;
  331.  
  332.     int RLD = 0; // PortD
  333.     int YLD = 0;
  334.     int GLD = 0;
  335.  
  336.     OUSB state;
  337.     int Current_stateB = 0;
  338.     int Current_stateD = 0;
  339.     TrafficLight light;
  340.  
  341.     for (int i = 0; i < transitions; i++)
  342.     {
  343.         // read traffixlx state
  344.  
  345.         if (start == "R")
  346.         {
  347.             val = light.lightOn(start);
  348.             PB = val[0];
  349.             PD1 = val[2];
  350.             PD2 = val[3];
  351.             PD1.append(PD2);
  352.  
  353.             RLB = stoi(PB); //make red
  354.             RLD = stoi(PD1); //make red
  355.  
  356.             Current_stateB = state.ReadPortB();
  357.             Current_stateD = state.readPORTD();
  358.  
  359.             Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  360.             RLB = RLB | Current_stateB; // inserts RLB into lower 4.
  361.  
  362.             Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  363.             RLD = RLD | Current_stateD; // inserts RLD into lower 4.
  364.  
  365.             state.WritePortB(RLB); // Write new Value
  366.             state.writePORTD(RLD); // Write new Value
  367.  
  368.             start = light.changeTrafficLightState(start); //start = G
  369.            
  370.         }
  371.         else if (start == "G")
  372.         {
  373.             val = light.lightOn(start);
  374.             PB = val[0];
  375.            
  376.  
  377.             GLB = stoi(PB); //make green
  378.            
  379.  
  380.             Current_stateB = state.ReadPortB();
  381.             Current_stateD = state.readPORTD();
  382.  
  383.             Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  384.             GLB = GLB | Current_stateB; // inserts GLX into lower 4.
  385.  
  386.             GLD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  387.  
  388.             state.writePORTD(GLD);
  389.             state.WritePortB(GLB); // Write new Value
  390.            
  391.             start = light.changeTrafficLightState(start); //start = Y
  392.         }
  393.         else if (start == "Y")
  394.         {
  395.             val = light.lightOn(start);
  396.             PB = val[0];
  397.             PD1 = val[2];
  398.             PD2 = val[3];
  399.             PD1.append(PD2);
  400.            
  401.  
  402.             YLB = stoi(PB); //make yellow
  403.             YLD = stoi(PD1); //make yellow
  404.  
  405.    
  406.             Current_stateB = state.ReadPortB();
  407.             Current_stateD = state.readPORTD();
  408.  
  409.             Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  410.             YLB = YLB | Current_stateB; // inserts RLB into lower 4.
  411.  
  412.             Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  413.             YLD = YLD | Current_stateD; // inserts RLD into lower 4.
  414.  
  415.             state.WritePortB(YLB); // Write new Value
  416.             state.writePORTD(YLD); // Write new Value
  417.  
  418.             start = light.changeTrafficLightState(start); //start = R
  419.         }
  420.  
  421.     }
  422.          
  423.  
  424.     return(0); // The convention is to return Zero to signal NO ERRORS, if you change it
  425.                // the AutoTester will assume you have made some major error.  
  426.                // Leave it as zero.
  427. }
  428. // do NOT put any functions/code after this closing bracket, if you use functions add them before main()
  429. // End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement