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
Game.h
- Committer:
- stevenle93
- Date:
- 2015-05-04
- Revision:
- 4:c141e252d786
- Parent:
- 3:1974db5993ef
- Child:
- 5:c0a58a6916f2
File content as of revision 4:c141e252d786:
/**
@file Game.h
@brief Header file of the game rule which contain member functions, variables and functions definition
*/
#ifndef GAME_H
#define GAME_H
#include "mbed.h"
#include "MMA8452.h"
#include "N5110.h"
#include "Clock.h"
Ticker gatimer;
int gatimerFlag = 0;
void gatimerExpired()
{
gatimerFlag = 1; //Set flag for timer of the game
}
class Game
{
private:
/**
* @param The variable Score is used to store score of user when
* @param the accelerometer excessed a threshold value.
*/
/**
* @param The acceleration variable recalled from MMA8452.h
*/
Acceleration accel; // Acceleration variable declared in MMA8452 class
public:
/**
* The GameRule member function has a bsic rule of converting acceleration value into score
*/
void easyMode();
void norMode();
void hardMode();
int score;
int scoArr[6];
void reset();
};
void Game::easyMode ()
{
if (gatimerFlag) {
gatimerFlag = 0;
accel = mma8452.readValues(); //Read the value from MMA8452 sensor
if (accel.x > 2 || accel.x < -2) { //Easy mode: If the value of accelerator data exceed 2g, the score will increase by one unit
score++;
char scorebuffer[14];
sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
lcd.printString(scorebuffer,6,3);
} else if (accel.x < 2 && accel.x > -2) { //Otherwise, if the score is unchange
lcd.printString("EASY MODE",16,0); //Display a small notice to the user
}
}
if ((scoArr[0] == 0) || (scoArr[1] == 0)) {
scoArr[0] = score;
} else if((scoArr[0] > 0) && (scoArr[1] = 0)) {
scoArr[1] = score;
} else if(scoArr[0] > scoArr[1]) {
scoArr[1] = score;
} else if(scoArr[0] < scoArr[1]) {
scoArr[0] = score;
}
}
void Game::norMode()
{
if (gatimerFlag) {
gatimerFlag = 0;
accel = mma8452.readValues(); //Read the value from MMA8452 sensor
if (accel.x > 3 || accel.x < -3) { //Easy mode: If the value of accelerator data exceed 3g, the score will increase by one unit
score++;
char scorebuffer[14];
sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
lcd.printString(scorebuffer,6,3);
} else if (accel.x < 3 && accel.x > -3) { //Otherwise, if the score is unchange
lcd.printString("NORMAL MODE",9,0); //Display a small notice to the user
}
}
if ((scoArr[2] == 0) || (scoArr[3] == 0)) {
scoArr[2] = score;
} else if((scoArr[2] > 0) && (scoArr[3] = 0)) {
scoArr[3] = score;
} else if(scoArr[2] > scoArr[3]) {
scoArr[3] = score;
} else if(scoArr[2] < scoArr[3]) {
scoArr[2] = score;
}
}
void Game::hardMode()
{
if (gatimerFlag) {
gatimerFlag = 0;
accel = mma8452.readValues(); //Read the value from MMA8452 sensor
if (accel.x > 3.5 || accel.x < -3.5) { //Easy mode: If the value of accelerator data exceed 3.5g, the score will increase by one unit
score++;
char scorebuffer[14];
sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
lcd.printString(scorebuffer,6,3);
} else if (accel.x < 3.5 && accel.x > -3.5) { //Otherwise, if the score is unchange
lcd.printString("HARD MODE",16,0); //Display a small notice to the user
}
}
if ((scoArr[4] == 0) || (scoArr[5] == 0)) {
scoArr[4] = score;
} else if((scoArr[4] > 0) && (scoArr[5] = 0)) {
scoArr[5] = score;
} else if(scoArr[4] > scoArr[5]) {
scoArr[5] = score;
} else if(scoArr[4] < scoArr[5]) {
scoArr[4] = score;
}
}
void Game::reset()
{
score = 0;
CClock = 60;
}
#endif