From 2172bc6cb1264e67a869f6ed28f6a59abccff705 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 14 Mar 2020 00:59:45 +0300 Subject: [PATCH] btn: rename pin status / state -> value --- code/espurna/DebounceEvent.cpp | 22 +++++++++++----------- code/espurna/button.ino | 18 +++++++++--------- code/espurna/libs/DebounceEvent.h | 12 ++++++------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/code/espurna/DebounceEvent.cpp b/code/espurna/DebounceEvent.cpp index 8137b9db..6bcc0a0c 100644 --- a/code/espurna/DebounceEvent.cpp +++ b/code/espurna/DebounceEvent.cpp @@ -39,10 +39,10 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t _callback(callback), _config(config), _is_switch(config.mode == types::Mode::Switch), - _default_status(config.default_state == types::DefaultState::High), + _default_value(config.default_value == types::PinValue::High), _delay(debounce_delay), _repeat(repeat), - _status(false), + _value(false), _ready(false), _reset_count(true), _event_start(0), @@ -60,7 +60,7 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t // - https://github.com/esp8266/Arduino/commit/1b3581d55ebf0f8c91e081f9af4cf7433d492ec9 #ifdef ESP8266 if (_pin->pin == 16) { - _pin->pinMode(_default_status ? INPUT : INPUT_PULLDOWN_16); + _pin->pinMode(_default_value ? INPUT : INPUT_PULLDOWN_16); } else { _pin->pinMode(INPUT); } @@ -71,7 +71,7 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t _pin->pinMode(INPUT); } - _status = _is_switch ? (_pin->digitalRead() == (HIGH)) : _default_status; + _value = _is_switch ? (_pin->digitalRead() == (HIGH)) : _default_value; } EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned long delay, unsigned long repeat) : @@ -79,7 +79,7 @@ EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned {} bool EventEmitter::isPressed() { - return (_status != _default_status); + return (_value != _default_value); } const types::Pin EventEmitter::getPin() const { @@ -105,23 +105,23 @@ types::Event EventEmitter::loop() { static_assert((LOW) == 0, "Arduino API LOW is not 0"); auto event = types::EventNone; - bool status = _pin->digitalRead() == (HIGH); + bool value = _pin->digitalRead() == (HIGH); - if (status != _status) { + if (value != _value) { // TODO: check each loop instead of blocking? auto start = millis(); while (millis() - start < _delay) delay(1); - status = _pin->digitalRead() == (HIGH); - if (status != _status) { + value = _pin->digitalRead() == (HIGH); + if (value != _value) { - _status = !_status; + _value = !_value; if (_is_switch) { event = types::EventChanged; } else { - if (_status == _default_status) { + if (_value == _default_value) { _event_length = millis() - _event_start; _ready = true; } else { diff --git a/code/espurna/button.ino b/code/espurna/button.ino index d9353a52..ec549436 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -40,13 +40,13 @@ debounce_event::types::Mode convert(const String& value) { } template<> -debounce_event::types::DefaultState convert(const String& value) { +debounce_event::types::PinValue convert(const String& value) { switch (value.toInt()) { case 0: - return debounce_event::types::DefaultState::Low; + return debounce_event::types::PinValue::Low; case 1: default: - return debounce_event::types::DefaultState::High; + return debounce_event::types::PinValue::High; } } @@ -74,15 +74,15 @@ constexpr const debounce_event::types::Config _buttonDecodeConfigBitmask(const u ? debounce_event::types::Mode::Pushbutton : debounce_event::types::Mode::Switch), ((bitmask & ButtonMask::DefaultHigh) - ? debounce_event::types::DefaultState::High - : debounce_event::types::DefaultState::Low), + ? debounce_event::types::PinValue::High + : debounce_event::types::PinValue::Low), ((bitmask & ButtonMask::SetPullup) ? debounce_event::types::PinMode::InputPullup : (bitmask & ButtonMask::SetPulldown) ? debounce_event::types::PinMode::InputPullup : debounce_event::types::PinMode::Input) }; } -constexpr const uint16_t _buttonDecodeEventAction(const button_actions_t& actions, button_event_t event) { +constexpr const button_action_t _buttonDecodeEventAction(const button_actions_t& actions, button_event_t event) { return ( (event == button_event_t::Pressed) ? actions.pressed : (event == button_event_t::Click) ? actions.click : @@ -120,7 +120,7 @@ debounce_event::types::Config _buttonConfig(unsigned char index) { const auto config = _buttonDecodeConfigBitmask(_buttonConfigBitmask(index)); return { getSetting({"btnMode", index}, config.mode), - getSetting({"btnDefState", index}, config.default_state), + getSetting({"btnDefVal", index}, config.default_value), getSetting({"btnPinMode", index}, config.pin_mode) }; } @@ -252,7 +252,7 @@ void _buttonWebSocketOnConnected(JsonObject& root) { schema.add("GPIO"); schema.add("Mode"); - schema.add("DefState"); + schema.add("DefVal"); schema.add("PinMode"); schema.add("Relay"); @@ -284,7 +284,7 @@ void _buttonWebSocketOnConnected(JsonObject& root) { button.add(getSetting({"btnGPIO", index}, _buttonPin(index))); const auto config = _buttonConfig(index); button.add(static_cast(config.mode)); - button.add(static_cast(config.default_state)); + button.add(static_cast(config.default_value)); button.add(static_cast(config.pin_mode)); } else { button.add(GPIO_NONE); diff --git a/code/espurna/libs/DebounceEvent.h b/code/espurna/libs/DebounceEvent.h index 45860bbc..5b5d0e78 100644 --- a/code/espurna/libs/DebounceEvent.h +++ b/code/espurna/libs/DebounceEvent.h @@ -50,7 +50,7 @@ namespace types { Switch }; - enum class DefaultState { + enum class PinValue { Low, High }; @@ -63,7 +63,7 @@ namespace types { struct Config { Mode mode; - DefaultState default_state; + PinValue default_value; PinMode pin_mode; }; @@ -83,8 +83,8 @@ class EventEmitter { public: - EventEmitter(types::Pin pin, const types::Config& config = {types::Mode::Pushbutton, types::DefaultState::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay); - EventEmitter(types::Pin pin, types::EventHandler callback, const types::Config& = {types::Mode::Pushbutton, types::DefaultState::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay); + EventEmitter(types::Pin pin, const types::Config& config = {types::Mode::Pushbutton, types::PinValue::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay); + EventEmitter(types::Pin pin, types::EventHandler callback, const types::Config& = {types::Mode::Pushbutton, types::PinValue::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay); types::Event loop(); bool isPressed(); @@ -103,12 +103,12 @@ class EventEmitter { const types::Config _config; const bool _is_switch; - const bool _default_status; + const bool _default_value; const unsigned long _delay; const unsigned long _repeat; - bool _status; + bool _value; bool _ready; bool _reset_count;