Browse Source

btn: rename pin status / state -> value

mcspr-patch-1
Maxim Prokhorov 4 years ago
committed by Max Prokhorov
parent
commit
2172bc6cb1
3 changed files with 26 additions and 26 deletions
  1. +11
    -11
      code/espurna/DebounceEvent.cpp
  2. +9
    -9
      code/espurna/button.ino
  3. +6
    -6
      code/espurna/libs/DebounceEvent.h

+ 11
- 11
code/espurna/DebounceEvent.cpp View File

@ -39,10 +39,10 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t
_callback(callback), _callback(callback),
_config(config), _config(config),
_is_switch(config.mode == types::Mode::Switch), _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), _delay(debounce_delay),
_repeat(repeat), _repeat(repeat),
_status(false),
_value(false),
_ready(false), _ready(false),
_reset_count(true), _reset_count(true),
_event_start(0), _event_start(0),
@ -60,7 +60,7 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t
// - https://github.com/esp8266/Arduino/commit/1b3581d55ebf0f8c91e081f9af4cf7433d492ec9 // - https://github.com/esp8266/Arduino/commit/1b3581d55ebf0f8c91e081f9af4cf7433d492ec9
#ifdef ESP8266 #ifdef ESP8266
if (_pin->pin == 16) { if (_pin->pin == 16) {
_pin->pinMode(_default_status ? INPUT : INPUT_PULLDOWN_16);
_pin->pinMode(_default_value ? INPUT : INPUT_PULLDOWN_16);
} else { } else {
_pin->pinMode(INPUT); _pin->pinMode(INPUT);
} }
@ -71,7 +71,7 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t
_pin->pinMode(INPUT); _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) : 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() { bool EventEmitter::isPressed() {
return (_status != _default_status);
return (_value != _default_value);
} }
const types::Pin EventEmitter::getPin() const { const types::Pin EventEmitter::getPin() const {
@ -105,23 +105,23 @@ types::Event EventEmitter::loop() {
static_assert((LOW) == 0, "Arduino API LOW is not 0"); static_assert((LOW) == 0, "Arduino API LOW is not 0");
auto event = types::EventNone; 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? // TODO: check each loop instead of blocking?
auto start = millis(); auto start = millis();
while (millis() - start < _delay) delay(1); 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) { if (_is_switch) {
event = types::EventChanged; event = types::EventChanged;
} else { } else {
if (_status == _default_status) {
if (_value == _default_value) {
_event_length = millis() - _event_start; _event_length = millis() - _event_start;
_ready = true; _ready = true;
} else { } else {


+ 9
- 9
code/espurna/button.ino View File

@ -40,13 +40,13 @@ debounce_event::types::Mode convert(const String& value) {
} }
template<> template<>
debounce_event::types::DefaultState convert(const String& value) {
debounce_event::types::PinValue convert(const String& value) {
switch (value.toInt()) { switch (value.toInt()) {
case 0: case 0:
return debounce_event::types::DefaultState::Low;
return debounce_event::types::PinValue::Low;
case 1: case 1:
default: 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::Pushbutton
: debounce_event::types::Mode::Switch), : debounce_event::types::Mode::Switch),
((bitmask & ButtonMask::DefaultHigh) ((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) ((bitmask & ButtonMask::SetPullup)
? debounce_event::types::PinMode::InputPullup : (bitmask & ButtonMask::SetPulldown) ? debounce_event::types::PinMode::InputPullup : (bitmask & ButtonMask::SetPulldown)
? debounce_event::types::PinMode::InputPullup : debounce_event::types::PinMode::Input) ? 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 ( return (
(event == button_event_t::Pressed) ? actions.pressed : (event == button_event_t::Pressed) ? actions.pressed :
(event == button_event_t::Click) ? actions.click : (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)); const auto config = _buttonDecodeConfigBitmask(_buttonConfigBitmask(index));
return { return {
getSetting({"btnMode", index}, config.mode), getSetting({"btnMode", index}, config.mode),
getSetting({"btnDefState", index}, config.default_state),
getSetting({"btnDefVal", index}, config.default_value),
getSetting({"btnPinMode", index}, config.pin_mode) getSetting({"btnPinMode", index}, config.pin_mode)
}; };
} }
@ -252,7 +252,7 @@ void _buttonWebSocketOnConnected(JsonObject& root) {
schema.add("GPIO"); schema.add("GPIO");
schema.add("Mode"); schema.add("Mode");
schema.add("DefState");
schema.add("DefVal");
schema.add("PinMode"); schema.add("PinMode");
schema.add("Relay"); schema.add("Relay");
@ -284,7 +284,7 @@ void _buttonWebSocketOnConnected(JsonObject& root) {
button.add(getSetting({"btnGPIO", index}, _buttonPin(index))); button.add(getSetting({"btnGPIO", index}, _buttonPin(index)));
const auto config = _buttonConfig(index); const auto config = _buttonConfig(index);
button.add(static_cast<int>(config.mode)); button.add(static_cast<int>(config.mode));
button.add(static_cast<int>(config.default_state));
button.add(static_cast<int>(config.default_value));
button.add(static_cast<int>(config.pin_mode)); button.add(static_cast<int>(config.pin_mode));
} else { } else {
button.add(GPIO_NONE); button.add(GPIO_NONE);


+ 6
- 6
code/espurna/libs/DebounceEvent.h View File

@ -50,7 +50,7 @@ namespace types {
Switch Switch
}; };
enum class DefaultState {
enum class PinValue {
Low, Low,
High High
}; };
@ -63,7 +63,7 @@ namespace types {
struct Config { struct Config {
Mode mode; Mode mode;
DefaultState default_state;
PinValue default_value;
PinMode pin_mode; PinMode pin_mode;
}; };
@ -83,8 +83,8 @@ class EventEmitter {
public: 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(); types::Event loop();
bool isPressed(); bool isPressed();
@ -103,12 +103,12 @@ class EventEmitter {
const types::Config _config; const types::Config _config;
const bool _is_switch; const bool _is_switch;
const bool _default_status;
const bool _default_value;
const unsigned long _delay; const unsigned long _delay;
const unsigned long _repeat; const unsigned long _repeat;
bool _status;
bool _value;
bool _ready; bool _ready;
bool _reset_count; bool _reset_count;


Loading…
Cancel
Save