A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
Diff: main.cpp
- Revision:
- 13:a1b3a373c5a4
- Parent:
- 12:eedda6554615
- Child:
- 14:c2c969e1c6e8
--- a/main.cpp Thu Apr 16 21:46:57 2015 +0000 +++ b/main.cpp Fri Apr 17 01:20:47 2015 +0000 @@ -11,83 +11,11 @@ #include <ctime> #include <cstdlib> -// change this to alter tolerance of joystick direction -#define DIRECTION_TOLERANCE 0.05 - - - -// create buzzer objecct -Beep buzzer(p21); - -// create local file system -//LocalFileSytem local("local"); - -// navigation/action buttons -DigitalIn buttonA(p19); -DigitalIn buttonB(p20); - -// LED indicators -AnalogOut ledR(p18);// RED LED -DigitalOut ledY(p24);// YELLOW LED - -// connections for joystick -DigitalIn button(p17); -AnalogIn xPot(p15); -AnalogIn yPot(p16); - -// timer to regularly read the joystick -Ticker pollJoystick; - -// serial for debug -Serial serial(USBTX,USBRX); - -// create enumerated type (0,1,2,3 etc. for direction) -// could be extended for diagonals etc. -enum DirectionName { - UP, - DOWN, - LEFT, - RIGHT, - CENTRE, - UNKNOWN -}; - -// struct for Joystick -typedef struct JoyStick Joystick; -struct JoyStick { - float x; // current x value - float x0; // 'centred' x value - float y; // current y value - float y0; // 'centred' y value - int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) - DirectionName direction; // current direction -}; - -// create struct variable -Joystick joystick; - -int printFlag = 0; - -// function prototypes -void calibrateJoystick(); -void updateJoystick(); - - int main() { ledR = 1;//power LED on - srand (time(NULL));//initial seed for randomisation - - // initial random x co-ordinates - // for falling hazards - // (values between 3 and 76) - randX1 = rand() % 74 + 5; - randX2 = rand() % 74 + 5; - randX3 = rand() % 74 + 5; - randX4 = rand() % 74 + 5; - randX5 = rand() % 74 + 5; - randX6 = rand() % 74 + 5; + randomise(); calibrateJoystick();//get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0);//read joystick 10 times per second @@ -101,6 +29,7 @@ int exitOption = 0;//counter for exit menu int option = 0;//counter for options menu int subOption = 0;//counter for sub options menu + int fxOption = 0;//counter for Sound FX menu while(1) { drawMainMenu();//draws main menu @@ -114,83 +43,9 @@ if((mainOption == 1)&&(buttonA == 1)) { scores(); } - // if 'option' selected if((mainOption == 2)&&(buttonA == 1)) { - actionButton(); - backButton(); - drawOptionsMenu();//draws options menu - - while(1) { - actionButton(); - backButton(); - optionsMenu(option);//presents options - -////////////////////// difficulty menu //////////////////////////////////// - if ((option == 0)&&(buttonA == 1)) { - actionButton(); - backButton(); - drawDifficultyMenu();//draws difficulty menu - - while(1) { - actionButton(); - backButton(); - difficultyMenu(subOption);//presents difficulty options - - if(buttonB == 1) { - lcd.clear(); - break; - } - } - } - - // back to menu - if(buttonB == 1) { - lcd.clear(); - break; - } - } + optionsMenu(option, subOption, fxOption); } } } - - - -// read default positions of the joystick to calibrate later readings -void calibrateJoystick() -{ - button.mode(PullDown); - // must not move during calibration - joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) - joystick.y0 = yPot; -} - -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 - if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { - joystick.direction = CENTRE; - } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { - joystick.direction = UP; - } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { - joystick.direction = DOWN; - } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { - joystick.direction = RIGHT; - } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { - joystick.direction = LEFT; - } else { - joystick.direction = UNKNOWN; - } - - // set flag for printing - printFlag = 1; -} - -