mirror of
https://github.com/arendst/Tasmota.git
synced 2025-04-25 15:27:17 +00:00
Extent ESP8266 virtual switch support
This commit is contained in:
parent
6ccacb4af1
commit
9673cca8b0
@ -57,6 +57,7 @@ const uint8_t MAX_INTERLOCKS = 14; // Max number of interlock groups (u
|
||||
const uint8_t MAX_SWITCHES = 28; // Max number of switches (up to MAX_SWITCHES_SET)
|
||||
const uint8_t MAX_KEYS = 28; // Max number of keys or buttons (up to 28)
|
||||
#endif // ESP32
|
||||
const uint8_t MAX_KEYS_SET = 28; // Max number of keys
|
||||
|
||||
// Changes to the following MAX_ defines will impact settings layout
|
||||
const uint8_t MAX_INTERLOCKS_SET = 14; // Max number of interlock groups (MAX_RELAYS / 2)
|
||||
|
@ -700,7 +700,7 @@ void CmndStatus(void)
|
||||
snprintf_P(stemp, sizeof(stemp), PSTR("%s%s\"%s\"" ), stemp, (i > 0 ? "," : ""), EscapeJSONString(SettingsText(SET_FRIENDLYNAME1 +i)).c_str());
|
||||
}
|
||||
stemp2[0] = '\0';
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
snprintf_P(stemp2, sizeof(stemp2), PSTR("%s%s%d" ), stemp2, (i > 0 ? "," : ""), Settings->switchmode[i]);
|
||||
}
|
||||
Response_P(PSTR("{\"" D_CMND_STATUS "\":{\"" D_CMND_MODULE "\":%d,\"" D_CMND_DEVICENAME "\":\"%s\",\"" D_CMND_FRIENDLYNAME "\":[%s],\"" D_CMND_TOPIC "\":\"%s\",\""
|
||||
@ -2137,7 +2137,7 @@ void CmndSwitchText(void) {
|
||||
|
||||
void CmndSwitchMode(void)
|
||||
{
|
||||
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES)) {
|
||||
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES_SET)) {
|
||||
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_SWITCH_OPTION)) {
|
||||
Settings->switchmode[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
||||
}
|
||||
|
@ -49,10 +49,10 @@ struct SWITCH {
|
||||
uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags
|
||||
uint32_t virtual_pin_used = 0; // Switch used bitmask
|
||||
uint32_t virtual_pin = 0; // Switch state bitmask
|
||||
uint8_t state[MAX_SWITCHES] = { 0 };
|
||||
uint8_t last_state[MAX_SWITCHES]; // Last wall switch states
|
||||
uint8_t hold_timer[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold
|
||||
uint8_t debounced_state[MAX_SWITCHES]; // Switch debounced states
|
||||
uint8_t state[MAX_SWITCHES_SET] = { 0 };
|
||||
uint8_t last_state[MAX_SWITCHES_SET]; // Last wall switch states
|
||||
uint8_t hold_timer[MAX_SWITCHES_SET] = { 0 }; // Timer for wallswitch push button hold
|
||||
uint8_t debounced_state[MAX_SWITCHES_SET]; // Switch debounced states
|
||||
uint8_t first_change = 0;
|
||||
uint8_t present = 0;
|
||||
bool probe_mutex;
|
||||
@ -68,6 +68,10 @@ void SwitchPulldownFlag(uint32 switch_bit) {
|
||||
bitSet(Switch.pulldown_mask, switch_bit);
|
||||
}
|
||||
|
||||
bool SwitchUsed(uint32_t index) {
|
||||
return (PinUsed(GPIO_SWT1, index) || bitRead(Switch.virtual_pin_used, index));
|
||||
}
|
||||
|
||||
// Preffered virtual switch support since v12.3.1.4
|
||||
void SwitchSetVirtualPinState(uint32_t index, uint32_t state) {
|
||||
bitWrite(Switch.virtual_pin, index, state);
|
||||
@ -127,7 +131,7 @@ void SwitchProbe(void) {
|
||||
}
|
||||
|
||||
uint32_t not_activated;
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
if (PinUsed(GPIO_SWT1, i)) {
|
||||
not_activated = digitalRead(Pin(GPIO_SWT1, i));
|
||||
}
|
||||
@ -220,7 +224,7 @@ void SwitchInit(void) {
|
||||
|
||||
Switch.present = 0;
|
||||
Switch.virtual_pin_used = 0;
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
Switch.last_state[i] = NOT_PRESSED; // Init global to virtual switch state;
|
||||
bool used = false;
|
||||
|
||||
@ -280,8 +284,9 @@ void SwitchHandler(uint32_t mode) {
|
||||
|
||||
uint32_t loops_per_second = 1000 / Settings->switch_debounce;
|
||||
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
if (PinUsed(GPIO_SWT1, i) || bitRead(Switch.virtual_pin_used, i)) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
// if (PinUsed(GPIO_SWT1, i) || bitRead(Switch.virtual_pin_used, i)) {
|
||||
if (SwitchUsed(i)) {
|
||||
uint32_t button = Switch.debounced_state[i];
|
||||
uint32_t switchflag = POWER_TOGGLE +1;
|
||||
uint32_t mqtt_action = POWER_NONE;
|
||||
|
@ -962,11 +962,11 @@ bool MqttShowSensor(bool call_show_sensor) {
|
||||
ResponseAppendTime();
|
||||
|
||||
int json_data_start = ResponseLength();
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
#ifdef USE_TM1638
|
||||
if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) {
|
||||
#else
|
||||
if (PinUsed(GPIO_SWT1, i)) {
|
||||
if (SwitchUsed(i)) {
|
||||
#endif // USE_TM1638
|
||||
ResponseAppend_P(PSTR(",\"%s\":\"%s\""), GetSwitchText(i).c_str(), GetStateText(SwitchState(i)));
|
||||
}
|
||||
|
@ -964,11 +964,11 @@ void RulesEvery50ms(void)
|
||||
RulesProcessEvent(json_event);
|
||||
}
|
||||
// Boot time SWITCHES Status
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
#ifdef USE_TM1638
|
||||
if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) {
|
||||
#else
|
||||
if (PinUsed(GPIO_SWT1, i)) {
|
||||
if (SwitchUsed(i)) {
|
||||
#endif // USE_TM1638
|
||||
snprintf_P(json_event, sizeof(json_event), PSTR("{\"%s\":{\"Boot\":%d}}"), GetSwitchText(i).c_str(), (SwitchState(i)));
|
||||
RulesProcessEvent(json_event);
|
||||
|
@ -144,18 +144,18 @@ void TasDiscoverMessage(void) {
|
||||
"\"swc\":[")); // Switch modes (start)
|
||||
|
||||
// Enable Discovery for Switches only if SetOption114 is enabled
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1);
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1);
|
||||
}
|
||||
|
||||
ResponseAppend_P(PSTR("]," // Switch modes (end)
|
||||
"\"swn\":[")); // Switch names (start)
|
||||
|
||||
// Enable Discovery for Switches only if SetOption114 is enabled
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
char sname[TOPSZ];
|
||||
snprintf_P(sname, sizeof(sname), PSTR("\"%s\""), GetSwitchText(i).c_str());
|
||||
ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null"));
|
||||
ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null"));
|
||||
}
|
||||
|
||||
ResponseAppend_P(PSTR("]," // Switch names (end)
|
||||
|
@ -338,18 +338,18 @@ void HassDiscoverMessage(void) {
|
||||
"\"swc\":[")); // Switch modes (start)
|
||||
|
||||
// Enable Discovery for Switches only if SetOption114 is enabled
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1);
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1);
|
||||
}
|
||||
|
||||
ResponseAppend_P(PSTR("]," // Switch modes (end)
|
||||
"\"swn\":[")); // Switch names (start)
|
||||
|
||||
// Enable Discovery for Switches only if SetOption114 is enabled
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
char sname[TOPSZ];
|
||||
snprintf_P(sname, sizeof(sname), PSTR("\"%s\""), GetSwitchText(i).c_str());
|
||||
ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null"));
|
||||
ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null"));
|
||||
}
|
||||
|
||||
ResponseAppend_P(PSTR("]," // Switch names (end)
|
||||
@ -790,7 +790,7 @@ void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint
|
||||
|
||||
void HAssAnnounceSwitches(void)
|
||||
{
|
||||
for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++)
|
||||
for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES_SET; switch_index++)
|
||||
{
|
||||
uint8_t switch_present = 0;
|
||||
uint8_t dual = 0;
|
||||
@ -798,7 +798,7 @@ void HAssAnnounceSwitches(void)
|
||||
uint8_t hold = 0;
|
||||
uint8_t pir = 0;
|
||||
|
||||
if (PinUsed(GPIO_SWT1, switch_index)) { switch_present = 1; }
|
||||
if (SwitchUsed(switch_index)) { switch_present = 1; }
|
||||
|
||||
if (KeyTopicActive(1) && strcmp(SettingsText(SET_MQTT_SWITCH_TOPIC), TasmotaGlobal.mqtt_topic)) // Enable Discovery for Switches only if SwitchTopic is set to a custom name
|
||||
{
|
||||
|
@ -586,8 +586,8 @@ extern "C" {
|
||||
int32_t l_getswitch(bvm *vm);
|
||||
int32_t l_getswitch(bvm *vm) {
|
||||
be_newobject(vm, "list");
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
||||
if (PinUsed(GPIO_SWT1, i)) {
|
||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||
if (SwitchUsed(i)) {
|
||||
be_pushbool(vm, SwitchGetVirtual(i) == PRESSED);
|
||||
be_data_push(vm, -2);
|
||||
be_pop(vm, 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user