Skip to content

Commit

Permalink
providers: relays, lights and buttons refactoring (#2414)
Browse files Browse the repository at this point in the history
- gpio module now tracks the known providers (right now, hardware and mcp expander)
- refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions
- refactored button module to use gpio provider instead of referencing types itself
- removed dual & stm code from buttons, migrate both to relay module
- added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did)  
- relays runtime configuration keys
- relay command now shows configured relays and current & target statuses
- refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead
- remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT
- allow to bind rf codes to real relays
- drop tuya-specific lights provider, remove tuya code from relays and lights modules
- integrate tuya via relay listeners and providers, use lights custom provider
- implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle)
- lights custom provider (global, not per-pin) and state listeners
- remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT
- lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT
- refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing
- transition time + step parameter for the lightUpdate
- report mask parameter for the lightUpdate
- minor fixes across the board

resolve #2222
  • Loading branch information
mcspr authored Jan 14, 2021
1 parent c1243fd commit 8ceeebd
Show file tree
Hide file tree
Showing 74 changed files with 27,625 additions and 26,220 deletions.
27 changes: 8 additions & 19 deletions code/espurna/DebounceEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,27 @@
#include <functional>
#include <memory>

#include "compat.h"
#include "libs/DebounceEvent.h"

namespace debounce_event {

EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const types::Config& config, unsigned long debounce_delay, unsigned long repeat) :
_pin(pin),
EventEmitter::EventEmitter(BasePinPtr&& pin, types::EventHandler callback, const types::Config& config, unsigned long debounce_delay, unsigned long repeat) :
_pin(std::move(pin)),
_callback(callback),
_config(config),
_is_switch(config.mode == types::Mode::Switch),
_delay(debounce_delay),
_repeat(repeat)
{
if (!pin) return;
if (!_pin) return;

switch (_config.pin_mode) {
case types::PinMode::InputPullup:
_pin->pinMode(INPUT_PULLUP);
break;
case types::PinMode::InputPulldown:
// ESP8266 does not have INPUT_PULLDOWN definition, and instead
// has a GPIO16-specific INPUT_PULLDOWN_16:
// - https://github.com/esp8266/Arduino/issues/478
// - https://github.com/esp8266/Arduino/commit/1b3581d55ebf0f8c91e081f9af4cf7433d492ec9
#ifdef ESP8266
if (_pin->pin == 16) {
_pin->pinMode(INPUT_PULLDOWN_16);
} else {
_pin->pinMode(INPUT);
}
#else
_pin->pinMode(INPUT_PULLDOWN);
#endif
break;
case types::PinMode::Input:
_pin->pinMode(INPUT);
Expand All @@ -83,19 +72,19 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t
_value = _default_value;
}

EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned long delay, unsigned long repeat) :
EventEmitter(pin, nullptr, config, delay, repeat)
EventEmitter::EventEmitter(BasePinPtr&& pin, const types::Config& config, unsigned long delay, unsigned long repeat) :
EventEmitter(std::move(pin), nullptr, config, delay, repeat)
{}

bool EventEmitter::isPressed() {
return (_value != _default_value);
}

const types::Pin EventEmitter::getPin() const {
const BasePinPtr& EventEmitter::pin() const {
return _pin;
}

const types::Config EventEmitter::getConfig() const {
const types::Config& EventEmitter::config() const {
return _config;
}

Expand Down
12 changes: 5 additions & 7 deletions code/espurna/alexa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ void _alexaBrokerCallback(const String& topic, unsigned char id, unsigned int va
}

if (topic.equals(MQTT_TOPIC_RELAY)) {
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
if (id > 0) return;
#endif
if (id > 0) return;
_alexa.setState(id, value, value > 0 ? 255 : 0);
}

Expand All @@ -97,13 +95,13 @@ void alexaLoop() {
alexa_queue_element_t element = _alexa_queue.front();
DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s value: %d\n"), element.device_id, element.state ? "ON" : "OFF", element.value);

#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if (0 == element.device_id) {
relayStatus(0, element.state);
lightState(element.state);
} else {
lightState(element.device_id - 1, element.state);
lightChannel(element.device_id - 1, element.value);
lightUpdate(true, true);
lightUpdate();
}
#else
relayStatus(element.device_id, element.state);
Expand All @@ -130,7 +128,7 @@ void alexaSetup() {
}

// Lights
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE

// Global switch
_alexa.addDevice(hostname.c_str());
Expand Down
Loading

0 comments on commit 8ceeebd

Please sign in to comment.