Lab Exercise 8:
The purpose of this lab time is to build on the classes we created last week to better understand how we can extend and modify classes. We'll also get more practice with saving and retrieving information from arrays.
Tasks
Board
The lab and project will use the same configuration of the board as the last 2 weeks, though you are welcome to add more LEDs.
Code
- For the last project, you created an LEDBank class that made it easy to manipulate a bank of LEDs. This week we're going to extend the LEDBank and ACSensor classes to give them new capabilities.
Make a new code file and save it as ledtest. You can copy and paste from this test code.
- Create a second tab and save it as LEDBank_p8.h. Copy your LEDBank_p7.h code into the file. Make a third tab and save it as LEDBank_p8.cpp, then copy your LEDBank_p7.cpp code into the file.
- The goal of the lab exercise is to enable your LEDBank class to record commands and play them back, with the option of playing them back at a different speed. We'll go through the design steps as a group, then go ahead and implement the changes to the class and the creation of the new member functions listed below.
- Modifications to the class definition (LEDBank_p8.h). These modifications will give our class the variables necessary to specify when to record, to record the actions, and to keep track of how many actions are in the recording.
- Use #define to create the constant LED_MAX_ACTIONS with the value 32.
- Create a new int field that specifies whether recording is active or not (e.g. rec_active).
- Create a new int field that specifies how many steps there are in the recording (e.g. rec_num). This is also the index of the next open space in the arrays to hold the action information.
- Create a new int array field of size LED_MAX_ACTIONS that holds the configuration of LEDs for each step (e.g. rec_state).
- Create a new int array field of size LED_MAX_ACTIONS that holds the time to delay for each step (e.g. rec_time).
- In your LEDBank constructor functions, make sure you initialize the rec_num and rec_active fields to 0.
- Modifications to the class member functions (LEDBank_p8.cpp). Create the following new member functions. Remember to put the prototype for each of these functions into the class definition.
void LEDBank::record(void)
- this function should enable recording by setting the recording flag (i.e. rec_active) to a non-zero value (i.e. 1). Note, you can put simple functions like this in the class definition itself in the .h file. These first three functions should be public.void LEDBank::stop(void)
- this function should disable recording by setting the recording flag (i.e. rec_active) to 0.void LEDBank::erase(void)
- this function should erase the current recording by setting the number of recorded steps (i.e. rec_num) to zero.void LEDBank::rec_add( int state, int time )
- this function should add the action specified by state and time to the next open space on the rec_state and rec_time arrays. The index of the next open space is given by the number of steps currently in the array (i.e. rec_num). The function should first check if rec_num is less than LED_MAX_ACTIONS. It should then check if recording is turned on (i.e. rec_active). If both conditions are true, then it should assign to the next open space in rec_state the value state, and it should assign to the next open space in rec_time the value time. Finally, it should increment rec_time since an item has been added to the recording.Make this function be private, as it is a helper function only used within the class.
void LEDBank::play( void )
- this function should loop over the number of steps in the recording (i.e. rec_num) and play back the given LED states. Inside the loop it should call setAll with the ith state and then call delay with the ith time.This and the remaining functions should all be made public.
void LEDBank::play( float speed )
- this function should also play back the recorded states but do so at the specified speed (e.g. if speed is 2.0 then it should playback twice as fast). Before doing anything, it should check if speed is less than or equal to zero, in which case it should just return without doing anything. Otherwise, it should loop over the states, calling setAll and delay, but it should divide the time to delay by the speed.void LEDBank::get(int index int *state, int *time)
- this function should assign to state and time the state and time values from the recording at position index in the array. Remember to de-reference state and time using the * operator when assigning something to them.void LEDBank::edit(int index, int state, int time)
- this function should assign the given state and time to the position in the recording arrays specified by index. It should first test if index is valid (i.e. greater than or equal to 0 and less than rec_num.
Go through each of your action functions that takes in a time argument (e.g. on, off, set, setAll, shift_left, shift_right, and invert) and, after the delay, add a call to
this->rec_add()
passing the current status/state and the time.This should complete the changes to your library. Try compiling it and fix any errors that occur.
- Return to the ledtest file and try compiling and running it. Does it do the right thing? The test file does not use the get method or the erase method. Add a call to the get method and either print out the resulting state and time values or otherwise use them in a way that makes it clear the function is working properly. Try adding an erase call and then record a couple of different actions and play them to make sure the erase function worked properly.
Come up with your own sequence of commands and demonstrate that the recording and playback process works correctly.
Take a short video of your LEDs working and submit it as part of the project report/handin.
When you are done with the lab exercises, you may start on the rest of the project.