#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2_1(U8G2_R0, /* clock=*/ A3, /* data=*/ A2, /* reset=*/ U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2_2(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE);
// Globale Variable für das Display in der drawMenu-Funktion
U8G2_SSD1306_128X64_NONAME_F_SW_I2C *currentDisplay;
#define ENCODER_CLK 2
#define ENCODER_DT 3
const int numMenuItems = 3;
const char* menuItems[numMenuItems] = {"Streamer", "Computer", "BluRay"};
const int relayPin1 = 4; // Relay channel 1
const int relayPin2 = 5; // Relay channel 2
const int relayPin3 = 6; // Relay channel 3
int currentMenuItem = 0;
int lastClk = HIGH;
// Pin für das Potentiometer
const int potentiometerPin = A0;
void displayVolume(int volumePercent, U8G2_SSD1306_128X64_NONAME_F_SW_I2C &display) {
display.clearBuffer();
display.setFont(u8g2_font_chargen_92_mf);
char volumeText[20]; // Genügend Platz für den Text
snprintf(volumeText, sizeof(volumeText), "Vol: %d%%", volumePercent);
int textWidth = display.getUTF8Width(volumeText);
int x = (128 - textWidth) / 2;
int y = (64 + display.getAscent() - display.getDescent()) / 2;
display.setCursor(x, y);
display.print(volumeText);
display.sendBuffer();
}
void drawMenu() {
currentDisplay->clearBuffer();
currentDisplay->setFont(u8g2_font_chargen_92_mf);
int textWidth = currentDisplay->getUTF8Width(menuItems[currentMenuItem]);
int x = (128 - textWidth) / 2;
int y = (64 + currentDisplay->getAscent() - currentDisplay->getDescent()) / 2;
currentDisplay->drawStr(x, y, menuItems[currentMenuItem]);
currentDisplay->sendBuffer();
}
void updateVolumeScreen() {
int potValue = analogRead(potentiometerPin);
int volumePercent = map(potValue, 0, 1023, 0, 100);
// Funktion displayVolume wird aufgerufen für u8g2_2
displayVolume(volumePercent, u8g2_2);
delay(100);
}
void updateEncoder() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
currentMenuItem = (currentMenuItem + 1) % numMenuItems;
drawMenu();
updateRelayChannels();
}
if (newClk == LOW && dtValue == LOW) {
currentMenuItem = (currentMenuItem - 1 + numMenuItems) % numMenuItems;
drawMenu();
updateRelayChannels();
}
}
}
void updateRelayChannels() {
digitalWrite(relayPin1, currentMenuItem == 0 ? HIGH : LOW);
digitalWrite(relayPin2, currentMenuItem == 1 ? HIGH : LOW);
digitalWrite(relayPin3, currentMenuItem == 2 ? HIGH : LOW);
}
void setup() {
u8g2_1.begin();
u8g2_2.begin();
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
// Setze alle Relais auf LOW beim Start
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_DT), updateEncoder, CHANGE);
// Setze den initialen Wert von currentDisplay auf u8g2_1
currentDisplay = &u8g2_1;
drawMenu();
updateRelayChannels();
}
void loop() {
// Hier könnte anderer Code stehen, der in der Hauptschleife ausgeführt werden soll
// ...
// Beispiel: drawMenu wird aufgerufen für u8g2_1
drawMenu();
// Hier könnte weiterer Code stehen, der in der Hauptschleife ausgeführt werden soll
// ...
// Beispiel: updateVolumeScreen wird aufgerufen
updateVolumeScreen();
}