/*
  Rotary Encoder volume control example

  Usage: rotate the knob to control the value,
  click the knob to change between volume/bass/treble.

  https://wokwi.com/arduino/projects/304919215794553409

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

#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;

typedef enum {
  SET_VOLUME,
  SET_BASS,
  SET_TREBLE,
} 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_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;
  }
}



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

  display.setCursor(42, 2);
  display.print("Volume");
  display.drawRoundRect(10, 12, 102, 9, 2, WHITE);
  display.fillRect(11, 13, volume, 7, WHITE);
  if (mode == SET_VOLUME) {
    display.setCursor(32, 2);
    display.print(">");
  }

  display.setCursor(48, 22);
  display.print("Bass");
  display.drawRoundRect(10, 32, 102, 9, 2, WHITE);
  display.fillRect(11, 33, bass, 7, WHITE);
  if (mode == SET_BASS) {
    display.setCursor(38, 22);
    display.print(">");
  }

  display.setCursor(42, 42);
  display.print("Treble");
  display.drawRoundRect(10, 52, 102, 9, 2, WHITE);
  display.fillRect(11, 53, treble, 7, WHITE);
  if (mode == SET_TREBLE) {
    display.setCursor(32, 42);
    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();
}

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;
}
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:GND.2
uno:RESET.2
uno:0
uno:1
uno:13
uno:3.3V
uno:AREF
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
uno:A6
uno:A7
uno:5V
uno:RESET
uno:GND.1
uno:VIN
uno:12.2
uno:5V.2
uno:13.2
uno:11.2
uno:RESET.3
uno:GND.3
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND
oled1:DATA
oled1:CLK
oled1:DC
oled1:RST
oled1:CS
oled1:3V3
oled1:VIN
oled1:GND