From feb7e31cdb62c722c7e24b5dfb7ee37083173740 Mon Sep 17 00:00:00 2001 From: C0d3v Date: Thu, 15 Jan 2026 17:23:15 +0100 Subject: [PATCH] Add master wheel code --- terminal/master/.gitignore | 5 + terminal/master/platformio.ini | 14 +++ terminal/master/src/main.cpp | 179 +++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 terminal/master/.gitignore create mode 100644 terminal/master/platformio.ini create mode 100644 terminal/master/src/main.cpp diff --git a/terminal/master/.gitignore b/terminal/master/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/terminal/master/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/terminal/master/platformio.ini b/terminal/master/platformio.ini new file mode 100644 index 0000000..8ba220f --- /dev/null +++ b/terminal/master/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega328new] +platform = atmelavr +board = nanoatmega328new +framework = arduino diff --git a/terminal/master/src/main.cpp b/terminal/master/src/main.cpp new file mode 100644 index 0000000..66306a8 --- /dev/null +++ b/terminal/master/src/main.cpp @@ -0,0 +1,179 @@ +#include + +const int PIN_POWER_SWITCH_INPUT = 2; +const int PIN_POWER_SWITCH_OUTPUT = 12; + +// I2C +const uint8_t ARCHIV_ADDR = 0x10; + +// I2C-Befehle +const uint8_t CMD_LED_LOCK_TOGGLE = 0x01; +const uint8_t CMD_ALLES_GESCHAFFT = 0x02; +const uint8_t CMD_AUSWAHLRAD_LINKS = 0x03; +const uint8_t CMD_AUSWAHLRAD_RECHTS = 0x04; + +// Randomizer-Ausgänge +const int randomizer_modul1_pin = 7; +const int randomizer_modul2_pin = 8; +const int randomizer_modul3_pin = 9; + +// Modul-Eingänge (D3 = Modul 1) +const int slavePins[3] = {3, 4, 5}; + +// Drehgeber A1 / A2 +const int encoderPinA = A1; +const int encoderPinB = A2; + +volatile long position = 0; +volatile uint8_t lastState; +const int stepsPerClick = 500; +long lastClickPosition = 0; + +// Zustände +bool powerOnState = true; + +// Schalterfilter +bool stableSwitchState; +bool lastRawSwitchState; +unsigned long lastChangeTime = 0; +const unsigned long stableTime = 150; + +// Lösungsstatus +bool slaveSolved[3] = {false, false, false}; +bool allSolvedSent = false; + +/* ========================================================= */ + +void setup() { + Wire.begin(); + + pinMode(encoderPinA, INPUT_PULLUP); + pinMode(encoderPinB, INPUT_PULLUP); + pinMode(PIN_POWER_SWITCH_INPUT, INPUT_PULLUP); + pinMode(PIN_POWER_SWITCH_OUTPUT, OUTPUT); + digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerOnState); + + pinMode(randomizer_modul1_pin, OUTPUT); + pinMode(randomizer_modul2_pin, OUTPUT); + pinMode(randomizer_modul3_pin, OUTPUT); + + for (int i = 0; i < 3; i++) + pinMode(slavePins[i], INPUT_PULLUP); + + lastState = (PINC >> 1) & 0x03; + + PCICR |= (1 << PCIE1); + PCMSK1 |= (1 << PCINT9) | (1 << PCINT10); + + stableSwitchState = digitalRead(PIN_POWER_SWITCH_INPUT); + lastRawSwitchState = stableSwitchState; + powerOnState = (stableSwitchState == HIGH); +} + +/* ========================================================= */ + +ISR(PCINT1_vect) { + if (!powerOnState) return; + + uint8_t state = (PINC >> 1) & 0x03; + + if ((lastState == 0b00 && state == 0b01) || + (lastState == 0b01 && state == 0b11) || + (lastState == 0b11 && state == 0b10) || + (lastState == 0b10 && state == 0b00)) + position++; + else + position--; + + lastState = state; +} + +/* ========================================================= */ + +void loop() { + checkPowerState(); + checkSlaves(); + + if (powerOnState) + checkEncoderPosition(); +} + +/* ========================================================= */ + +void checkPowerState() { + bool rawState = digitalRead(PIN_POWER_SWITCH_INPUT); + + if (rawState != lastRawSwitchState) { + lastChangeTime = millis(); + lastRawSwitchState = rawState; + } + + if ((millis() - lastChangeTime) > stableTime && + rawState != stableSwitchState) { + + stableSwitchState = rawState; + powerOnState = (stableSwitchState == HIGH); + digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerOnState); + sendI2CCommand(CMD_LED_LOCK_TOGGLE); + } +} + +/* ========================================================= */ + +void sendI2CCommand(uint8_t cmd) { + Wire.beginTransmission(ARCHIV_ADDR); + Wire.write(cmd); + Wire.endTransmission(); +} + +/* ========================================================= */ + +void checkSlaves() { + if (allSolvedSent) return; + + for (int i = 0; i < 3; i++) { + if (!slaveSolved[i] && digitalRead(slavePins[i]) == HIGH) + slaveSolved[i] = true; + } + + if (slaveSolved[0] && slaveSolved[1] && slaveSolved[2]) { + sendI2CCommand(CMD_ALLES_GESCHAFFT); + allSolvedSent = true; + + // 🔒 Zyklus beenden → alles zurücksetzen + //for (int i = 0; i < 3; i++) + //slaveSolved[i] = false; + slaveSolved[0] = false; + } +} + +/* ========================================================= */ + +void checkEncoderPosition() { + long diff = position - lastClickPosition; + + if (abs(diff) >= stepsPerClick) { + + sendRandomizerPulse(); + + if (diff > 0) + sendI2CCommand(CMD_AUSWAHLRAD_RECHTS); + else + sendI2CCommand(CMD_AUSWAHLRAD_LINKS); + + lastClickPosition = position; + } +} + +/* ========================================================= */ + +void sendRandomizerPulse() { + digitalWrite(randomizer_modul1_pin, HIGH); + digitalWrite(randomizer_modul2_pin, HIGH); + digitalWrite(randomizer_modul3_pin, HIGH); + delay(30); + digitalWrite(randomizer_modul1_pin, LOW); + digitalWrite(randomizer_modul2_pin, LOW); + digitalWrite(randomizer_modul3_pin, LOW); + allSolvedSent = false; +} \ No newline at end of file