please help me to sort this out
Microsoft Word - EEET2246 - Week_5_and_6_Tasks.docx EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 1 EEET2246–EngineeringComputing1–Week5and6LaboratoryTasks Due: Friday ‐ Week 7, 11:59pm via Canvas / Total Marks 8%. Overview: Laboratory Exercise 2 consists of two sets of Laboratory Tasks (defined in this document) and one Laboratory Test (Laboratory Test 2). The Laboratory Tasks are designed to help you build your code solution for the corresponding test. Part A is designed to be completed in week 5, whereas, Part B should be completed in week 6. Your combined report for Part A (worth 4%) and Part B (worth 4%), as well as your Laboratory Test 2 submission (worth 8%) are due in week 7 via Canvas. The total combined value for Laboratory Exercise 2 is 16% of your final mark for EEET2246. All assessment items for Laboratory Exercise 2 are due by Friday, Week 7 at 11:59pm. IMPORTANT: Please use the Collaborate Ultra Laboratory sessions as Q & A to work through the tasks. Your laboratory tutor will not mark any work during these sessions and will be available online during this time to answer questions and provide assistance. Assessment: There are two types of tasks – explanation‐ and code‐based. Explanation questions require you to provide a written descriptive answer or screen capture based on the question in your report. These types of questions will have a heading of Explanation based. Code‐based means you will need to submit a working standalone *.cpp file. This type of question can be identified by the heading Code Submission. The submitted *.cpp files must be fully functional and compile without modification to achieve full marks. Each question that is marked as “Code Submission” requires a separate .cpp file to be submitted. The naming convention is as follows: ‘s1234567_week5_6_question_PartAx.cpp’, where s1234567 is your student number and question_PartAx corresponds to the actual question you are answering. Please do not submit all tasks in one .cpp file. Note: Some tasks will not attract any marks at all. These tasks are either introductory or bridging tasks to assist in your understanding of the topic. The report only needs to provide the required diagrams and text to inform the assessor what the required inputs are and the expected output of the code. Text explanations should be kept to a minimum and it is suggested to use dot points where possible. The report should use the same question number referencing as this document so that it is clear which answers are for what task questions. Note: All tasks are marked on a scale, however if code is not submitted for a given task (when required) or if it does not compile you will not score marks for that item. SubmissionProcess: You will be required to submit a report with specific standalone *.cpp files to address the tasks outlined in this document. The document should contain the content relating to both Part A and Part B tasks from both week 5 and week 6. THE FIRST PAGE OF THE REPORT MUST SPECIFY: Your full name, student number, number of *.cpp files submitted, enrolled Laboratory session time slot and tutor name. The report along with *.cpp files must be uploaded via Canvas before the deadline to avoid any late penalties. A deduction of 10% per 24‐hour period (including weekends) will be applied to late assessments. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 2 PartA–Week5CollaborateUltraSession(4%) Aim: The aim of these laboratory tasks is to continue developing your C++ skills within the Microsoft Visual Studio 2019 Integrated Development Environment (IDE). When combined with the Week 6 Laboratory Tasks, you will have effectively developed the necessary code for a significant portion of the Week 7 Laboratory Test. You are strongly recommended to download and familiarize yourself with the Laboratory Test 2 proforma .cpp file which is available on Canvas and start writing a practice solution after completing these tasks. An essential requirement for Engineers is the ability to write software that will interface with a wide variety of hardware systems. In the EEET2246 laboratories we will be using the Open‐USB‐IO (OUSB) Simulator to demonstrate how users can interact with simulated hardware and the influence that this has on the software we will be developing. Note that the OUSB‐IO simulator board only works with a Microsoft Windows‐based PC. The details of the simulator (ousb.exe) can be found on the course ‘Canvas’ website under OUSB Board Resources. Interfacing your programs with simulated hardware will present a new series of challenges. To simplify the process the communication with the virtual OUSB‐IO Board we will be using the OUSB.exe file, which handles the interface between the simulated OUSB‐IO Board virtual USB Ports. It is also suggested that you obtain a copy of the OUSB.exe file as well as the OUSB‐IO Board reference manual which is available on the EEET2246 Canvas site as well as on the www.pjradcliffe.wordpress.com website. This laboratory will require you to perform the following tasks with the simulated OUSB Board: Use the provided OUSB.exe file to explore the various command‐line options. With the aid of the reference manual determine the parameters required to write an integer value to the simulated OUSB‐IO Board Light Emitting Diodes (LEDs). Use the provided template code to create a ‘communication pipe’ between your application and the OUSB.exe executable. Convert the value of the OUSB‐IO Board virtual switch state to an integer that can be used for further calculations. Furthermore, you should become more familiar with the IDE and how to use its debugging features such as breakpoints and ‘watch’ windows. This week you are encouraged to build up your code solutions sequentially (where appropriate) as you move through each task, however you should keep a copy of each solution as a standalone piece of code by using block comment (/* */) to save the code for future reference. Remember to use the lecture notes as a reference as well as the prescribed textbook and the OUSB‐IO Board reference manual. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 3 Tasks 1. To communicate with the simulated OUSB‐IO Board an intermediate program is required (OUSB.exe). This file can be downloaded from the EEET2246 Canvas shell website by navigating to the Modules page and then selecting ‘OUSB Board Resources’ page. Once on the “OUSB Board Resources” page download the ousb.exe file by clicking on ‘OUSB.exe’ link. For your application to access the OUSB.exe, file it must be placed in the same folder (directory) as your .cpp file. However, initially we will explore the various command‐line options of the OUSB.exe application by sending it commands via the console command window. To do this, create a new Visual Studio Project and place the OUSB.exe file in the same folder as the projects *.cpp file. Open a command prompt (using the techniques developed in the Week 2 tasks) and navigate to the folder containing your Visual Studio project with the OUSB.exe file in it. Run the executable by typing ‘OUSB’ and then press enter at the command line console. You should be displayed with a screen similar to Figure 1. Figure 1 ‐ OUSB Command Line Options. Even though we will be using the ousb.exe simulator, it is helpful to understand what the simulator is trying to emulate. Below is a picture of the OUSB‐IO Board showing the DIP switches connected to PINC and the 8 LEDs connected to PORTB. Figure 2 ‐ OUSB Board showing the DIP switches connected to PINC and the 8 LEDs connected to PORTB. The LEDs on the OUSB‐IO Board are connected to the individual bits of PORTB, from bit 0 through to bit 7. Hence PORTB can represent the values 0 to 255 in binary (0b00000000 – 0b11111111). Similarly, each switch is connected to the input of PORTC. However, as these bits are setup as inputs, you would need to read the value of the DIP switches using the PINC register. Additionally, the wiring of the DIP switch inverts EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 4 the logic on the port which results in the settings shown in Figure 2 having a value of 254 (0b11111110) as bit 0 is set to ON. Given the description of the parameters in Figure 1 and Figure 2, determine the command line arguments that would be required to turn on every second LED (Most Significant to Least Significant) on the simulated OUSB‐IO Board. Hint: What value is required to turn a LED on and how can you determine the value in binary? You can also use the OUSB‐IO Board reference manual to determine the name of the register that is connected to the 8 LEDs on the board. 2. Another requirement for the Laboratory 2 Test is to read input from the virtual switches on the OUSB‐IO Board. Based on the discussion above, Figure 2 and the OUSB‐IO reference manual, what would the command be to read the current value of the switches? What is the position of the switches to obtain a reading of 63 decimal? Confirm your answer by setting the corresponding virtual switch value (via the pinc.txt file) and obtaining the results from the OUSB.exe application. Explanation based 3. From the results obtained in Questions 1 and 2, the output contained significantly more information than required. What command line switch would be required to display the output from the simulated OUSB‐IO Board as only a decimal value? Repeat Questions 1 and 2 with the modified command which incorporates the optional flag. Place a screenshot and a small discussion in your report demonstrating the difference in command lines for Questions 1 – 3. [2 marks] Explanation based . 4. The work conducted to date has utilised the command line to run the OUSB.exe executable. Whilst demonstrating how the application operates and the parameters required this technique needs to be extended for the Laboratory 2 Test. One method that can be utilised to communicate with an external application from your program is by utilising a construct known as a ‘pipe’. You can think of a ‘pipe’ as a method of transferring data to / from another application. Utilising a ‘pipe’ commands can be sent to the OUSB.exe file and the data returned. As a starting point, it is suggested that you download the ‘OUSB_board_Sample_code.cpp’ file from the Canvas shell (under ‘Week 5 and 6 Laboratory Tasks’ page). This code example contains the all the basic code required to communicate to the virtual OUSB‐IO executable via a C/C++ pipe. You might also find it useful to look at the ‘OUSB_getting_started_code.cpp‘ file, which is an extension of the sample code file. The code is fully commented, and it is highly recommended that you work through each line it in detail using the lecture notes a reference / resource. Note that the essential communication code will be provided in the Laboratory 2 Test proforma, so there is no need to memorise the exact syntax of the pipe code. For this question, identify the code that is responsible for writing to ‘PORTB’ via the OUSB executable. Write a program by copying the relevant OUSB commands from Question 1 into your program and use a program control statement to automatically increment the value of ‘PORTB’ from 0 to 100 with a step size of 1. Note that you should open and close the pipe (_popen() and _pclose()) every time you attempt to read / update the values of the LEDS. Hint: Consider using the _itoa_s() function. Take a screenshot of the console window demonstrating that the virtual OUSB‐IO board is being updated. [2 marks] Explanation based 5. Now that you have been able to write data to the virtual LEDs on the OUSB Board, the next step is to obtain the current value of the 8 dip switches on PINC. From the result of Question 2, set the virtual EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 5 switches to decimal 63 (via the pinc.txt file). Refer back to the ‘OUSB_getting_started_code.cpp‘ file and determine the portion of the code that reads data form the board. Copy the relevant code into your application and extend it to develop a small application that outputs the data returned from the OUSB Board to the console in decimal mode. Only output the value that has been read from the switches and display it to the console screen without any additional text. Hint: You will need to modify the command variable text with the name of the register that is connected to the DIP switches on the OUSB‐IO Board. Note: Use the OUSB‐IO reference manual to determine the correct command. Provide a screenshot of the actual ‘switch’ value and the corresponding output from your solution. [2 marks] Code Submission 6. The data obtained from the OUSB‐IO Board is returned as a character array in the variable ‘line’. From your previous work, what should be done to convert the characters back into an integer value? Extend your program to convert the characters back into an integer and then add 12 to it before displaying the final value back on the console. Hint: Consider using the _itoa_s() and atoi() functions together with some string modification functions such as strcpy_s() and strcat_s(). What parameters do the functions take? You might consider search or reading about each function on the Microsoft Developer Network: msdn.microsoft.com. Submit you code for this question. [3 mark] Once you have finished these tasks you are free to start your week 6 lab tasks. You can also start practicing for the Week 7 Laboratory Test. Make sure that you download and read the Laboratory Test 2 proforma .cpp file which is available on Canvas. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 6 PartB–Week6CollaborateUltraSession(4%) Aim: The aim of these laboratory tasks is to continue developing your C++ skills within the Microsoft Visual Studio 2019 Integrated Development Environment (IDE). When combined with the Week 5 Laboratory Tasks, you will have effectively developed the necessary code for a significant portion of the Week 7 Laboratory Test. You are strongly recommended to download and familiarize yourself with the Laboratory Test 2 proforma .cpp file which is available on Canvas and start writing a practice solution after completing these tasks. To date, many of the programs that you have written are based around the use of program control statements and converting user input (in the form of command line arguments) into numerical form. However, as you begin to write more complex programs additional functionality will be required. Whilst most processes can be developed based on the material we have covered, one of the key benefits of using a programming language such as C++ is the ability to incorporate code that has been written by others. In C++, we can do this by utilising a concept known as functions. A function can either be provided as part of the programming language (inbuilt function) or you can create a user‐defined function (developed by the programmer) to perform a specific task. The aim of a function is to create a reusable piece of code that can be applied to many situations. For example, to convert an character array (such as argv[][]) to a floating point number we have already used the atof() function. In C++, there are a wide variety of functions available for use which are consistent between different operating systems (Microsoft Windows, MacOS and Linux). A subset of the same functions also exists for embedded hardware environments such as the OUSB‐IO Board. A function consists of three core components (definition) which are the function name, function parameters and the function return type. If we take the function atof() that we have been using throughout this course the full function definition is written as: ( http://www.cplusplus.com/reference/ ) double atof (const char *str); In this example, the function name is atof(), the function parameters are a constant character pointer (const char *) and the function return type is a double. We will discuss the details of a character pointer later in the lecture series, but if you closely examine the main() function in all of your C++ programs you will notice that argc is of type char * and hence the parameters match. Note that you cannot use a function if the parameter data types do not match. More details will be provided in the Week 6 lecture on the essential details of functions. This laboratory will require you to perform the following tasks with a variety of functions: Determine the absolute value of a numerical data type. Utilise a function to determine if a command line argument contains only numerical data. Create extended functionality to determine if the command line argument contains other valid characters for numerical data types. Join two null‐terminated strings together with the aid of a string function and write the result out to the simulated OUSB‐IO Board. Compare two null‐terminated strings using a string function. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 7 Read data from the DIP switches on the simulated OUSB‐IO board that are connected to PORTC (PINC). Furthermore, you should become more familiar with the IDE and how to use its debugging features such as breakpoints and ‘watch’ windows. This week you are encouraged to build up your code solutions sequentially (where appropriate) as you move through each task, however you should keep a copy of each solution as a standalone piece of code by using block comment (/* */) to save the solutions for future reference. Remember to use the lecture notes as a reference as well as the prescribed textbook. Tasks 1. When working with simulated hardware such as the OUSB‐IO Board, it is important that the correct data types are transmitted for further processing. Any data sent should be in the correct range otherwise undefined behaviour may occur causing at best an error message, or in a worst case a system crash. As an example, consider the OUSB‐IO Board LEDs which are PORTB. Using the OUSB.exe program, determine the effect of writing a negative number to the OUSB LEDs. What is the response and is it what you expected? Explanation based 2. In the case of the virtual OUSB‐IO Board sending a negative number does not cause a catastrophic failure. Any data written to the PORTB LEDs should be an absolute value (positive) and between 0 and 255. To convert a negative number to positive an absolute function is required. Using the C++ reference website ( http://www.cplusplus.com/reference/ ) determine how the abs() function can be used. What are the function parameters and the return type? Write a small program that defines a negative integer (between ‐1 and ‐255), converts it to a positive value and then displays it on the console window. Place a table in your report indicating the specific values you applied to your code as well as an example of the output. [2 marks] 3. One of the issues that we have been facing throughout the course is the ability to detect valid user input. Any user input needs to be carefully examined to ensure that it meets the necessary requirements before any further processing is performed or unexpected behaviour could result. To determine if an input argument is valid we have been iterating through the input arguments checking to see whether every character is between '0' and '9'. Generally, this has involved the use of an 'if' statement that was inside a 'for' loop. However, there is a function that we can use to simplify the process: isdigit(). The full function prototype for the isdigit() function is given as: int isdigit ( int c ); The isdigit() function takes an integer 'c' as an input and then returns a non‐zero value if the value of 'c' is a valid digit. Note that even though the input parameter 'c' is an integer, in this case the complier will convert an input character to an integer for you. Via the command line arguments enter the value of 168. Write a small program to determine if the input argument is truly numerical by stepping through the argument via a 'for' loop. At the first invalid digit your program should display an error message and exit. Change the command line argument to 45a21 to check your result. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 8 Explanation based 4. The code written in the previous question was only capable of determining if a numerical digit was entered. For example, what would be the output if you entered 168.03? Extend your program to allow other valid characters such as a decimal point, +/‐ signs, and scientific notation. Note that the placement of the characters should not be checked. List the various inputs and corresponding outputs in your report and with a screenshot of the terminal window demonstrate the output for one of your test values. [2 marks] Explanation based 5. When communicating with the virtual OUSB‐IO Board it is often essential to send a command followed by a numerical value. For example, to display the decimal value of 42 on the LEDs, the following command would be required: ousb –r io PORTB 42 In the case where you wanted to update the OUSB LEDs value, then then a similar command would be required, with the exception of the change in the numerical value. A convenient method to do this is to join two strings together. This process is called string concatenation and as expected, C++ provides the ability to do this. The function of interest is call strcat_s() with a definition of: char * strcat_s ( char * destination, const char * source ); The parameter destination is the first null‐terminated string and the parameter source is the null‐ terminated string that is to be appended to destination. In the case of the OUSB Board command, the destination string would be set to "ousb –r io PORTB " and then the character value of '42' would be added. Using the ‘OUSB_Read_Write_Functions.cpp’ file from the Week 5 and 6 laboratory tasks page on the EEET2246 Canvas shell as a reference, write a small application that creates the complete command string that would be sent to the OUSB Board to increment the LEDs sequentially from 0 to 10 and display it on the console window. (As an extra task you may want to write it out to the OUSB Board). Note that you will also need to consider the function _itoa_s() to convert the number to write to a character. Place a screenshot and short explanation in your report of how the concatenation functions using a sample piece of text. Ensure that you indicate the contents of the null‐terminated string. [2 marks] 6. Another common task is to compare two null‐terminated strings to confirm that they are identical. Unlike numerical data, the equivalence ( == ) operator cannot be used. To compare null‐terminated strings the function strcmp() can be utilised. Using the reference website http://www.cplusplus.com/reference/ determine the function definition. Note that this function will return zero if the null‐terminated strings are equal. Set the command line input argument to the letter ‘a’ and write a small program using strcmp() to determine if the character is indeed an 'a'. To create the null‐terminated string to compare against you can use the following code: const char letterA[2] = {'a','\0'}; Code Submission & Explanation 7. Another common task when writing software that controls hardware like the virtual OUSB‐IO Board is the need to read data from the device as well as write data to it within the same program. The OUSB‐IO board has a set of 8 DIP switches that are connected to PORTC of the ATmega32 microprocessor on the OUSB‐IO board. These switches can be set to 256 discrete values (0‐>255) as there are 8 switches in total. You should familiarise yourself with the OUSB‐IO board reference manual and modify a version of pipe code in EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 9 the ‘OUSB_Read_Write_Functions.cpp’ file from lecture 4 with the correct command to read the values from the 8 DIP switches. Thereafter, write a small program that displays the value of the DIP switches on the LEDs connected to PORTB. You program should update the values of LEDs to reflect changes made to the switches in real time, therefore you might consider using an infinite loop to perform the read/write operations. Submit your code and provide a screenshot of the test values being used and the corresponding update of the virtual LEDs. [4 marks] Once you have finished these tasks you are free to start practicing for the Week 7 Laboratory Test. Make sure that you download and read the Laboratory Test 2 proforma .cpp file which is available on Canvas. It is suggested that you use the Six Basic Steps of Program Design discussed during the lectures and tutorials: 1. Statement of the Problem Requirements Analysis Determine aims of program, input and output requirements, and specifications 2. Initial Design Overall program operation and sequence using flowcharts, block diagrams, also develop initial test plan 3. Detailed Design Write pseudocode (e.g. structured English) based on the flowcharts or block diagrams you designed above. 4. Coding Write the program 5. Test and Debug Thoroughly test the program, debug to locate and correct faults 6. Refinement Ensure program meets customer specifications EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 10 Part A ‐ Summary of Code Expressions: Week 5 laboratory tasks would have most likely exposed you to the following code expressions/statements: Expression Brief description if ( condition ) { // execute these statement only // if condition is true } if statements provided the ability to control the flow of your program. If the expression in the brackets is true or NOT zero, then the expressions within the block of code (code inside the {} braces) will be executed. [n] The [] operators allow you to access the contents of an array via an index value, n. Eg. argv[1] will access the first character of first argument passed to the program via the command line argument. argv[0] will be the name of your program. _popen() Open a communication pipe to another process. Must be called before the pipe is accessed. _pclose() Close an existing communication pipe that was previously opened via the _popen() command. == Is the equality operator, which is used in condition expressions such as if statements or for loops. ++ Increment operator. i++ is equivalent to i = i + 1; for ( init; condition; increment ) { // statement(s) to execute if // condition is true. } A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Once the condition expression resolves as NOT true the loop will cease to execute. strcpy_s(char * destination, char * source); Copy a null‐terminated string from ‘source’ to ‘destination’. _itoa_s(int value, char * str, int base) C function used to convert an integer in value into a null‐terminated string which is stored in str. The value of base determines that numerical base to interpret the integer as. Note that base 10 is considered to be decimal. int atoi(char * str); C function used to parses a C string str, interpreting its content as a whole number and returns its value as an integer. EEET2246 – Engineering Computing 1 – Week 5 and 6 Laboratory Tasks 14/08/20 11 Part B ‐ Summary of Code Expressions: Week 6 laboratory tasks would have most likely exposed you to the following code expressions/statements: Expression Brief description if ( condition ) { // execute these statement only // if condition is true } if statements provided the ability to control the flow of your program. If the expression in the brackets is true or NOT zero, then the expressions within the block of code (code inside the {} braces) will be executed. [n] The [] operators allow you to access the contents of an array via an index value, n. Eg. argv[1] will access the first character of first argument passed to the program via the command line argument. argv[0] will be the name of your program. _popen() Open a communication pipe to another process. Must be called before the pipe is accessed. _pclose() Close an existing communication pipe that was previously opened via the _popen() command. == Is the equality operator, which is used in condition expressions such as if statements or for loops. ++ Increment operator. i++ is equivalent to i = i + 1; for ( init; condition; increment ) { // statement(s) to execute if // condition is true. } A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Once the condition expression resolves as NOT true the loop will cease to execute. strcpy_s(char * destination, char * source); Copy a null‐terminated string from ‘source’ to ‘destination’. _itoa_s(int value, char * str, int base) C function used to convert an integer in value into a null‐terminated string which is stored in str. The value of base determines that numerical base to interpret the integer as. Note that base 10 is considered to be decimal. int atoi(char * str); C function used to parses a C string str, interpreting its content as a whole number and returns its value as an integer. int isdigit ( int c ); C function to determine if the value of c is a digit (0 – 9). Returns a non‐zero value if true. char * strcat_s ( char * destination, const char * source ); C function to join the null‐terminated string in destination to that of source. The resultant string is stored in destination. int strcmp ( const char * str1, const char * str2 ); C function to compare two null‐terminated strings str1 to str2 and returns zero if they are equal