From ebe308acfc4cac691078f5f2803fbf871587239f Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 25 Mar 2023 18:15:24 +0100 Subject: [PATCH] refactor button/switch code --- tasmota/tasmota_xdrv_driver/xdrv_28_pcf8574_v2.ino | 12 ++---------- tasmota/tasmota_xdrv_driver/xdrv_67_mcp23xxx.ino | 12 ++---------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_28_pcf8574_v2.ino b/tasmota/tasmota_xdrv_driver/xdrv_28_pcf8574_v2.ino index af90ea263..ff08fd033 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_28_pcf8574_v2.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_28_pcf8574_v2.ino @@ -107,8 +107,6 @@ #endif struct PCF8574 { - uint32_t button_used; - uint32_t switch_used; uint32_t relay_inverted; uint32_t button_inverted; uint16_t pin[PCF8574_MAX_PINS]; @@ -252,38 +250,32 @@ bool Pcf8574LoadTemplate(void) { uint16_t mpin = val.getUInt(); if (mpin) { // Above GPIO_NONE if ((mpin >= AGPIO(GPIO_SWT1)) && (mpin < (AGPIO(GPIO_SWT1) + MAX_SWITCHES_SET))) { - bitSet(Pcf8574.switch_used, mpin - AGPIO(GPIO_SWT1)); Pcf8574.switch_max++; Pcf8574DigitalWrite(pin, 1); // INPUT_PULLUP } else if ((mpin >= AGPIO(GPIO_SWT1_NP)) && (mpin < (AGPIO(GPIO_SWT1_NP) + MAX_SWITCHES_SET))) { mpin -= (AGPIO(GPIO_SWT1_NP) - AGPIO(GPIO_SWT1)); - bitSet(Pcf8574.switch_used, mpin - AGPIO(GPIO_SWT1)); Pcf8574.switch_max++; Pcf8574DigitalWrite(pin, 1); // INPUT } else if ((mpin >= AGPIO(GPIO_KEY1)) && (mpin < (AGPIO(GPIO_KEY1) + MAX_KEYS_SET))) { - bitSet(Pcf8574.button_used, mpin - AGPIO(GPIO_KEY1)); Pcf8574.button_max++; Pcf8574DigitalWrite(pin, 1); // INPUT_PULLUP } else if ((mpin >= AGPIO(GPIO_KEY1_NP)) && (mpin < (AGPIO(GPIO_KEY1_NP) + MAX_KEYS_SET))) { mpin -= (AGPIO(GPIO_KEY1_NP) - AGPIO(GPIO_KEY1)); - bitSet(Pcf8574.button_used, mpin - AGPIO(GPIO_KEY1)); Pcf8574.button_max++; Pcf8574DigitalWrite(pin, 1); // INPUT } else if ((mpin >= AGPIO(GPIO_KEY1_INV)) && (mpin < (AGPIO(GPIO_KEY1_INV) + MAX_KEYS_SET))) { bitSet(Pcf8574.button_inverted, mpin - AGPIO(GPIO_KEY1_INV)); mpin -= (AGPIO(GPIO_KEY1_INV) - AGPIO(GPIO_KEY1)); - bitSet(Pcf8574.button_used, mpin - AGPIO(GPIO_KEY1)); Pcf8574.button_max++; Pcf8574DigitalWrite(pin, 1); // INPUT_PULLUP } else if ((mpin >= AGPIO(GPIO_KEY1_INV_NP)) && (mpin < (AGPIO(GPIO_KEY1_INV_NP) + MAX_KEYS_SET))) { bitSet(Pcf8574.button_inverted, mpin - AGPIO(GPIO_KEY1_INV_NP)); mpin -= (AGPIO(GPIO_KEY1_INV_NP) - AGPIO(GPIO_KEY1)); - bitSet(Pcf8574.button_used, mpin - AGPIO(GPIO_KEY1)); Pcf8574.button_max++; Pcf8574DigitalWrite(pin, 1); // INPUT } @@ -383,7 +375,7 @@ bool Pcf8574AddButton(void) { index -= Pcf8574.button_offset; if (index >= Pcf8574.button_max) { return false; } } else { - if (!bitRead(Pcf8574.button_used, index)) { return false; } + if (!Pcf8574PinUsed(GPIO_KEY1, index)) { return false; } Pcf8574.button_offset = 0; } XdrvMailbox.index = (Pcf8574DigitalRead(Pcf8574Pin(GPIO_KEY1, index)) != bitRead(Pcf8574.button_inverted, index)); @@ -398,7 +390,7 @@ bool Pcf8574AddSwitch(void) { index -= Pcf8574.switch_offset; if (index >= Pcf8574.switch_max) { return false; } } else { - if (!bitRead(Pcf8574.switch_used, index)) { return false; } + if (!Pcf8574PinUsed(GPIO_SWT1, index)) { return false; } Pcf8574.switch_offset = 0; } XdrvMailbox.index = Pcf8574DigitalRead(Pcf8574Pin(GPIO_SWT1, index)); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_67_mcp23xxx.ino b/tasmota/tasmota_xdrv_driver/xdrv_67_mcp23xxx.ino index 640e4fbf0..fa7a914fa 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_67_mcp23xxx.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_67_mcp23xxx.ino @@ -143,8 +143,6 @@ typedef struct { struct MCP230 { tMcp23xDevice device[MCP23XXX_MAX_DEVICES]; - uint32_t button_used; - uint32_t switch_used; uint32_t relay_inverted; uint32_t button_inverted; uint8_t chip; @@ -506,38 +504,32 @@ bool MCP23xLoadTemplate(void) { uint16_t mpin = val.getUInt(); if (mpin) { // Above GPIO_NONE if ((mpin >= AGPIO(GPIO_SWT1)) && (mpin < (AGPIO(GPIO_SWT1) + MAX_SWITCHES_SET))) { - bitSet(Mcp23x.switch_used, mpin - AGPIO(GPIO_SWT1)); Mcp23x.switch_max++; MCP23xSetPinModes(pin, INPUT_PULLUP); } else if ((mpin >= AGPIO(GPIO_SWT1_NP)) && (mpin < (AGPIO(GPIO_SWT1_NP) + MAX_SWITCHES_SET))) { mpin -= (AGPIO(GPIO_SWT1_NP) - AGPIO(GPIO_SWT1)); - bitSet(Mcp23x.switch_used, mpin - AGPIO(GPIO_SWT1)); Mcp23x.switch_max++; MCP23xSetPinModes(pin, INPUT); } else if ((mpin >= AGPIO(GPIO_KEY1)) && (mpin < (AGPIO(GPIO_KEY1) + MAX_KEYS_SET))) { - bitSet(Mcp23x.button_used, mpin - AGPIO(GPIO_KEY1)); Mcp23x.button_max++; MCP23xSetPinModes(pin, INPUT_PULLUP); } else if ((mpin >= AGPIO(GPIO_KEY1_NP)) && (mpin < (AGPIO(GPIO_KEY1_NP) + MAX_KEYS_SET))) { mpin -= (AGPIO(GPIO_KEY1_NP) - AGPIO(GPIO_KEY1)); - bitSet(Mcp23x.button_used, mpin - AGPIO(GPIO_KEY1)); Mcp23x.button_max++; MCP23xSetPinModes(pin, INPUT); } else if ((mpin >= AGPIO(GPIO_KEY1_INV)) && (mpin < (AGPIO(GPIO_KEY1_INV) + MAX_KEYS_SET))) { bitSet(Mcp23x.button_inverted, mpin - AGPIO(GPIO_KEY1_INV)); mpin -= (AGPIO(GPIO_KEY1_INV) - AGPIO(GPIO_KEY1)); - bitSet(Mcp23x.button_used, mpin - AGPIO(GPIO_KEY1)); Mcp23x.button_max++; MCP23xSetPinModes(pin, INPUT_PULLUP); } else if ((mpin >= AGPIO(GPIO_KEY1_INV_NP)) && (mpin < (AGPIO(GPIO_KEY1_INV_NP) + MAX_KEYS_SET))) { bitSet(Mcp23x.button_inverted, mpin - AGPIO(GPIO_KEY1_INV_NP)); mpin -= (AGPIO(GPIO_KEY1_INV_NP) - AGPIO(GPIO_KEY1)); - bitSet(Mcp23x.button_used, mpin - AGPIO(GPIO_KEY1)); Mcp23x.button_max++; MCP23xSetPinModes(pin, INPUT); } @@ -770,7 +762,7 @@ bool MCP23xAddButton(void) { index -= Mcp23x.button_offset; if (index >= Mcp23x.button_max) { return false; } } else { - if (!bitRead(Mcp23x.button_used, index)) { return false; } + if (!MCP23xPinUsed(GPIO_KEY1, index)) { return false; } Mcp23x.button_offset = 0; } XdrvMailbox.index = (MCP23xDigitalRead(MCP23xPin(GPIO_KEY1, index)) != bitRead(Mcp23x.button_inverted, index)); @@ -785,7 +777,7 @@ bool MCP23xAddSwitch(void) { index -= Mcp23x.switch_offset; if (index >= Mcp23x.switch_max) { return false; } } else { - if (!bitRead(Mcp23x.switch_used, index)) { return false; } + if (!MCP23xPinUsed(GPIO_SWT1, index)) { return false; } Mcp23x.switch_offset = 0; } XdrvMailbox.index = MCP23xDigitalRead(MCP23xPin(GPIO_SWT1, index));