Refactor device_present calculation

This commit is contained in:
Theo Arends 2023-02-19 15:28:20 +01:00
parent f0eef7771f
commit a994c71de4
3 changed files with 55 additions and 37 deletions

View File

@ -233,6 +233,9 @@ void ButtonProbe(void) {
void ButtonInit(void) {
bool ac_detect = (Settings->button_debounce % 10 == 9);
Button.used = 0;
/*
uint32_t last_used = 0;
*/
for (uint32_t i = 0; i < MAX_KEYS_SET; i++) {
Button.last_state[i] = NOT_PRESSED;
#ifdef ESP8266
@ -261,14 +264,13 @@ void ButtonInit(void) {
}
#endif // USE_ADC
else {
// Insert, Skip and Append virtual buttons
XdrvMailbox.index = i;
if (XdrvCall(FUNC_ADD_BUTTON)) {
/*
At entry:
XdrvMailbox.index = button index
At exit:
XdrvMailbox.index bit 0 = current state
*/
// At entry:
// XdrvMailbox.index = button index
// At exit:
// XdrvMailbox.index bit 0 = current state
bitSet(Button.used, i); // This pin is used
bool state = (XdrvMailbox.index &1);
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
@ -280,7 +282,37 @@ void ButtonInit(void) {
}
}
Button.debounced_state[i] = Button.last_state[i];
/*
if (bitRead(Button.used, i)) {
last_used = i +1;
}
*/
}
/*
// Append virtual buttons
for (uint32_t i = last_used; i < MAX_KEYS_SET; i++) {
Button.last_state[i] = NOT_PRESSED;
XdrvMailbox.index = i;
if (XdrvCall(FUNC_ADD_BUTTON)) {
// At entry:
// XdrvMailbox.index = button index
// At exit:
// XdrvMailbox.index bit 0 = current state
bitSet(Button.used, i); // This pin is used
bool state = (XdrvMailbox.index &1);
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
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"), i +1, Button.last_state[i]);
}
Button.debounced_state[i] = Button.last_state[i];
}
*/
// AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X, Invert %08X"), Button.used, Button.virtual_pin, Button.inverted_mask);

View File

@ -2196,6 +2196,22 @@ void GpioInit(void)
XdrvCall(FUNC_I2C_INIT); // Init RTC
TasmotaGlobal.devices_present = 0;
uint32_t bi_device = 0;
for (uint32_t i = 0; i < MAX_RELAYS; i++) {
if (PinUsed(GPIO_REL1, i)) {
TasmotaGlobal.devices_present++;
#ifdef ESP8266
if (EXS_RELAY == TasmotaGlobal.module_type) {
if (i &1) { TasmotaGlobal.devices_present--; }
}
#endif // ESP8266
if (bitRead(TasmotaGlobal.rel_bistable, i)) {
if (bi_device &1) { TasmotaGlobal.devices_present--; }
bi_device++;
}
}
}
TasmotaGlobal.light_type = LT_BASIC; // Use basic PWM control if SetOption15 = 0
XsnsCall(FUNC_MODULE_INIT);
@ -2225,22 +2241,6 @@ void GpioInit(void)
GpioInitPwm();
uint32_t bi_device = 0;
for (uint32_t i = 0; i < MAX_RELAYS; i++) {
if (PinUsed(GPIO_REL1, i)) {
TasmotaGlobal.devices_present++;
#ifdef ESP8266
if (EXS_RELAY == TasmotaGlobal.module_type) {
if (i &1) { TasmotaGlobal.devices_present--; }
}
#endif // ESP8266
if (bitRead(TasmotaGlobal.rel_bistable, i)) {
if (bi_device &1) { TasmotaGlobal.devices_present--; }
bi_device++;
}
}
}
for (uint32_t i = 0; i < MAX_LEDS; i++) {
if (PinUsed(GPIO_LED1, i)) {
#ifdef USE_ARILUX_RF

View File

@ -163,21 +163,7 @@ void TmInit(void) {
}
digitalWrite(Tm1638.strobe_pin, HIGH);
// Dirty hack to offset TM1638 leds from GPIO relays
// At this time in code sequence the number of GPIO relays has not been established
uint32_t bi_device = 0;
uint32_t devices_present = 0;
for (uint32_t i = 0; i < MAX_RELAYS; i++) {
if (PinUsed(GPIO_REL1, i)) {
devices_present++;
if (bitRead(TasmotaGlobal.rel_bistable, i)) {
if (bi_device &1) { devices_present--; }
bi_device++;
}
}
}
Tm1638.led_offset = devices_present;
Tm1638.led_offset = TasmotaGlobal.devices_present;
TasmotaGlobal.devices_present += TM1638_MAX_LEDS;
Tm1638.key_offset = -1;
Tm1638.detected = true;