User Tools

Site Tools


lab_7

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Previous revision
lab_7 [2018/06/08 09:33]
lab_7 [2019/04/10 14:15] (current)
hutch [Videos]
Line 41: Line 41:
 **Note: Much of the functionality of the various functions is described in the commented code in the provided files. Carefully read these comments as it will save you a lot of time.** **Note: Much of the functionality of the various functions is described in the commented code in the provided files. Carefully read these comments as it will save you a lot of time.**
  
----- 
 ---- ----
  
 ===== Overall Implementation Strategy ===== ===== Overall Implementation Strategy =====
  
-This following sections discuss how the wamDisplay and wamControl code are organized. +This following sections discuss how the wamDisplay and wamControl code are organized. ​Links to source code are also provided.
- +
-==== wamDisplay: Implementation Strategy for Mole Info Records ====+
  
-Moles are represented using a struct that is shown below. Carefully read the comments associated ​with the struct to understand how the struct is used. The ticksUntilAwake/​ticksUntilDormant will be decremented at regular rate by the controlling state machine. +  - [[Representing ​Moles with a Data Structure]] 
-<code C> +  - [[Using malloc() and free()]] 
-typedef struct { +  [[Using rand() ​to Create Random Behavior]] 
-        wamDisplay_point_t origin; ​ // This is the origin of the hole for this mole. +  - [[Drawing ​and Erasing Moles]] 
-        // A mole is active if either of the tick counts are non-zero. The mole is dormant otherwise. +  - [[Implementing wamControl]] 
-        // During operation, non-zero tick counts are decremented at a regular rate by the control state machine. +  [[wamControl and wamMain]] 
-        // The mole remains in his hole until ticksUntilAwake decrements ​to zero and then he pops out. +  - [[Source Code]]
-        // The mole remains popped out of his hole until ticksUntilDormant decrements to zero. +
-        // Once ticksUntilDormant goes to zero, the mole hides in his hole and remains dormant until activated again. +
-        ​wamDisplay_moleTickCount_t ticksUntilAwake; ​ // Moles wake up (pop out of hole) when this goes from 1 -> 0. +
-        ​wamDisplay_moleTickCount_t ticksUntilDormant;​ // Moles go dormant (back in hole) this goes 1 -> 0. +
-} wamDisplay_moleInfo_t;​ +
-</​code>​+
  
-Each mole is represented by a single mole-info record that provides the mole's screen coordinates and awake/​dormant status (''​ticksUntilAwake'',​ ''​ticksUntilDormant''​). You will use a one-dimensional array of pointers to keep track of, and to access your mole-info records. You will need to resize this array whenever the user selects a different number of moles for game play. You will allocate/​deallocate memory using ''​malloc()/​free()''​ whenever you resize the array. 
 ---- ----
  
-==== Coding Example for malloc() and free() ==== 
  
-The mole-info records will be accessed via a one-dimensional array of pointers, where each pointer will point to a single mole-info record. The order that these mole-info records occur within the array is unimportant. It is only important that you can access each of the mole-info records. You will find the following code in the provided wamDisplay.c code: 
-<​code>​ 
-// This will contain pointers to all of the mole info records. 
-// This will ultimately be treated as an array of pointers. 
-static wamDisplay_moleInfo_t** wamDisplay_moleInfo;​ 
- 
-// Allocates the memory for wamDisplay_moleInfo_t records. 
-// Computes the origin for each mole assuming a simple row-column layout: 
-// 9 moles: 3 rows, 3 columns, 6 moles: 2 rows, 3 columns, 4 moles: 2 rows, 2 columns 
-// Also inits the tick counts for awake and dormant. 
-void wamDisplay_computeMoleInfo() { 
-    // Setup all of the moles, creates and inits mole info records. 
-    // Create the container array. It contains pointers to each of the mole-hole info records. 
-    //    wamDisplay_moleInfo =  // Need to uncomment this line and initialize this variable via malloc(). 
-} 
-</​code>​ 
-The array of pointers will be stored at ''​wamDisplay_moleInfo''​ (see above). To illustrate how this is done, here is a simple example that performs tasks similar to those that you will need to perform. You can compile this program if you like. Also, though the steps that assign and print-out the values of the structs is not described here it is provided so you can see how to access the variables in the struct. 
- 
-<​code>​ 
-#include <​stdint.h>​ 
-#include <​stdlib.h>​ 
-#include <​stdio.h>​ 
- 
-typedef struct {uint16_t x; uint16_t y;} myStruct; ​ // Simple struct as an example. 
- 
-// Step 1: Define the variable that will contain the array of pointers. 
-static myStruct** arrayOfPointers; ​ // This is just a pointer, it doesn'​t point to anything yet. 
- 
-#define NUMBER_OF_POINTERS 4 
- 
-int main() { 
-  
-    // Step 2: allocate enough memory to hold NUMBER_OF_POINTERS. I need enough storage for 4 
-    // pointers. sizeof(myStruct*) returns the number of bytes for a single pointer to a myStruct. ​ 
-    // Multiply that value by 4 to get enough memory for 4 pointers. ​ 
-    // malloc() returns a (void *) so you must cast it to match the type of arrayOfPointers. 
-    // Rule of thumb: the number of stars (*) must match on both sides of the equation. 
-    // arrayOfPointers is pointer-to-pointer (myStruct**) so you need two stars (*) for the cast. 
-    arrayOfPointers = (myStruct**) malloc(NUMBER_OF_POINTERS * sizeof(myStruct*));​ 
- 
-    // Step 3: Allocate memory for each myStruct. 
-    // sizeof(myStruct) returns the number of bytes required by a single myStruct. 
-    // The for-loop iterates 4 times, during each iteration, it grabs enough memory 
-    // for a single myStruct and stores it address (the pointer value) in a location in arrayOfPointers. 
-    for (uint16_t i=0; i<​NUMBER_OF_POINTERS;​ i++) { 
-        // Second, allocate an instance of myStruct and point to it. 
-        arrayOfPointers[i] = (myStruct*) malloc(sizeof(myStruct));​ 
-    } 
-    // Step 4: assign values to these structs. 
-    for (uint16_t j=0; j<​NUMBER_OF_POINTERS;​ j++) { 
-        arrayOfPointers[j]->​x = j;  // Just need a number. 
-        arrayOfPointers[j]->​y = NUMBER_OF_POINTERS - j;  // Just need a number. 
-    } 
-    // Verify that the assignments worked. 
-    for (uint16_t k=0; k<​NUMBER_OF_POINTERS;​ k++) { 
-        printf("​Struct contents x:%d, y:​%d\n\r",​ arrayOfPointers[k]->​x,​ arrayOfPointers[k]->​y);​ 
-    } 
-    // When you are done, you must return the memory to the system or you will create a memory leak. 
-    // First deallocate all of the structs. 
-    for (uint16_t l=0; l<​NUMBER_OF_POINTERS;​ l++) { 
-        free(arrayOfPointers[l]); ​  // This deallocates the memory. ​ 
-        arrayOfPointers[l] = NULL;  // This step is not necessary but will keep you from reusing deallocated memory. 
-    } 
-    // Next, deallocate the array that contains the pointers to the structs. 
-    free(arrayOfPointers); ​  // Deallocates the container of arrays. 
-    arrayOfPointers = NULL;  // Also keeps you from reusing the deallocated memory. 
-} 
-</​code>​ 
- 
-Let's work through the three labeled steps of the above example. See the figure below for additional explanation. 
- 
-Step 1: Define the pointer to the array of pointers. This pointer doesn'​t yet point to anything. 
- 
-Step 2: Allocate sufficient memory to contain 4 pointers, where each pointer will eventually point to a struct. ''​arrayOfPointers''​ now points to something; however, the memory it contains does not point to anything. 
- 
-Step 3: Allocate 4 instances of ''​myStruct''​ and point to them using the ''​arrayOfPointers''​. 
- 
-{{::​pointerexample.jpg?​700|}} 
- 
-The code example also demonstrates how to deallocate memory when you are done. This is an important step to make sure that the memory footprint of your program doesn'​t grow indiscriminately over time. 
- 
----- 
- 
-==== wamDisplay: Using ''​rand()''​ and ''​srand() to Create Random Behavior ==== 
-Whack a Mole exhibits random behavior in at least three different ways: 
-  - moles are randomly activated, 
-  - moles randomly wake up, and 
-  - moles randomly go back to sleep (become dormant). 
- 
-In your game, a mole is randomly selected and activated when the ''​wamControl''​ state machine invokes ''​wamDisplay_activateRandomMole()''​. This function randomly selects a random mole that is not currently active and sets its ''​ticksUntilAwake''​ and ''​ticksUntilDormant''​ variables to random values (more on that later). First of all, how do you randomly select a mole? 
- 
-==== Selecting a Mole with A Randomly-Generated Array Index ==== 
-As discussed above, all moles are represented by mole-info records and these are accessed by a one-dimensional array. Randomly selecting a currently dormant mole and activating it can be performed using the following steps: 
-  * Step 1: generate a random index that is in the proper range, e.g, if you have a total of 9 moles, the index must be between 0 and 8. 
-  * Step 2: access the mole-info record from the array. If the mole is currently dormant, activate it, if not, go back to Step 1.  
- 
-It is a good idea to limit this to some number of attempts in case you have a bug of some sort. Otherwise, you may get stuck in an infinite loop though this should only happen in the presence of a bug. 
- 
-Generating a random index that is in range is straightforward if you know how to use the ''​%''​ (modulo) operator. As you should recall, the modulo operator performs integer division and returns the remainder. Thus the following code will give you a random number between 0 and 4: 
-<​code>​ 
-rand() % 5 
-</​code>​ 
- 
----- 
- 
-==== Generating Random Time Intervals ==== 
-In addition to activating random moles, you need to also set mole's ''​ticksUntilWake''​ and ''​ticksUntilDormant''​ to reasonable random values. By reasonable, I mean that the time intervals have to be such that they provide good game play. It wouldn'​t make much sense to set the ''​ticksUntilAwake''​ variable so that it takes an hour for the mole to wake up and pop up out of its hole. You probably don't want the value to be 0 either. Something between 0.5 and 1.0 seconds might be a good place to start, for example. In any case, you can simply use ''​rand()''​ again for this purpose. The code below will generate a tick-count that will consume between 0.5 and 1.5 seconds, assuming that ticks occur every 50 ms (the controller updates mole tick-counts via ''​wamDisplay_updateAllMoleTickCounts()''​). 
-<​code>​ 
-(rand() % 21) + 10 
-</​code>​ 
- 
-If the result of ''​rand() % 21''​ is 0, 0 + 10 = 10, 10 ticks at 50ms/tick = 0.5 seconds. That represents the lower bound. If the result of ''​rand() % 21''​ is 20, 20+10 = 30, 30 ticks at 50ms/tick = 1.5 seconds. This is the upper bound. 
- 
----- 
- 
-==== Setting Mole Asleep/​Awake Intervals in Seconds ==== 
-You use ''​wamControl_setMsPerTick()''​ to tell the controller how many ms will transpire between each tick. The ''​wamDisplay''​ code then uses ''​wamControl_getRandomMoleAsleepInterval()''​ and ''​wamControl_getRandomMoleAwakeInterval()''​ to compute a tick count for each of the ''​ticksUntilAwake''​ and ''​ticksUntilDormant''​ variables for each mole when it is activated. Doing things this way makes the time intervals independent of the tick rate and makes it much easier to determine good values for the amount of time the mole spends asleep before awaking (''​ticksUntilAwake''​) and the time the mole spends awake before going dormant (''​ticksUntilDormant''​). 
- 
----- 
- 
-==== Drawing and Erasing Moles ==== 
-There are no functions for drawing and erasing moles in ''​wamDisplay.h''​ as this functionality is only necessary within ''​wamDisplay.c''​. The following two functions, implemented in ''​wamDisplay.c''​ draw and erase moles as necessary: 
-  * ''​wamDisplay_updateAllMoleTickCounts()''​ 
-    * Once the mole tick-counts have been updated, this function will either draw the mole (the mole has awakened) or erase the mole (the mole has become inactive). 
-  * ''​wamDisplay_whackMole(wamDisplay_point_t* whackOrigin)''​ 
-    * This function will erase an active mole if the coordinates of the "​whack"​ match the coordinates of an active mole. 
- 
----- 
-==== wamControl: What Does the Controller Do? ==== 
-The controller is relatively simple for this lab. It does three things: 
-  - waits for the user to touch the screen, 
-  - waits for the touch analog-to-digital converters (ADC) to settle, and 
-  - sends the coordinates of the touch to ''​wamDisplay''​ by calling the ''​wamDisplay_whackMole()'',​ passing the origin of the touch as the argument to the function. 
- 
-In addition, while waiting for the user to touch or while waiting for the touch ADC to settle, it calls ''​wamDisplay_updateAllMoleTickCounts()''​ to update the mole tick counts, as well as activating moles whenever the number of active moles falls below the minimum allowed. That's about it. 
- 
-As you may have noted, the controller has no notion of how many moles are active, how many moles may be visible, or where the moles are located on the screen. It's primary job is to pass the coordinates of touches to ''​wamDisplay''​ routines to be processed. This makes it much easier to develop the display code so it can be very flexible. As the controller knows nothing about the positions of the moles, for example, the controller does not have to be changed when the mole count is changed from 9 to 6, for example. In either case the controller simply sends touch coordinates to the display software. The display software attempts to "​whack"​ the mole with the given coordinates. If the coordinates are over a mole that is awake, the display software erases the mole and increments the hit count in the score screen. Otherwise, the display software increments the miss value in the score screen. 
- 
-==== wamControl: Handling Initialization ==== 
- 
-The function ''​wamControl_init()''​ initializes the state machine as if it was starting for the first time. Use this to initialize the controller state machine before starting the game and when restarting the game when the game is interrupted by pressing one of the push-buttons. There are also a number of other functions associated with the controller state machine as can be seen in ''​wan_control.h''​ file (see below). 
- 
----- 
- 
-==== wamMain ==== 
- 
-''​wamMain.c''​ is provided in its entirety to you. With one exception, do not modify it, you must use it as is during pass off. It should also provide some insight into how the various functions will be used. The one exception is that you may change the ''#​define''​ values as you see fit, as long as you still meet all of the requirements of the game as described in the Requirements Section. 
- 
----- 
  
 ===== Requirements ===== ===== Requirements =====
Line 256: Line 96:
  
 ===== Videos ===== ===== Videos =====
-  * [[https://​www.youtube.com/​watch?​v=E4fJg1MWaHI|Video demonstration of Milestone 1]] 
-  * [[https://​www.youtube.com/​watch?​v=akiCjLoxr3Q|Video Demonstration of Complete Game]] 
  
-----+==== Video Demonstration of Milestone 1 ==== 
 +{{youtube>​E4fJg1MWaHI?​rel=0&​allowfullscreen&​noborder&​560x315}}
  
 +==== Video Demonstration of Complete Game ====
 +{{youtube>​akiCjLoxr3Q?​rel=0&​allowfullscreen&​noborder&​560x315}}
 +----
 ===== Other Resources ===== ===== Other Resources =====
 The web has quite a bit of information on how to use ''​rand()''​ and pointers, including ''​malloc()''​ and ''​free()''​. Here are a few curated links. The web has quite a bit of information on how to use ''​rand()''​ and pointers, including ''​malloc()''​ and ''​free()''​. Here are a few curated links.
Line 269: Line 111:
  
 ---- ----
-===== Source Code ===== 
-<code C wamControl.h>​ 
-#ifndef WAMCONTROL_H_ 
-#define WAMCONTROL_H_ 
- 
-#include "​wamDisplay.h"​ 
-#include <​stdint.h>​ 
- 
-// Call this before using any wamControl_ functions. 
-void wamControl_init();​ 
- 
-// Call this to set how much time is consumed by each tick of the controlling state machine. 
-// This information makes it possible to set the awake and sleep time of moles in ms, not ticks. 
-void wamControl_setMsPerTick(uint16_t msPerTick); 
- 
-// This returns the time consumed by each tick of the controlling state machine. 
-uint16_t wamControl_getMsPerTick();​ 
- 
-// Standard tick function. 
-void wamControl_tick();​ 
- 
-// Returns a random value that indicates how long the mole should sleep before awaking. 
-wamDisplay_moleTickCount_t wamControl_getRandomMoleAsleepInterval();​ 
- 
-// Returns a random value that indicates how long the mole should stay awake before going dormant. 
-wamDisplay_moleTickCount_t wamControl_getRandomMoleAwakeInterval();​ 
- 
-// Set the maximum number of active moles. 
-void wamControl_setMaxActiveMoles(uint16_t count); 
- 
-// Get the current allowable count of active moles. 
-uint16_t wamControl_getMaxActiveMoles();​ 
- 
-// Set the seed for the random-number generator. 
-void wamControl_setRandomSeed(uint32_t seed); 
- 
-// Set the maximum number of misses until the game is over. 
-void wamControl_setMaxMissCount(uint16_t missCount); 
- 
-// Use this predicate to see if the game is finished. 
-bool wamControl_isGameOver();​ 
- 
-#endif /* WAMCONTROL_H_ */ 
-</​code>​ 
- 
-<code C wamDisplay.h>​ 
-#ifndef WAMDISPLAY_H_ 
-#define WAMDISPLAY_H_ 
- 
-#include <​stdint.h>​ 
-#include <​stdbool.h>​ 
- 
-// The index used to tell the display which mole is active/​inactive. 
-typedef int16_t wamDisplay_moleIndex_t;​ 
-// Mole coordinates represented by this. 
-typedef int16_t wamDisplay_coord_t;​ 
-// Mole origins, whack coordinates,​ etc. are represented by this. 
-typedef struct {wamDisplay_coord_t x, y;} wamDisplay_point_t;​ 
-// Represents a mole clock interval (asleep or awake). 
-typedef uint32_t wamDisplay_moleTickCount_t;​ 
- 
-// Make it possible to select games with different numbers of moles. 
-// The display will determine the actual layout. Moles are always indexed by a single integer. 
-typedef enum {wamDisplay_moleCount_9,​ wamDisplay_moleCount_6,​ wamDisplay_moleCount_4} wamDisplay_moleCount_e;​ 
- 
-// Provide support to set games with varying numbers of moles. This function 
-// would be called prior to calling wamDisplay_init();​ 
-void wamDisplay_selectMoleCount(wamDisplay_moleCount_e moleCount); 
- 
-// Call this before using any wamDisplay_ functions. 
-void wamDisplay_init();​ 
- 
-// Draw the game display with a background and mole holes. 
-void wamDisplay_drawMoleBoard();​ 
- 
-// Draw the initial splash (instruction) screen. 
-void wamDisplay_drawSplashScreen();​ 
- 
-// Draw the game-over screen. 
-void wamDisplay_drawGameOverScreen();​ 
- 
-// Selects a random mole and activates it. 
-// Activating a mole means that the ticksUntilAwake and ticksUntilDormant counts are initialized. 
-// See the comments for wamDisplay_moleInfo_t for details. 
-// Returns true if a mole was successfully activated. False otherwise. You can 
-// use the return value for error checking as this function should always be successful 
-// unless you have a bug somewhere. 
-bool wamDisplay_activateRandomMole();​ 
- 
-// This takes the provided coordinates and attempts to whack a mole. If a 
-// mole is successfully whacked, all internal data structures are updated and 
-// the display and score is updated. You can only whack a mole if the mole is awake (visible). 
-// The return value can be used during testing (you could just print which mole is 
-// whacked without having to implement the entire game). 
-wamDisplay_moleIndex_t wamDisplay_whackMole(wamDisplay_point_t* whackOrigin);​ 
- 
-// This updates the ticksUntilAwake/​ticksUntilDormant clocks for all of the moles. 
-void wamDisplay_updateAllMoleTickCounts();​ 
- 
-// Returns the count of currently active moles. 
-// A mole is active if it is not dormant, if: 
-// ticksUntilAwake or ticksUntilDormant are non-zero (in the moleInfo_t struct). 
-uint16_t wamDisplay_getActiveMoleCount();​ 
- 
-// Sets the hit value in the score window. 
-void wamDisplay_setHitScore(uint16_t hits); 
- 
-// Gets the current hit value. 
-uint16_t wamDisplay_getHitScore();​ 
- 
-// Sets the miss value in the score window. 
-void wamDisplay_setMissScore(uint16_t misses); 
- 
-// Gets the miss value. 
-// Can be used for testing and other functions. 
-uint16_t wamDisplay_getMissScore();​ 
- 
-// Sets the level value on the score board. 
-void wamDisplay_incrementLevel();​ 
- 
-// Retrieves the current level value. 
-// Can be used for testing and other functions. 
-uint16_t wamDisplay_getLevel();​ 
- 
-// Completely draws the score screen. 
-// This function renders all fields, including the text fields for "​Hits"​ and "​Misses"​. 
-// Usually only called once when you are initializing the game. 
-void wamDisplay_drawScoreScreen();​ 
- 
-// Make this function available for testing purposes. 
-void wamDisplay_incrementMissScore();​ 
- 
-// Reset the scores and level to restart the game. 
-void wamDisplay_resetAllScoresAndLevel();​ 
- 
-// Test function that can be called from main() to demonstrate milestone 1. 
-// Invoking this function should provide the same behavior as shown in the Milestone 1 video. 
-void wamDisplay_runMilestone1_test();​ 
- 
-#endif /* WAMDISPLAY_H_ */ 
-</​code>​ 
- 
-<code C wamDisplay.c>​ 
-#include "​supportFiles/​display.h"​ 
-#include "​wamDisplay.h"​ 
-#include "​wamControl.h"​ 
-#include <​stdio.h>​ 
-#include <​stdlib.h>​ 
-#include <​string.h>​ 
- 
-/​********************** typedefs **********************/​ 
-// This keeps track of all mole information. 
-typedef struct { 
-        wamDisplay_point_t origin; ​ // This is the origin of the hole for this mole. 
-        // A mole is active if either of the tick counts are non-zero. The mole is dormant otherwise. 
-        // During operation, non-zero tick counts are decremented at a regular rate by the control state machine. 
-        // The mole remains in his hole until ticksUntilAwake decrements to zero and then he pops out. 
-        // The mole remains popped out of his hole until ticksUntilDormant decrements to zero. 
-        // Once ticksUntilDomant goes to zero, the mole hides in his hole and remains dormant until activated again. 
-        wamDisplay_moleTickCount_t ticksUntilAwake; ​ // Mole will wake up (pop out of hole) when this goes from 1 -> 0. 
-        wamDisplay_moleTickCount_t ticksUntilDormant;​ // Mole will go dormant (back in hole) this goes 1 -> 0. 
-} wamDisplay_moleInfo_t;​ 
- 
-// This will contain pointers to all of the mole info records. 
-// This will ultimately be treated as an array of pointers. 
-static wamDisplay_moleInfo_t** wamDisplay_moleInfo;​ 
- 
-// Allocates the memory for wamDisplay_moleInfo_t records. 
-// Computes the origin for each mole assuming a simple row-column layout: 
-// 9 moles: 3 rows, 3 columns, 6 moles: 2 rows, 3 columns, 4 moles: 2 rows, 2 columns 
-// Also inits the tick counts for awake and dormant. 
-void wamDisplay_computeMoleInfo() { 
-    // Setup all of the moles, creates and inits mole info records. 
-    // Create the container array. It contains pointers to each of the mole-hole info records. 
-    //    wamDisplay_moleInfo =  // Need to uncomment this line and initialize this variable via malloc(). 
-} 
-</​code>​ 
- 
-<code C wamMain.c>​ 
-#include "​wamDisplay.h"​ 
-#include "​wamControl.h"​ 
-#include "​supportFiles/​utils.h"​ 
-#include "​supportFiles/​display.h"​ 
-#include "​supportFiles/​intervalTimer.h" ​ // Modify as necessary to point to your intervalTimer.h 
-#include "​supportFiles/​leds.h"​ 
-#include "​supportFiles/​interrupts.h"​ 
-#include "​supportFiles/​switches.h" ​ // Modify as necessary to point to your switches.h 
-#include "​supportFiles/​buttons.h" ​  // Modify as necessary to point to your buttons.h 
-#include <​stdio.h>​ 
-#include <​xparameters.h>​ 
- 
-// The formula for computing the load value is based upon the formula from 4.1.1 (calculating timer intervals) 
-// in the Cortex-A9 MPCore Technical Reference Manual 4-2. 
-// Assuming that the prescaler = 0, the formula for computing the load value based upon the desired period is: 
-// load-value = (period * timer-clock) - 1 
-#define TIMER_PERIOD 50.0E-3 ​   // 50 ms. 
-#define TIMER_CLOCK_FREQUENCY (XPAR_CPU_CORTEXA9_0_CPU_CLK_FREQ_HZ / 2) 
-#define TIMER_LOAD_VALUE ((TIMER_PERIOD * TIMER_CLOCK_FREQUENCY) - 1.0) 
- 
-#define MAX_ACTIVE_MOLES 1  // Start out with this many moles. 
-#define MAX_MISSES 50       // Game is over when there are this many misses. 
-#define FOREVER 1           // Syntactic sugar for while (1) statements. 
-#define MS_PER_TICK (TIMER_PERIOD * 1000)   // Just multiply the timer period by 1000 to get ms. 
- 
-#define SWITCH_VALUE_9 9  // Binary 9 on the switches indicates 9 moles. 
-#define SWITCH_VALUE_6 6  // Binary 6 on the switches indicates 6 moles. 
-#define SWITCH_VALUE_4 4  // Binary 9 on the switches indicates 4 moles. 
-#define SWITCH_MASK 0xf   // Ignore potentially extraneous bits. 
- 
-// Mole count is selected by setting the slide switches. The binary value for the switches 
-// determines the mole count (1001 - nine moles, 0110 - 6 moles, 0100 - 4 moles). 
-// All other switch values should default to 9 moles). 
-void wamMain_selectMoleCountFromSwitches(uint16_t switchValue) { 
-    switch(switchValue & SWITCH_MASK) { 
-    case SWITCH_VALUE_9: ​   // this switch pattern = 9 moles. 
-        wamDisplay_selectMoleCount(wamDisplay_moleCount_9);​ 
-        break; 
-    case SWITCH_VALUE_6: ​   // this switch pattern = 6 moles. 
-        wamDisplay_selectMoleCount(wamDisplay_moleCount_6);​ 
-        break; 
-    case SWITCH_VALUE_4: ​  // this switch pattern = 4 moles. 
-        wamDisplay_selectMoleCount(wamDisplay_moleCount_4);​ 
-        break; 
-    default: ​   // Any other pattern of switches = 9 moles. 
-        wamDisplay_selectMoleCount(wamDisplay_moleCount_9);​ 
-        break; 
-    } 
-} 
- 
-// Top-level control loop is implemented with while-loops in main(). 
-int main() { 
-    /​************************* System Initialization Code ***********************/​ 
-    switches_init(); ​ // Init the slide switches. 
-    buttons_init(); ​  // Init the push buttons. 
-    leds_init(true); ​ // You need to init the LEDs so that LD4 can function as a heartbeat. 
-    // Init all interrupts (but does not enable the interrupts at the devices). 
-    // Prints an error message if an internal failure occurs because the argument = true. 
-    interrupts_initAll(true); ​  // Init the interrupt code. 
-    interrupts_setPrivateTimerLoadValue(TIMER_LOAD_VALUE); ​ // Set the timer period. 
-    interrupts_enableTimerGlobalInts(); ​ // Enable interrupts at the timer. 
-    interrupts_startArmPrivateTimer(); ​  // Start the private ARM timer running. 
-    uint32_t personalInterruptCount = 0; // Used to compare with internal count from interrupt code. 
-    /​******************** Game-Specific Code ********************/​ 
-    uint32_t randomSeed; ​   // Used to make the game seem more random. 
-    display_init(); ​        // Init the display (make sure to do it only once). 
-    wamControl_setMaxActiveMoles(MAX_ACTIVE_MOLES);​ // Start out with this many simultaneous active moles. 
-    wamControl_setMaxMissCount(MAX_MISSES); ​        // Allow this many misses before ending game. 
-    wamControl_setMsPerTick(MS_PER_TICK); ​          // Let the controller know how ms per tick.. 
-    wamDisplay_drawSplashScreen(); ​ // Draw the game splash screen. 
-    while (FOREVER) {               // Endless loop. 
-        wamMain_selectMoleCountFromSwitches(switches_read()); ​ // Mole count selected via slide switches. 
-        wamDisplay_init(); ​             // Initialize the WAM display. 
-        wamControl_init(); ​             // Initialize the WAM controller. 
-        while (!display_isTouched()) {  // Wait for the user to touch the screen. 
-            randomSeed++; ​              // Increment a random seed while you wait. 
-        } 
-        while (display_isTouched()); ​           // Now wait for the user to remove their finger. 
-        wamControl_setRandomSeed(randomSeed); ​  // Set the random-seed. 
-        wamDisplay_drawMoleBoard(); ​            // Draw the WAM mole board. 
-        interrupts_enableArmInts(); ​            // Enable interrupts at the ARM. 
-        while (!wamControl_isGameOver() && !buttons_read()) {// Game runs until over or interrupted. 
-            if (interrupts_isrFlagGlobal) {     // If an interrupt occurs, time to call tick. 
-                interrupts_isrFlagGlobal = 0;   // Reset the interrupt flag. 
-                personalInterruptCount++; ​      // Count interrupts. 
-                wamControl_tick(); ​             // tick the WAM controller. 
-            } 
-        } 
-        interrupts_disableArmInts(); ​           // Game is over, turn off interrupts. 
-        // Print out the interrupt counts to ensure that you didn't miss any interrupts. 
-        printf("​isr invocation count: %ld\n\r",​ interrupts_isrInvocationCount());​ 
-        printf("​internal interrupt count: %ld\n\r",​ personalInterruptCount);​ 
-        wamDisplay_drawGameOverScreen(); ​       // Draw the game-over screen. 
-        while (!display_isTouched()); ​          // Wait here until the user touches the screen to try again. 
-        wamDisplay_resetAllScoresAndLevel(); ​   // Reset all game statistics so you can start over. 
-    } 
-} 
- 
-void isr_function() { 
-    // Empty for now. 
-} 
-</​code>​ 
lab_7.1528472024.txt.gz · Last modified: 2019/04/08 20:12 (external edit)