Skip to content

Commit 966f82e

Browse files
committed
gpio checker
1 parent 118cbd9 commit 966f82e

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/system.cpp

+24-10
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ void System::get_settings() {
173173
// Board profile
174174
board_profile_ = settings.board_profile;
175175
});
176-
177-
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
178-
hostname(networkSettings.hostname.c_str());
179-
LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
180-
});
181176
}
182177

183178
// adjust WiFi settings
@@ -207,6 +202,17 @@ void System::wifi_tweak() {
207202
#endif
208203
}
209204

205+
// check for valid ESP32 pins
206+
// 1, 6-11, 12, 14 & 15 are not allowed
207+
// we allow 0 (since its pulled high)
208+
// See https://diyprojects.io/esp32-how-to-use-gpio-digital-io-arduino-code/#.YFpVEq9KhjG
209+
bool System::is_valid_gpio(uint8_t pin) {
210+
if ((pin == 1) || (pin >= 6 && pin <= 12) || (pin >= 14 && pin <= 15)) {
211+
return false; // bad
212+
}
213+
return true;
214+
}
215+
210216
// first call. Sets memory and starts up the UART Serial bridge
211217
void System::start(uint32_t heap_start) {
212218
#if defined(EMSESP_DEBUG)
@@ -221,14 +227,19 @@ void System::start(uint32_t heap_start) {
221227
// load in all the settings first
222228
get_settings();
223229

230+
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
231+
hostname(networkSettings.hostname.c_str());
232+
LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
233+
});
234+
224235
commands_init(); // console & api commands
225236
led_init(false); // init LED
226237
adc_init(false); // analog ADC
227238
syslog_init(false); // init SysLog
228239
button_init(false); // the special button
229240
network_init(false); // network
230241

231-
EMSESP::init_tx(); // start UART
242+
EMSESP::init_uart(); // start UART
232243
}
233244

234245
// adc and bluetooth
@@ -285,11 +296,14 @@ void System::button_init(bool refresh) {
285296
get_settings();
286297
}
287298

288-
// Allow 0 for Boot-button on NodeMCU-32s?
289-
if (!myPButton_.init(pbutton_gpio_, HIGH)) {
290-
LOG_INFO(F("External multi-functional button not detected"));
299+
if (is_valid_gpio(pbutton_gpio_)) {
300+
if (!myPButton_.init(pbutton_gpio_, HIGH)) {
301+
LOG_INFO(F("External multi-functional button not detected"));
302+
} else {
303+
LOG_INFO(F("External multi-functional button enabled"));
304+
}
291305
} else {
292-
LOG_INFO(F("External multi-functional button enabled"));
306+
LOG_WARNING(F("Invalid button GPIO. Check config."));
293307
}
294308

295309
myPButton_.onClick(BUTTON_Debounce, button_OnClick);

src/system.h

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class System {
7474
void button_init(bool refresh);
7575
void commands_init();
7676

77+
static bool is_valid_gpio(uint8_t pin);
78+
7779
bool check_upgrade();
7880
void send_heartbeat();
7981

0 commit comments

Comments
 (0)