diff --git a/code/espurna/button.ino b/code/espurna/button.ino index c2dbdbec..bea00dc2 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -139,6 +139,56 @@ void _buttonExecuteEvent(unsigned int id, unsigned char event) { } +void _buttonClear() { + for (unsigned char i = 0; i < _buttons.size(); i++) { + button_t element = _buttons[i]; + delete(element.button); + } + _buttons.clear(); +} + +void _buttonConfigure() { + + _buttonClear(); + + #ifdef ITEAD_SONOFF_DUAL + + unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt(); + unsigned long actions = BUTTON_MODE_TOGGLE << 4; + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1}); + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2}); + _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId}); + + #else + + // TODO: maybe this setting should be changed, btnDelay => btnClickDelay? + unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); + + unsigned char index = 0; + while (index < MAX_COMPONENTS) { + + unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt(); + if (GPIO_NONE == pin) break; + unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt(); + unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt(); + unsigned long actions = _buttonGetActionMask(index); + + // DebounceEvent takes 4 parameters + // * GPIO + // * Button mode + // * Debounce delay + // * Wait delay for more clicks + _buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId}); + ++index; + + } + + #endif + + DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size()); + +} + void _buttonLoop() { #ifdef ITEAD_SONOFF_DUAL @@ -200,45 +250,12 @@ void _buttonLoop() { void buttonSetup() { - #ifdef ITEAD_SONOFF_DUAL - - unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt(); - unsigned long actions = BUTTON_MODE_TOGGLE << 4; - _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1}); - _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2}); - _buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId}); - - #else - - // TODO: maybe this setting should be changed, btnDelay => btnClickDelay? - unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); - - unsigned char index = 0; - while (index < MAX_COMPONENTS) { - - unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt(); - if (GPIO_NONE == pin) break; - unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt(); - unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt(); - unsigned long actions = _buttonGetActionMask(index); - - // DebounceEvent takes 4 parameters - // * GPIO - // * Button mode - // * Debounce delay - // * Wait delay for more clicks - _buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId}); - ++index; - - } - - #endif - - DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size()); + _buttonConfigure(); // Websocket Callbacks #if WEB_SUPPORT wsOnSendRegister(_buttonWebSocketOnSend); + wsOnAfterParseRegister(_buttonConfigure); #endif settingsRegisterKeyCheck(_buttonKeyCheck); diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 3afbb8e4..ed296a56 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -121,11 +121,35 @@ unsigned char _ledCount() { return _leds.size(); } +void _ledClear() { + _leds.clear(); +} + void _ledConfigure() { - for (unsigned int i=0; i < _leds.size(); i++) { - _ledMode(i, getSetting("ledMode", i, _ledMode(i)).toInt()); + + _ledClear(); + + unsigned char index = 0; + while (index < MAX_COMPONENTS) { + + unsigned char pin = getSetting("ledGPIO", index, GPIO_NONE).toInt(); + if (pin == GPIO_NONE) break; + + bool inverse = getSetting("ledLogic", index, 0).toInt() == 1; + unsigned char mode = getSetting("ledMode", index, index==0 ? LED_MODE_WIFI : LED_MODE_MQTT).toInt(); + unsigned char relayId = getSetting("ledRelay", index, RELAY_NONE).toInt(); + + _leds.push_back((led_t) { pin, inverse, mode, relayId }); + pinMode(pin, OUTPUT); + _ledStatus(index, false); + ++index; + } + + DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); + _led_update = true; + } void _ledLoop() { @@ -263,22 +287,7 @@ void ledUpdate(bool value) { void ledSetup() { - unsigned char index = 0; - while (index < MAX_COMPONENTS) { - - unsigned char pin = getSetting("ledGPIO", index, GPIO_NONE).toInt(); - if (pin == GPIO_NONE) break; - - bool inverse = getSetting("ledLogic", index, 0).toInt() == 1; - unsigned char mode = getSetting("ledMode", index, index==0 ? LED_MODE_WIFI : LED_MODE_MQTT).toInt(); - unsigned char relayId = getSetting("ledRelay", index, RELAY_NONE).toInt(); - - _leds.push_back((led_t) { pin, inverse, mode, relayId }); - pinMode(pin, OUTPUT); - _ledStatus(index, false); - ++index; - - } + _ledConfigure(); #if MQTT_SUPPORT mqttRegister(_ledMQTTCallback); @@ -289,8 +298,6 @@ void ledSetup() { wsOnAfterParseRegister(_ledConfigure); #endif - DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); - // Registers espurnaRegisterLoop(_ledLoop); settingsRegisterKeyCheck(_ledKeyCheck); diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 812912f6..94e617b7 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -498,15 +498,59 @@ void _relayBoot() { } +void _relayClear() { + + for (unsigned char i = 0; i < _relays.size(); i++) { + relay_t element = _relays[i]; + element.pulseTicker.detach(); + } + _relays.clear(); + +} + void _relayConfigure() { - for (unsigned int i=0; i<_relays.size(); i++) { - pinMode(_relays[i].pin, OUTPUT); - if (_relays[i].type == RELAY_TYPE_LATCHED || _relays[i].type == RELAY_TYPE_LATCHED_INVERSE) { - pinMode(_relays[i].reset_pin, OUTPUT); + + _relayClear(); + + // Dummy relays for AI Light, Magic Home LED Controller, H801, + // Sonoff Dual and Sonoff RF Bridge + #if DUMMY_RELAY_COUNT > 0 + + for (unsigned char index=0; index < DUMMY_RELAY_COUNT; index++) { + unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt(); + unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt(); + _relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL, 0, delay_on, delay_off}); } - _relays[i].pulse = getSetting("rlyPulse", i, RELAY_PULSE_MODE).toInt(); - _relays[i].pulse_ms = 1000 * getSetting("rlyTime", i, RELAY_PULSE_MODE).toFloat(); - } + + #else + + unsigned char index = 0; + while (index < MAX_COMPONENTS) { + + unsigned char pin = getSetting("rlyGPIO", index, GPIO_NONE).toInt(); + if (GPIO_NONE == pin) break; + pinMode(pin, OUTPUT); + + unsigned char type = getSetting("rlyType", index, RELAY_TYPE_NORMAL).toInt(); + unsigned char reset = getSetting("rlyResetGPIO", index, GPIO_NONE).toInt(); + if (((type & RELAY_TYPE_LATCHED) == RELAY_TYPE_LATCHED) && (GPIO_NONE == reset)) break; + if (GPIO_NONE != reset) pinMode(reset, OUTPUT); + + unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt(); + unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt(); + + unsigned char pulse = getSetting("rlyPulse", index, RELAY_PULSE_MODE).toInt(); + float pulse_ms = 1000 * getSetting("rlyTime", index, RELAY_PULSE_TIME).toFloat(); + + _relays.push_back((relay_t) { pin, type, reset, delay_on, delay_off, pulse, pulse_ms }); + ++index; + + } + + #endif + + DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size()); + } //------------------------------------------------------------------------------ @@ -905,37 +949,6 @@ void _relayLoop() { void relaySetup() { - // Dummy relays for AI Light, Magic Home LED Controller, H801, - // Sonoff Dual and Sonoff RF Bridge - #if DUMMY_RELAY_COUNT > 0 // TODO: this is yet hardcoded - - for (unsigned char index=0; index < DUMMY_RELAY_COUNT; index++) { - unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt(); - unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt(); - _relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL, 0, delay_on, delay_off}); - } - - #else - - unsigned char index = 0; - while (index < MAX_COMPONENTS) { - - unsigned char pin = getSetting("rlyGPIO", index, GPIO_NONE).toInt(); - if (GPIO_NONE == pin) break; - unsigned char type = getSetting("rlyType", index, 0).toInt(); - unsigned char reset = getSetting("rlyResetGPIO", index, GPIO_NONE).toInt(); - if (((type & RELAY_TYPE_LATCHED) == RELAY_TYPE_LATCHED) && (GPIO_NONE == reset)) break; - - unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt(); - unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt(); - - _relays.push_back((relay_t) { pin, type, reset, delay_on, delay_off }); - ++index; - - } - - #endif - _relayBackwards(); _relayConfigure(); _relayBoot(); @@ -955,6 +968,4 @@ void relaySetup() { _relayInitCommands(); #endif - DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size()); - }