Use bit based communication for I2C

This commit is contained in:
C0d3v 2026-01-15 20:00:26 +01:00
parent c7e5a92edf
commit 7a77347bc2

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;
} }
} }