#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 = 0;
uint8_t bass = 25;
uint8_t treble = 66;
typedef enum {
SET_VOLUME,
SET_BASS,
SET_TREBLE,
} Mode;
Mode mode = SET_BASS;
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();
}
// 'logo-iot', 128x64px
const unsigned char epd_bitmap_logo_iot [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0c, 0x7f, 0x00, 0x71, 0xff, 0xf0, 0x00, 0x0c, 0x00, 0x3f, 0xff, 0xf8, 0x1f, 0xe0, 0x7f, 0xfe,
0x0c, 0x7f, 0xc0, 0x71, 0xff, 0xfc, 0x00, 0x8e, 0x20, 0x3f, 0xff, 0xf8, 0x3f, 0xf0, 0x7f, 0xfe,
0x0c, 0x63, 0xf0, 0x71, 0x80, 0x1e, 0x01, 0x8e, 0x30, 0x30, 0x00, 0x38, 0x78, 0x7c, 0x03, 0x80,
0x0c, 0x60, 0x7c, 0x71, 0x80, 0x0f, 0x03, 0x8e, 0x38, 0x30, 0x00, 0x30, 0xe0, 0x1e, 0x03, 0x80,
0x0c, 0x60, 0x1e, 0x71, 0x80, 0x03, 0x07, 0x0e, 0x18, 0x30, 0x00, 0x71, 0xc0, 0x0e, 0x03, 0x80,
0x0c, 0x60, 0x0f, 0x71, 0x80, 0x03, 0x87, 0x0e, 0x1c, 0x30, 0x00, 0x61, 0x80, 0x07, 0x03, 0x80,
0x0c, 0x60, 0x07, 0x71, 0x80, 0x01, 0x8e, 0x0e, 0x0c, 0x30, 0x00, 0xe3, 0x80, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x03, 0xf1, 0x80, 0x01, 0x8e, 0x0e, 0x0e, 0x30, 0x07, 0xc3, 0x00, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x01, 0xf1, 0x80, 0x01, 0xcc, 0x0e, 0x0e, 0x3f, 0xff, 0x83, 0x00, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x00, 0xf1, 0x80, 0x01, 0xcc, 0x0c, 0x0e, 0x3f, 0xff, 0x83, 0x00, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x00, 0xf1, 0x80, 0x01, 0x8e, 0x00, 0x0e, 0x38, 0x07, 0xc3, 0x00, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x00, 0xf1, 0x80, 0x01, 0x8e, 0x00, 0x0e, 0x30, 0x01, 0xe3, 0x80, 0x03, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0x80, 0x03, 0x86, 0x00, 0x1c, 0x30, 0x00, 0xe3, 0x80, 0x07, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0x80, 0x03, 0x07, 0x00, 0x1c, 0x30, 0x00, 0x71, 0xc0, 0x06, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0x80, 0x07, 0x03, 0x80, 0x38, 0x30, 0x00, 0x31, 0xe0, 0x0e, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0x80, 0x1e, 0x01, 0xe0, 0xf0, 0x30, 0x00, 0x38, 0xf8, 0x3c, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0xff, 0xfc, 0x00, 0xff, 0xe0, 0x3f, 0xff, 0xf8, 0x3f, 0xf8, 0x03, 0x80,
0x0c, 0x60, 0x00, 0x71, 0xff, 0xf0, 0x00, 0x7f, 0xc0, 0x3f, 0xff, 0xf8, 0x1f, 0xe0, 0x03, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x80, 0x00, 0x78, 0x00, 0x18, 0x00, 0x3f, 0x80, 0x03, 0xfc, 0x01, 0x00, 0x60, 0x18, 0x08,
0x03, 0x80, 0x01, 0xfe, 0x00, 0x1c, 0x00, 0x3f, 0xe0, 0x03, 0xfc, 0x01, 0x80, 0x60, 0x0c, 0x18,
0x03, 0xc0, 0x03, 0x86, 0x00, 0x3c, 0x00, 0x38, 0x70, 0x03, 0x00, 0x01, 0xc0, 0xe0, 0x0e, 0x30,
0x06, 0xc0, 0x07, 0x00, 0x00, 0x36, 0x00, 0x30, 0x38, 0x03, 0x00, 0x01, 0xe0, 0xe0, 0x06, 0x30,
0x06, 0x60, 0x06, 0x00, 0x00, 0x66, 0x00, 0x30, 0x18, 0x03, 0x00, 0x01, 0xe1, 0xe0, 0x07, 0x60,
0x0c, 0x60, 0x06, 0x00, 0x00, 0x67, 0x00, 0x30, 0x18, 0x03, 0xf8, 0x01, 0xf3, 0x60, 0x03, 0xe0,
0x0c, 0x30, 0x06, 0x00, 0x00, 0xc3, 0x00, 0x30, 0x18, 0x03, 0xf8, 0x01, 0xb3, 0x60, 0x01, 0xc0,
0x0f, 0xf0, 0x06, 0x00, 0x00, 0xff, 0x00, 0x30, 0x18, 0x03, 0x00, 0x01, 0x9e, 0x60, 0x01, 0x80,
0x1f, 0xf8, 0x03, 0x00, 0x01, 0xff, 0x80, 0x30, 0x38, 0x03, 0x00, 0x01, 0x9c, 0x60, 0x01, 0x80,
0x18, 0x18, 0x03, 0x86, 0x01, 0x81, 0x80, 0x38, 0xf0, 0x03, 0x00, 0x01, 0x8c, 0x60, 0x01, 0x80,
0x30, 0x18, 0x01, 0xfc, 0x01, 0x81, 0xc0, 0x3f, 0xe0, 0x03, 0xfc, 0x01, 0x80, 0x60, 0x01, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x10, 0x00, 0x10, 0x90, 0x08, 0x01, 0x20, 0x02, 0x00, 0x00, 0x20, 0x48, 0x10, 0x90, 0x00,
0x02, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08, 0x00, 0x02, 0x00, 0x01, 0x20, 0x09, 0x00, 0x10, 0x00,
0x00, 0x00, 0x03, 0x80, 0x10, 0x00, 0x12, 0x70, 0x02, 0x00, 0x01, 0x20, 0x09, 0x80, 0x10, 0x00,
0x00, 0x18, 0x00, 0x18, 0x18, 0x80, 0x00, 0x00, 0x00, 0x30, 0x06, 0x20, 0x00, 0x48, 0x18, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
updateDisplay();
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_logo_iot, 128, 64, WHITE);
display.display();
}
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;
}