Instantly share code, notes, and snippets.
Last active
May 2, 2022 21:43
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save mcspr/0a5afe1172291f6162a39e7dfd494f85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp | |
index da8acc5b..fc9eb0dd 100644 | |
--- a/code/espurna/button.cpp | |
+++ b/code/espurna/button.cpp | |
@@ -715,24 +715,35 @@ void buttonSetup() { | |
_buttons.reserve(buttons); | |
- // TODO: allow to change gpio pin type based on config? | |
- #if (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_GENERIC) | |
- using gpio_type = GpioPin; | |
- #elif (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_MCP23S08) | |
- using gpio_type = McpGpioPin; | |
- #endif | |
- | |
- for (unsigned char index = 0; index < ButtonsMax; ++index) { | |
- const auto pin = getSetting({"btnGPIO", index}, _buttonPin(index)); | |
- #if (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_GENERIC) | |
+ auto choose = [](int type, unsigned char pin) { | |
+ switch (type) { | |
+ case BUTTON_EVENTS_SOURCE_GENERIC: | |
if (!gpioValid(pin)) { | |
break; | |
} | |
- #elif (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_MCP23S08) | |
+ return std::shared_ptr<BasePin>(new GpioPin(pin)); | |
+#if (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_MCP23S08) | |
+ case BUTTON_EVENTS_SOURCE_MCP23S08: | |
if (!mcpGpioValid(pin)) { | |
break; | |
} | |
- #endif | |
+ return std::shared_ptr<BasePin>(new McpGpioPin(pin)); | |
+#endif | |
+ default: | |
+ break; | |
+ } | |
+ | |
+ return std::shared_ptr<BasePin>(nullptr); | |
+ }; | |
+ | |
+ for (unsigned char index = 0; index < ButtonsMax; ++index) { | |
+ const auto source = getSetting({"btnEvent", index}, _buttonEventsSource(index)); | |
+ const auto pin = getSetting({"btnGPIO", index}, _buttonPin(index)); | |
+ | |
+ auto managed_pin = choose(source, pin); | |
+ if (!managed_pin) { | |
+ break; | |
+ } | |
const auto relayID = getSetting({"btnRelay", index}, _buttonRelay(index)); | |
@@ -756,7 +767,7 @@ void buttonSetup() { | |
const auto config = _buttonConfig(index); | |
_buttons.emplace_back( | |
- std::make_shared<gpio_type>(pin), config, | |
+ managed_pin, config, | |
relayID, actions, delays | |
); | |
} | |
diff --git a/code/espurna/button_config.h b/code/espurna/button_config.h | |
index 2660c635..7e3afa71 100644 | |
--- a/code/espurna/button_config.h | |
+++ b/code/espurna/button_config.h | |
@@ -230,3 +230,16 @@ constexpr const bool _buttonMqttRetain(unsigned char index) { | |
(index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : (1 == BUTTON_MQTT_RETAIN) | |
); | |
} | |
+ | |
+constexpr const int _buttonEventsSource(unsigned char index) { | |
+ return ( | |
+ (index == 0) ? (BUTTON1_EVENTS_SOURCE) : | |
+ (index == 1) ? (BUTTON2_EVENTS_SOURCE) : | |
+ (index == 2) ? (BUTTON3_EVENTS_SOURCE) : | |
+ (index == 3) ? (BUTTON4_EVENTS_SOURCE) : | |
+ (index == 4) ? (BUTTON5_EVENTS_SOURCE) : | |
+ (index == 5) ? (BUTTON6_EVENTS_SOURCE) : | |
+ (index == 6) ? (BUTTON7_EVENTS_SOURCE) : | |
+ (index == 7) ? (BUTTON8_EVENTS_SOURCE) : BUTTON_EVENTS_SOURCE_GENERIC | |
+ ); | |
+} | |
diff --git a/code/espurna/config/defaults.h b/code/espurna/config/defaults.h | |
index 8c2e5adc..1763e806 100644 | |
--- a/code/espurna/config/defaults.h | |
+++ b/code/espurna/config/defaults.h | |
@@ -397,6 +397,31 @@ | |
#define BUTTON8_MQTT_RETAIN BUTTON_MQTT_RETAIN | |
#endif | |
+#ifndef BUTTON1_EVENTS_SOURCE | |
+#define BUTTON1_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON2_EVENTS_SOURCE | |
+#define BUTTON2_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON3_EVENTS_SOURCE | |
+#define BUTTON3_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON4_EVENTS_SOURCE | |
+#define BUTTON4_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON5_EVENTS_SOURCE | |
+#define BUTTON5_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON6_EVENTS_SOURCE | |
+#define BUTTON6_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON7_EVENTS_SOURCE | |
+#define BUTTON7_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif | |
+#ifndef BUTTON8_EVENTS_SOURCE | |
+#define BUTTON8_EVENTS_SOURCE BUTTON_EVENTS_SOURCE_GENERIC | |
+#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment