#include <Arduino.h>
#include <GyverOLED.h>
#include <EncButton2.h>
GyverOLED<SSH1106_128x64, OLED_NO_BUFFER> oled(0x3C);
const int NUM_SLIDERS = 4;
int analogSliderValues[NUM_SLIDERS] = {0};
EncButton2<EB_BTN> button_pin[NUM_SLIDERS];
EncButton2<EB_ENC> encoder[NUM_SLIDERS];
int knobState[NUM_SLIDERS];
int Mute[NUM_SLIDERS];
int knobVal[NUM_SLIDERS];
int mappedvalueslider[NUM_SLIDERS];
long unsigned int remappedvalue[NUM_SLIDERS];
const uint8_t sound_8x8[] PROGMEM = {0x3c, 0x7e, 0xff, 0x00, 0x18, 0x42, 0x3c, 0x00};
const uint8_t browser_8x8[] PROGMEM = {0xff, 0x85, 0x89, 0x91, 0x91, 0x89, 0x85, 0xff};
const uint8_t other_8x8[] PROGMEM = {0x3f, 0xa1, 0xa1, 0xe1, 0xe1, 0xa1, 0xa1, 0x3f};
const uint8_t microphone_8x8[] PROGMEM = {0x1c, 0x30, 0x20, 0xef, 0xef, 0x20, 0x30, 0x1c};
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.setClock(100000);
oled.init(); // инициализация
oled.clear();
oled.home();
oled.update();
//--Кнопки (SW)--//
button_pin[0].setPins(INPUT_PULLUP, 15); //A1
button_pin[1].setPins(INPUT_PULLUP, 16); //A2
button_pin[2].setPins(INPUT_PULLUP, 17); //A3
button_pin[3].setPins(INPUT_PULLUP, 20); //A6
//--Энкодер и Кнопки (DT & CLK)--//
encoder[0].setPins(INPUT_PULLUP, 4, 5);
encoder[1].setPins(INPUT_PULLUP, 6, 7);
encoder[2].setPins(INPUT_PULLUP, 8, 9);
encoder[3].setPins(INPUT_PULLUP, 10, 11);
for (int i = 0; i < NUM_SLIDERS; i++) button_pin[i].tick();
for (int i = 0; i < NUM_SLIDERS; i++){
button_pin[i].click();
analogSliderValues[i] = 1023;
knobState[i] = 0;
Mute[i] = 0;
encoder[i].counter = 51;
}
display_draw();
}
void checkButtons() {
for (int i = 0; i < NUM_SLIDERS; i++) {
if (digitalRead(button_pin[i].click()) == 0 && knobState[i] == 0) {
Mute[i] = !Mute[i];
knobState[i] = 1;
delay(25);
}
if (digitalRead(button_pin[i].click()) == 1) {
knobState[i] = 0;
}
}
}
void checkEncoders() {
for (int i = 0; i < NUM_SLIDERS; i++) {
knobVal[i] = encoder[i].counter;
if (digitalRead(encoder[i].left()) || digitalRead(encoder[i].right()) && knobVal[i] > 0 && knobVal[i] < 102 && Mute[i] == 0) {
analogSliderValues[i] = map(knobVal[i], 0, 102, 0, 1023);
} else if (Mute[i] == 0 && (knobVal[i] >= 102)) {
analogSliderValues[i] = 1023;
encoder[i].counter = 102;
} else if (Mute[i] == 1) {
analogSliderValues[i] = 0;
} else {
analogSliderValues[i] = 0;
encoder[i].counter = 0;
}
}
}
void mappingvalues(){
for(int i = 0; i < 4; i++){
mappedvalueslider[i] = map(analogSliderValues[i], 0, 1023, 0, 97);
remappedvalue[i] = map(analogSliderValues[i], 0, 1023, 0, 100);
}
}
void display_draw(){
oled.clear(); // Очистка буфера
oled.home();
oled.setScale(1);
byte textPos1 = 2;
byte textPos2 = 19;
byte textPos3 = 36;
byte textPos4 = 53;
//рисует 4 рамоки
/*for(int i = 2; i < 54; i= i + 17){
oled.rect(20, i, 97, i + 6, OLED_STROKE);
}*/
//рисует знаки %) рядом с 4 рамками
for(int i = 2; i < 54; i = i + 17){
oled.setCursorXY(120, i);
oled.print("%");
}
//заполняет каждый из 4 индикаторов выполнения их значениями
for(int i = 0; i < 4; i++) {
oled.rect(20, 2, 97, mappedvalueslider[i] + 8, OLED_FILL);
oled.rect(20, 19, 97, mappedvalueslider[i] + 25, OLED_FILL);
oled.rect(20, 36, 97, mappedvalueslider[i] + 42, OLED_FILL);
oled.rect(20, 53, 97, mappedvalueslider[i] + 59, OLED_FILL);
}
//Выведите процентное значение каждого ползунка
for(int i = 0; i < 4; i++) {
if(remappedvalue[i] < 10){
oled.setCursorXY(113, 2 + (17 * i));
} else if(remappedvalue[i] > 9 && remappedvalue[i] < 100) {
oled.setCursorXY(107, 2 + (17 * i));
} else if(remappedvalue[i] == 100) {
oled.setCursorXY(101, 2 + (17 * i));
}
oled.print(remappedvalue[i]);
}
oled.drawBitmap(6, textPos1, sound_8x8, 8, 8);
oled.drawBitmap(6, textPos2, browser_8x8, 8, 8);
oled.drawBitmap(6, textPos3, other_8x8, 8, 8);
oled.drawBitmap(6, textPos4, microphone_8x8, 8, 8);
oled.update();
}
void sendSliderValues() {
String builtString = String("");
for (int i = 0; i < NUM_SLIDERS; i++) {
builtString += String((int)analogSliderValues[i]);
if (i < NUM_SLIDERS - 1) {
builtString += String("|");
}
}
Serial.println(builtString);
}
void loop(){
mappingvalues();
for (int i = 0; i < NUM_SLIDERS; i++) encoder[i].tick();
for (int i = 0; i < NUM_SLIDERS; i++) button_pin[i].tick();
for (int i = 0; i < NUM_SLIDERS; i++) {
knobVal[i] = encoder[i].counter;
}
checkButtons();
checkEncoders();
sendSliderValues();
}