#include "SPI.h"
#include "Wire.h"
#include "SH1106Wire.h"
#include "AiEsp32RotaryEncoder.h"
#include "OLEDDisplayUi.h"
#include "images.h"
#define ROTARY_ENCODER_A_PIN 10
#define ROTARY_ENCODER_B_PIN 11
#define ROTARY_ENCODER_BUTTON_PIN 12
#define ROTARY_ENCODER_STEPS 4
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
SH1106Wire display(0x3c, 8, 9);
OLEDDisplayUi ui ( &display );
unsigned long shortPressAfterMiliseconds = 50;
unsigned long longPressAfterMiliseconds = 600;
void shortPress() {
Serial.println("Button SHORT press");
Serial.println(ui.getUiState()->frameState);
}
void longPress() {
Serial.println("Button LONG press");
}
void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
void encoderButton() {
static unsigned long lastTimeButtonDown = 0;
static bool wasButtonDown = false;
static bool wasLong = false;
bool isEncoderButtonDown = rotaryEncoder.isEncoderButtonDown();
if ( isEncoderButtonDown ) {
if ( !wasButtonDown ) {
lastTimeButtonDown = millis();
wasButtonDown = true;
}
if ( !wasLong ) {
if ( millis() - lastTimeButtonDown >= longPressAfterMiliseconds ) {
longPress();
wasLong = true;
}
}
return;
}
if ( wasButtonDown && !wasLong ) {
if ( millis() - lastTimeButtonDown >= shortPressAfterMiliseconds ) {
shortPress();
wasLong = false;
}
}
wasButtonDown = false;
wasLong = false;
}
void encoderLoop() {
if (rotaryEncoder.encoderChanged()) {
static int8_t prev = 0;
int8_t curr = rotaryEncoder.readEncoder();
if ( curr > prev ) {
ui.nextFrame();
} else if ( curr < prev ) {
ui.previousFrame();
}
prev = rotaryEncoder.readEncoder();
}
encoderButton();
}
void frame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_24);
display->drawString(64 + x, 19 + y, "Presets");
}
void frame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_24);
display->drawString(64 + x, 19 + y, "Precise");
}
FrameCallback frames[] = { frame1, frame2 };
int frameCount = 2;
void setupMainMenu() {
ui.setTargetFPS(60);
ui.setActiveSymbol(activeSymbol);
ui.setInactiveSymbol(inactiveSymbol);
ui.setIndicatorPosition(TOP);
ui.setIndicatorDirection(LEFT_RIGHT);
ui.setFrameAnimation(SLIDE_LEFT);
ui.setFrames(frames, frameCount);
ui.disableAutoTransition();
ui.setTimePerTransition(60);
ui.init();
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(ROTARY_ENCODER_A_PIN, INPUT_PULLUP);
pinMode(ROTARY_ENCODER_B_PIN, INPUT_PULLUP);
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setAcceleration(80);
setupMainMenu();
display.flipScreenVertically();
}
void loop() {
encoderLoop();
ui.update();
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1