Skip to content

Commit cafc610

Browse files
committed
board profiles: pre-configured pin layouts #11
1 parent 6d3feaf commit cafc610

7 files changed

+53
-42
lines changed

makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ CXX_STANDARD := -std=c++11
3333
#----------------------------------------------------------------------
3434
# Defined Symbols
3535
#----------------------------------------------------------------------
36-
DEFINES += -DARDUINOJSON_ENABLE_STD_STRING=1 -DARDUINOJSON_ENABLE_ARDUINO_STRING -DEMSESP_DEBUG -DEMSESP_STANDALONE -DEMSESP_TEST
36+
DEFINES += -DARDUINOJSON_ENABLE_STD_STRING=1 -DARDUINOJSON_ENABLE_ARDUINO_STRING -DEMSESP_DEBUG -DEMSESP_STANDALONE -DEMSESP_TEST -DEMSESP_DEFAULT_BOARD_PROFILE=\"LOLIN\"
3737

3838
#----------------------------------------------------------------------
3939
# Sources & Files

pio_local.ini_example

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
; example custom platformio.ini file for EMS-ESP
2+
;
3+
14
[env]
25
upload_protocol = espota
36
upload_flags =
@@ -8,7 +11,7 @@ upload_port = 10.10.10.101
811
[common]
912
; debug_flags = -DENABLE_CORS -DEMSESP_TEST
1013
; debug_flags = -DEMSESP_DEBUG -DEMSESP_TEST
11-
debug_flags = -DEMSESP_TEST
14+
; debug_flags = -DEMSESP_DEFAULT_BOARD_PROFILE=\"LOLIN\"
1215

1316
[env:esp32]
1417
monitor_filters = esp32_exception_decoder

src/WebSettingsService.cpp

+27-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,28 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
6060
root["board_profile"] = settings.board_profile;
6161
}
6262

63+
// call on initialization and also when settings are updated via web or console
6364
StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) {
65+
// set or load board profile
66+
settings.board_profile = root["board_profile"] | EMSESP_DEFAULT_BOARD_PROFILE;
67+
68+
// load default GPIO configuration based on board profile
69+
std::vector<uint8_t> data; // led, dallas, rx, tx, button
70+
if (!System::load_board_profile(data, settings.board_profile.c_str())) {
71+
// invalid board configuration, override the default in case it has been misspelled
72+
settings.board_profile = "S32";
73+
}
74+
uint8_t default_led_gpio = data[0];
75+
uint8_t default_dallas_gpio = data[1];
76+
uint8_t default_rx_gpio = data[2];
77+
uint8_t default_tx_gpio = data[3];
78+
uint8_t default_pbutton_gpio = data[4];
79+
80+
// check to see if we have a settings file, if not it's a fresh install
81+
if (!root.size()) {
82+
EMSESP::logger().info(F("Initializing configuration with board profile %s"), settings.board_profile.c_str());
83+
}
84+
6485
int prev;
6586
reset_flags();
6687

@@ -72,10 +93,10 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
7293
settings.tx_delay = root["tx_delay"] | EMSESP_DEFAULT_TX_DELAY;
7394
check_flag(prev, settings.tx_delay, ChangeFlags::UART);
7495
prev = settings.rx_gpio;
75-
settings.rx_gpio = root["rx_gpio"] | EMSESP_DEFAULT_RX_GPIO;
96+
settings.rx_gpio = root["rx_gpio"] | default_rx_gpio;
7697
check_flag(prev, settings.rx_gpio, ChangeFlags::UART);
7798
prev = settings.tx_gpio;
78-
settings.tx_gpio = root["tx_gpio"] | EMSESP_DEFAULT_TX_GPIO;
99+
settings.tx_gpio = root["tx_gpio"] | default_tx_gpio;
79100
check_flag(prev, settings.tx_gpio, ChangeFlags::UART);
80101

81102
// syslog
@@ -93,12 +114,9 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
93114

94115
String old_syslog_host = settings.syslog_host;
95116
settings.syslog_host = root["syslog_host"] | EMSESP_DEFAULT_SYSLOG_HOST;
96-
97-
#ifndef EMSESP_STANDALONE
98-
if (old_syslog_host.equals(settings.syslog_host)) {
117+
if (old_syslog_host.equals(settings.syslog_host.c_str())) {
99118
add_flags(ChangeFlags::SYSLOG);
100119
}
101-
#endif
102120

103121
prev = settings.syslog_port;
104122
settings.syslog_port = root["syslog_port"] | EMSESP_DEFAULT_SYSLOG_PORT;
@@ -116,12 +134,12 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
116134

117135
// button
118136
prev = settings.pbutton_gpio;
119-
settings.pbutton_gpio = root["pbutton_gpio"] | EMSESP_DEFAULT_PBUTTON_GPIO;
137+
settings.pbutton_gpio = root["pbutton_gpio"] | default_pbutton_gpio;
120138
check_flag(prev, settings.pbutton_gpio, ChangeFlags::BUTTON);
121139

122140
// dallas
123141
prev = settings.dallas_gpio;
124-
settings.dallas_gpio = root["dallas_gpio"] | EMSESP_DEFAULT_DALLAS_GPIO;
142+
settings.dallas_gpio = root["dallas_gpio"] | default_dallas_gpio;
125143
check_flag(prev, settings.dallas_gpio, ChangeFlags::DALLAS);
126144
prev = settings.dallas_parasite;
127145
settings.dallas_parasite = root["dallas_parasite"] | EMSESP_DEFAULT_DALLAS_PARASITE;
@@ -137,7 +155,7 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
137155

138156
// led
139157
prev = settings.led_gpio;
140-
settings.led_gpio = root["led_gpio"] | EMSESP_DEFAULT_LED_GPIO;
158+
settings.led_gpio = root["led_gpio"] | default_led_gpio;
141159
check_flag(prev, settings.led_gpio, ChangeFlags::LED);
142160
prev = settings.hide_led;
143161
settings.hide_led = root["hide_led"] | EMSESP_DEFAULT_HIDE_LED;
@@ -150,9 +168,6 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
150168
// doesn't need any follow-up actions
151169
settings.api_enabled = root["api_enabled"] | EMSESP_DEFAULT_API_ENABLED;
152170

153-
// board profiles
154-
settings.board_profile = root["board_profile"] | EMSESP_DEFAULT_BOARD_PROFILE;
155-
156171
return StateUpdateResult::CHANGED;
157172
}
158173

src/WebSettingsService.h

+10-16
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,18 @@
4343
#define EMSESP_DEFAULT_API_ENABLED false // turn off, because its insecure
4444
#define EMSESP_DEFAULT_BOOL_FORMAT 1 // on/off
4545
#define EMSESP_DEFAULT_ANALOG_ENABLED false
46-
#define EMSESP_DEFAULT_BOARD_PROFILE "S32"
47-
48-
// Default GPIO PIN definitions
49-
#if defined(ESP32)
50-
#define EMSESP_DEFAULT_RX_GPIO 23 // D7 on Wemos D1-32, OR 17 for UART2 on Lolin D32
51-
#define EMSESP_DEFAULT_TX_GPIO 5 // D8 on Wemos D1-32, OR 16 for UART2 on Lolin D32
52-
#define EMSESP_DEFAULT_DALLAS_GPIO 18 // 18 on Wemos D1-32, 14 on LOLIN D32
53-
#define EMSESP_DEFAULT_LED_GPIO 2 // 2 on Wemos D1-32, 5 on LOLIN D32
54-
#define EMSESP_DEFAULT_PBUTTON_GPIO 0 // default GPIO is 0 (off)
55-
#else
56-
// for standalone
57-
#define EMSESP_DEFAULT_RX_GPIO 0
58-
#define EMSESP_DEFAULT_TX_GPIO 0
59-
#define EMSESP_DEFAULT_DALLAS_GPIO 0
60-
#define EMSESP_DEFAULT_LED_GPIO 0
61-
#define EMSESP_DEFAULT_PBUTTON_GPIO 0
46+
47+
#ifndef EMSESP_DEFAULT_BOARD_PROFILE
48+
#define EMSESP_DEFAULT_BOARD_PROFILE "S32" // Gateway S32
6249
#endif
6350

51+
// Default GPIO PIN definitions - based on Wemos/Nodemcu
52+
#define EMSESP_DEFAULT_RX_GPIO 23 // D7
53+
#define EMSESP_DEFAULT_TX_GPIO 5 // D8
54+
#define EMSESP_DEFAULT_DALLAS_GPIO 18
55+
#define EMSESP_DEFAULT_LED_GPIO 2
56+
#define EMSESP_DEFAULT_PBUTTON_GPIO 0
57+
6458
namespace emsesp {
6559

6660
class WebSettings {

src/console.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -615,16 +615,15 @@ std::string EMSESPStreamConsole::console_name() {
615615
// Log order is off, err, warning, notice, info, debug, trace, all
616616
void Console::start() {
617617
shell = std::make_shared<EMSESPStreamConsole>(Serial, true);
618-
shell->maximum_log_messages(100); // default is 50
618+
shell->maximum_log_messages(100); // default was 50
619619
shell->start();
620620

621621
#if defined(EMSESP_DEBUG)
622622
shell->log_level(uuid::log::Level::DEBUG);
623623
#endif
624624

625625
#if defined(EMSESP_STANDALONE)
626-
// always start in su/admin mode when running tests
627-
shell->add_flags(CommandFlags::ADMIN);
626+
shell->add_flags(CommandFlags::ADMIN); // always start in su/admin mode when running tests
628627
#endif
629628

630629
// start the telnet service

src/emsesp.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1090,13 +1090,12 @@ void EMSESP::start() {
10901090
// start the file system
10911091
#ifndef EMSESP_STANDALONE
10921092
if (!LITTLEFS.begin(true)) {
1093-
Serial.println("LITTLEFS Mount Failed");
1093+
Serial.println("LITTLEFS Mount Failed. EMS-ESP stopped.");
10941094
return;
10951095
}
10961096
#endif
10971097

1098-
esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
1099-
webSettingsService.begin(); // load EMS-ESP specific settings
1098+
esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
11001099

11011100
system_.check_upgrade(); // do any upgrades
11021101

@@ -1105,7 +1104,10 @@ void EMSESP::start() {
11051104
#include "device_library.h"
11061105
};
11071106

1108-
console_.start(); // telnet and serial console
1107+
console_.start(); // telnet and serial console
1108+
1109+
webSettingsService.begin(); // load EMS-ESP specific settings, like GPIO configurations
1110+
11091111
mqtt_.start(); // mqtt init
11101112
system_.start(heap_start); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
11111113
shower_.start(); // initialize shower timer and shower alert

src/system.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,9 @@ void System::start(uint32_t heap_start) {
230230

231231
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
232232
hostname(networkSettings.hostname.c_str());
233-
LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
233+
LOG_INFO(F("System %s booted (EMS-ESP version %s) "), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
234234
});
235235

236-
LOG_INFO("Loaded Board profile: %s", board_profile_.c_str());
237-
238236
commands_init(); // console & api commands
239237
led_init(false); // init LED
240238
adc_init(false); // analog ADC
@@ -833,14 +831,12 @@ void System::console_commands(Shell & shell, unsigned int context) {
833831
flash_string_vector{F_(set), F_(board_profile)},
834832
flash_string_vector{F_(name_mandatory)},
835833
[](Shell & shell, const std::vector<std::string> & arguments) {
836-
// check for valid profile
837834
std::vector<uint8_t> data; // led, dallas, rx, tx, button
838835
std::string board_profile = Helpers::toUpper(arguments.front());
839836
if (!load_board_profile(data, board_profile)) {
840837
shell.println(F("Invalid board profile"));
841838
return;
842839
}
843-
shell.printfln("Loaded board profile %s with %d,%d,%d,%d,%d", board_profile.c_str(), data[0], data[1], data[2], data[3], data[4]);
844840
EMSESP::webSettingsService.update(
845841
[&](WebSettings & settings) {
846842
settings.board_profile = board_profile.c_str();
@@ -1040,6 +1036,7 @@ bool System::command_test(const char * value, const int8_t id) {
10401036

10411037
// takes a board profile and populates a data array with GPIO configurations
10421038
// data = led, dallas, rx, tx, button
1039+
// returns false if profile is not found, and uses default
10431040
bool System::load_board_profile(std::vector<uint8_t> & data, const std::string & board_profile) {
10441041
if (board_profile == "S32") {
10451042
data = {2, 3, 23, 5, 0}; // Gateway S32
@@ -1058,6 +1055,7 @@ bool System::load_board_profile(std::vector<uint8_t> & data, const std::string &
10581055
} else if (board_profile == "TLK110") {
10591056
data = {2, 4, 5, 17, 33}; // Ethernet (TLK110)
10601057
} else {
1058+
data = {EMSESP_DEFAULT_LED_GPIO, EMSESP_DEFAULT_DALLAS_GPIO, EMSESP_DEFAULT_RX_GPIO, EMSESP_DEFAULT_TX_GPIO, EMSESP_DEFAULT_PBUTTON_GPIO};
10611059
return false;
10621060
}
10631061

0 commit comments

Comments
 (0)