#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Rozlišení OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
// Pin tlačítka (připojeno k zemi při sepnutí)
#define BUTTON_PIN 19 // můžeš změnit na jiný GPIO
// Prototyp ISR, aby ho Arduino IDE našlo
void IRAM_ATTR onButtonPressed();
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
enum Screen {
INTRO,
RPM_SCREEN
};
volatile Screen currentScreen = INTRO;
void setup() {
Serial.begin(115200);
delay(100);
// I2C piny ESP32
Wire.begin(21, 22);
// Inicializace OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Tlačítko jako vstup s pull-up
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Přerušení na sestupnou hranu (stisk = LOW)
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onButtonPressed, FALLING);
// První vykreslení
drawCurrentScreen();
}
void loop() {
// Vše řídí přerušení a drawCurrentScreen()
delay(10);
}
// ISR: přepne obrazovku a překreslí
void IRAM_ATTR onButtonPressed() {
static uint32_t lastInterrupt = 0;
uint32_t now = millis();
if (now - lastInterrupt < 200) return;
lastInterrupt = now;
currentScreen = (currentScreen == INTRO) ? RPM_SCREEN : INTRO;
drawCurrentScreen();
}
// funkce pro vykreslení podle stavu
void drawCurrentScreen() {
display.clearDisplay();
const char* msg = (currentScreen == INTRO) ? "DRTICKA" : "RPM:";
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(msg, 0, 0, &x1, &y1, &w, &h);
int16_t x = (SCREEN_WIDTH - w) / 2;
int16_t y = (SCREEN_HEIGHT - h) / 2;
display.setCursor(x, y);
display.print(msg);
display.display();
}