Tuan Le / Mbed 2 deprecated AccelerometerProject

Dependencies:   MMA8452 N5110 PowerControl beep mbed

Game.h

Committer:
stevenle93
Date:
2015-05-04
Revision:
3:1974db5993ef
Parent:
2:57d1ed1f0ad3
Child:
4:c141e252d786

File content as of revision 3:1974db5993ef:

/**
@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; // Variable to store scores has a data type of interger
    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];
            int scorelength = sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
            if (scorelength <=14) {
                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
        }
    }
}

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];
            int scorelength = sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
            if (scorelength <=14) {
                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
        }
    }
}

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];
            int scorelength = sprintf(scorebuffer,"Score:%d",score); //Score will be displayed as unsigned Decimal Interger number
            if (scorelength <=14) {
                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
        }
    }
}

void Game::reset()
{
    score = 0;
    CClock = 60;
}

#endif