#include <Arduino.h>
#include <Bounce2.h>
#include <TM1637Display.h>
#include <U8g2lib.h>
#include <FastLED.h>
//#region pin map
#define LED_DATA 4 // WS2812B data pin
#define LED_COUNT 5 // number of LEDs
#define SEVSEG_CLK 16 // 7-segment display CLK
#define SEVSEG_DIO 17 // 7-segment display DIO
#define ENCODER_CLK 14 // rotary encoder CLK
#define ENCODER_DT 12 // rotary encoder DT
#define ENCODER_SW 13 // rotary encoder SW
#define SWITCH_OL 27 // outer left
#define SWITCH_IL 26 // inner left
#define SWITCH_M 25 // middle
#define SWITCH_IR 33 // inner right
#define SWITCH_OR 32 // outer right
//#endregion
//#region globals
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
TM1637Display sevseg{SEVSEG_CLK, SEVSEG_DIO};
Bounce encoderSW{ENCODER_SW, 10};
Bounce switchOL{SWITCH_OL, 10};
Bounce switchIL{SWITCH_IL, 10};
Bounce switchM{SWITCH_M, 10};
Bounce switchIR{SWITCH_IR, 10};
Bounce switchOR{SWITCH_OR, 10};
CRGB leds[LED_COUNT];
//#endregion
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.clearBuffer();
u8g2.setCursor(0, 20);
u8g2.print("Hello, ESP32!");
u8g2.sendBuffer();
sevseg.setBrightness(0x0f);
encoderSW.attach(ENCODER_SW, INPUT_PULLUP);
switchOL.attach(SWITCH_OL, INPUT_PULLUP);
switchIL.attach(SWITCH_IL, INPUT_PULLUP);
switchM.attach(SWITCH_M, INPUT_PULLUP);
switchIR.attach(SWITCH_IR, INPUT_PULLUP);
switchOR.attach(SWITCH_OR, INPUT_PULLUP);
FastLED.addLeds<WS2812B, LED_DATA, GRB>(leds, LED_COUNT);
}
uint16_t hexMap[16] {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
uint8_t curHex = 0;
void loop() {
if (encoderSW.update() && encoderSW.fell()) {
curHex = (curHex + 1) % 16;
}
for (auto &item: leds) {
item = CRGB::Black;
}
if (switchOL.update() && switchOL.fell()) {
leds[0] = CRGB::Red;
}
if (switchIL.update() && switchIL.fell()) {
leds[1] = CRGB::Green;
}
if (switchM.update() && switchM.fell()) {
leds[2] = CRGB::Blue;
}
if (switchIR.update() && switchIR.fell()) {
leds[3] = CRGB::Yellow;
}
if (switchOR.update() && switchOR.fell()) {
leds[4] = CRGB::Purple;
}
FastLED.show();
sevseg.showNumberDec(curHex, false);
u8g2.clearBuffer();
u8g2.setCursor(0, 20);
u8g2.print("Hello, ESP32!");
u8g2.setCursor(0, 40);
u8g2.print(millis() / 1000);
u8g2.sendBuffer();
delay(10);
}