Advertisement
Guest User

Untitled

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 42.46 KB | None | 0 0
  1. /*==============  Proforma : EEET2250 Lab Test 3 OUSB class and traffic light state machine ============
  2. Proforma version number: 2019.001.b (updated 03-05-2019)
  3.  
  4. Document Revision Details:
  5. version 2019.001.a (11-04-2019) - Original version
  6. version 2019.001.b (03-05-2019) - G13 updated
  7.  
  8. PURPOSE:
  9. This file is a proforma for the EEET2250 Lab Test 3: OUSB class and traffic light state machine operations
  10. with the OUSB board. This file defines the assignment - there is no other documentation. You must extend
  11. this C++ program to cope with the functionality identified below.
  12.  
  13. GOAL:
  14. To write a C++ Win32 Console Application in Visual Studio that implements access to the OUSB board using
  15. classes. Your program will implement a traffic light state machine class called TrafficLight as well as
  16. an OUSB class. Failure to instantiate and use the OUSB and TrafficLight classes will result is a large
  17. marks penalty. PORTB and PORTD of the OUSB board will be used to indicate the current traffic light state.
  18. Text with very specific formatting will also be displayed to the console. The program should perform error
  19. checking.
  20.  
  21. To put the problem into perceptive, the three LEDs (Red, Yellow and Green) that are wired into the OUSB
  22. board prototype area will be used to represent the actual traffic light lamps, whereas the standard bank
  23. of 8 LEDs connected to PORTB will be used to represents a simplified troubleshooting panel inside the
  24. traffic light control box (e.g. the box that is normally on the side of the road at an intersection).
  25. The troubleshooting (PORTB) lights will have the light states as well as other information encoded in
  26. the 8-bit. Both PORTB and PORTD are used to control the Red, Yellow and Green prototype LEDs.
  27.  
  28. RESOURCES:
  29. During this Lab test you will have access to the following PDF resources as the autotester will open the
  30. following PDF files along with Visual Studio:
  31.     1. "Teach Yourself C++ in 21 Days" PDF ebook
  32.     2. "Open-USB-IO Reference, version 1.13" PDF file
  33. These PDF files can be found on the EEET2250 Canvas Shell.
  34.  
  35. WARNING: Please make sure that during the lab test you do not
  36. close the PDF files as you will not be able to open them again!
  37.  
  38. SCHEMATIC FOR TRAFFIC LIGHT LEDs
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. The following circuitry has been already added to a standard Open-USB-IO board.
  41.  
  42. Your C++ Program     OUSB       .  Components Soldered onto Prototype Area
  43. on the PC            Board      .
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45.              ________           .
  46.             |UNL2003A|          .                     Green LED
  47. PB3         |4     13|          .        270 ohms      //
  48. ----->------|--|> ---|-----J5---.->-----[\\\]--------|<|------| +5V DC
  49. OC0         |        |   pin 27 .
  50.             |        |          .                     Yellow LED
  51. PD5         |2     15|          .        100 ohms      //
  52. ----->------|--|> ---|-----J5---.->-----[\\\]--------|<|------| +5V DC
  53. OC1A        |        |   pin 23 .
  54.             |        |          .                     Red LED
  55. PD4         |1     16|          .        270 ohms      //
  56. ----->------|--|> ---|-----J5---.->-----[\\\]--------|<|------| +5V DC
  57. OC1B        |________|   pin 21 .
  58.                                 .
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. NOTE: Yellow LED is lower intensity, hence the lower resistor value.
  61.  
  62.  
  63. ___________________________________________________________________________________________
  64. ___ GENERAL SPECIFICATIONS (common to all three EEET2250 Laboratory Tests):
  65.  
  66. G1. You must rename your file to lab3_1234567.cpp, where 1234567 is your student number.
  67. Your filename MUST NEVER EVER contain any spaces. _under_score_is_Fine.
  68. You do not need to include the 's' in front of your student number.
  69.  
  70. G2. Edit the name/email address string in the main() function to your student number,
  71. student email and student name. The format of the student ID line is CSV (Comma Separated
  72. Variables) with NO SPACES-
  73. student_id,student_email,student_name
  74. When the program is run without any operands i.e. simply the name of the executable such as:
  75. lab3_1234567.exe
  76. the program MUST print student ID string in Comma Separated Values (CSV) format with no
  77. spaces. For example the following text should be outputted to the console updated with
  78. your student details:
  79. "1234567,s1234567@student.rmit.edu.au,FirstName_LastName"
  80.  
  81. G3. All outputs are a single error character or a numerical number, as specified by the
  82. FUNCTIONAL SPECIFICATIONS, followed by a linefeed ( endl or \n).
  83.  
  84. G4. DO NOT add more than what is specified. Do NOT add additional information, text or
  85. comments to the output console.
  86.  
  87. G5. DO NOT use 'cin', system("pause"), getchar(), gets(), etc. type functions. Do NOT ask
  88. for user input from the keyboard.
  89.  
  90. G6. All input MUST be specified on the command line separated by blank spaces (i.e. use
  91. the argc and argv[] input parameters). All input and output is case sensitive.
  92.  
  93. G7. You should use the Integrated Debugging Environment (IDE) to change input arguments during
  94. the development process. During the lab tests, the command line window (i.e. cmd command) will
  95. not be available. However you will be able to compile and run your code during the lab test
  96. session and used the Visual studio command line debugging dialog box. You should be using
  97. debug mode and a breakpoint at the return statement prior to program finish in main during the
  98. process of developing your solution.
  99.  
  100. G8. When your code exits the 'main()' function using the 'return' command, you MUST
  101. use zero as the return value. This requirement is for exiting the 'main()' function ONLY.
  102. A return value other than zero will indicate that something went wrong to the autotester and
  103. no marks will be awarded.
  104.  
  105. G9. Do NOT put any functions or class declarations AFTER the 'main()' function - if you write
  106. your own functions/Classes, add them BEFORE the 'main()' function. This is a requirement of the
  107. Autotester and failure to do so will result in the Autotester not compiling your code correctly
  108. and loss of marks, even though the code will compile during the lab test session. Hence any
  109. user-defined functions to read and write to the OUSB board must be written before the 'main()'
  110. function.
  111.  
  112. G10. You MUST run this file as part of a Project - No other *.cpp files or *.h files should be
  113. added to your solution. The OUSB.exe file will be provided as part of the project.
  114.  
  115. G11. You are not permitted to add any other #includes statements to your solution. The only
  116. libraries permitted to be used are the ones predefined in this file.
  117.  
  118. G12. If an error (as per the definition in the specification) occurs, then the value on
  119. the OUSB LEDs must not to be updated.
  120.  
  121. G13. All numerical values read from / written to the OUSB Board are considered to be unsigned
  122. and are between 0 and 255 (inclusive), except the ADC values which are 0 to 1023. You as the
  123. programmer must ensure that you construct and send correctly formed OUSB commands to the OUSB
  124. board via a file pipe, as well as read back the correct values.
  125.  
  126. G14. Any binary numbers within this document have the prefix 0b. This notation is not supported
  127. by C++ cout statements, however is used to avoid confusion between decimal, hexadecimal and binary
  128. number formats within the description and specification provided in this document. For
  129. example the number 10 in decimal could be written as 0xA in hexadecimal or 0b1010 in binary.
  130. It can equally be written with leading zeros such as: 0x0A or 0b00001010. For output
  131. to the console screen you should only ever display the numerical characters to represent the number
  132. being displayed (in the expected base format) and omit the 0x or 0b prefixes. Assume all outputs to
  133. the console are in base 10 format (i.e. Decimal) unless specifically specified.
  134.  
  135. G15. Do NOT attempt to read/write to any other OUSB Board IO port other than: PORTB 0-7 (all 8 bits),
  136. PORTD bits PD5 and PD4 (all other portD bits should remain untouched), PINC 0-7, ADC pins 0 through
  137. to 6 and the PWM pin 1, 2 or 3 (on port D). Other pins on the OUSB board are reserved for the
  138. autotester, rs-232 port and the USB interface back to the computer.
  139.  
  140. ___________________________________________________________________________________________
  141. ___ PROGRAM STRUCTURE and FUNCTIONAL REQUIREMENTS:
  142.  
  143. F1. For situations where NO command line arguments are passed to your program:
  144.  
  145. F1.1    Your program must display your correct student details in the format:
  146. "1234567,s1234567@student.rmit.edu.au,FirstName_LastName"
  147.  
  148. OUSB BOARD ACCESS:
  149. F2. Your program MUST access the OUSB board using the OUSB class (including read from/write to PORTB
  150. and PORTD). You must ensure that you have private class data members to store the current value of
  151. PORTB and PORTD and include appropriate accessor methods and private methods to read from and write to
  152. PORTB and PORTD.
  153.  
  154. F2.1 The OUSB class must be instantiated in main() and be used to access the OUSB board IO. The tag
  155. name of the class must be OUSB. You can also instantiate the OUSB class in other parts of the program
  156. if you need too.
  157.  
  158. F2.2 Your program should accept the following command line parameters to read and write to PORTB:
  159.  
  160. - 'B' command: 'B' is the first parameter and the optional second parameter is the number to write to
  161. PORTB.  If the 'B' parameter is use without the optional second parameter, print the current value of
  162. PORTB (only the numeric value) to the console. If the 'B' command is used with a valid unsigned integer
  163. between 0 and 255 as the second parameter, write the value to PORTB and print the new value on PORTB
  164. (after the successful write) to the console.
  165.  
  166. Example 1:
  167. lab3_1234567.exe B     // Read from PORTB and display the number read from PORTB to the console screen
  168. lab3_1234567.exe B 10  // Write 10 to PORTB and display 10 to the console screen if write was successful
  169.  
  170. F2.3 Your program should accept the following command line parameters to read and write to PORTD:
  171.  
  172. - 'D' command: 'D' is the first parameter and the optional second parameter is the number to write to
  173. PORTD.  If the 'D' parameter is use without the optional second parameter, print the current value of
  174. PORTD (only the numeric value) to the console. If the 'D' command is used with a valid unsigned integer
  175. between 0 and 255 as the second parameter, write the value to PORTD and print the new value on PORTD
  176. (after the successful write) to the console.
  177.  
  178. Example 2:
  179. lab3_1234567.exe D     // Read from PORTD and display the number read from PORTD to the console screen
  180. lab3_1234567.exe D 16  // Write 16 to PORTD and display 16 to the console screen if write was successful
  181.  
  182. NOTE:  As selected bits in PORTD are used for USB and RS-232 support, bits: PD7, PD2 will not stick. For
  183. example, if the command arguments were:
  184.  lab3_1234567.exe D 16 D 255
  185. Bits PD7 and PD2 will stay at logic Zero. Therefore when reading back the actual value on PORTD you will
  186. get 123 (0b01111011). Therefore the correct value your program should display to console will be: 123
  187. as this is the actual value that is now on PORTD after writing 255 to it.
  188.  
  189. Example 3:
  190. lab3_1234567.exe D     // Read from PORTD and display the number read from PORTD to the console screen
  191. lab3_1234567.exe D 255 // Write 255 to PORTD and display 123 to the console screen if write was successful
  192. lab3_1234567.exe D 16  // Write 16 to PORTD and display 16 to the console screen if write was successful
  193.  
  194. TRAFFIC LIGHT STATE MACHINE:
  195. F3. Your program MUST have a Traffic light state machine class. You must ensure that you have private
  196. class data members to store the current state of the traffic light (either as a single state to represent
  197. all three lamps, or three lamp states). Include appropriate accessor functions and methods to change the
  198. traffic light state in the class.
  199.  
  200. F3.1 The tag name of the class must be TrafficLight. The TrafficLight class must be instantiated
  201. in main() and be used to control the state of the traffic light. The traffic light is composed of three
  202. lamps (LEDs): Red, Yellow, and Green. The only lamp sequence possible is: Green-->Yellow-->Red (then
  203. back to Green and repeat). Only one lamp can be on at a time.
  204.  
  205. PB3 (PORT_B bit 3) -> Green LED
  206. PD5 (PORT_D bit 5) -> Yellow LED
  207. PD4 (PORT_D bit 4) -> Red LED
  208.  
  209. F3.2 Your program should accept the following command line parameters as the first and second arguments:
  210. - 'R', 'Y' or 'G' as the first argument: The letter indicates which state to start the traffic light
  211.   state-machine in. The corresponding LED should be illuminated if first and second argument are valid.
  212. - The second parameter is a valid unsigned integer between 0 and 15, which indicates the number of state
  213.   transitions the program should run for.
  214.  
  215. Note: Due to the wiring and the configuration of the OUSB board PCB layout, when you switch on the Green LED
  216. by writing a 1 to PORTB bit 3, the independently wired LED on the prototype area as well as the bit 3 LED
  217. on the PORTB LED array will illuminate both at the same time. The Red and Yellow LEDs are independent as
  218. they are connected to bit4 and bit5 of PORTD, respectively.
  219.  
  220. Example 4:
  221. lab3_1234567 R 3  // Start the traffic light on Red and run for three transitions  i.e., R-->G-->Y-->R
  222.  
  223. F3.3 Use PORTB bits: bit0, bit1 and bit2 to indicate the Red and Yellow states of the state-machine.
  224. In addition to the Red LED connected to PORTD being illuminated for the Red state, bit 0 of PORTB should
  225. also be illuminated when the state machine is in the Red state. Similarly, for the Yellow state, bit 1 as
  226. well as bit 2 of PORTB should be illuminated in conjunction with the Yellow LED when the state machine is
  227. in the Yellow state. The second parameter (a valid unsigned integer between 0 and 15) will indicates the
  228. number of state transitions the program should run for. Each transition should have the correct LED
  229. illuminated with the correct LEDs on PORTB switched on for each state transition.
  230.  
  231. Example 5:
  232. lab3_1234567 Y 2  // Start the traffic light on Yellow - Yellow LED and bit 1 and bit 2 of PORTB will be
  233.                   // illuminated. Then the state machine will run for two transitions i.e., Y-->R-->G
  234. Example 6:
  235. lab3_1234567 G 5  // Start the traffic light on Green - Green LED and bit 3 of PORTB will be illuminated.
  236.                   // Then the state machine will run for five transitions i.e., G-->Y-->R-->G-->Y-->R
  237.  
  238. Note: On exit the current state of the traffic light will be maintained on the OUSB Board.
  239.  
  240.  
  241. F3.4 Upon completing all the state transitions, the program should print out the last value of PORTB in
  242. decimal followed directly by a comma symbol (i.e. ',') and then the last value of PORTD in decimal,
  243. followed by a line return. There should be NO white spaces in the output line.
  244.  
  245. Example 7:
  246. lab3_1234567 G 5  // Start the traffic light on Green - Green LED and bit 3 of PORTB will be illuminated.
  247.                   // Then the state machine will run for five transitions i.e., G-->Y-->R-->G-->Y-->R
  248.                   // Output to console will be a single line containing the text:
  249. 1,16
  250.  
  251. F3.5 Your program should accept the following command line parameters as the first and second arguments:
  252. - "CR", "CY" or "CG" as the first argument: The first character 'C', which is common to all 3 commands
  253. indicates that you should maintain an incrementing counter for each state machine step. The value of the
  254. counter then should be encoded into the top 4 bits of PORTB without effecting the lower 4 bits.
  255.  
  256. Note: Bits 0, 1, 2 and 3 of PORTB are already designated to represent the different state values of the
  257. state machine.
  258.  
  259. Example 8:
  260. lab3_1234567 CG 5 // Start the traffic light on Green - Green LED and bit 3 of PORTB will be illuminated.
  261.                   // Then the state machine will run for five transitions i.e., G-->Y-->R-->G-->Y-->R
  262.                   // Output to console will be a single line containing the text:
  263. 81,16
  264.                   // The value 81 means bits 0, 4 and 6 of PORTB will be high. Bit 0 represents the
  265.                   // final state being Red. Bits 4 and 6 mean five (5) state machine transitions were
  266.                   // stepped through.
  267.  
  268. Example 9:
  269. lab3_1234567 CG 1 // Start the traffic light on Green - Green LED and bit 3 of PORTB will be illuminated.
  270.                   // Then the state machine will run for one transitions i.e., G-->Y
  271.                   // Output to console will be a single line containing the text:
  272. 22,32
  273.                   // The value 22 means bits 1, 2 and 4 of PORTB will be high. Bits 1 and 2 represents
  274.                   // the final state being Yellow. Bit 4 mean one (1) state machine transitions was
  275.                   // stepped through. The number 32 means bit 5 of PORTD is high, which indicates the
  276.                   // Yellow LED is on (as the last state of the state machine was Yellow).
  277.  
  278. F3.6 All other bits on PORTB and PORTD should not be modified when individual bits in each port are set
  279. high or cleared to zero. Therefore, when writing values to PORTB or PORTD at anytime throughout your
  280. program, you should be maintaining the current value on the port and only set or clear the bit (or bits)
  281. of interest. For instance when modifying PORTB in response to a valid 'G', 'Y' or 'R' command, then only
  282. the first 4 bits of PORTB should be modified and the upper 4 bits should maintain state.
  283.  
  284. Example 10:
  285. If the OUSB board was setup with the value 255 on PORTB and 123 on PORTD prior to your code running
  286. with the following command line arguments:
  287. lab3_1234567 G 1
  288. The last (and only) line on the console will be:
  289. 246,107
  290.  
  291. Note: In the case of the above example, the program should have only modified bits 0, 1, 2 and 3 of PORTB
  292. and bits 4 and 5 of PORTD.
  293.  
  294. However, when your program responses to a 'CG', 'CY' or 'CR' commands, then the upper 4 bits and lower 4 bits
  295. of PORTB be can be fully modified as the upper 4 bits of PORTB be are used to maintain a counter.
  296.  
  297. Example 11:
  298. If the OUSB board was setup with the value 128 on PORTB and 123 on PORTD prior to your code running
  299. with the following command line arguments:
  300. lab3_1234567 CY 1
  301. The last (and only) line on the console will be:
  302. 22,107
  303.  
  304. Commands 'B' and 'D' can modify all 8 bits in their respective ports with the exception of bits 2 and 7 in
  305. PORTD. Remember that bits 2 and 7 of PORTD are used for the USB interface and always read as logic 0 when
  306. using ousb.exe commands.
  307.  
  308. F3.7 Other considerations:
  309.  
  310. Your program should only display a single line on the console for all specifications followed by a
  311. newline character and then exit (with a return 0).
  312.  
  313. Your program MUST use pipes to communicate with the OUSB board, system calls are NOT allowed:
  314. system("ousb -r io portb 127");   // not allowed
  315.  
  316. System calls take longer and automatically echo the result to the command shell window. If you use system calls
  317. in your code it is very likely the code will not work properly with the autotester or meet specifications. You
  318. will most likely lose all marks for this lab. Therefore, you MUST use the code using pipes. See the code fragments
  319. given to you and explained in lectures and on Blackboard. Sample code to access the OUSB board via pipes is provided.
  320.  
  321. Please remember to use good programming practice, hence every time you open a file pipe you MUST close the pipe using
  322. '_pclose(<pipename>);. Failure to not do this will result in your code being highly unreliable and you could lose
  323. a lot of marks due to your program not behaving correctly while being tested by the autotester.
  324.  
  325. ************  IMPORTANT !!!  *********   IMPORTANT !!!   *******************************
  326. ***
  327. ***  IN LAB TEST 3 YOU MUST IMPLEMENT A CLASS to access the OUSB board - this
  328. ***  will be checked - your maximum score will be capped at 40% if you do not use
  329. ***  an OUSB class!!!  Therefore if you code scores 80% from the autotester, your
  330. ***  actual final mark will be reduced to 40%.
  331. ***
  332. ***  IN LAB TEST 3 YOU WILL AUTOMATICALLY LOSE 20% OF YOUR MARK IF YOU
  333. ***  DO NOT HAVE MATCHING _popen(...) and _pclose(...) - this will be checked!!!
  334. ***
  335. ************  IMPORTANT !!!  *********   IMPORTANT !!!   *******************************
  336.  
  337. About pipes and _popen:
  338. http://www.cplusplus.com/forum/windows/1341/
  339. http://www.cplusplus.com/forum/windows/10766/
  340.  
  341. ___________________________________________________________________________________________
  342. ___ ERROR HANDLING:
  343.  
  344. The following text lists errors you must detect and a priority of testing.
  345. Note: order of testing is important as each test is slightly more difficult than
  346. the previous test.
  347.  
  348. All outputs are a single character followed by a newline ( endl or \n).
  349. DO NOT add more than what is specified, do NOT add additional information.
  350.  
  351. E1. Error precedence:
  352. The order of errors can theoretically be checked in any order, however if multiple errors
  353. occur during a program execution event, your program should only display one error code
  354. followed by a newline character and then exit (with a return 0). The displayed error
  355. codes to the console window should occur in this order:
  356.     'I' - Incorrect input command line argument for first operand (second parameter)
  357.     'P' - Incorrect number of input command line arguments
  358.     'N' - Invalid numerical command line argument (second operand or third parameter)
  359.     'R' - The second operand argument is a numerical value that is out of range
  360.     'Z' - MURPHY'S LAW (undefined error)
  361. Therefore if an invalid numerical command line argument (operand 2) with an invalid first
  362. operand are passed to the program, the first error code should be displayed to the console,
  363. which in this case would be 'I'. Displaying 'N' or 'R' would be result in a loss of marks.
  364.  
  365.  
  366. E2. NUMBER OF PARAMETERS
  367.  
  368. E2.1 If the number of parameters is 1 the program MUST print student ID string in CSV format described
  369. above and then exit. That is, when the program is run without any operands i.e. with no command line
  370. arguments other than the name of the program.
  371.  
  372. E2.2 The second parameter that is passed into the program can only be 'B' 'D', 'R', 'Y', 'G', 'CR',
  373. 'CY' or 'CG'. For any other (case sensitive) letter or character combination or argument string, the
  374. output shall be 'I' which signals the second parameter is erroneous. After outputting 'I' (and new
  375. line), your program should exit without effecting the OUSB board.
  376.  
  377. E2.3 If the number of parameters is 2 and the (first) operand is not 'B' or 'D', the output shall be
  378. 'P' which signals a parameter error. Then program should return 0 and exit.
  379.  
  380. Example 12: That is, following the program name, the command line can take two or three parameters only:
  381. lab3_1234567.exe  Operand1
  382. Testples:
  383. lab3_1234567.exe B          (valid)
  384. lab3_1234567.exe G          (NOT valid: output: P)
  385. lab3_1234567.exe CG         (NOT valid: output: P)
  386.  
  387. E2.4 If the number of parameters is 4 or more: the output shall be 'P' which signals a parameter error.
  388. Then program should return 0 and exit.
  389.  
  390. E2.5 If a numerical unsigned integer is not entered as the third parameter as required for all commands
  391. (apart from when 'B' or 'D' are used alone), the output shall be 'N' which signals a numerical
  392. parameter error in the third parameter. Please note, this does not include out of range values. After
  393. outputting 'N' (and a new line), your program should exit without effecting the OUSB board.
  394.  
  395. Example 13: That is, following the program name, the command line can take two or three parameters only:
  396. lab3_1234567.exe  Operand1 Operand2
  397. Testples:
  398. lab3_1234567.exe G 1        (valid)
  399. lab3_1234567.exe G 1.0      (NOT valid: output: N)
  400. lab3_1234567.exe G -1       (NOT valid: output: N)
  401. lab3_1234567.exe G t1       (NOT valid: output: N)
  402. lab3_1234567.exe G 1t       (NOT valid: output: N)
  403.  
  404. E3.  VALID NUMBER RANGE
  405.  
  406. E3.1 If the number entered as the third parameter on the command line is not within an accepted range,
  407. then the output shall be 'R' which signals a range error. Then program should return 0 and exit.
  408.  
  409. The accepted number ranges are:
  410.     - 'B' and 'D' commands: Second operand must be between 0 and 255 in valid integer format
  411.     - 'R', 'Y' or 'G' commands: Second operand must be between 0 and 15 in valid integer format
  412.     - 'CR', 'CY' or 'CG' commands: Second operand must be between 0 and 15 in valid integer format
  413.  
  414. Example 14: That is, following the program name, the command line can take two or three parameters only:
  415. lab3_1234567.exe  Operand1 Operand2
  416. Testples:
  417. lab3_1234567.exe B 250      (valid)
  418. lab3_1234567.exe G 10       (valid)
  419. lab3_1234567.exe B 256      (NOT valid: output: R)
  420. lab3_1234567.exe G 16       (NOT valid: output: R)
  421.  
  422. Note: All numerical arguments must be an unsigned integer value, therefore if a decimal or exponential
  423. number is provided as an argument, your program should have already responded with 'N' from previous
  424. error check.
  425.  
  426. E4.  ANYTHING ELSE THAT CAN GO WRONG (MURPHY'S LAW TEST)
  427. If there are any other kinds of errors not covered here, the output shall be 'Z'. That is: anything
  428. else that can possibly go wrong and has not be explicitly tested for or covered under the tests above.
  429. For example:
  430.     - no OUSB board connection.
  431.     - disconnection during operation
  432.     - HW dead.
  433.     - ???? anything else Murphy can dream up.
  434.     - everything that has not been covered elsewhere is Murphy's domain.
  435.  
  436. Note: The OUSB.exe will give a message "Fatal Exception" if the board is not connected or is disconnected
  437. during normal operation. This message should NOT be returned by your code. The only output your code
  438. should give is the single character: 'Z', followed by a new line and then it should return 0 and exit.
  439.  
  440. Suggestion: only worry about Hardware errors/test if you have clearly covered ALL preceding tests!
  441.  
  442. ERROR CHECKING HINTS: The basic programming rules are simple (as covered in lectures):
  443. 1) Check that the input is valid.
  444. 2) Check that the output is valid.
  445. 3) If any library function returns an error code USE IT !!! CHECK FOR IT !!! If that means more than 50%
  446. of your code is error checking, then that's the way it has to be.
  447.  
  448. ___________________________________________________________________________________________
  449. ___ GENERAL INFORMATION AND HINTS:
  450.  
  451. - This program must be in one file named lab3_1234567.cpp where 1234567 is your
  452. student number.
  453. For Testple : lab3_1234567.cpp or lab3_3987654.cpp
  454. Your filename MUST NEVER EVER contain any spaces.  _under_score_is_Fine.
  455.  
  456. - When text output is required you must generate EXACTLY that
  457. output and no other output.  For Testple-
  458. Specified output: "RX"    Actual program output: "rx"      Result: zero marks.
  459. Specified output: "RX"    Actual program output: "Got RX"  Result: zero marks.
  460. Specified output: "RX"    Actual program output: " RX"     Result: zero marks.
  461.  
  462. - Use debug mode and a breakpoint at the return statement prior to program finish in main.
  463.  
  464. - What string conversion routines do you know that convert strings to numbers?
  465. Look carefully as they will be needed to convert a command line parameter to a number and
  466. also check for errors.
  467.  
  468. - Most conversion routines do have inbuilt error checking - USE IT !!!
  469. That means: test for the error condition and take some action if the error is true.
  470. If that means more than 70% of your code is error checking, then that's
  471. the way it has to be.
  472.  
  473. - Tackle the easiest tasks first and build your program up in stages. The easiest tasks are
  474. are normally the first tasks mentioned. Therefore has you get down to the last few tasks these
  475. are the tasks that can trip you up or lead to issues if you have not designed your code
  476. with an adequate structure to handle all the possible issues and combinations.
  477.  
  478. - Your mark will result from testing your program against a whole range of inputs. The secret to
  479. getting a good mark is to carefully analyse all possible errors and odd situations BEFORE you
  480. write the code so read the proforma VERY carefully. Ensure in each circumstance you can detect
  481. the error or condition and report the appropriate result. Hint: write test cases before you
  482. write any code.
  483.  
  484. - General OUSB commands that might be useful:
  485. To read the value at port B:                    ousb -r io portb
  486. To write 20 to port B:                          ousb -r io portb 20
  487. To write 16 to port D:                          ousb -r io portd 16
  488. To read the value of the DIP switches (pinc):   ousb -r io pinc
  489. To read the potentiometer (ADC5):               ousb -r adc 5
  490. To set pwm-freq to 45Hz on pin 1:               ousb -r pwm-freq 1 45
  491. To set pwm duty cycle to 50% on pin 1:          ousb -r pwm 1 50
  492.  
  493. - Description of _itoa_s() library function - Converts an integer to a string.
  494. Prototype:
  495.            errno_t _itoa_s(int value, char *buffer, int radix);
  496. Parameters:
  497. [in] value   - Number to be converted.
  498. [out] buffer - buffer is filled with the result of the conversions a C string.
  499. [in]  radix  - Base of value, which must be in the range of 2-36.
  500. Return Value - Zero if successful; an error code on failure.
  501.  
  502.  
  503. - Description of strcpy_s() library function - Copies a C string.
  504. Prototype:
  505.            errno_t strcpy_s(char *strDestination, const char *strSource);
  506. Parameters:
  507. strDestination - Location of the destination string buffer.
  508. strSource      - Null-terminated source string buffer.
  509. Return Value   - Zero if successful; an error code on failure.
  510.  
  511.  
  512. - Description of strcat_s() library function - Appends a C string.
  513. Prototype:
  514.            errno_t strcat_s(char *strDestination, const char *strSource);
  515. Parameters:
  516. strDestination - Null-terminated destination string buffer.
  517. strSource      - Null-terminated source string buffer.
  518. Return Value   - Zero if successful; an error code on failure.
  519.  
  520.  
  521. - The following code is the syntax required to open a pipe to the device.
  522. It may / may not include all of the necessary code to complete the task.
  523.  
  524. //----Start of template standalone .cpp file--------------------------------------------
  525. #include "pch.h"
  526. #include <iostream>
  527. #define Debug 1             // set debug to ON
  528. using namespace std;
  529. int main(int argc, char* argv[])
  530. {
  531.     char command[256] = {};
  532.     strcpy_s(command, "ousb -r io PORTB 20"); // command to write 20 to OUSB board
  533.  
  534.     if (Debug) cout << "DEBUG :" << command << endl; // print out OUSB data for debug purposes
  535.  
  536.     FILE *fpipe;
  537.     char line[250] = {}; // size of Line should be smaller than command
  538.  
  539.     fpipe = (FILE*)_popen(command, "r");    // attempt to open pipe and execute a command
  540.     if (fpipe != NULL)                     // check that the pipe opened correctly
  541.     {
  542.         while (fgets(line, sizeof(line), fpipe))
  543.         {   // do nothing here, or print out debug data
  544.             if (Debug) cout << "DEBUG :" << line ; // print out OUSB data for debug purposes
  545.         }
  546.         _pclose(fpipe);   // close pipe
  547.     }
  548.     else cout << "Error, problems with pipe!\n";
  549.  
  550.     // do something with the value returned by the OUSB board, eg:
  551.     unsigned short portb = (unsigned short)atoi(line); // convert from char array to unsigned short
  552.     if (Debug) cout << "DEBUG :" << "value = " << portb << endl;
  553.  
  554.     return 0;
  555. }
  556. //----End of template standalone .cpp file----------------------------------------------
  557. ____________________________________________________________________________________________
  558. */
  559. // These are the libraries you are allowed to use to write your solution. Do not add any
  560. // additional libraries as the auto-tester will be locked down to the following:
  561. #include "pch.h"
  562. #include <iostream>
  563. #include <sstream>
  564. #include <fstream>
  565. #include <cstdlib>
  566. #include <string.h>
  567. #include <windows.h>
  568. #include <cmath>    
  569. // Do NOT Add or remove any #include statements to this project!!
  570. // All library functions required should be covered by the above
  571. // include list. Do not add a *.h file for this project as all your
  572. // code should be included in this file.
  573.  
  574. using namespace std; // Please do not remove this line
  575.  
  576.                      // optional: your choice if you want to use/modify these definitions for MAX-MIN-Range
  577. const double MAXRANGE = 255;
  578. const double MINRANGE = 0;
  579.  
  580. // optional: your choice if you want to write class method and function prototypes here.
  581. //           They should be defined above the OUSB and TrafficLight Class definitions
  582. //           if they are going to be used by the classes themselves as all code must
  583. //           contained in this file (ie. no separate *.h files for autotester)
  584.  
  585.  
  586. class OUSB          // Do NOT change class name
  587. {
  588. private:
  589.     // data inputs
  590.     unsigned short PORTA;
  591.     unsigned short PORTB; // private, used to hold the current VAL of PortB, can only be accessed by OUSB::Public members
  592.     unsigned short PORTC;
  593.     unsigned short PORTD;
  594.  
  595.     unsigned short runOUSBcommand(string *command);
  596.  
  597. public:
  598.     OUSB()
  599.     {
  600.         PORTA, PORTB, PORTC, PORTD = 0;
  601.     }
  602.     // Constructor: Remember to define this else it won't compile
  603.  
  604.      // Interface functions (These can be changed....)
  605.     // functions for class
  606.     void WritePortB(unsigned short newValue); // class method
  607.     void writePORTD(unsigned short newValue);
  608.  
  609.  
  610.     unsigned short readPORTA();
  611.     unsigned short ReadPortB();
  612.     unsigned short readPORTC();
  613.     unsigned short readPORTD();
  614.  
  615.  
  616.     // Values
  617.  
  618.     unsigned short PASS; // whatever we pass to the function -- // class member
  619.  
  620.     // constructors
  621.  
  622.  
  623.  
  624.     // you may choose to implement operator overloaded functions for read and write functions for portb
  625.     // void operator<<(const unsigned short &val); // write portb
  626.     // void operator>>(unsigned short &val);       // read portb  
  627. };
  628.  
  629. // Traffic Light class
  630. class TrafficLight // Do NOT change class name
  631. {
  632. private:
  633.     bool redLamp;
  634.     bool yellowLamp;
  635.     bool greenLamp;
  636. public:
  637.     TrafficLight()
  638.     {
  639.         redLamp, yellowLamp, greenLamp = 0;
  640.     }
  641.     // Constructor: Remember to define this else it won't compile
  642.  
  643.     // Interface functions (These can be changed....)
  644.     string lightOn(string val);
  645.     bool isREDon(string x) // Accessor function  
  646.     {
  647.         if (x == "R" || x =="CR")
  648.         {
  649.             redLamp = 1;
  650.             return true;
  651.         }
  652.         else
  653.         {
  654.             redLamp = 0;
  655.             return false;
  656.         }
  657.     }
  658.  
  659.     //  string yellowOn();
  660.     bool isYELLOWon(string x) // Accessor function
  661.     {
  662.         if (x == "Y" || x == "CY")
  663.         {
  664.             yellowLamp = 1;
  665.             return true;
  666.         }
  667.         else
  668.         {
  669.             yellowLamp = 0;
  670.             return false;
  671.         }
  672.     }
  673.  
  674.     //string greenOn();
  675.     bool isGREENon(string x) // Accessor function
  676.     {
  677.         if (x == "G" || x == "CG")
  678.         {
  679.             greenLamp = 1;
  680.             return true;
  681.         }
  682.         else
  683.         {
  684.             greenLamp = 0;
  685.             return false;
  686.         }
  687.     }
  688.  
  689.     string changeTrafficLightState(string state);
  690. };
  691. // write all class methods and stand alone functions here below this line
  692.  
  693. // CLASS OUSB
  694.  
  695. unsigned short OUSB::runOUSBcommand(string *command)
  696. {
  697.     FILE *fpipe;
  698.     char line[250] = {}; // size of Line should be smaller than command
  699.     fpipe = (FILE*)_popen(command->c_str(), "r");    // attempt to open pipe and execute a command
  700.     if (fpipe != NULL)                     // check that the pipe opened correctly
  701.     {
  702.         while (fgets(line, sizeof(line), fpipe))
  703.         {   // do nothing here, or print out debug data
  704.         //  if (Debug) cout << "DEBUG :" << line; // print out OUSB data for debug purposes
  705.         }
  706.         _pclose(fpipe);   // close pipe
  707.     }
  708.     //else cout << "Error, problems with pipe!\n";
  709.     // do something with the value returned by the OUSB board, eg:
  710.     //unsigned short portb = (unsigned short)atoi(line); // convert from char array to unsigned short
  711.     unsigned short TEMP = (unsigned short)atoi(line);
  712.     return TEMP; // value for command
  713. }
  714.  
  715. void OUSB::WritePortB(unsigned short PASS)
  716.  
  717. {
  718.     char LED[10] = {};
  719.     _itoa_s(PASS, LED, 10); // convert int to string
  720.     string command = "ousb -r io PORTB "; // command to write 20 to OUSB board
  721.     command.append(LED);
  722.     //strcat_s(command, LED); // slap em together
  723.  
  724.     PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
  725.  
  726.  
  727. }
  728.  
  729. void OUSB::writePORTD(unsigned short PASS)
  730.  
  731. {
  732.     char LED[10] = {};
  733.     _itoa_s(PASS, LED, 10); // convert int to string
  734.     string command = "ousb -r io PORTD "; // command to write 20 to OUSB board
  735.     command.append(LED);
  736.     //strcat_s(command, LED); // slap em together
  737.  
  738.     PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference
  739.  
  740.  
  741. }
  742.  
  743. unsigned short OUSB::ReadPortB()
  744.  
  745. {
  746.     string command = "ousb -r io PORTB";
  747.  
  748.     PORTB = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  749.  
  750.     return PORTB;
  751. }
  752.  
  753. unsigned short OUSB::readPORTD()
  754.  
  755. {
  756.     string command = "ousb -r io PORTD";
  757.  
  758.     PORTD = runOUSBcommand(&command); // passes string to COMMAND class. Also stores last value for a reference                                
  759.  
  760.     return PORTD;
  761. }
  762.  
  763. // CLASS Traffic LX
  764.  
  765. string TrafficLight::lightOn(string val)
  766. {
  767.     if (isGREENon(val) == true)
  768.     {
  769.         val = { "8" };
  770.         return val;
  771.     }
  772.     else if (isYELLOWon(val) == true)
  773.     {
  774.         val = { "6 32" }; // 1 for now, will change later
  775.         return val;
  776.     }
  777.     else if (isREDon(val) == true)
  778.     {
  779.         val = { "1 16" }; // 1 for now, will change later
  780.         return val;
  781.     }
  782.  
  783.     return val;
  784. }
  785.  
  786. string TrafficLight::changeTrafficLightState(string state)
  787. {
  788.     if (state == "R")
  789.     {
  790.         state = "G";
  791.         return state;
  792.     }
  793.     if (state == "G")
  794.     {
  795.         state = "Y";
  796.         return state;
  797.     }
  798.     if (state == "Y")
  799.     {
  800.         state = "R";
  801.         return state;
  802.     }
  803.     if (state == "CR")
  804.     {
  805.         state = "CG";
  806.         return state;
  807.     }
  808.     if (state == "CG")
  809.     {
  810.         state = "CY";
  811.         return state;
  812.     }
  813.     if (state == "CY")
  814.     {
  815.         state = "CR";
  816.         return state;
  817.     }
  818.     return state;
  819. }
  820.  
  821. int main(int argc, char *argv[])
  822. {
  823.     //--- When no parameters MUST print id string in CSV format.
  824.     if (argc == 1)  // no parameters print this line.
  825.     {
  826.         cout << "3662132,s3662132@student.rmit.edu.au,Isaac_Munoz"
  827.             << endl;
  828.         // Any errors in the line above,  or the naming of the file
  829.         //  will result in 10% being deducted from your mark.
  830.         return(0);
  831.     }
  832.  
  833.     //--- START YOUR CODE HERE.
  834.  
  835.     string start = argv[1]; // R, G, Y starting position
  836.     string val = {};
  837.     string PB = {};
  838.     string PD1 = {};
  839.     string PD2 = {};
  840.  
  841.     int transitions = atoi(argv[2]); // number of transitions
  842.    
  843.  
  844.     // traffic lx states
  845.  
  846.     int LB = 0; // PortB
  847.  
  848.  
  849.     int LD = 0; // PortD
  850.  
  851.  
  852.     OUSB state;
  853.     int Current_stateB = 0;
  854.     int Current_stateD = 0;
  855.     TrafficLight light;
  856.  
  857.     if (argc == 2)
  858.     {
  859.         if (argv[1] == "B")
  860.         {
  861.             LB = state.ReadPortB();
  862.             cout << LB << endl;
  863.             return 0;
  864.         }
  865.         if (argv[1] == "D")
  866.         {
  867.             LD = state.readPORTD();
  868.             cout << LB << endl;
  869.             return 0;
  870.         }
  871.         else
  872.         {
  873.             cerr << "I" << endl;
  874.             return 0;
  875.         }
  876.     }
  877.     if (argc == 3)
  878.     {
  879.         for (int i = 0; i < transitions + 1; i++)
  880.         {
  881.             // read traffixlx state
  882.  
  883.             if (start == "R")
  884.             {
  885.                 val = light.lightOn(start);
  886.                 PB = val[0];
  887.                 PD1 = val[2];
  888.                 PD2 = val[3];
  889.                 PD1.append(PD2);
  890.  
  891.                 LB = stoi(PB); //make red
  892.                 LD = stoi(PD1); //make red
  893.  
  894.                 Current_stateB = state.ReadPortB();
  895.                 Current_stateD = state.readPORTD();
  896.  
  897.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  898.                 LB = LB | Current_stateB; // inserts RLB into lower 4.
  899.  
  900.                 Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  901.                 LD = LD | Current_stateD; // inserts RLD into lower 4.
  902.  
  903.                 state.WritePortB(LB); // Write new Value
  904.                 state.writePORTD(LD); // Write new Value
  905.  
  906.                 start = light.changeTrafficLightState(start); //start = G
  907.  
  908.             }
  909.             if (start == "G")
  910.             {
  911.                 val = light.lightOn(start);
  912.                 PB = val[0];
  913.  
  914.  
  915.                 LB = stoi(PB); //make green
  916.  
  917.  
  918.                 Current_stateB = state.ReadPortB();
  919.                 Current_stateD = state.readPORTD();
  920.  
  921.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  922.                 LB = LB | Current_stateB; // inserts GLX into lower 4.
  923.  
  924.                 LD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  925.  
  926.                 state.writePORTD(LD);
  927.                 state.WritePortB(LB); // Write new Value
  928.  
  929.                 start = light.changeTrafficLightState(start); //start = Y
  930.             }
  931.             if (start == "Y")
  932.             {
  933.                 val = light.lightOn(start);
  934.                 PB = val[0];
  935.                 PD1 = val[2];
  936.                 PD2 = val[3];
  937.                 PD1.append(PD2);
  938.  
  939.  
  940.                 LB = stoi(PB); //make yellow
  941.                 LD = stoi(PD1); //make yellow
  942.  
  943.  
  944.                 Current_stateB = state.ReadPortB();
  945.                 Current_stateD = state.readPORTD();
  946.  
  947.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  948.                 LB = LB | Current_stateB; // inserts RLB into lower 4.
  949.  
  950.                 Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  951.                 LD = LD | Current_stateD; // inserts RLD into lower 4.
  952.  
  953.                 state.WritePortB(LB); // Write new Value
  954.                 state.writePORTD(LD); // Write new Value
  955.  
  956.                 start = light.changeTrafficLightState(start); //start = R
  957.             }
  958.             if (start == "CR")
  959.             {
  960.                 val = light.lightOn(start);
  961.                 PB = val[0];
  962.                 PD1 = val[2];
  963.                 PD2 = val[3];
  964.                 PD1.append(PD2);
  965.  
  966.                 LB = stoi(PB); //make red
  967.                 LD = stoi(PD1); //make red
  968.  
  969.                 //Current_stateB = state.ReadPortB();
  970.                 //Current_stateD = state.readPORTD();
  971.  
  972.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  973.                 Current_stateB = Current_stateB | (i << 4);
  974.                 LB = LB | Current_stateB; // inserts RLB into lower 4.
  975.  
  976.                 Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  977.                 LD = LD | Current_stateD; // inserts RLD into lower 4.
  978.  
  979.                 //state.WritePortB(LB); // Write new Value
  980.                 //state.writePORTD(LD); // Write new Value
  981.  
  982.                 cout << LB << "," << LD << endl;
  983.                 start = light.changeTrafficLightState(start); //start = G
  984.                 continue;
  985.             }
  986.             if (start == "CG")
  987.             {
  988.                 val = light.lightOn(start);
  989.                 PB = val[0];
  990.  
  991.  
  992.                 LB = stoi(PB); //make green
  993.  
  994.  
  995.                 //Current_stateB = state.ReadPortB();
  996.                 //Current_stateD = state.readPORTD();
  997.  
  998.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  999.                 Current_stateB = Current_stateB | (i << 4);
  1000.                 LB = LB | Current_stateB; // inserts GLX into lower 4.
  1001.  
  1002.                 LD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  1003.  
  1004.                 //state.writePORTD(LD);
  1005.                 //state.WritePortB(LB); // Write new Value
  1006.  
  1007.                 cout << LB << "," << LD << endl;
  1008.                 start = light.changeTrafficLightState(start); //start = Y
  1009.                 continue;
  1010.             }
  1011.             if (start == "CY")
  1012.             {
  1013.                 val = light.lightOn(start);
  1014.                 PB = val[0];
  1015.                 PD1 = val[2];
  1016.                 PD2 = val[3];
  1017.                 PD1.append(PD2);
  1018.  
  1019.  
  1020.                 LB = stoi(PB); //make yellow
  1021.                 LD = stoi(PD1); //make yellow
  1022.  
  1023.  
  1024.                 //Current_stateB = state.ReadPortB();
  1025.                 //Current_stateD = state.readPORTD();
  1026.  
  1027.                 Current_stateB = Current_stateB & 240; // bitshift: clears bottom 4 bits
  1028.                 Current_stateB = Current_stateB | (i << 4);
  1029.                 LB = LB | Current_stateB; // inserts RLB into lower 4.
  1030.  
  1031.                 Current_stateD = Current_stateD & 207; // bitshift: clears bottom 4 bits
  1032.                 LD = LD | Current_stateD; // inserts RLD into lower 4.
  1033.  
  1034.                 //state.WritePortB(LB); // Write new Value
  1035.                 //state.writePORTD(LD); // Write new Value
  1036.                 cout << LB << "," << LD << endl;
  1037.                 start = light.changeTrafficLightState(start); //start = R
  1038.                 continue;
  1039.             }
  1040.  
  1041.         }
  1042.        
  1043.         cout << LB << "," << LD << endl;
  1044.     }
  1045.  
  1046.  
  1047.  
  1048.     return(0); // The convention is to return Zero to signal NO ERRORS, if you change it
  1049.                // the AutoTester will assume you have made some major error.  
  1050.                // Leave it as zero.
  1051. }
  1052. // do NOT put any functions/code after this closing bracket, if you use functions add them before main()
  1053. // End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement