#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Picopixel.h>

#define ENCODER_CLK A0
#define ENCODER_DT  A1
#define ENCODER_SW  A2

uint8_t volume = 50;
uint8_t bass = 25;
uint8_t treble = 66;

typedef enum {
  SET_VOLUME,
  SET_BASS,
  SET_TREBLE,
} Mode;

Mode mode = SET_VOLUME;

Adafruit_SSD1306 display(128, 64, &Wire, -1);

void nextMode() {
  mode = static_cast<Mode>((mode + 1) % 3);
}

void updateValue(int delta) {
  switch (mode) {
    case SET_VOLUME:
      volume = constrain(volume + delta, 0, 100);
      break;

    case SET_BASS:
      bass = constrain(bass + delta, 0, 100);
      break;

    case SET_TREBLE:
      treble = constrain(treble + delta, 0, 100);
      break;
  }
}

void displayControl(const char *label, uint8_t value, bool isSelected, int yPos) {
  display.setCursor(42, yPos);
  display.print(label);
  display.drawRoundRect(10, yPos + 10, 102, 9, 2, WHITE);
  display.fillRect(11, yPos + 11, value, 7, WHITE);
  if (isSelected) {
    display.setCursor(32, yPos);
    display.print(">");
  }
}

void updateDisplay() {
  display.clearDisplay();
  display.setFont();
  display.setTextColor(1);

  displayControl("Volume", volume, mode == SET_VOLUME, 2);
  displayControl("Bass", bass, mode == SET_BASS, 22);
  displayControl("Treble", treble, mode == SET_TREBLE, 42);

  display.display();
}

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);
  pinMode(ENCODER_SW, INPUT_PULLUP);
  updateDisplay();
}

long int modeLastChanged = 0;
int prevClk = HIGH;
void loop() {
  if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > 300) {
    modeLastChanged = millis();
    nextMode();
    updateDisplay();
  }

  int clk = digitalRead(ENCODER_CLK);
  if (clk != prevClk && clk == LOW) {
    int dt = digitalRead(ENCODER_DT);
    int delta = dt == HIGH ? 5 : -5;
    updateValue(delta);
    updateDisplay();
  }
  prevClk = clk;
}