Refactor master to communicate with new archiv
This commit is contained in:
parent
feb7e31cdb
commit
4b6eefae65
@ -1,179 +1,194 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
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 resetPuzzles();
|
||||
|
||||
/* ========================================================= */
|
||||
const uint8_t ARCHIV_ADDR = 0x10;
|
||||
const uint8_t CMD_POWER_OFF = 0x01;
|
||||
const uint8_t CMD_LIGHT_INDEX = 0x02;
|
||||
const uint8_t CMD_DRAWER_INDEX = 0x03;
|
||||
|
||||
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);
|
||||
void setDrawerLight(uint8_t index)
|
||||
{
|
||||
Wire.beginTransmission(ARCHIV_ADDR);
|
||||
Wire.write(CMD_LIGHT_INDEX);
|
||||
Wire.write(index);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
void turnLightsOff()
|
||||
{
|
||||
Wire.beginTransmission(ARCHIV_ADDR);
|
||||
Wire.write(CMD_POWER_OFF);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
void openDrawer(uint8_t index)
|
||||
{
|
||||
Wire.beginTransmission(ARCHIV_ADDR);
|
||||
Wire.write(CMD_DRAWER_INDEX);
|
||||
Wire.write(index);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
/* ========================================================= */
|
||||
const int encoderPinA = A1;
|
||||
const int encoderPinB = A2;
|
||||
const int stepsPerClick = 500;
|
||||
long encoderPostion = 0;
|
||||
uint8_t lastState;
|
||||
long lastClickPosition = 0;
|
||||
uint8_t selectedIndex = 0;
|
||||
void setupEncoder()
|
||||
{
|
||||
lastState = (PINC >> 1) & 0x03;
|
||||
PCICR |= (1 << PCIE1);
|
||||
PCMSK1 |= (1 << PCINT9) | (1 << PCINT10);
|
||||
}
|
||||
void enableEncoder() {
|
||||
PCICR |= (1 << PCIE1);
|
||||
}
|
||||
void disableEncoder() {
|
||||
PCICR &= ~(1 << PCIE1);
|
||||
}
|
||||
|
||||
ISR(PCINT1_vect) {
|
||||
if (!powerOnState) return;
|
||||
|
||||
ISR(PCINT1_vect)
|
||||
{
|
||||
uint8_t state = (PINC >> 1) & 0x03;
|
||||
|
||||
if ((lastState == 0b00 && state == 0b01) ||
|
||||
(lastState == 0b01 && state == 0b11) ||
|
||||
(lastState == 0b11 && state == 0b10) ||
|
||||
(lastState == 0b10 && state == 0b00))
|
||||
position++;
|
||||
{
|
||||
encoderPostion++;
|
||||
}
|
||||
else
|
||||
position--;
|
||||
|
||||
{
|
||||
encoderPostion--;
|
||||
}
|
||||
lastState = state;
|
||||
}
|
||||
|
||||
/* ========================================================= */
|
||||
|
||||
void loop() {
|
||||
checkPowerState();
|
||||
checkSlaves();
|
||||
|
||||
if (powerOnState)
|
||||
checkEncoderPosition();
|
||||
void handleEncoderMovement()
|
||||
{
|
||||
long diff = encoderPostion - lastClickPosition;
|
||||
if (abs(diff) >= stepsPerClick)
|
||||
{
|
||||
resetPuzzles();
|
||||
if (diff > 0)
|
||||
{
|
||||
selectedIndex = (selectedIndex + 1) % 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedIndex = (selectedIndex + 7) % 8;
|
||||
}
|
||||
setDrawerLight(selectedIndex);
|
||||
lastClickPosition = encoderPostion;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========================================================= */
|
||||
const int PIN_POWER_SWITCH_INPUT = 2;
|
||||
const int PIN_POWER_SWITCH_OUTPUT = 12;
|
||||
const unsigned long POWER_STABLE_THRESHOLD = 400;
|
||||
bool powerStableState;
|
||||
bool powerLastRawState;
|
||||
unsigned long powerLastChangeTime = 0;
|
||||
|
||||
void checkPowerState() {
|
||||
bool checkPowerState()
|
||||
{
|
||||
bool rawState = digitalRead(PIN_POWER_SWITCH_INPUT);
|
||||
|
||||
if (rawState != lastRawSwitchState) {
|
||||
lastChangeTime = millis();
|
||||
lastRawSwitchState = rawState;
|
||||
if (rawState != powerLastRawState)
|
||||
{
|
||||
powerLastChangeTime = millis();
|
||||
powerLastRawState = 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);
|
||||
if ((millis() - powerLastChangeTime) > POWER_STABLE_THRESHOLD &&
|
||||
rawState != powerStableState)
|
||||
{
|
||||
powerStableState = rawState;
|
||||
digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerStableState == HIGH);
|
||||
if (powerStableState == HIGH)
|
||||
{
|
||||
setDrawerLight(selectedIndex);
|
||||
enableEncoder();
|
||||
}
|
||||
else
|
||||
sendI2CCommand(CMD_AUSWAHLRAD_LINKS);
|
||||
|
||||
lastClickPosition = position;
|
||||
{
|
||||
turnLightsOff();
|
||||
disableEncoder();
|
||||
}
|
||||
}
|
||||
return (powerStableState == HIGH);
|
||||
}
|
||||
|
||||
/* ========================================================= */
|
||||
const int puzzleSolvedInputPins[3] = {3, 4, 5};
|
||||
const int puzzleResetPins[3] = {7, 8, 9};
|
||||
bool puzzlesSolved[3] = {false, false, false};
|
||||
bool allSolvedSent = false;
|
||||
|
||||
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);
|
||||
void resetPuzzles()
|
||||
{
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
{
|
||||
puzzlesSolved[i] = false;
|
||||
digitalWrite(puzzleResetPins[i], HIGH);
|
||||
delay(30);
|
||||
digitalWrite(puzzleResetPins[i], LOW);
|
||||
}
|
||||
allSolvedSent = false;
|
||||
}
|
||||
|
||||
void checkPuzzleSolvedStatus()
|
||||
{
|
||||
if (allSolvedSent)
|
||||
return;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (!puzzlesSolved[i] && digitalRead(puzzleSolvedInputPins[i]) == HIGH)
|
||||
{
|
||||
puzzlesSolved[i] = true;
|
||||
}
|
||||
}
|
||||
if (puzzlesSolved[0] && puzzlesSolved[1] && puzzlesSolved[2])
|
||||
{
|
||||
openDrawer(selectedIndex);
|
||||
allSolvedSent = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========================================================= */
|
||||
|
||||
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);
|
||||
powerStableState = digitalRead(PIN_POWER_SWITCH_INPUT);
|
||||
powerLastRawState = powerStableState;
|
||||
digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerStableState == HIGH);
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
pinMode(puzzleResetPins[i], OUTPUT);
|
||||
pinMode(puzzleSolvedInputPins[i], INPUT_PULLUP);
|
||||
}
|
||||
|
||||
setupEncoder();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (checkPowerState())
|
||||
{
|
||||
handleEncoderMovement();
|
||||
checkPuzzleSolvedStatus();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user