#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SnakeGame.h"
#include "FlappyBird.h"
#include "JoystickButton.h"
#include "Button.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long currentMillis;
// Button upButton(26);
// Button rightButton(12);
// Button downButton(13);
// Button leftButton(14);
// Button aButton(32);
// Button bButton(33);
const int pinJoyX = 25;
const int pinJoyY = 27;
const int pinJoyButton = 34;
JoystickButton left(pinJoyX, 0, 2047);
JoystickButton right(pinJoyX, 2049, 4095);
JoystickButton down(pinJoyY, 0, 2047);
JoystickButton up(pinJoyY, 2049, 4095);
Button aButton(pinJoyButton);
// Button bButton(33);
enum GameState { MENU, SNAKE_GAME, FLAPPY_BIRD };
GameState currentState = MENU;
SnakeGame *snakeGame;
FlappyBird *flappyBird;
int selectedGame = 0;
void displayMenu(Adafruit_SSD1306 &display) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Menu"));
display.println(F(""));
if(selectedGame == 0)display.print(" > ");
else display.print(" ");
display.println(F("a. Snake Game"));
display.println(F(""));
if(selectedGame == 1)display.print(" > ");
else display.print(" ");
display.println(F("a. Flappy bird"));
display.display();
}
void setup() {
Serial.begin(9600);
Serial.println(F("Setup started"));
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) ;
}
display.clearDisplay();
display.display();
pinMode(pinJoyButton, INPUT_PULLUP); //activate pull-up resistor
Serial.println(F("Setup completed"));
}
// void loop() {
// int Xvalue = 0;
// int Yvalue = 0;
// bool buttonValue = false;
// //read values
// Xvalue = analogRead(pinJoyX);
// //delay(100); //a small pause between analog readings is necessary
// Yvalue = analogRead(pinJoyY);
// buttonValue = digitalRead(pinJoyButton);
// //display values via serial
// Serial.print("X:" );
// Serial.print(Xvalue);
// Serial.print(" | Y: ");
// Serial.print(Yvalue);
// Serial.print(" | Button: ");
// Serial.println(buttonValue);
//delay(1000);
// }
// void loop() {
// currentMillis = millis();
// up.update();
// left.update();
// right.update();
// down.update();
// if (up.isPressed()) Serial.println("Up Button Pressed");
// if (right.isPressed()) Serial.println("Right Button Pressed");
// if (down.isPressed()) Serial.println("Down Button Pressed");
// if (left.isPressed()) Serial.println("Left Button Pressed");
// }
void loop() {
currentMillis = millis();
up.update();
left.update();
right.update();
down.update();
aButton.update();
if (up.isPressed()) Serial.println("Up Button Pressed");
if (right.isPressed()) Serial.println("Right Button Pressed");
if (down.isPressed()) Serial.println("Down Button Pressed");
if (left.isPressed()) Serial.println("Left Button Pressed");
if (aButton.isPressed()) Serial.println("A Button Pressed");
// if (b.isPressed()) Serial.println("B Button Pressed");
if(up.isPressed()){
selectedGame++;
selectedGame %= 2;
}
if(down.isPressed()){
selectedGame--;
selectedGame += 2;
selectedGame %= 2;
}
switch (currentState) {
case MENU:
displayMenu(display);
if (aButton.isPressed() && selectedGame == 0) {
Serial.println("Switching to Snake Game");
currentState = SNAKE_GAME;
snakeGame = new SnakeGame(&display, &up, &down, &left, &right);
snakeGame->start();
}
if (aButton.isPressed() && selectedGame == 1) {
Serial.println("Switching to Flappy Bird");
currentState = FLAPPY_BIRD;
flappyBird = new FlappyBird(&display, &up, &down, &left, &right);
}
break;
case SNAKE_GAME:
snakeGame->update();
snakeGame->draw();
if (aButton.isPressed()) {
Serial.println("Switching to Menu");
currentState = MENU;
}
break;
case FLAPPY_BIRD:
flappyBird->draw();
if (aButton.isPressed()) {
Serial.println("Switching to Menu");
currentState = MENU;
}
break;
}
}