User Tools

Site Tools


writing_the_globals_code

Differences

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

Link to this comparison view

writing_the_globals_code [2019/04/10 11:33] (current)
hutch created
Line 1: Line 1:
 +==== Writing the globals Code ====
 +
 +There a small amount of information that is shared between state machines. This global information is managed by code in ''​globals.h''​ and ''​globals.c''​. Rather than just directly reading and/or writing global variables directly, it is much better practice to create functional interfaces to these variables. The ''​globals''​ code provides functions to set the sequence of colors by copying a supplied array of region numbers. A function to get the sequence length and functions to get and set the sequence iteration length are also provided. The iteration length indicates how far along you are in the current sequence. You can see how these functions are used in the various ''​runTest()''​ functions that I have provided.
 +I provide ''​globals.h''​ and part of ''​globals.c''​. You will need to finish ''​globals.c''​.
 +<code C globals.h>​
 +#ifndef GLOBALS_H_
 +#define GLOBALS_H_
 +
 +#define GLOBALS_MAX_FLASH_SEQUENCE 1000                  // Make it big so you can use it for a splash screen.
 +
 +// This is the length of the complete sequence at maximum length.
 +// You must copy the contents of the sequence[] array into the global variable that you maintain.
 +// Do not just grab the pointer as this will fail.
 +void globals_setSequence(const uint8_t sequence[], uint16_t length);
 +
 +// This returns the value of the sequence at the index.
 +uint8_t globals_getSequenceValue(uint16_t index);
 +
 +// Retrieve the sequence length.
 +uint16_t globals_getSequenceLength();​
 +
 +// This is the length of the sequence that you are currently working on.
 +void globals_setSequenceIterationLength(uint16_t length);
 +
 +// This is the length of the sequence that you are currently working on, 
 +// not the maximum length but the interim length as
 +// the person playing the game works through the pattern one color at a time.
 +uint16_t globals_getSequenceIterationLength();​
 +
 +#endif /* GLOBALS_H_ */
 +
 +</​code>​
 +
 +<code C globals.c>​
 +#include "​globals.h"​
 +
 +// The length of the sequence.
 +// The static keyword means that globals_sequenceLength can only be accessed
 +// by functions contained in this file. 
 +static uint16_t globals_sequenceLength = 0;  // The length of the sequence.
 +
 +// This is the length of the sequence that you are currently working on, 
 +// not the maximum length but the interim length as
 +// the user works through the pattern one color at a time.
 +void globals_setSequenceIterationLength(uint16_t length) {globals_sequenceIterationLength = length;}
 +  ​
 +// You will need to implement the other functions.
 +
 +</​code>​
  
writing_the_globals_code.txt ยท Last modified: 2019/04/10 11:33 by hutch