User Tools

Site Tools


lab_6

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
lab_6 [2019/04/10 11:39]
hutch
lab_6 [2019/04/10 11:41] (current)
hutch
Line 108: Line 108:
  
  
- 
-  
- 
-==== Helps: Generating Random Sequences ==== 
-If you include "​stdlib.h"​ you have access to two functions that help to generate random sequences. The first function ''​srand(seed)''​ allows you to provide a "​seed"​ value for the random number generator. Each time you call ''​rand()'',​ it will return an integer that is part of a pseudorandom sequence. However, each time you start your program from scratch, ''​rand()''​ will return the same sequence of integers. If, each time you run the program, you start out by invoking ''​srand(seed)''​ with a different seed value, the sequence will be different each time you run the program. So, how do you generate random seed values? In my Simon game I am incrementing a counter while waiting for the user to touch the screen. Once the user touches the screen, I take the counter value and use that as the seed value for ''​srand()''​. 
- 
-Review this [[http://​www.cplusplus.com/​reference/​cstdlib/​srand/​|link]] for a more detailed explanation of and another example of how to use ''​srand()''​ and ''​rand()''​. 
- 
- 
----- 
- 
-==== Helps: Enabling and disabling test code using ''#​ifdef''​ and ''#​endif''​ ==== 
-See the code below for an example of how to individually compile test code. This comes from my ''​simonMain.c''​ file. As currently configured, this code will run the ''​simonDisplay_runTest()''​. 
-<code C> 
-#define RUN_SIMON_DISPLAY_TEST 
- 
-#ifdef RUN_SIMON_DISPLAY_TEST 
-#define MAX_TOUCH_COUNT 10 
-int main() { 
-  simonDisplay_runTest(MAX_TOUCH_COUNT);​ 
-} 
-#endif 
- 
-//#define RUN_BUTTON_HANDLER_TEST 
-#ifdef RUN_BUTTON_HANDLER_TEST 
-#define MAX_TOUCH_COUNT 10 
-int main() { 
-  buttonHandler_runTest(MAX_TOUCH_COUNT);​ 
-} 
-#endif 
- 
-// #define RUN_FLASH_SEQUENCE_TEST 
-#ifdef RUN_FLASH_SEQUENCE_TEST 
-int main() { 
-  flashSequence_runTest();​ 
-} 
-#endif 
- 
-//#define RUN_VERIFY_SEQUENCE_TEST 
-#ifdef RUN_VERIFY_SEQUENCE_TEST 
-int main() { 
-  verifySequence_runTest();​ 
-} 
-#endif 
-</​code>​ 
- 
----- 
- 
-==== Helps: Using Function Pointers to Time the Duration of Individual Ticks ==== 
-I wrote a function called ''​tickTimer()''​ that made it relatively easy to measure how long various tick functions were taking. You may find it useful to debug timing issues. I'll provide the code here and you can modify it to suit you. Note that it uses a function-pointer so that you can pass in the pointer to a tick function so it can be invoked and timed. As currently written, it keeps track of which tick() function consumed the most time. I found it quite helpful while debugging my Simon game because I could see what state-machine was taking the most time at any point during the game. 
- 
-<code C> 
-typedef void (functionPointer_t)();​ 
-#define MAX_DURATION_TIMER_INDEX 2 
-static double maxDuration_g; ​                   // The max duration across all ticks. 
-static const char* maxDurationFunctionName_g; ​  // Name of the function with longest duration is here. 
-#define SIMON_CONTROL_TICK "​simonControl_tick()"​ 
-#define VERIFY_SEQUENCE_TICK "​verifySequence_tick()"​ 
-#define FLASH_SEQUENCE_TICK "​flashSequency_tick()"​ 
-#define BUTTON_HANDLER_TICK "​buttonHandler_tick()"​ 
- 
-// This assumes that the interval timer was initialized. 
-static void tickTimer(functionPointer_t* fp, 
-                      const char* functionName) { 
-    intervalTimer_reset(MAX_DURATION_TIMER_INDEX); ​ // Reset the timer. 
-    intervalTimer_start(MAX_DURATION_TIMER_INDEX); ​ // Start the timer. 
-    fp();                                           // Invoke the tick function passed in by argument. 
-    intervalTimer_stop(MAX_DURATION_TIMER_INDEX); ​  // Stop the timer. 
-    double duration; ​                               // Keep track of tick duration. 
-    duration = intervalTimer_getTotalDurationInSeconds(MAX_DURATION_TIMER_INDEX); ​ // Get the duration. 
-    if (maxDuration_g < duration) {                 // Keep the max duration. 
-        maxDuration_g = duration; 
-        maxDurationFunctionName_g = functionName; ​  // Keep track of which function had the max. duration. 
-    } 
-} 
-</​code>​ 
- 
-To use the code, just wrap ''​tickTimer() around your normal tick function as shown below. 
- 
-<code C> 
-  tickTimer(flashSequence_tick,​ FLASH_SEQUENCE_TICK); ​   // Tick a state machine. 
-  tickTimer(verifySequence_tick,​ VERIFY_SEQUENCE_TICK); ​ // Tick a state machine. 
-  tickTimer(buttonHandler_tick,​ BUTTON_HANDLER_TICK); ​   // Tick a state machine. 
-  tickTimer(simonControl_tick,​ SIMON_CONTROL_TICK); ​     // Tick a state machine. 
-  ... 
-</​code>​ 
lab_6.txt ยท Last modified: 2019/04/10 11:41 by hutch