Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DocTest by
Revision 3:b78d65300f24, committed 2015-05-10
- Comitter:
- 200784123
- Date:
- Sun May 10 20:07:18 2015 +0000
- Parent:
- 2:5446154a4028
- Child:
- 4:a257abc56858
- Commit message:
- Sorted a few notation issues
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat May 09 09:38:26 2015 +0000
+++ b/main.cpp Sun May 10 20:07:18 2015 +0000
@@ -10,7 +10,7 @@
///minimal amount of code in the main, see All() for more
All();
}
-
+///all of the function that would normally reside in the main
void All(){
calibrateJoystick(); ///get the centred values of the joystick
bright=5;
@@ -40,8 +40,8 @@
c=1;
}
-
-void powerDown(){///goes into deep power down if nothing happens for a minute
+///goes into deep power down if nothing happens for a set time
+void powerDown(){
power++;
if(power==55){
red1=0;
@@ -78,11 +78,11 @@
yel=0.1*bright;
buzzer=0.4;
}
- if(power>60){
- red1=0;
- red2=0.1*bright;
- red3=0;
- yel=0.1*bright;
+ if(power>=60){
+ red1=0.1*bright;
+ red2=0;
+ red3=0.1*bright;
+ yel=0;
buzzer=0.9;
DeepPowerDown();
}
@@ -90,7 +90,7 @@
///the start screen, shows up at the begining and when the player runs out of lives
-///links to the options menu, the highscores, the level select, and the game
+///links to the options menu, the highscores, the level select etc.
void start(){
power=0;
clear();
@@ -155,12 +155,14 @@
lcd.clearPixel(i,j);
clear();
}
+///function called to escape back to the main menu
void menus(){
power=0;
lcd.clearPixel(i,j);
clear();
timeout.attach_us(&toggle,10);
}
+///function called to toggle, clear, or set various flags
void toggle(){
power=0;
l=!l;
@@ -171,6 +173,7 @@
b=!b;
startFlag=1;
}
+///the options menu, links to the reset, brightness, and end game
void Options(){
power=0;
lcd.clearPixel(i,j);
@@ -245,25 +248,31 @@
lcd.clearPixel(i,j);
clear();
}
+///debouncing code for the light() function
void Light(){
timeout.attach_us(&light,100);
}
+///debouncing code for the dark() function
void Dark(){
timeout.attach_us(&dark,100);
}
+///function called to increase the LED and backlight birghtness variable
void light(){
bright++;
if(bright>10)
bright=10;
}
+///function called to decrease the LED and backlight brightness variable
void dark(){
bright--;
if(bright<1)
bright=1;
}
+///function called to set the LCD backlight brightness from the brigtness variable
void brightness(){
lcd.setBrightness(0.1*bright);
}
+///the level select menu, allows the player to choose between three levels by moveing the joystick left, down, or right
void Levels(){
power=0;
push.rise(&menus);
@@ -345,6 +354,8 @@
lcd.clearPixel(i,j);
clear();
}
+///the scores menu, displays the currently saved high score for the three levels
+/// moving the joystick different directions displays the scores for the different levels
void Score(){
power=0;
lcd.clearPixel(i,j);
@@ -423,11 +434,12 @@
fprintf(fp,"%i",score1);///prints the integer value for the score to file
fclose(fp);
}
+///reads the currently saved score for the first level
void scoreRead1(){
int Score1;///tells it that the value to be read is an integer value
FILE *fp1 = fopen("/local/score1.txt","r");///opens score1.txt
- fscanf(fp1,"%i",&Score1);///searches the file for an integer followed by a string
+ fscanf(fp1,"%i",&Score1);///searches the file for an integer
fclose(fp1);
char Points1[14];///bufer to store the points value
@@ -442,11 +454,12 @@
fprintf(fp2,"%i",score2);///prints the integer value for the score to file
fclose(fp2);
}
+///reads the currently saved score for the second level
void scoreRead2(){
int Score2;///tells it that the value to be read is an integer value
FILE *fp3 = fopen("/local/score2.txt","r");///opens score2.txt
- fscanf(fp3,"%i",&Score2);///searches the file for an integer followed by a string
+ fscanf(fp3,"%i",&Score2);///searches the file for an integer
fclose(fp3);
char Points2[14];///bufer to store the points value
@@ -461,18 +474,20 @@
fprintf(fp4,"%i",score3);///prints the integer value for the score to file
fclose(fp4);
}
+///reads the curently saved score for the third level
void scoreRead3(){
int Score3;///tells it that the value to be read is an integer value
FILE *fp5 = fopen("/local/score3.txt","r");///opens score3.txt
- fscanf(fp5,"%i",&Score3);///searches the file for an integer followed by a string
+ fscanf(fp5,"%i",&Score3);///searches the file for an integer
fclose(fp5);
char Points3[14];///bufer to store the points value
sprintf(Points3,"%i",Score3);///sends the score to the buffer
lcd.printString(Points3,30,3);///prints the score to the LCD
}
-
+///function called to quite the game and return to the menu
+///, has the same effect on the game as running out of lives while playing
void pause(){
power=0;
lcd.clearPixel(i,j);
@@ -482,7 +497,7 @@
life=0;
}
///all of the functions that make up the main game
-void mainGame(){
+void mainGame(){
count.attach(&counter,1.0);///counts every second for the lives function etc.
flash.attach(&Flash,0.25);
push.rise(&pause);
@@ -555,7 +570,7 @@
clear();
}
-///mini game
+///mini game fashioned after the style of etch-a-sketch
void endGame(){
flash.attach(&Flash,0.25);
red1=0;
@@ -704,7 +719,7 @@
///function that checks if the goal area has been reached
void winning(){
- if(j==0){
+ if(j==0){///if the pixel reaches the top top of the screen
lcd.clearPixel(i,j);
i=1;
j=24;
@@ -761,7 +776,7 @@
if (j<0)
j=ny-1;
}
-
+///function called to check if the player is trying to move through a solid object
void collide1(){
int block=0;
if(joystick.direction==UP){
@@ -870,13 +885,13 @@
joystick.x0 = xPot; /// initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
joystick.y0 = yPot;
}
+ /// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
void updateJoystick(){
- /// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
joystick.x = xPot - joystick.x0;
joystick.y = yPot - joystick.y0;
/// read button state
joystick.button = button;
-
+
/// calculate direction depending on x,y values
/// tolerance allows a little lee-way in case joystick not exactly in the stated direction
--- a/main.h Sat May 09 09:38:26 2015 +0000
+++ b/main.h Sun May 10 20:07:18 2015 +0000
@@ -20,29 +20,41 @@
unsigned char imgbuffer[3*84];/*!< buffer to hold the image */
FILE *img;/*!< creating a file calling img */
LocalFileSystem local("local");/*!< file system to retrieve files from flash memory */
+///
/*!< connections for the joystick */
+/// joystick button
DigitalIn button(p17);
+/// potentiometer controlling X component
AnalogIn xPot(p15);
+/// potentiometer controlling Y component
AnalogIn yPot(p16);
+///
/*!< connections for objects using pwm, excluding the LCD backlight */
+/// red LED 1
PwmOut red1(p26);
-PwmOut red2(p25);
-PwmOut red3(p24);
+/// red LED 2
+PwmOut red2(p25);
+/// red LED 3
+PwmOut red3(p24);
+/// yellow LED
PwmOut yel(p23);
+/// peizo buzzer
PwmOut buzzer(p22);
-
+///
/*!< inerrupting objects */
Ticker count;/*!< timer to count down the time for the lives */
Ticker flash;/*!<timer to flash the pixel on a fewtimes a second */
-InterruptIn butt(p17);/*!< button on the joystick */
-InterruptIn push(p18);/*!< external button on the PCB */
+/// button on the joystick
+InterruptIn butt(p17);
+/// external button on the PCB
+InterruptIn push(p18);
Timeout timeout;/*!< timeout used for debouncing on buttons */
Ticker backlight;/*!< ticker to regularly check the brightness level for backlight and LEDs */
Ticker Power;/*!< ticker to count up until the time limit is reached and then power down */
Ticker pollJoystick;/*!< ticker to regularly read and update the possition of the joystick */
-
+///
/*!< create enumerated type (0,1,2,3 etc. for the direction of the joystick)
can be extended to include diagonals etc. */
enum DirectionName {
@@ -53,7 +65,7 @@
CENTRE,
UNKNOWN
};
-
+///
/*!< struct for Joystick */
typedef struct JoyStick Joystick;
struct JoyStick {
@@ -64,9 +76,10 @@
int button; /*!< button state (assume pull-down used, so 1 = pressed, 0 = unpressed) */
DirectionName direction; /*!< current direction */
};
+///
/*!< create a struct variable */
Joystick joystick;
-
+///
/*!< variables to be initialised */
int nx=84;/*!< number of pixels that make up the horizontal component of the LCD */
int ny=48;/*!< number of pixels that make up the vertical component of the LCD */
@@ -86,7 +99,7 @@
int score2=0;/*!< score for the second level, highest saved in flash memory */
int score3=0;/*!< score for the third level, highest saved in flash memory */
int power=0;/*!< counts up to power down the device if nothing happens for a set time */
-
+///
/*!< function prototypes */
void calibrateJoystick();
/*!< sets the default position of the joystick
@@ -95,7 +108,8 @@
@param joystick.x0
@param joystick.y0
@return xPot - inital position from 0.0 to 1.0 (0.5 if exactly centred)
-@return yPot - inital position from 0.0 to 1.0 (0.5 if exactly centred)*/
+@return yPot - inital position from 0.0 to 1.0 (0.5 if exactly centred)
+*/
void updateJoystick();
/*!< checks the possiton fo the joystick by
@@ -108,7 +122,8 @@
@return DOWN - if the Y POT is possitive of the tolerance
@return UP if the Y POT is negative of the tolerance
@return RIGHT if the X POT is positive of the tolerance
-@return LEFT if the X POT is negative of the tolerance */
+@return LEFT if the X POT is negative of the tolerance
+*/
void move();
/*!< checks which direction the joystick is pointing and
@@ -119,7 +134,8 @@
@param LEFT - joystick direction left
@param RIGHT - joystick direction right
@return i++/-- ~ depending on the screen orientation and joystick direction
-@return j++/-- ~ depending on the screen orientation and joystick direction*/
+@return j++/-- ~ depending on the screen orientation and joystick direction
+*/
void endMove();
/*!< the same as move but doesn't clear off where the pixel
@@ -129,7 +145,8 @@
@param LEFT - joystick direction left
@param RIGHT - joystick direction right
@return i++/-- ~ depending on the screen orientation and joystick direction
-@return j++/-- ~ depending on the screen orientation and joystick direction*/
+@return j++/-- ~ depending on the screen orientation and joystick direction
+*/
void maze1();
/*!< checks the value of the mazeFlag and opens the
@@ -138,7 +155,8 @@
@return img - name given to the file being loaded
@return TEST.BMP - name of the file to open for the first level
@return MAZE1.BMP - name of the file to open for the second level
-@return MAZE2.BMP - name of the file to open for the third level*/
+@return MAZE2.BMP - name of the file to open for the third level
+*/
void border();
/*!<
@@ -150,7 +168,8 @@
@param nx - horizontal screen component
@param ny - vertical screen component
@return j - stops at top edge and bottom edge
-@return i - stops at left edge and right edge */
+@return i - stops at left edge and right edge
+*/
void endBorder();
/*!< same as the border, only for the mini game,
@@ -161,7 +180,8 @@
@param nx - horizontal screen component
@param ny - vertical screen component
@return j - warps from top edge to bottom edge and vice versa
-@return i - warps from left edge to right edge and vice versa */
+@return i - warps from left edge to right edge and vice versa
+*/
void winning();
/*!< checks to see if the goal area has been reached,
@@ -171,7 +191,8 @@
@return lcd.clearPixel(i,j) - clears the current pixel: see N5110 for more
@return i - resets to the start position if the goal area (j==0) is reached
@return j - resets to the start position if the goal area (j==0) is reached
-@return winFlag - sets the flag to signify the goal has been reached*/
+@return winFlag - sets the flag to signify the goal has been reached
+*/
void clear();
/*!< clears off the pixel and then the rest of the LCD
@@ -187,7 +208,8 @@
lives remain
@return lcd.clearPixel(i,j) - clears the current pixel: see N5110 for more
@return lcd.clear() - clears the whole LCD: see N5110 for more
-@return lcd.refresh() - updates the LCD: see N5110 for more */
+@return lcd.refresh() - updates the LCD: see N5110 for more
+*/
void counter();
/*!< called once a second to count up the time and remove a
@@ -205,12 +227,14 @@
@return life-- ~ takes one life away every time the player runs out of time
@return lcd.clearPixel(i,j) - clears the current pixel when the time is up: see N5110 for more
@return i - resets to the start position when time runs out
-@return j - resets to the start position when time runs out */
+@return j - resets to the start position when time runs out
+*/
void Flash();
/*!< attached to flash ticker, toggles the flag between 1 and 0 and sets the pixel to flash
@return flashflag=!flashFlag - toggles the flag each time the function is called
-@return lcd.setPixel(i,j) - sets the pixel: see N5110 for more */
+@return lcd.setPixel(i,j) - sets the pixel: see N5110 for more
+*/
void Initialise();
/*!< all of the variables, flags, tickers, and anything else that needs to be set when the device first powers up
@@ -224,7 +248,8 @@
@return winFlag=0 - sets the flag signifying the gaol area has yet to be reached
@return startFlag=1 - flag used in the main menu
@return buzzer.period(0.002)- sets the frequency at which PWM is sent to the buzzer
-@return c=1 - flag used to set a while loop*/
+@return c=1 - flag used to set a while loop
+*/
void mainGame();
/*!< contains all of the relevant loops/checks/functions that make up the main game
@@ -264,7 +289,8 @@
@return fclose(fp# - closes the file (# indicates that a number variable is attached to the statement): see scoreRead() for more
@param score#>Score# - compares the score that was just set to the score on the file to see if it's greater (# indicates that a number variable is attached to the statement)
@return scoreWrite#() - calls the scoreWrite#() function if the latest score is larger than the one on file (# indicates that a number variable is attached to the statement)
-@return clear() - calls the clear() function (at the end of the mainGame() code) */
+@return clear() - calls the clear() function (at the end of the mainGame() code)
+*/
void endGame();
/*!< the functions/variables/loops that make up the mini game
@@ -293,7 +319,8 @@
@return clear() - calls the clear() function (outside the main loop of the endGame() function)
@return lcd.refresh() - updates the LCD: see N5110 for more (outside the main loop of the endGame() function)
@return i=1 - resets the horizontal starting coordinate (outside the main loop of the endGame() function)
-@return j=24 - resest the vertical starting coordinate (outside the main loop of the endGame() function) */
+@return j=24 - resest the vertical starting coordinate (outside the main loop of the endGame() function)
+*/
void start();
/*!< all of the variable/loops/functions that make up the main menu
@@ -347,7 +374,8 @@
@return lcd.normalMode() - sets the LCD to normal mode: see N5110 for more (if in the statement set by !screen)
@return lcd.refresh() - updates the LCD: see N5110 for more (if in the statement set by joystick.direction==RIGHT)
@return lcd.clearPixel(i,j) - clears the current pixel: see N5110 for more (outside the main loop for the start() function)
-@return clear() - calls the clear() function (outside the main loop for the start() function) */
+@return clear() - calls the clear() function (outside the main loop for the start() function)
+*/
void Options();
/*!< all of the variables/loops/functions that make up the options menu
@@ -698,9 +726,9 @@
@return the same as when the power==56 (if in the statement set by power==58)
@param power==59 - if the power flag is equal to 59, when 57 seconds have gone by without the flag being reset
@return the same as when power==55 (if in the statement set by power==59)
-@param power==60 - if the power flag is equal to 60, when 58 seconds have gone by without the flag being reset
-@return the same as when the power==56 (if in the statement set by power==60)
-@return DeepPowerDown() - calls the function DeepPowerDown() : see PowerControl for more (if in the statement set by power==60)
+@param power>=60 - if the power flag is equal to or greater than 60, when at least 60 seconds have gone by without the flag being reset
+@return the same as when the power==56 (if in the statement set by power>=60)
+@return DeepPowerDown() - calls the function DeepPowerDown() : see PowerControl for more (if in the statement set by power>=60)
*/
#endif
\ No newline at end of file
