Skip to content

Commit

Permalink
system: refactor build configurations
Browse files Browse the repository at this point in the history
Namespace build configurations of modules, make more things into constexpr
(not fully finished though)

Unify code using ...Count() to parse IDs

Avoid using unsigned char aka uint8_t as index, prefer size_t
as most code already uses it anyway. Making sure we never accidentally
truncate the value or try to read it as 32bit-wide. Also, simplify
access to built in containers, since those use the wide type as well.

Renames led and button types, more consistent initialization and field access.
  • Loading branch information
mcspr committed Mar 31, 2021
1 parent ec220b7 commit f921163
Show file tree
Hide file tree
Showing 24 changed files with 1,062 additions and 910 deletions.
456 changes: 199 additions & 257 deletions code/espurna/button.cpp

Large diffs are not rendered by default.

33 changes: 15 additions & 18 deletions code/espurna/button.h
Expand Up @@ -10,8 +10,6 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

#include "espurna.h"

#include "broker.h"

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

Expand All @@ -28,7 +26,7 @@ enum class ButtonProvider : int {
Analog
};

enum class button_event_t {
enum class ButtonEvent {
None,
Pressed,
Released,
Expand Down Expand Up @@ -71,9 +69,9 @@ struct ButtonActions {
ButtonAction trplclick;
};

struct button_event_delays_t {
button_event_delays_t();
button_event_delays_t(unsigned long debounce, unsigned long repeat, unsigned long lngclick, unsigned long lnglngclick);
struct ButtonEventDelays {
ButtonEventDelays();
ButtonEventDelays(unsigned long debounce, unsigned long repeat, unsigned long lngclick, unsigned long lnglngclick);

unsigned long debounce;
unsigned long repeat;
Expand All @@ -82,28 +80,27 @@ struct button_event_delays_t {
};

struct button_t {
button_t(ButtonActions&& actions, button_event_delays_t&& delays);
button_t(ButtonActions&& actions, ButtonEventDelays&& delays);
button_t(BasePinPtr&& pin, const debounce_event::types::Config& config,
ButtonActions&& actions, button_event_delays_t&& delays);
ButtonActions&& actions, ButtonEventDelays&& delays);

bool state();
button_event_t loop();
ButtonEvent loop();

std::unique_ptr<debounce_event::EventEmitter> event_emitter;

ButtonActions actions;
button_event_delays_t event_delays;
ButtonEventDelays event_delays;
};

BrokerDeclare(ButtonBroker, void(unsigned char id, button_event_t event));

using ButtonCustomAction = void(*)(unsigned char id, button_event_t event);
void buttonSetCustomAction(ButtonCustomAction);
using ButtonEventHandler = void(*)(size_t id, ButtonEvent event);
void buttonSetCustomAction(ButtonEventHandler);
void buttonSetNotifyAction(ButtonEventHandler);

bool buttonState(unsigned char id);
ButtonAction buttonAction(unsigned char id, const button_event_t event);
bool buttonState(size_t id);
ButtonAction buttonAction(size_t id, const ButtonEvent event);

void buttonEvent(unsigned char id, button_event_t event);
void buttonEvent(size_t id, ButtonEvent event);

unsigned char buttonCount();
size_t buttonCount();
void buttonSetup();
103 changes: 74 additions & 29 deletions code/espurna/button_config.h
Expand Up @@ -20,7 +20,10 @@ constexpr int SetPulldown { 1 << 6 };

} // namespace ButtonMask

constexpr unsigned char _buttonPin(unsigned char index) {
namespace button {
namespace build {

constexpr size_t pin(size_t index) {
return (
(index == 0) ? BUTTON1_PIN :
(index == 1) ? BUTTON2_PIN :
Expand All @@ -33,7 +36,7 @@ constexpr unsigned char _buttonPin(unsigned char index) {
);
}

constexpr GpioType _buttonPinType(unsigned char index) {
constexpr GpioType pinType(size_t index) {
return (
(index == 0) ? BUTTON1_PIN_TYPE :
(index == 1) ? BUTTON2_PIN_TYPE :
Expand All @@ -46,7 +49,7 @@ constexpr GpioType _buttonPinType(unsigned char index) {
);
}

constexpr int _buttonConfigBitmask(unsigned char index) {
constexpr int configBitmask(size_t index) {
return (
(index == 0) ? (BUTTON1_CONFIG) :
(index == 1) ? (BUTTON2_CONFIG) :
Expand All @@ -59,7 +62,38 @@ constexpr int _buttonConfigBitmask(unsigned char index) {
);
}

constexpr ButtonAction _buttonRelease(unsigned char index) {
namespace internal {

constexpr debounce_event::types::Config decode(int bitmask) {
return {
((bitmask & ButtonMask::Pushbutton)
? debounce_event::types::Mode::Pushbutton
: debounce_event::types::Mode::Switch),
((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
: debounce_event::types::PinMode::Input)
};
}

} // namespace internal

constexpr debounce_event::types::Mode mode(size_t index) {
return internal::decode(configBitmask(index)).mode;
}

constexpr debounce_event::types::PinValue defaultValue(size_t index) {
return internal::decode(configBitmask(index)).default_value;
}

constexpr debounce_event::types::PinMode pinMode(size_t index) {
return internal::decode(configBitmask(index)).pin_mode;
}

constexpr ButtonAction release(size_t index) {
return (
(index == 0) ? BUTTON1_RELEASE :
(index == 1) ? BUTTON2_RELEASE :
Expand All @@ -72,7 +106,7 @@ constexpr ButtonAction _buttonRelease(unsigned char index) {
);
}

constexpr ButtonAction _buttonPress(unsigned char index) {
constexpr ButtonAction press(size_t index) {
return (
(index == 0) ? BUTTON1_PRESS :
(index == 1) ? BUTTON2_PRESS :
Expand All @@ -85,7 +119,7 @@ constexpr ButtonAction _buttonPress(unsigned char index) {
);
}

constexpr ButtonAction _buttonClick(unsigned char index) {
constexpr ButtonAction click(size_t index) {
return (
(index == 0) ? BUTTON1_CLICK :
(index == 1) ? BUTTON2_CLICK :
Expand All @@ -98,7 +132,7 @@ constexpr ButtonAction _buttonClick(unsigned char index) {
);
}

constexpr ButtonAction _buttonDoubleClick(unsigned char index) {
constexpr ButtonAction doubleClick(size_t index) {
return (
(index == 0) ? BUTTON1_DBLCLICK :
(index == 1) ? BUTTON2_DBLCLICK :
Expand All @@ -111,7 +145,7 @@ constexpr ButtonAction _buttonDoubleClick(unsigned char index) {
);
}

constexpr ButtonAction _buttonTripleClick(unsigned char index) {
constexpr ButtonAction tripleClick(size_t index) {
return (
(index == 0) ? BUTTON1_TRIPLECLICK :
(index == 1) ? BUTTON2_TRIPLECLICK :
Expand All @@ -124,7 +158,7 @@ constexpr ButtonAction _buttonTripleClick(unsigned char index) {
);
}

constexpr ButtonAction _buttonLongClick(unsigned char index) {
constexpr ButtonAction longClick(size_t index) {
return (
(index == 0) ? BUTTON1_LNGCLICK :
(index == 1) ? BUTTON2_LNGCLICK :
Expand All @@ -137,7 +171,7 @@ constexpr ButtonAction _buttonLongClick(unsigned char index) {
);
}

constexpr ButtonAction _buttonLongLongClick(unsigned char index) {
constexpr ButtonAction longLongClick(size_t index) {
return (
(index == 0) ? BUTTON1_LNGLNGCLICK :
(index == 1) ? BUTTON2_LNGLNGCLICK :
Expand All @@ -150,7 +184,7 @@ constexpr ButtonAction _buttonLongLongClick(unsigned char index) {
);
}

constexpr unsigned char _buttonRelay(unsigned char index) {
constexpr size_t relay(size_t index) {
return (
(index == 0) ? (BUTTON1_RELAY - 1) :
(index == 1) ? (BUTTON2_RELAY - 1) :
Expand All @@ -163,11 +197,11 @@ constexpr unsigned char _buttonRelay(unsigned char index) {
);
}

constexpr unsigned long _buttonDebounceDelay() {
constexpr unsigned long debounceDelay() {
return BUTTON_DEBOUNCE_DELAY;
}

constexpr unsigned long _buttonDebounceDelay(unsigned char index) {
constexpr unsigned long debounceDelay(size_t index) {
return (
(index == 0) ? BUTTON1_DEBOUNCE_DELAY :
(index == 1) ? BUTTON2_DEBOUNCE_DELAY :
Expand All @@ -176,15 +210,15 @@ constexpr unsigned long _buttonDebounceDelay(unsigned char index) {
(index == 4) ? BUTTON5_DEBOUNCE_DELAY :
(index == 5) ? BUTTON6_DEBOUNCE_DELAY :
(index == 6) ? BUTTON7_DEBOUNCE_DELAY :
(index == 7) ? BUTTON8_DEBOUNCE_DELAY : _buttonDebounceDelay()
(index == 7) ? BUTTON8_DEBOUNCE_DELAY : debounceDelay()
);
}

constexpr unsigned long _buttonRepeatDelay() {
constexpr unsigned long repeatDelay() {
return BUTTON_REPEAT_DELAY;
}

constexpr unsigned long _buttonRepeatDelay(unsigned char index) {
constexpr unsigned long repeatDelay(size_t index) {
return (
(index == 0) ? BUTTON1_REPEAT_DELAY :
(index == 1) ? BUTTON2_REPEAT_DELAY :
Expand All @@ -193,15 +227,15 @@ constexpr unsigned long _buttonRepeatDelay(unsigned char index) {
(index == 4) ? BUTTON5_REPEAT_DELAY :
(index == 5) ? BUTTON6_REPEAT_DELAY :
(index == 6) ? BUTTON7_REPEAT_DELAY :
(index == 7) ? BUTTON8_REPEAT_DELAY : _buttonRepeatDelay()
(index == 7) ? BUTTON8_REPEAT_DELAY : repeatDelay()
);
}

constexpr unsigned long _buttonLongClickDelay() {
constexpr unsigned long longClickDelay() {
return BUTTON_LNGCLICK_DELAY;
}

constexpr unsigned long _buttonLongClickDelay(unsigned char index) {
constexpr unsigned long longClickDelay(size_t index) {
return (
(index == 0) ? BUTTON1_LNGCLICK_DELAY :
(index == 1) ? BUTTON2_LNGCLICK_DELAY :
Expand All @@ -210,15 +244,15 @@ constexpr unsigned long _buttonLongClickDelay(unsigned char index) {
(index == 4) ? BUTTON5_LNGCLICK_DELAY :
(index == 5) ? BUTTON6_LNGCLICK_DELAY :
(index == 6) ? BUTTON7_LNGCLICK_DELAY :
(index == 7) ? BUTTON8_LNGCLICK_DELAY : _buttonLongClickDelay()
(index == 7) ? BUTTON8_LNGCLICK_DELAY : longClickDelay()
);
}

constexpr unsigned long _buttonLongLongClickDelay() {
constexpr unsigned long longLongClickDelay() {
return BUTTON_LNGLNGCLICK_DELAY;
}

constexpr unsigned long _buttonLongLongClickDelay(unsigned char index) {
constexpr unsigned long longLongClickDelay(size_t index) {
return (
(index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
(index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
Expand All @@ -227,11 +261,15 @@ constexpr unsigned long _buttonLongLongClickDelay(unsigned char index) {
(index == 4) ? BUTTON5_LNGLNGCLICK_DELAY :
(index == 5) ? BUTTON6_LNGLNGCLICK_DELAY :
(index == 6) ? BUTTON7_LNGLNGCLICK_DELAY :
(index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : _buttonLongLongClickDelay()
(index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : longLongClickDelay()
);
}

constexpr bool _buttonMqttSendAllEvents(unsigned char index) {
constexpr bool mqttSendAllEvents() {
return (1 == BUTTON_MQTT_SEND_ALL_EVENTS);
}

constexpr bool mqttSendAllEvents(size_t index) {
return (
(index == 0) ? (1 == BUTTON1_MQTT_SEND_ALL_EVENTS) :
(index == 1) ? (1 == BUTTON2_MQTT_SEND_ALL_EVENTS) :
Expand All @@ -240,11 +278,15 @@ constexpr bool _buttonMqttSendAllEvents(unsigned char index) {
(index == 4) ? (1 == BUTTON5_MQTT_SEND_ALL_EVENTS) :
(index == 5) ? (1 == BUTTON6_MQTT_SEND_ALL_EVENTS) :
(index == 6) ? (1 == BUTTON7_MQTT_SEND_ALL_EVENTS) :
(index == 7) ? (1 == BUTTON8_MQTT_SEND_ALL_EVENTS) : (1 == BUTTON_MQTT_SEND_ALL_EVENTS)
(index == 7) ? (1 == BUTTON8_MQTT_SEND_ALL_EVENTS) : mqttSendAllEvents()
);
}

constexpr bool _buttonMqttRetain(unsigned char index) {
constexpr bool mqttRetain() {
return (1 == BUTTON_MQTT_RETAIN);
}

constexpr bool mqttRetain(size_t index) {
return (
(index == 0) ? (1 == BUTTON1_MQTT_RETAIN) :
(index == 1) ? (1 == BUTTON2_MQTT_RETAIN) :
Expand All @@ -253,11 +295,11 @@ constexpr bool _buttonMqttRetain(unsigned char index) {
(index == 4) ? (1 == BUTTON5_MQTT_RETAIN) :
(index == 5) ? (1 == BUTTON6_MQTT_RETAIN) :
(index == 6) ? (1 == BUTTON7_MQTT_RETAIN) :
(index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : (1 == BUTTON_MQTT_RETAIN)
(index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : mqttRetain()
);
}

constexpr ButtonProvider _buttonProvider(unsigned char index) {
constexpr ButtonProvider provider(size_t index) {
return (
(index == 0) ? (BUTTON1_PROVIDER) :
(index == 1) ? (BUTTON2_PROVIDER) :
Expand All @@ -270,7 +312,7 @@ constexpr ButtonProvider _buttonProvider(unsigned char index) {
);
}

constexpr int _buttonAnalogLevel(unsigned char index) {
constexpr int analogLevel(size_t index) {
return (
(index == 0) ? (BUTTON1_ANALOG_LEVEL) :
(index == 1) ? (BUTTON2_ANALOG_LEVEL) :
Expand All @@ -282,3 +324,6 @@ constexpr int _buttonAnalogLevel(unsigned char index) {
(index == 7) ? (BUTTON8_ANALOG_LEVEL) : 0
);
}

} // namespace build
} // namespace button
20 changes: 10 additions & 10 deletions code/espurna/config/types.h
Expand Up @@ -199,16 +199,16 @@
// LED
//------------------------------------------------------------------------------

#define LED_MODE_MANUAL 0 // LED will be managed manually (OFF by default)
#define LED_MODE_WIFI 1 // LED will blink according to the WIFI status
#define LED_MODE_FOLLOW 2 // LED will follow state of linked LED#_RELAY relay ID
#define LED_MODE_FOLLOW_INVERSE 3 // LED will follow the opposite state of linked LED#_RELAY relay ID
#define LED_MODE_FINDME 4 // LED will be ON if all relays are OFF
#define LED_MODE_FINDME_WIFI 5 // A mixture between WIFI and FINDME
#define LED_MODE_ON 6 // LED always ON
#define LED_MODE_OFF 7 // LED always OFF
#define LED_MODE_RELAY 8 // If any relay is ON, LED will be ON, otherwise OFF
#define LED_MODE_RELAY_WIFI 9 // A mixture between WIFI and RELAY, the reverse of MIXED
#define LED_MODE_MANUAL LedMode::Manual // LED will be managed manually (OFF by default)
#define LED_MODE_WIFI LedMode::WiFi // LED will blink according to the WIFI status
#define LED_MODE_FOLLOW LedMode::Follow // LED will follow state of linked LED#_RELAY relay ID
#define LED_MODE_FOLLOW_INVERSE LedMode::FollowInverse // LED will follow the opposite state of linked LED#_RELAY relay ID
#define LED_MODE_FINDME LedMode::FindMe // LED will be ON if all relays are OFF
#define LED_MODE_FINDME_WIFI LedMode::FindMeWiFi // A mixture between WIFI and FINDME
#define LED_MODE_ON LedMode::On // LED always ON
#define LED_MODE_OFF LedMode::Off // LED always OFF
#define LED_MODE_RELAY LedMode::Relay // If any relay is ON, LED will be ON, otherwise OFF
#define LED_MODE_RELAY_WIFI LedMode::RelayWiFi // A mixture between WIFI and RELAY, the reverse of MIXED

// -----------------------------------------------------------------------------
// UI
Expand Down

0 comments on commit f921163

Please sign in to comment.