Browse Source

btn: option to read default value on initialization

- provide a generic way to read default status of the pin after boot
  allows us to use switch as both on and off, independent of the position on boot
- add BUTTON_DEFAULT_LOW & BUTTON_DEFAULT_BOOT (now it's not just BUTTON_DEFAULT_HIGH)
  wiki described raw numbers for some reason :/ this will break that
- clean-up 'constexpr const' and 'const' function args, both are redundant
- clean-up debounce event member defaults, put things in the header
mcspr-patch-1
Maxim Prokhorov 3 years ago
parent
commit
f497312f29
5 changed files with 86 additions and 67 deletions
  1. +31
    -20
      code/espurna/DebounceEvent.cpp
  2. +14
    -9
      code/espurna/button.cpp
  3. +27
    -27
      code/espurna/button_config.h
  4. +2
    -0
      code/espurna/config/types.h
  5. +12
    -11
      code/espurna/libs/DebounceEvent.h

+ 31
- 20
code/espurna/DebounceEvent.cpp View File

@ -39,37 +39,48 @@ EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const t
_callback(callback),
_config(config),
_is_switch(config.mode == types::Mode::Switch),
_default_value(config.default_value == types::PinValue::High),
_delay(debounce_delay),
_repeat(repeat),
_value(_default_value),
_ready(false),
_reset_count(true),
_event_start(0),
_event_length(0),
_event_count(0)
_repeat(repeat)
{
if (!pin) return;
if (_config.pin_mode == types::PinMode::InputPullup) {
switch (_config.pin_mode) {
case types::PinMode::InputPullup:
_pin->pinMode(INPUT_PULLUP);
} else if (_config.pin_mode == types::PinMode::InputPulldown) {
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(_default_value ? INPUT : INPUT_PULLDOWN_16);
} else {
_pin->pinMode(INPUT);
}
#else
_pin->pinMode(INPUT_PULLDOWN);
#endif
} else {
#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);
break;
}
switch (config.default_value) {
case types::PinValue::Low:
_default_value = false;
break;
case types::PinValue::High:
_default_value = true;
break;
case types::PinValue::Initial:
_default_value = ((HIGH) == _pin->digitalRead());
break;
}
_value = _default_value;
}
EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned long delay, unsigned long repeat) :


+ 14
- 9
code/espurna/button.cpp View File

@ -65,11 +65,13 @@ String serialize(const debounce_event::types::Mode& value) {
template<>
debounce_event::types::PinValue convert(const String& value) {
switch (value.toInt()) {
case 0:
return debounce_event::types::PinValue::Low;
case 1:
default:
return debounce_event::types::PinValue::High;
case 2:
return debounce_event::types::PinValue::Initial;
default:
case 0:
return debounce_event::types::PinValue::Low;
}
}
@ -81,9 +83,11 @@ String serialize(const debounce_event::types::PinValue& value) {
result = "0";
break;
case debounce_event::types::PinValue::High:
default:
result = "1";
break;
case debounce_event::types::PinValue::Initial:
result = "2";
break;
}
return result;
}
@ -124,13 +128,14 @@ String serialize(const debounce_event::types::PinMode& mode) {
// -----------------------------------------------------------------------------
constexpr const debounce_event::types::Config _buttonDecodeConfigBitmask(const unsigned class="kt">char bitmask) {
constexpr debounce_event::types::Config _buttonDecodeConfigBitmask(int bitmask) {
return {
((bitmask & ButtonMask::Pushbutton)
? debounce_event::types::Mode::Pushbutton
: debounce_event::types::Mode::Switch),
((bitmask & ButtonMask::DefaultHigh)
? debounce_event::types::PinValue::High
((bitmask & ButtonMask::DefaultLow) ? debounce_event::types::PinValue::Low
: (bitmask & ButtonMask::DefaultHigh) ? debounce_event::types::PinValue::High
: (bitmask & ButtonMask::DefaultBoot) ? debounce_event::types::PinValue::Initial
: debounce_event::types::PinValue::Low),
((bitmask & ButtonMask::SetPullup) ? debounce_event::types::PinMode::InputPullup
: (bitmask & ButtonMask::SetPulldown) ? debounce_event::types::PinMode::InputPulldown
@ -138,7 +143,7 @@ constexpr const debounce_event::types::Config _buttonDecodeConfigBitmask(const u
};
}
constexpr const button_action_t _buttonDecodeEventAction(const button_actions_t& actions, button_event_t event) {
constexpr 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::Released) ? actions.released :
@ -150,7 +155,7 @@ constexpr const button_action_t _buttonDecodeEventAction(const button_actions_t&
);
}
constexpr const button_event_t _buttonMapReleased(uint8_t count, unsigned long length, unsigned long lngclick_delay, unsigned long lnglngclick_delay) {
constexpr button_event_t _buttonMapReleased(uint8_t count, unsigned long length, unsigned long lngclick_delay, unsigned long lnglngclick_delay) {
return (
(0 == count) ? button_event_t::Released :
(1 == count) ? (


+ 27
- 27
code/espurna/button_config.h View File

@ -10,17 +10,17 @@ BUTTON MODULE
namespace ButtonMask {
enum {
Pushbutton = 1 << 0,
Switch = 1 << 1,
DefaultHigh = 1 << 2,
SetPullup = 1 << 3,
SetPulldown = 1 << 4
};
constexpr int Pushbutton { 1 << 0 };
constexpr int Switch { 1 << 1 };
constexpr int DefaultLow { 1 << 2 };
constexpr int DefaultHigh { 1 << 3 };
constexpr int DefaultBoot { 1 << 4 };
constexpr int SetPullup { 1 << 5 };
constexpr int SetPulldown { 1 << 6 };
} // namespace ButtonMask
constexpr const unsigned char _buttonPin(unsigned char index) {
constexpr unsigned char _buttonPin(unsigned char index) {
return (
(index == 0) ? BUTTON1_PIN :
(index == 1) ? BUTTON2_PIN :
@ -33,7 +33,7 @@ constexpr const unsigned char _buttonPin(unsigned char index) {
);
}
constexpr const unsigned class="kt">char _buttonConfigBitmask(unsigned char index) {
constexpr int _buttonConfigBitmask(unsigned char index) {
return (
(index == 0) ? (BUTTON1_CONFIG) :
(index == 1) ? (BUTTON2_CONFIG) :
@ -46,7 +46,7 @@ constexpr const unsigned char _buttonConfigBitmask(unsigned char index) {
);
}
constexpr const unsigned char _buttonRelease(unsigned char index) {
constexpr unsigned char _buttonRelease(unsigned char index) {
return (
(index == 0) ? BUTTON1_RELEASE :
(index == 1) ? BUTTON2_RELEASE :
@ -59,7 +59,7 @@ constexpr const unsigned char _buttonRelease(unsigned char index) {
);
}
constexpr const unsigned char _buttonPress(unsigned char index) {
constexpr unsigned char _buttonPress(unsigned char index) {
return (
(index == 0) ? BUTTON1_PRESS :
(index == 1) ? BUTTON2_PRESS :
@ -72,7 +72,7 @@ constexpr const unsigned char _buttonPress(unsigned char index) {
);
}
constexpr const unsigned char _buttonClick(unsigned char index) {
constexpr unsigned char _buttonClick(unsigned char index) {
return (
(index == 0) ? BUTTON1_CLICK :
(index == 1) ? BUTTON2_CLICK :
@ -85,7 +85,7 @@ constexpr const unsigned char _buttonClick(unsigned char index) {
);
}
constexpr const unsigned char _buttonDoubleClick(unsigned char index) {
constexpr unsigned char _buttonDoubleClick(unsigned char index) {
return (
(index == 0) ? BUTTON1_DBLCLICK :
(index == 1) ? BUTTON2_DBLCLICK :
@ -98,7 +98,7 @@ constexpr const unsigned char _buttonDoubleClick(unsigned char index) {
);
}
constexpr const unsigned char _buttonTripleClick(unsigned char index) {
constexpr unsigned char _buttonTripleClick(unsigned char index) {
return (
(index == 0) ? BUTTON1_TRIPLECLICK :
(index == 1) ? BUTTON2_TRIPLECLICK :
@ -111,7 +111,7 @@ constexpr const unsigned char _buttonTripleClick(unsigned char index) {
);
}
constexpr const unsigned char _buttonLongClick(unsigned char index) {
constexpr unsigned char _buttonLongClick(unsigned char index) {
return (
(index == 0) ? BUTTON1_LNGCLICK :
(index == 1) ? BUTTON2_LNGCLICK :
@ -124,7 +124,7 @@ constexpr const unsigned char _buttonLongClick(unsigned char index) {
);
}
constexpr const unsigned char _buttonLongLongClick(unsigned char index) {
constexpr unsigned char _buttonLongLongClick(unsigned char index) {
return (
(index == 0) ? BUTTON1_LNGLNGCLICK :
(index == 1) ? BUTTON2_LNGLNGCLICK :
@ -137,7 +137,7 @@ constexpr const unsigned char _buttonLongLongClick(unsigned char index) {
);
}
constexpr const unsigned char _buttonRelay(unsigned char index) {
constexpr unsigned char _buttonRelay(unsigned char index) {
return (
(index == 0) ? (BUTTON1_RELAY - 1) :
(index == 1) ? (BUTTON2_RELAY - 1) :
@ -150,11 +150,11 @@ constexpr const unsigned char _buttonRelay(unsigned char index) {
);
}
constexpr const unsigned long _buttonDebounceDelay() {
constexpr unsigned long _buttonDebounceDelay() {
return BUTTON_DEBOUNCE_DELAY;
}
constexpr const unsigned long _buttonDebounceDelay(unsigned char index) {
constexpr unsigned long _buttonDebounceDelay(unsigned char index) {
return (
(index == 0) ? BUTTON1_DEBOUNCE_DELAY :
(index == 1) ? BUTTON2_DEBOUNCE_DELAY :
@ -167,11 +167,11 @@ constexpr const unsigned long _buttonDebounceDelay(unsigned char index) {
);
}
constexpr const unsigned long _buttonRepeatDelay() {
constexpr unsigned long _buttonRepeatDelay() {
return BUTTON_REPEAT_DELAY;
}
constexpr const unsigned long _buttonRepeatDelay(unsigned char index) {
constexpr unsigned long _buttonRepeatDelay(unsigned char index) {
return (
(index == 0) ? BUTTON1_REPEAT_DELAY :
(index == 1) ? BUTTON2_REPEAT_DELAY :
@ -184,11 +184,11 @@ constexpr const unsigned long _buttonRepeatDelay(unsigned char index) {
);
}
constexpr const unsigned long _buttonLongClickDelay() {
constexpr unsigned long _buttonLongClickDelay() {
return BUTTON_LNGCLICK_DELAY;
}
constexpr const unsigned long _buttonLongClickDelay(unsigned char index) {
constexpr unsigned long _buttonLongClickDelay(unsigned char index) {
return (
(index == 0) ? BUTTON1_LNGCLICK_DELAY :
(index == 1) ? BUTTON2_LNGCLICK_DELAY :
@ -201,11 +201,11 @@ constexpr const unsigned long _buttonLongClickDelay(unsigned char index) {
);
}
constexpr const unsigned long _buttonLongLongClickDelay() {
constexpr unsigned long _buttonLongLongClickDelay() {
return BUTTON_LNGLNGCLICK_DELAY;
}
constexpr const unsigned long _buttonLongLongClickDelay(unsigned char index) {
constexpr unsigned long _buttonLongLongClickDelay(unsigned char index) {
return (
(index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
(index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
@ -218,7 +218,7 @@ constexpr const unsigned long _buttonLongLongClickDelay(unsigned char index) {
);
}
constexpr const bool _buttonMqttSendAllEvents(unsigned char index) {
constexpr bool _buttonMqttSendAllEvents(unsigned char index) {
return (
(index == 0) ? (1 == BUTTON1_MQTT_SEND_ALL_EVENTS) :
(index == 1) ? (1 == BUTTON2_MQTT_SEND_ALL_EVENTS) :
@ -231,7 +231,7 @@ constexpr const bool _buttonMqttSendAllEvents(unsigned char index) {
);
}
constexpr const bool _buttonMqttRetain(unsigned char index) {
constexpr bool _buttonMqttRetain(unsigned char index) {
return (
(index == 0) ? (1 == BUTTON1_MQTT_RETAIN) :
(index == 1) ? (1 == BUTTON2_MQTT_RETAIN) :


+ 2
- 0
code/espurna/config/types.h View File

@ -53,7 +53,9 @@
// compat definitions for DebounceEvent
#define BUTTON_PUSHBUTTON ButtonMask::Pushbutton
#define BUTTON_SWITCH ButtonMask::Switch
#define BUTTON_DEFAULT_LOW ButtonMask::DefaultLow
#define BUTTON_DEFAULT_HIGH ButtonMask::DefaultHigh
#define BUTTON_DEFAULT_BOOT ButtonMask::DefaultBoot
#define BUTTON_SET_PULLUP ButtonMask::SetPullup
#define BUTTON_SET_PULLDOWN ButtonMask::SetPulldown


+ 12
- 11
code/espurna/libs/DebounceEvent.h View File

@ -52,7 +52,8 @@ namespace types {
enum class PinValue {
Low,
High
High,
Initial
};
enum class PinMode {
@ -101,20 +102,20 @@ class EventEmitter {
const types::Config _config;
const bool _is_switch;
const bool _default_value;
const bool _is_switch { false };
const unsigned long _delay;
const unsigned long _repeat;
const unsigned long _delay { 0ul };
const unsigned long _repeat { 0ul };
bool _value;
bool _default_value { true };
bool _value { true };
bool _ready;
bool _reset_count;
bool _ready { false };
bool _reset_count { true };
unsigned long _event_start;
unsigned long _event_length;
unsigned char _event_count;
unsigned long _event_start { 0ul };
unsigned long _event_length { 0ul };
unsigned char _event_count { 0ul };
};


Loading…
Cancel
Save