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.
Dependencies: MMA8452 N5110 PowerControl beep mbed
Revision 9:522f9311ff4b, committed 2015-05-10
- Comitter:
- stevenle93
- Date:
- Sun May 10 17:21:39 2015 +0000
- Parent:
- 8:aebb468546c5
- Commit message:
- Changes: all header file are documented, changing in debounce function of all the buttons, little change with condition of putting mbed into sleep mode.;
Changed in this revision
--- a/Accel-cize.cpp Sat May 09 22:59:15 2015 +0000
+++ b/Accel-cize.cpp Sun May 10 17:21:39 2015 +0000
@@ -1,44 +1,49 @@
+/**
+@file Accel-cize.cpp
+@brief This is a main file where the code runs.
+@brief Revision 1.0
+@author Huynh Minh Tuan Le
+@date April 2015
+*/
+
#include "MMA8452Test.h"
#include "GameFSM.h"
+#include "PowerControl/PowerControl.h"
-AnalogIn adjust(p20);
-Timer t;
-FSM program;
-Menu wel;
+AnalogIn adjust(p20); ///<A analogue-in variable for potentiometer.
+Timer t; ///<A timer object t for timing the moment to put mbed into sleep mode.
+FSM program; ///<A "program" variable declared with FSM class.
+Menu wel; ///<A "wel" variable declared with Menu class.
+Test init; ///<A "init" variable declared with Test class from MMA8452Test.h header file.
+
int main()
{
- Test init;
- init.initial();
-
- ButtonU.rise(&BuUPress);
+ init.initial(); //A Function to initialise the Nokia 5110 screen and reading data from MMA8452.
+
+ ButtonU.rise(&BuUPress);
ButtonD.rise(&BuDPress);
ButtonA.rise(&BuAPress);
ButtonB.rise(&BuBPress);
- wel.welcome();
- debounce.start();
+ wel.welcome(); //Run the welcome function from Menu class.
while(1) {
- lcd.setBrightness(adjust);
+ lcd.setBrightness(adjust); //Attach the screen LED brightness value to potentiometer.
- program.proact();
+ program.proact(); //Running proact function from FSM class.
//////// Timer for the mbed go to sleep mode when waiting for 2 mins ////////
- if ((BuAFlag==0) && (BuBFlag==0) && (BuDFlag==0) && (BuUFlag==0)) {
+ if ((BuAFlag==0) && (BuBFlag==0) && (BuDFlag==0) && (BuUFlag==0)) { //If all buttons are not pressed, start the timer t.
t.start();
- float time = t.read();
- char buffer[14];
- sprintf(buffer,"Time: %0.1f",time);
- lcd.printString(buffer,0,5);
}
- if ((BuAFlag)||(BuBFlag)||(BuUFlag)||(BuDFlag)) {
+ if ((BuAFlag)||(BuBFlag)||(BuUFlag)||(BuDFlag)) { //If one of the buttons is pressed, reset timer t.
t.reset();
}
- if (t.read()>10) {
- lcd.setBrightness(0);
- t.reset();
- Sleep();
+ if (t.read()>10) { //If timer t greater than 10s,
+ lcd.setBrightness(0); //Set LCD brightness to zero.
+ t.stop(); //Stop timer t.
+ Sleep(); //Put mbed into sleep mode.
}
}
}
--- a/Clock.h Sat May 09 22:59:15 2015 +0000
+++ b/Clock.h Sun May 10 17:21:39 2015 +0000
@@ -1,7 +1,9 @@
/**
-@ file Clock.h
-
-@ brief Header file of a count down clock containing Ticker on RTC and display time on N5110 Nokia Screen
+@file Clock.h
+@brief Header file of a count down clock containing Ticker on RTC and display time on N5110 Nokia Screen
+@brief Revision 1.0
+@author Huynh Minh Tuan Le
+@date April 2015
*/
#ifndef CLOCK_H
@@ -12,25 +14,51 @@
#include "MMA8452.h"
#include "beep.h"
-Ticker timer;
-int CClock;
-DigitalOut led(p22);
-BusOut leds(LED1, LED2, LED3, LED4);
-Beep beep(p26);
+Ticker timer; ///<Ticker object for the count down clock. Called as a timer
+int CClock; ///<A variable for storing clock value in seconds.
+/**
+@namespace Flashing led.
+@brief A led to give a notification when the game is in last 10s.
+*/
+DigitalOut led(p22); ///<A variable of the flashing led.
+/**
+@namespace Knightrunner leds.
+*/
+BusOut leds(LED1, LED2, LED3, LED4); ///<A variable for outputing a set of four on-board leds.
+/**
+@namespace Beep sound.
+*/
+Beep beep(p26); ///<An output analgoue signal of a piezzo buzzer.
-int timerFlag = 0;
+int timerFlag = 0; ///<A flag for timer ticker object.
-void timerExpired()
+void timerExpired() ///Function of setting flag for timer.
{
timerFlag = 1; //Set flag for timer of the countdown
}
+///Clock Class
class Clock
{
public:
+ /**
+ * The function that makes a LED flash.
+ */
void flashLed();
+ /**
+ * The function that display LEDs on the embed.
+ */
void knightrunner();
+ /**
+ * The count down function occur during the game.
+ * The clock start from 60s to 0s.
+ * In the last 10s of the clock, the function will flash an LED and generate alerting sound.
+ */
void countDown();
+ /**
+ * The function that display "Time Out" message on the Nokia screen
+ * and run funtion knightrunner.
+ */
void timeout();
};
--- a/Game.h Sat May 09 22:59:15 2015 +0000
+++ b/Game.h Sun May 10 17:21:39 2015 +0000
@@ -1,8 +1,9 @@
/**
@file Game.h
-
@brief Header file of the game rule which contain member functions, variables and functions definition
-
+@brief Revision 1.0
+@author Huynh Minh Tuan Le
+@date April 2015
*/
#ifndef GAME_H
@@ -13,55 +14,57 @@
#include "N5110.h"
#include "Clock.h"
-Ticker gatimer;
-int gatimerFlag = 0;
+
+Ticker gatimer; ///< Set a ticker for the game timing
+int gatimerFlag = 0; ///< Declare game ISR flag as a integer type variable
void gatimerExpired()
{
- gatimerFlag = 1; //Set flag for timer of the game
+ gatimerFlag = 1; ///Set flag for timer of the game
}
+///Game Class
class Game
{
private:
/**
- * @param The acceleration variable recalled from MMA8452.h
+ * The acceleration variable recalled from MMA8452.h
*/
- Acceleration accel; // Acceleration variable declared in MMA8452 class
+ Acceleration accel; ///Acceleration variable declared in MMA8452 class
public:
/**
- * A function that contains condition of easy mode
- * The conditions: Threshold value of 2g - Time interval for reading data of 0.1s
+ * A function that contains condition of easy mode.
+ * The conditions: Threshold value of 2g - Time interval for reading data of 0.1s.
*/
void easyMode();
/**
- * A function that contains condition of normal mode
- * The conditions: Threshold value of 3g - Time interval for reading data of 0.2s
+ * A function that contains condition of normal mode.
+ * The conditions: Threshold value of 3g - Time interval for reading data of 0.2s.
*/
void norMode();
/**
- * A functionthat contains condition of hard mode
- * The conditions: Threshold value of 3g - Time interval for reading data of 0.4s
+ * A functionthat contains condition of hard mode.
+ * The conditions: Threshold value of 3g - Time interval for reading data of 0.4s.
*/
void hardMode();
/**
- * @param A global variable to store play score.
- * @param This score will later transfer into an array
+ * A global variable to store play score.
+ * This score will later transfer into an array.
*/
int score;
/**
- * @param An array to store set of scores
- * @param The array structure is as follow:
- * @param Easy high score, Easy current score, Normal high score, Normal current score, Hard high score, Hard current score.
+ * An array to store set of scores.
+ * The array structure is as follow:
+ * Easy high score, Easy current score, Normal high score, Normal current score, Hard high score, Hard current score.
*/
int scoArr[6];
/**
- * A function used to reset the game condition
+ * A function used to reset the game condition.
*/
void reset();
/**
- * A funtion to draw a battery image when playing game
+ * A funtion to draw a battery image when playing game.
*/
void battImag();
};
@@ -83,11 +86,11 @@
battImag();
}
}
-
- scoArr[1] = score;
- if(scoArr[0] > scoArr[1]) {
+ //Condition for storing "score" and transferring "score" between scoArr elements.
+ scoArr[1] = score; //Store "score" variable into 2nd element of scoArr array
+ if(scoArr[0] > scoArr[1]) { //If 1st element is greater than 2nd element, store in 2nd element
scoArr[1] = score;
- } else if(scoArr[0] < scoArr[1]) {
+ } else if(scoArr[0] < scoArr[1]) { //Vice versa, store in 1st element
scoArr[0] = score;
}
}
@@ -109,10 +112,11 @@
battImag();
}
}
- scoArr[3] = score;
- if(scoArr[2] > scoArr[3]) {
+ //Condition for storing "score" and transferring "score" between scoArr elements.
+ scoArr[3] = score; //Store "score" variable into 4th element of scoArr array
+ if(scoArr[2] > scoArr[3]) { //If 3rd element is greater than 4th element, store in 4th element
scoArr[3] = score;
- } else if(scoArr[2] < scoArr[3]) {
+ } else if(scoArr[2] < scoArr[3]) { //Vice versa, store in 3rd element
scoArr[2] = score;
}
}
@@ -134,10 +138,11 @@
battImag();
}
}
- scoArr[5] = score;
- if(scoArr[4] > scoArr[5]) {
+ //Condition for storing "score" and transferring "score" between scoArr elements.
+ scoArr[5] = score; //Store "score" variable into 6th element of scoArr array
+ if(scoArr[4] > scoArr[5]) { //If 5th element is greater than 6th element, store in 6th element
scoArr[5] = score;
- } else if(scoArr[4] < scoArr[5]) {
+ } else if(scoArr[4] < scoArr[5]) { //Vice versa, store in 5th element
scoArr[4] = score;
}
}
--- a/GameFSM.h Sat May 09 22:59:15 2015 +0000
+++ b/GameFSM.h Sun May 10 17:21:39 2015 +0000
@@ -1,7 +1,13 @@
/**
@file GameFSM.h
-
-@brief Header file of Game's Finite stage Machine
+@brief Header file of Game's Finite Stage Machine.
+@brief This header file contains header file from Game.h, Clock.h, Menu.h, N5110.h and mbed.h.
+@brief It also has functions of setting flag for four buttons.
+@brief It shows stages of the whole programme and devided into 4 sectors and an initial stage with
+@brief regarding Game Mode sector, High Score sector, Game Screen sector, Your Score sector and Main Menu stage.
+@brief Revision 1.0
+@author Huynh Minh Tuan Le
+@date April 2015
*/
#ifndef GAMEFSM_H
#define GAMEFSM_H
@@ -10,80 +16,91 @@
#include "Clock.h"
#include "N5110.h"
#include "Menu.h"
-#include "PowerControl/PowerControl.h"
#include "mbed.h"
-Game game;
-Clock countdown;
-Menu menu;
+
+Game game; ///<A game object declared from the Game class.
+Clock countdown; ///<A countdown object declared from the Clock class.
+Menu menu; ///<A menu object declared from the Menu class.
+
/**
@namespace ButtonA
-@brief Button A of interrupting service
+@brief Button A of interrupting service.
*/
InterruptIn ButtonA(p16);
/**
@namespace ButtonB
-@brief Button B of interrupting service
+@brief Button B of interrupting service.
*/
InterruptIn ButtonB(p15);
/**
@namespace ButtonD
-@brief Button Down of interrupting service
+@brief Down button of interrupting service.
*/
InterruptIn ButtonD(p24);
/**
@namespace ButtonU
-@brief Button Up of interrupting service
+@brief Up button of interrupting service.
*/
InterruptIn ButtonU(p23);
-int BuAFlag = 0;
-int BuBFlag = 0;
-int BuDFlag = 0;
-int BuUFlag = 0;
+int BuAFlag = 0; ///<A button A flag variable.
+int BuBFlag = 0; ///<A button B flag variable.
+int BuDFlag = 0; ///<A Down button flag variable.
+int BuUFlag = 0; ///<A up button flag variable.
-int stage = 0;
-int substage = 0;
+int stage = 0; ///<Stage variable for Finite Stage Machine.
-Timer debounce;
+Timer debounce; ///<A timer object for debouncing the buttons.
void BuAPress()
{
- if (debounce.read_ms()>300) {
- BuAFlag = 1; //Set flag for button A
+ debounce.start(); //Start timer for debounce object.
+ if (debounce.read_ms()>200) {
+ BuAFlag = 1; ///Set flag for button A
+ debounce.stop(); //Stop timer
}
}
void BuBPress()
{
- if (debounce.read_ms()>300) {
- BuBFlag = 1; //Set flag for button B
+ debounce.start(); //Start timer for debounce object.
+ if (debounce.read_ms()>200) {
+ BuBFlag = 1; ///Set flag for button B
+ debounce.stop(); //Stop timer
}
}
void BuDPress()
{
- if (debounce.read_ms()>300) {
- BuDFlag = 1; //Set flag for button Down
+ debounce.start(); //Start timer for debounce object.
+ if (debounce.read_ms()>200) {
+ BuDFlag = 1; ///Set flag for button Down
+ debounce.stop(); //Stop timer
}
}
void BuUPress()
{
- if (debounce.read_ms()>300) {
- BuUFlag = 1; //Set flag for button Up
+ debounce.start(); //Start timer for debounce object.
+ if (debounce.read_ms()>200) {
+ BuUFlag = 1; ///Set flag for button Up
+ debounce.stop(); //Stop timer
}
}
+///Game FSM Class
class FSM
{
public:
/**
- The function contains FSM of the whole programme
+ * The function contains FSM of the programme.
*/
void proact();
+ /**
+ * The function that set all the buttons flag to 0.
+ */
void resetButton();
- void sleepMode();
};
void FSM::resetButton()
@@ -127,7 +144,8 @@
}
break;
-
+
+//////////HIGH SCORE SECTOR//////////
case 2: //(Main stage 2)Sub stage 0: HIGH SCORE sector - Cursor is at Easy line
menu.highscore();
@@ -248,7 +266,8 @@
}
break;
-
+
+//////////RESET STAGE//////////
//This case is a game condition setting case
//Clock countdown was set to 60 seconds
//Score was set to 0
@@ -257,7 +276,8 @@
resetButton();
stage = 4;
break;
-//////////GAME MODE SECTOR/////////
+
+//////////GAME MODE SECTOR//////////
case 4: //(Main stage 4)Sub stage 0: GAME MODE sector - Cursor at Easy line
menu.gameset();
@@ -318,10 +338,11 @@
}
break;
-
+
+//////////GAME SCREEN SECTOR//////////
case 7: //(Main stage 4)Sub stage 3: GAME SCREEN sector - Easy mode
- timer.attach(&timerExpired,0.5);
+ timer.attach(&timerExpired,1.0);
gatimer.attach(&gatimerExpired,0.1); //Update time for Easy mode is 0.1s
while(CClock > 0) {
countdown.countDown();
@@ -339,7 +360,7 @@
case 8: //(Main stage 4)Sub stage 4: GAME SCREEN sector - Normal mode
- timer.attach(&timerExpired,0.5);
+ timer.attach(&timerExpired,1.0);
gatimer.attach(&gatimerExpired,0.2); //Update time for Normal mode is 0.2s
while(CClock > 0) {
countdown.countDown();
@@ -357,7 +378,7 @@
case 9: //(Main stage 4)Sub stage 5: GAME SCREEN sector - Hard mode
- timer.attach(&timerExpired,0.5);
+ timer.attach(&timerExpired,1.0);
gatimer.attach(&gatimerExpired,0.4); //Update time for Hard mode is 0.4s
while(CClock > 0) {
countdown.countDown();
@@ -372,7 +393,8 @@
stage = 10; //Go to stage 10: Your score menu
break;
-
+
+//////////YOUR SCORE SECTOR//////////
case 10: //Main stage 5: YOUR SCORE sector - Cursor at Main menu line
menu.yourscore();
--- a/Menu.h Sat May 09 22:59:15 2015 +0000
+++ b/Menu.h Sun May 10 17:21:39 2015 +0000
@@ -12,31 +12,32 @@
#include "mbed.h"
#include "N5110.h"
+///Menu class
class Menu
{
public:
/**
- Welcome function
+ * Welcome function
* @brief It shows a welcome message on screen.
*/
- void welcome();
+ void welcome();
/**
- The first sub menu of Main menu.
- @brief It has same factor of the Main menu which are Main menu title, Play and Score.
- @brief It shows a cursor of circle pointing at "Play" factor.
+ * The first sub menu of Main menu.
+ * @brief It has same factor of the Main menu which are Main menu title, Play and Score.
+ * @brief It shows a cursor of circle pointing at "Play" factor.
*/
void main();
/**
- The Highscore sector contains the highest score of player and the current score they have.
+ * The Highscore sector contains the highest score of player and the current score they have.
*/
void highscore();
/**
- The Startgame sector contains options of game difficulty.
- * @brief It allows player to choose which difficulty to play.
+ * The Startgame sector contains options of game difficulty.
+ * @brief It allows player to choose which difficulty level to play.
*/
void gameset();
/**
- The Yourscore sector is a place showing your current score.
+ * The Yourscore sector is a place showing your current score.
*/
void yourscore();
};
@@ -65,9 +66,7 @@
}
lcd.clear();
}
-/**
-The first sub menu of Main menu.
-*/
+
void Menu::main()
{
lcd.printString("MAIN MENU",16,1);