Simplify virtual buttons/switches

This commit is contained in:
Theo Arends 2023-02-09 14:46:54 +01:00
parent d029d8bcbc
commit 8cbb62d8c9
5 changed files with 28 additions and 25 deletions

View File

@ -27,8 +27,6 @@
#define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1 #define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1
#define BUTTON_INVERT 0x02 // Invert bitmask
const uint8_t BUTTON_PROBE_INTERVAL = 10; // Time in milliseconds between button input probe const uint8_t BUTTON_PROBE_INTERVAL = 10; // Time in milliseconds between button input probe
const uint8_t BUTTON_FAST_PROBE_INTERVAL = 2; // Time in milliseconds between button input probe for AC detection const uint8_t BUTTON_FAST_PROBE_INTERVAL = 2; // Time in milliseconds between button input probe for AC detection
const uint8_t BUTTON_AC_PERIOD = (20 + BUTTON_FAST_PROBE_INTERVAL - 1) / BUTTON_FAST_PROBE_INTERVAL; // Duration of an AC wave in probe intervals const uint8_t BUTTON_AC_PERIOD = (20 + BUTTON_FAST_PROBE_INTERVAL - 1) / BUTTON_FAST_PROBE_INTERVAL; // Duration of an AC wave in probe intervals
@ -256,20 +254,19 @@ void ButtonInit(void) {
if (XdrvCall(FUNC_ADD_BUTTON)) { if (XdrvCall(FUNC_ADD_BUTTON)) {
/* /*
At entry: At entry:
XdrvMailbox.index = key index XdrvMailbox.index = button index
At exit: At exit:
XdrvMailbox.index bit 0 = current state XdrvMailbox.index bit 0 = current state
XdrvMailbox.index bit 1 = invert signal
*/ */
Button.present++; Button.present++;
bitSet(Button.virtual_pin_used, i); // This pin is used bitSet(Button.virtual_pin_used, i); // This pin is used
bool state = (XdrvMailbox.index &1); bool state = (XdrvMailbox.index &1);
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
bool invert = (XdrvMailbox.index &BUTTON_INVERT); if (!state) { ButtonInvertFlag(i); } // Set inverted flag
if (invert) { ButtonInvertFlag(i); } // Set inverted flag // last_state[i] must be 1 to indicate no button pressed
Button.last_state[i] = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i)); Button.last_state[i] = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i));
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Add vButton%d, State %d, Info %02X"), Button.present, Button.last_state[i], XdrvMailbox.index); AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Add vButton%d, State %d"), Button.present, Button.last_state[i]);
used = true; used = true;
} }

View File

@ -254,7 +254,7 @@ void SwitchInit(void) {
SwitchSetVirtualPinState(i, state); // Virtual hardware pin state SwitchSetVirtualPinState(i, state); // Virtual hardware pin state
Switch.last_state[i] = bitRead(Switch.virtual_pin, i); Switch.last_state[i] = bitRead(Switch.virtual_pin, i);
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d, Info %02X"), Switch.present, Switch.last_state[i], XdrvMailbox.index); AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), Switch.present, Switch.last_state[i]);
used = true; used = true;
} }

View File

@ -185,15 +185,15 @@ void TmInit(void) {
} }
void TmLoop(void) { void TmLoop(void) {
uint8_t buttons = Tm1638GetButtons(); uint8_t keys = Tm1638GetButtons();
for (uint32_t i = 0; i < TM1638_MAX_KEYS; i++) { for (uint32_t i = 0; i < TM1638_MAX_KEYS; i++) {
uint32_t state = buttons &1; uint32_t state = keys &1;
#ifdef TM1638_USE_BUTTONS #ifdef TM1638_USE_BUTTONS
ButtonSetVirtualPinState(Tm1638.key_offset +i, state); ButtonSetVirtualPinState(Tm1638.key_offset +i, state);
#else #else
SwitchSetVirtualPinState(Tm1638.key_offset +i, state ^1); SwitchSetVirtualPinState(Tm1638.key_offset +i, state);
#endif #endif
buttons >>= 1; keys >>= 1;
} }
} }
@ -212,13 +212,13 @@ bool TmAddKey(void) {
if (Tm1638.key_offset < 0) { Tm1638.key_offset = XdrvMailbox.index; } if (Tm1638.key_offset < 0) { Tm1638.key_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - Tm1638.key_offset; uint32_t index = XdrvMailbox.index - Tm1638.key_offset;
if (index >= TM1638_MAX_KEYS) { return false; } if (index >= TM1638_MAX_KEYS) { return false; }
uint8_t buttons = Tm1638GetButtons(); /*
uint32_t state = bitRead(buttons, index); uint8_t keys = Tm1638GetButtons();
#ifdef TM1638_USE_BUTTONS uint32_t state = bitRead(keys, index);
XdrvMailbox.index = state | BUTTON_INVERT; // Invert - default is 0 AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: Default state %d"), state);
#else XdrvMailbox.index = state; // Default is 0 - Button will also set invert
XdrvMailbox.index = state ^1; // Invert - default is 0 */
#endif XdrvMailbox.index = 0; // Default is 0 - Button will also set invert
return true; return true;
} }

View File

@ -241,8 +241,12 @@ bool ShellyProAddButton(void) {
if (SPro.button_offset < 0) { SPro.button_offset = XdrvMailbox.index; } if (SPro.button_offset < 0) { SPro.button_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - SPro.button_offset; uint32_t index = XdrvMailbox.index - SPro.button_offset;
if (index > 2) { return false; } // Support three buttons if (index > 2) { return false; } // Support three buttons
/*
uint32_t state = bitRead(SPro.input_state, sp4_button_pin[index]); // 1 on power on and restart uint32_t state = bitRead(SPro.input_state, sp4_button_pin[index]); // 1 on power on and restart
AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: Button default state %d"), state);
XdrvMailbox.index = state; XdrvMailbox.index = state;
*/
XdrvMailbox.index = 1; // 1 on power on and restart
return true; return true;
} }
@ -251,8 +255,12 @@ bool ShellyProAddSwitch(void) {
if (SPro.switch_offset < 0) { SPro.switch_offset = XdrvMailbox.index; } if (SPro.switch_offset < 0) { SPro.switch_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - SPro.switch_offset; uint32_t index = XdrvMailbox.index - SPro.switch_offset;
if (index > 3) { return false; } // Support four switches if (index > 3) { return false; } // Support four switches
/*
uint32_t state = bitRead(SPro.input_state, sp4_switch_pin[index]); // 0 on power on and restart uint32_t state = bitRead(SPro.input_state, sp4_switch_pin[index]); // 0 on power on and restart
AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: Switch default state %d"), state);
XdrvMailbox.index = state; XdrvMailbox.index = state;
*/
XdrvMailbox.index = 0; // 0 on power on and restart
return true; return true;
} }

View File

@ -239,7 +239,6 @@ struct Ade7953 {
uint8_t model = 0; // 0 = Shelly 2.5, 1 = Shelly EM, 2 = Shelly Plus 2PM, 3 = Shelly Pro 1PM, 4 = Shelly Pro 2PM, 5 = Shelly Pro 4PM uint8_t model = 0; // 0 = Shelly 2.5, 1 = Shelly EM, 2 = Shelly Plus 2PM, 3 = Shelly Pro 1PM, 4 = Shelly Pro 2PM, 5 = Shelly Pro 4PM
uint8_t cs_index; uint8_t cs_index;
#ifdef USE_ESP32_SPI #ifdef USE_ESP32_SPI
SPISettings spi_settings;
int8_t pin_cs[ADE7953_MAX_CHANNEL / 2]; int8_t pin_cs[ADE7953_MAX_CHANNEL / 2];
#endif // USE_ESP32_SPI #endif // USE_ESP32_SPI
bool use_spi; bool use_spi;
@ -343,8 +342,8 @@ int32_t Ade7953Read(uint16_t reg) {
} }
#ifdef ADE7953_DUMP_REGS #ifdef ADE7953_DUMP_REGS
void Ade7953DumpRegs(void) { void Ade7953DumpRegs(uint32_t chip) {
AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: *** SAGCYC DISNOLD Resrvd Resrvd LCYCMOD Resrvd Resrvd PGAV PGAIA PGAIB")); AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: *** Chip%d **** SAGCYC DISNOLD Resrvd Resrvd LCYCMOD Resrvd Resrvd PGAV PGAIA PGAIB"), chip +1);
char data[200] = { 0 }; char data[200] = { 0 };
for (uint32_t i = 0; i < 10; i++) { for (uint32_t i = 0; i < 10; i++) {
int32_t value = Ade7953Read(ADE7953_SAGCYC + i); int32_t value = Ade7953Read(ADE7953_SAGCYC + i);
@ -399,7 +398,7 @@ void Ade7953Init(void) {
Ade7953.cs_index = chip; Ade7953.cs_index = chip;
#ifdef ADE7953_DUMP_REGS #ifdef ADE7953_DUMP_REGS
Ade7953DumpRegs(); Ade7953DumpRegs(chip);
#endif // ADE7953_DUMP_REGS #endif // ADE7953_DUMP_REGS
Ade7953Write(ADE7953_CONFIG, 0x0004); // Locking the communication interface (Clear bit COMM_LOCK), Enable HPF Ade7953Write(ADE7953_CONFIG, 0x0004); // Locking the communication interface (Clear bit COMM_LOCK), Enable HPF
@ -457,7 +456,7 @@ void Ade7953Init(void) {
} }
#ifdef ADE7953_DUMP_REGS #ifdef ADE7953_DUMP_REGS
Ade7953DumpRegs(); Ade7953DumpRegs(chip);
#endif // ADE7953_DUMP_REGS #endif // ADE7953_DUMP_REGS
} }
} }
@ -724,7 +723,6 @@ void Ade7953DrvInit(void) {
Ade7953.cs_index = 0; Ade7953.cs_index = 0;
Ade7953.use_spi = true; Ade7953.use_spi = true;
SPI.begin(Pin(GPIO_SPI_CLK), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_MOSI), -1); SPI.begin(Pin(GPIO_SPI_CLK), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_MOSI), -1);
Ade7953.spi_settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); // Set up SPI at 1MHz, MSB first, Capture at rising edge
AddLog(LOG_LEVEL_INFO, PSTR("SPI: ADE7953 found")); AddLog(LOG_LEVEL_INFO, PSTR("SPI: ADE7953 found"));
} else { } else {
return; // No CS pin defined return; // No CS pin defined