/* KILIZU Computing Lab — ESP32 Reflex Challenge
A: start / react / replay. L: choose mode / player B / return to menu.
Release both buttons, wait for GREEN, then press. In DUEL, first to 3 wins.
Simulator scores include browser timing: this is a game, not a lab test.
*/
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <esp_system.h>
#include "game.h"
constexpr uint8_t BUTTON_A = 18, BUTTON_B = 23;
constexpr uint8_t LED_R = 25, LED_G = 26, LED_B = 27;
constexpr uint8_t BUZZER = 19;
constexpr uint8_t OLED_SDA = 21, OLED_SCL = 22;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
reflex::Game game;
bool displayReady = false;
uint32_t drawnRevision = UINT32_MAX;
reflex::Phase previousPhase = reflex::Phase::HOME;
void rgb(bool red, bool green, bool blue) {
// Each RGB channel has its own 330-ohm resistor. COM connects to GND.
digitalWrite(LED_R, red);
digitalWrite(LED_G, green);
digitalWrite(LED_B, blue);
}
void line(uint8_t y, const char *text, uint8_t size = 1) {
display.setTextSize(size);
display.setCursor(0, y);
display.print(text);
}
void drawScreen() {
if (!displayReady) return;
using namespace reflex;
char text[32];
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextWrap(false);
if (game.mode() == Mode::DUEL) {
snprintf(text, sizeof(text), "DUEL A %u : %u B", game.score(0), game.score(1));
line(0, text);
} else {
line(0, "REFLEX / SOLO");
}
display.drawFastHLine(0, 11, 128, SSD1306_WHITE);
switch (game.phase()) {
case Phase::HOME:
line(18, "A: START", 2);
line(37, "L: CHANGE MODE");
if (game.mode() == Mode::DUEL) line(52, "First to 3 points");
else if (game.hasBest()) {
snprintf(text, sizeof(text), "Session best: %lu ms", (unsigned long)game.bestMs());
line(52, text);
} else line(52, "Wait for green!");
break;
case Phase::ARM:
line(22, "RELEASE", 2);
line(45, "both buttons");
break;
case Phase::WAIT:
line(22, "WAIT...", 2);
line(45, "Green = press!");
break;
case Phase::GO:
line(22, "GO!", 3);
line(51, game.mode() == Mode::SOLO ? "Press A!" : "Press A or L!");
break;
case Phase::RESULT:
if (game.matchFinished()) {
line(15, "MATCH WINNER");
line(27, game.score(0) >= Game::MATCH_POINTS ? "PLAYER A" : "PLAYER B", 2);
} else if (game.outcome() == Outcome::SCORE) {
line(15, game.mode() == Mode::SOLO ? "YOUR TIME" :
(game.winner() == 0 ? "POINT: PLAYER A" : "POINT: PLAYER B"));
snprintf(text, sizeof(text), "%lu ms", (unsigned long)game.reactionMs());
line(27, text, 2);
} else if (game.outcome() == Outcome::EARLY) {
line(18, "TOO EARLY", 2);
line(39, game.mode() == Mode::SOLO ? "Wait for GREEN" :
(game.winner() == 0 ? "Point: PLAYER A" : "Point: PLAYER B"));
} else if (game.outcome() == Outcome::DOUBLE_EARLY) {
line(18, "BOTH EARLY", 2);
line(39, "Draw / no points");
} else if (game.outcome() == Outcome::DRAW) {
line(18, "DRAW!", 2);
line(39, "Within 15 ms");
} else {
line(18, "TIME UP", 2);
line(39, "No press in 5 s");
}
line(54, game.resultReady() ? "A: AGAIN L: MENU" : "Release both buttons");
break;
}
display.display();
}
void updateOutputs() {
using namespace reflex;
switch (game.phase()) {
case Phase::HOME: rgb(false, false, true); break;
case Phase::ARM: rgb(true, true, false); break;
case Phase::WAIT: rgb(true, false, false); break;
case Phase::GO: rgb(false, true, false); break;
case Phase::RESULT:
if (game.outcome() == Outcome::EARLY || game.outcome() == Outcome::DOUBLE_EARLY)
rgb(true, false, false);
else rgb(false, true, true);
break;
}
if (game.phase() != previousPhase) {
if (game.phase() == Phase::ARM)
game.setWaitMs(random(1500, 4501)); // A new unpredictable wait each round.
if (game.phase() == Phase::GO) tone(BUZZER, 1600, 70);
if (game.phase() == Phase::RESULT) {
const bool error = game.outcome() == Outcome::EARLY ||
game.outcome() == Outcome::DOUBLE_EARLY;
tone(BUZZER, error ? 220 : 1000, error ? 180 : 80);
Serial.printf("Result=%d winner=%d time=%lu ms score A:%u B:%u\n",
int(game.outcome()), int(game.winner()), (unsigned long)game.reactionMs(),
game.score(0), game.score(1));
}
previousPhase = game.phase();
}
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(LED_R, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_B, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Pull-up inputs read HIGH at rest, LOW when their button connects to GND.
Wire.begin(OLED_SDA, OLED_SCL);
displayReady = display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
if (!displayReady) Serial.println("OLED failed: check 3V3, GND, SDA21, SCL22.");
randomSeed(esp_random());
updateOutputs();
Serial.println("KILIZU Reflex Challenge: A=start/react; L=mode/player B/menu.");
}
void loop() {
// No gameplay delay(): keep checking buttons while the state machine waits.
const uint32_t now = millis();
game.tick(now, digitalRead(BUTTON_A) == LOW, digitalRead(BUTTON_B) == LOW);
updateOutputs();
if (game.revision() != drawnRevision) {
drawnRevision = game.revision();
drawScreen(); // Redraw only after a visible change, not on every loop.
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306
ESP32 REFLEX CHALLENGE
OLED SCORE
SDA 21 / SCL 22
RGB SIGNAL
25 / 26 / 27
3 x 330 ohm
Common cathode
SOUND
GPIO 19
PLAYER A
GPIO 18
Release both buttons. Wait for GREEN. Press!
A = start / replay L = mode / menu
PLAYER B
GPIO 23