#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Picopixel.h>
#include <Wire.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;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

static const uint8_t  PROGMEM indobot[] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xc7, 0x01, 0xff, 0x1c, 0x00, 0x1f, 0xff, 0xf3, 0xff, 0xc0, 0x00, 0x07, 0xf0, 0x1f, 0xc0, 0x01, 
	0xc7, 0x00, 0x7f, 0x1c, 0x00, 0x07, 0xff, 0xf3, 0xdf, 0xc0, 0x00, 0x07, 0xc0, 0x07, 0xc0, 0x01, 
	0xc7, 0x18, 0x1f, 0x1c, 0x7f, 0xc3, 0xfe, 0xf3, 0xcf, 0xcf, 0xff, 0xc7, 0x87, 0xc3, 0xfe, 0x7f, 
	0xc7, 0x3f, 0x0f, 0x1c, 0x7f, 0xf1, 0xfc, 0xf3, 0xc7, 0xcf, 0xff, 0xcf, 0x1f, 0xf1, 0xfe, 0x7f, 
	0xc7, 0x3f, 0xc3, 0x1c, 0x7f, 0xf8, 0xf8, 0xf3, 0xc7, 0xcf, 0xff, 0xce, 0x3f, 0xf8, 0xfe, 0x7f, 
	0xc7, 0x3f, 0xe1, 0x1c, 0x7f, 0xfc, 0xf9, 0xf3, 0xe3, 0xcf, 0xff, 0x8e, 0x7f, 0xfc, 0xfe, 0x7f, 
	0xc7, 0x3f, 0xf9, 0x1c, 0x7f, 0xfc, 0xf3, 0xf3, 0xf3, 0xcf, 0xff, 0x1c, 0x7f, 0xfc, 0xfe, 0x7f, 
	0xc7, 0x3f, 0xf8, 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	0xc7, 0x3f, 0xff, 0x9c, 0x00, 0x7f, 0xff, 0xf3, 0xff, 0xc0, 0x00, 0x07, 0xfc, 0x7f, 0xfe, 0x7f, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xf8, 0xff, 0xfe, 0x07, 0xff, 0xc7, 0xff, 0xc0, 0x3f, 0xfc, 0x03, 0xfe, 0x7f, 0x9f, 0xf3, 0xf3, 
	0xf8, 0x7f, 0xfc, 0x03, 0xff, 0xc7, 0xff, 0xc0, 0x1f, 0xfc, 0x03, 0xfe, 0x3f, 0x9f, 0xf9, 0xe7, 
	0xf0, 0x7f, 0xf9, 0xff, 0xff, 0x83, 0xff, 0xcf, 0x8f, 0xfc, 0xff, 0xfe, 0x3f, 0x1f, 0xf9, 0xe7, 
	0xf3, 0x7f, 0xf1, 0xff, 0xff, 0x9b, 0xff, 0xcf, 0xc7, 0xfc, 0xff, 0xfe, 0x1f, 0x1f, 0xfc, 0xcf, 
	0xe3, 0x3f, 0xf3, 0xff, 0xff, 0x39, 0xff, 0xcf, 0xe7, 0xfc, 0x7f, 0xfe, 0x0e, 0x1f, 0xfe, 0x1f, 
	0xe7, 0x3f, 0xf3, 0xff, 0xff, 0x39, 0xff, 0xcf, 0xe7, 0xfc, 0x07, 0xfe, 0x4c, 0xdf, 0xfe, 0x1f, 
	0xe7, 0x9f, 0xf3, 0xff, 0xff, 0x38, 0xff, 0xcf, 0xc7, 0xfc, 0xff, 0xfe, 0x64, 0xdf, 0xff, 0x3f, 
	0xc0, 0x1f, 0xf9, 0xff, 0xfe, 0x00, 0xff, 0xcf, 0xcf, 0xfc, 0xff, 0xfe, 0x71, 0xdf, 0xff, 0x3f, 
	0xcf, 0x8f, 0xf8, 0xff, 0xfe, 0x7c, 0xff, 0xcf, 0x8f, 0xfc, 0xff, 0xfe, 0x73, 0xdf, 0xff, 0x3f, 
	0x9f, 0xcf, 0xfc, 0x03, 0xfc, 0xfe, 0x7f, 0xc0, 0x1f, 0xfc, 0x03, 0xfe, 0x7f, 0xdf, 0xff, 0x3f, 
	0x9f, 0xef, 0xff, 0x0f, 0xfd, 0xff, 0x7f, 0xc0, 0xff, 0xfc, 0x03, 0xfe, 0x7f, 0xdf, 0xff, 0xbf, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xf7, 0xff, 0xff, 0xfd, 0xdf, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xbd, 0xff, 0xff, 0xff, 
	0xf7, 0xff, 0xbf, 0xff, 0xfe, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xbb, 0xff, 0xe7, 0xff, 0xdf, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};


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

Mode mode = SET_VOLUME;

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(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);
  display.drawBitmap(0, 0, indobot, 128, 64, 1);
  display.display();
  delay(2000);
  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;
}