Simple fish eat program
Dependencies: mbed mbed-rtos N5110 ShiftReg Tone
Diff: classes/FishEngine.cpp
- Revision:
- 6:7aafcaf7cbe2
- Parent:
- 5:02e7b5197827
- Child:
- 8:fe51fbf81dee
diff -r 02e7b5197827 -r 7aafcaf7cbe2 classes/FishEngine.cpp --- a/classes/FishEngine.cpp Wed Mar 24 19:33:59 2021 +0000 +++ b/classes/FishEngine.cpp Thu Mar 25 11:31:57 2021 +0000 @@ -4,30 +4,12 @@ #define DOWN 1 #define CENTRE 2 -//attach -BusOut RGB_led(p24,p23,p22); //RGB bus MSB - LSB, inverted logic 1 = low -DigitalIn button_A(p29); -DigitalIn button_B(p28); -DigitalIn button_C(p27); -DigitalIn button_D(p26); - //objects -ShiftReg shift; +Settings settings; Graphics graphics; -//initialise all of the board -void FishEngine::system_init(N5110 &lcd, Tone &dac, Joystick &joystick){ - lcd.init(); - lcd.setBrightness(0.5); - joystick.init(); - dac.init(); - shift.write(0x00); - RGB_led.write(0b111); - button_A.mode(PullNone); - button_B.mode(PullNone); - button_C.mode(PullNone); - button_D.mode(PullNone); -} +int ButtonA; +int ButtonB; // creates intro sequence void FishEngine::titleSequence(N5110 &lcd){ @@ -36,7 +18,7 @@ lcd.refresh(); wait_ms(2000); - while(button_A.read() == 0){ + while(settings.buttonA_state(ButtonA) == 0){ lcd.clear(); graphics.confirmationScreen(lcd); //calls confirmation function (held in graphics.h) lcd.refresh(); @@ -103,7 +85,7 @@ //case for new game case 0: - if(button_A.read()){ + if(settings.buttonA_state(ButtonA)){ gamePlay(lcd,dac); } switch(direction) { @@ -121,7 +103,7 @@ //case for HighScore case 1: - if(button_A.read()){ + if(settings.buttonA_state(ButtonA)){ highScore(lcd); } switch(direction) { @@ -139,7 +121,7 @@ //case for how to play case 2: - if(button_A.read()){ + if(settings.buttonA_state(ButtonA)){ Instructions(lcd); } switch(direction) { @@ -157,8 +139,9 @@ //case for settings case 3: - if(button_A.read()){ - Settings(lcd, dac, joystick); + if(settings.buttonA_state(ButtonA)){ + graphics.screenFlash(lcd); + settings.settingsMenu(lcd, dac, joystick); } switch(direction) { case UP: @@ -195,7 +178,7 @@ lcd.printString(" Play game", 10, 2); lcd.refresh(); wait_ms(1000/10); - }while(button_B.read() == false); + }while(settings.buttonB_state(ButtonA) == false); } //function for viewing high score @@ -208,7 +191,7 @@ lcd.printString(" High score", 10, 2); lcd.refresh(); wait_ms(1000/10); - }while(button_B.read() == false); + }while(settings.buttonB_state(ButtonB) == false); } //function for providing instruction on how to play @@ -221,131 +204,5 @@ lcd.printString(" How to play", 10, 2); lcd.refresh(); wait_ms(1000/10); - }while(button_B.read() == false); + }while(settings.buttonB_state(ButtonA) == false); } - -//functions for changing settings -void FishEngine::Settings(N5110 &lcd, Tone &dac, Joystick &joystick){ - graphics.screenFlash(lcd); - RGB_led.write(0b101); - int rect_width = 32; - const int rightArrow[6][3] = { - { 1,0,0, }, - { 1,1,0, }, - { 1,1,1, }, - { 1,1,1, }, - { 1,1,0, }, - { 1,0,0, }, - }; - - // outputs for the state machine are used to draw Fish_1 beside the corrosponding - // menu choice - int g_setting_menu_output[2] = {7, 37}; - int state = 0; //set inital state to 0 - int direction = CENTRE; //set inital direction to remain on that state until an input is received - int x_pos; - - do{ - lcd.clear(); - lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border - lcd.printString("Contrast", 15, 1); - lcd.printString("Music", 15, 3); - lcd.printString("On", 15, 4); - lcd.printString("Off", 45, 4); - - if(joystick.get_direction() == E && rect_width < 64){ - rect_width = rect_width + 16; - }else if(joystick.get_direction() == W && rect_width > 0){ - rect_width = rect_width - 16; - } - - //contrast bar is 20 pixels high and 64 pixels long - lcd.drawRect(10,16,64,5,FILL_TRANSPARENT); //draws outer rectangle - lcd.drawRect(10,16,rect_width,5,FILL_BLACK); //draws inner rectangle - - if(rect_width == 0){ - lcd.setBrightness(0.3); - - }else if(rect_width == 16){ - lcd.setBrightness(0.4); - - }else if(rect_width == 32){ - lcd.setBrightness(0.5); - - }else if(rect_width == 48){ - lcd.setBrightness(0.6); - - }else if(rect_width == 64){ - lcd.setBrightness(0.7); - } - - /*implementation of state machine allows the determination of the position of the arrow marker - based on the users joystick input: - - inputs - state 0 1 2 where 0 is UP and 1 is DOWN 2 is CENTRE - - 0 1 1 0 - 1 0 0 1 */ - - if(joystick.get_direction() == N){ //if the joystick is moved upward, then direction is UP - direction = UP; - }else if (joystick.get_direction() == S){//if the joystick is moved downward, then direction is DOWN - direction = DOWN; - }else{ - direction = CENTRE; //if the joystick is left in the centre position then direction is CENTRE - }; - - x_pos = g_setting_menu_output[state];//x_position is the array output at position 'state' - - - //implement state machine - switch(state) { - //case for music on - case 0: - if(button_A.read()){ - RGB_led.write(0b101); - } - switch(direction) { - case UP: - state = 1; - break; - case DOWN: - state = 1; - break; - case CENTRE: - state = 0; - break; - } - break; - - //case for music off - case 1: - if(button_A.read()){ - RGB_led.write(0b110); - } - switch(direction) { - case UP: - state = 0; - break; - case DOWN: - state = 0; - break; - case CENTRE: - state = 1; - break; - } - break; - - default: - state = 0; - break; - } - - //pos / sprite x y / x y - lcd.drawSprite(x_pos,32,6,3,(int*)rightArrow); - lcd.refresh(); - - wait_ms(1000/10); - }while(button_B.read() == false); -}