/*
  Rotary Encoder multiple variable control example

  https://wokwi.com/projects/417720757017823233
  
  Usage: rotate the knob to control the value,
  click the knob to change between variables.

  See also 
  https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754/9
  
  Built from:
  https://wokwi.com/arduino/projects/304919215794553409

  Released under the MIT license.
  Copyright (C) 2021, Uri Shaked.
  Copyright (C) 2024, DaveX.
*/

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

#define ENCODER_CLK 2
#define ENCODER_DT  3
#define ENCODER_SW  4

uint8_t volume = 50;
uint8_t bass = 25;
uint8_t treble = 66;
uint8_t hue = 66;
uint8_t sat = 66;
uint8_t value = 66;

typedef enum {
  SET_VOLUME,
  SET_BASS,
  SET_TREBLE,
  SET_HUE,
  SET_SAT,
  SET_XVALUE,
} Mode;

Mode mode = SET_VOLUME;

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

void nextMode() {
  switch (mode) {
    case SET_VOLUME:
      mode = SET_BASS;
      break;

    case SET_BASS:
      mode = SET_TREBLE;
      break;

    case SET_TREBLE:
      mode = SET_HUE;
      break;

    case SET_HUE:
      mode = SET_SAT;
      break;

    case SET_SAT:
      mode = SET_XVALUE;
      break;

    case SET_XVALUE:
      mode = SET_VOLUME;
      break;
  }
}

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;

    case SET_HUE:
      hue = constrain(hue + delta, 0, 255);
      break;

    case SET_SAT:
      hue = constrain(sat + delta, 0, 255);
      break;

    case SET_XVALUE:
      hue = constrain(value + delta, 0, 255);
      break;
  }
}

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

  display.setCursor(10, 2);
  display.print("V");
  display.drawRoundRect(20, 2, 102, 9, 2, WHITE);
  display.fillRect(21, 3, volume, 7, WHITE);
  if (mode == SET_VOLUME) {
    display.setCursor(0, 2);
    display.print(">");
  }

  display.setCursor(10, 12);
  display.print("B");
  display.drawRoundRect(20, 12, 102, 9, 2, WHITE);
  display.fillRect(21, 13, bass, 7, WHITE);
  if (mode == SET_BASS) {
    display.setCursor(0, 12);
    display.print(">");
  }

  display.setCursor(10, 22);
  display.print("T");
  display.drawRoundRect(20, 22, 102, 9, 2, WHITE);
  display.fillRect(21, 23, treble, 7, WHITE);
  if (mode == SET_TREBLE) {
    display.setCursor(0, 22);
    display.print(">");
  }

  display.setCursor(10, 32);
  display.print("H");
  display.drawRoundRect(20, 32, 102, 9, 2, WHITE);
  display.fillRect(21, 33, hue, 7, WHITE);
  if (mode == SET_HUE) {
    display.setCursor(0, 32);
    display.print(">");
  }

  display.setCursor(10, 42);
  display.print("S");
  display.drawRoundRect(20, 42, 102, 9, 2, WHITE);
  display.fillRect(21, 43, sat, 7, WHITE);
  if (mode == SET_SAT) {
    display.setCursor(0, 42);
    display.print(">");
  }

  display.setCursor(10, 52);
  display.print("X");
  display.drawRoundRect(20, 52, 102, 9, 2, WHITE);
  display.fillRect(21, 53, value, 7, WHITE);
  if (mode == SET_XVALUE) {
    display.setCursor(0, 52);
    display.print(">");
  }

  display.display();
}

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

uint32_t 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;
}