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 BUTTON_INVERT 0x02 // Invert bitmask
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_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)) {
/*
At entry:
XdrvMailbox.index = key index
XdrvMailbox.index = button index
At exit:
XdrvMailbox.index bit 0 = current state
XdrvMailbox.index bit 1 = invert signal
*/
Button.present++;
bitSet(Button.virtual_pin_used, i); // This pin is used
bool state = (XdrvMailbox.index &1);
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
bool invert = (XdrvMailbox.index &BUTTON_INVERT);
if (invert) { ButtonInvertFlag(i); } // Set inverted flag
if (!state) { 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));
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;
}

View File

@ -254,7 +254,7 @@ void SwitchInit(void) {
SwitchSetVirtualPinState(i, state); // Virtual hardware pin state
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;
}

View File

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

View File

@ -241,8 +241,12 @@ bool ShellyProAddButton(void) {
if (SPro.button_offset < 0) { SPro.button_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - SPro.button_offset;
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
AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: Button default state %d"), state);
XdrvMailbox.index = state;
*/
XdrvMailbox.index = 1; // 1 on power on and restart
return true;
}
@ -251,8 +255,12 @@ bool ShellyProAddSwitch(void) {
if (SPro.switch_offset < 0) { SPro.switch_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - SPro.switch_offset;
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
AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: Switch default state %d"), state);
XdrvMailbox.index = state;
*/
XdrvMailbox.index = 0; // 0 on power on and restart
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 cs_index;
#ifdef USE_ESP32_SPI
SPISettings spi_settings;
int8_t pin_cs[ADE7953_MAX_CHANNEL / 2];
#endif // USE_ESP32_SPI
bool use_spi;
@ -343,8 +342,8 @@ int32_t Ade7953Read(uint16_t reg) {
}
#ifdef ADE7953_DUMP_REGS
void Ade7953DumpRegs(void) {
AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: *** SAGCYC DISNOLD Resrvd Resrvd LCYCMOD Resrvd Resrvd PGAV PGAIA PGAIB"));
void Ade7953DumpRegs(uint32_t chip) {
AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: *** Chip%d **** SAGCYC DISNOLD Resrvd Resrvd LCYCMOD Resrvd Resrvd PGAV PGAIA PGAIB"), chip +1);
char data[200] = { 0 };
for (uint32_t i = 0; i < 10; i++) {
int32_t value = Ade7953Read(ADE7953_SAGCYC + i);
@ -399,7 +398,7 @@ void Ade7953Init(void) {
Ade7953.cs_index = chip;
#ifdef ADE7953_DUMP_REGS
Ade7953DumpRegs();
Ade7953DumpRegs(chip);
#endif // ADE7953_DUMP_REGS
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
Ade7953DumpRegs();
Ade7953DumpRegs(chip);
#endif // ADE7953_DUMP_REGS
}
}
@ -724,7 +723,6 @@ void Ade7953DrvInit(void) {
Ade7953.cs_index = 0;
Ade7953.use_spi = true;
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"));
} else {
return; // No CS pin defined