Compare commits

...

2 Commits

Author SHA1 Message Date
876b2f0db6 Use bit based communication with drawers 2026-01-15 20:04:02 +01:00
7a77347bc2 Use bit based communication for I2C 2026-01-15 20:00:26 +01:00
2 changed files with 19 additions and 16 deletions

View File

@ -3,9 +3,12 @@
const uint8_t I2C_ADDR = 0x10; const uint8_t I2C_ADDR = 0x10;
const uint8_t CMD_POWER_OFF = 0x01; const uint8_t BITMASK_CMD = 0b00011000;
const uint8_t CMD_LIGHT_INDEX = 0x02; const uint8_t BITMASK_PAYLOAD = 0b00000111;
const uint8_t CMD_DRAWER_INDEX = 0x03;
const uint8_t CMD_POWER_OFF = 0b00001000;
const uint8_t CMD_LIGHT_INDEX = 0b00010000;
const uint8_t CMD_DRAWER_INDEX = 0b00011000;
struct Schublade { struct Schublade {
int schlossPin; int schlossPin;
@ -47,22 +50,20 @@ void openLock(uint8_t indexOn) {
void onI2CReceive(int len) { void onI2CReceive(int len) {
while (Wire.available()) { while (Wire.available()) {
uint8_t cmd = Wire.read(); uint8_t data = Wire.read();
switch (cmd) { switch (data & BITMASK_CMD) {
case CMD_POWER_OFF: case CMD_POWER_OFF:
turnAllLedsOf(); turnAllLedsOf();
break; break;
case CMD_LIGHT_INDEX: case CMD_LIGHT_INDEX:
uint8_t lightIndex = Wire.read(); turnOnSingleLed(data & BITMASK_PAYLOAD);
turnOnSingleLed(lightIndex);
break; break;
case CMD_DRAWER_INDEX: case CMD_DRAWER_INDEX:
uint8_t drawerIndex = Wire.read(); openLock(data & BITMASK_PAYLOAD);
openLock(drawerIndex);
break; break;
} }
} }

View File

@ -5,15 +5,18 @@ void resetPuzzles();
/* ========================================================= */ /* ========================================================= */
const uint8_t ARCHIV_ADDR = 0x10; const uint8_t ARCHIV_ADDR = 0x10;
const uint8_t CMD_POWER_OFF = 0x01;
const uint8_t CMD_LIGHT_INDEX = 0x02; const uint8_t BITMASK_CMD = 0b00011000;
const uint8_t CMD_DRAWER_INDEX = 0x03; const uint8_t BITMASK_PAYLOAD = 0b00000111;
const uint8_t CMD_POWER_OFF = 0b00001000;
const uint8_t CMD_LIGHT_INDEX = 0b00010000;
const uint8_t CMD_DRAWER_INDEX = 0b00011000;
void setDrawerLight(uint8_t index) void setDrawerLight(uint8_t index)
{ {
Wire.beginTransmission(ARCHIV_ADDR); Wire.beginTransmission(ARCHIV_ADDR);
Wire.write(CMD_LIGHT_INDEX); Wire.write(CMD_LIGHT_INDEX | (index & BITMASK_PAYLOAD));
Wire.write(index);
Wire.endTransmission(); Wire.endTransmission();
} }
@ -27,8 +30,7 @@ void turnLightsOff()
void openDrawer(uint8_t index) void openDrawer(uint8_t index)
{ {
Wire.beginTransmission(ARCHIV_ADDR); Wire.beginTransmission(ARCHIV_ADDR);
Wire.write(CMD_DRAWER_INDEX); Wire.write(CMD_DRAWER_INDEX | (index & BITMASK_PAYLOAD));
Wire.write(index);
Wire.endTransmission(); Wire.endTransmission();
} }
/* ========================================================= */ /* ========================================================= */