From 24d5da36a6442db101172fdd6fedb0b5135b6df2 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 29 Aug 2020 15:16:19 +0200 Subject: [PATCH] (v2) add GPIOs to WebUI as configurable options (LED, Rx, Tx, Dallas sensor) #466 --- .../src/project/EMSESPSettingsController.tsx | 48 +++++++++++++++++++ interface/src/project/EMSESPtypes.ts | 4 ++ lib_standalone/emsuart_standalone.cpp | 2 +- lib_standalone/emsuart_standalone.h | 2 +- src/EMSESPSettingsService.cpp | 8 ++++ src/EMSESPSettingsService.h | 23 +++++++++ src/emsesp.cpp | 9 ++-- src/sensors.cpp | 6 ++- src/sensors.h | 13 +---- src/system.cpp | 16 ++++--- src/system.h | 27 +++++------ src/uart/emsuart_esp32.cpp | 16 +++---- src/uart/emsuart_esp32.h | 12 +---- src/uart/emsuart_esp8266.cpp | 25 +++++----- src/uart/emsuart_esp8266.h | 2 +- src/version.h | 2 +- 16 files changed, 142 insertions(+), 73 deletions(-) diff --git a/interface/src/project/EMSESPSettingsController.tsx b/interface/src/project/EMSESPSettingsController.tsx index 6481a29ad..157e0c235 100644 --- a/interface/src/project/EMSESPSettingsController.tsx +++ b/interface/src/project/EMSESPSettingsController.tsx @@ -76,6 +76,54 @@ function EMSESPSettingsControllerForm(props: EMSESPSettingsControllerFormProps) Time Module (0x0F) Alarm Module (0x12) + + + + devices() const; private: -#if defined(ESP8266) - static constexpr uint8_t SENSOR_GPIO = 14; // D5 -#elif defined(ESP32) -#ifdef WEMOS_D1_32 - static constexpr uint8_t SENSOR_GPIO = 18; // Wemos D1-32 for compatibility D5 -#else - static constexpr uint8_t SENSOR_GPIO = 14; // D5 is LED on wemos lolin D32, so use GPIO14 -#endif -#endif - static constexpr uint8_t MAX_SENSORS = 20; enum class State { IDLE, READING, SCANNING }; @@ -117,7 +107,8 @@ class Sensors { bool registered_ha_[MAX_SENSORS]; uint8_t mqtt_format_; - uint8_t retrycnt_ = 0; + uint8_t retrycnt_ = 0; + uint8_t dallas_gpio_ = 0; }; } // namespace emsesp diff --git a/src/system.cpp b/src/system.cpp index 712665a92..beec9d28f 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -34,6 +34,7 @@ uint32_t System::heap_start_ = 0; int System::reset_counter_ = 0; bool System::upload_status_ = false; bool System::hide_led_ = false; +uint8_t System::led_gpio_ = 0; // send on/off to a gpio pin // value: true = HIGH, false = LOW @@ -155,9 +156,10 @@ void System::start() { void System::set_led() { EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { hide_led_ = settings.hide_led; - if (LED_GPIO) { - pinMode(LED_GPIO, OUTPUT); // LED_GPIO 0 means disabled - digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever + led_gpio_ = settings.led_gpio; + if (led_gpio_) { + pinMode(led_gpio_, OUTPUT); // 0 means disabled + digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever } }); } @@ -265,8 +267,8 @@ void System::system_check() { // if it was unhealthy but now we're better, make sure the LED is solid again cos we've been healed if (!system_healthy_) { system_healthy_ = true; - if (LED_GPIO) { - digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever + if (led_gpio_) { + digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever } } } @@ -275,7 +277,7 @@ void System::system_check() { // flashes the LED void System::led_monitor() { - if (!LED_GPIO) { + if (!led_gpio_) { return; } @@ -286,7 +288,7 @@ void System::led_monitor() { // if bus_not_connected or network not connected, start flashing if (!system_healthy_) { - digitalWrite(LED_GPIO, !digitalRead(LED_GPIO)); + digitalWrite(led_gpio_, !digitalRead(led_gpio_)); } } } diff --git a/src/system.h b/src/system.h index 024b79ad0..64e2e48ef 100644 --- a/src/system.h +++ b/src/system.h @@ -76,20 +76,16 @@ class System { // internal LED #ifndef EMSESP_NO_LED #if defined(ESP8266) - static constexpr uint8_t LED_GPIO = 2; - static constexpr uint8_t LED_ON = LOW; + static constexpr uint8_t LED_ON = LOW; #elif defined(ESP32) #ifdef WEMOS_D1_32 - static constexpr uint8_t LED_GPIO = 2; // on Wemos D1-32 - static constexpr uint8_t LED_ON = HIGH; + static constexpr uint8_t LED_ON = HIGH; #else - static constexpr uint8_t LED_GPIO = 5; - static constexpr uint8_t LED_ON = LOW; + static constexpr uint8_t LED_ON = LOW; #endif #endif #else - static constexpr uint8_t LED_GPIO = 0; // no LED - static constexpr uint8_t LED_ON = 0; + static constexpr uint8_t LED_ON = 0; #endif void led_monitor(); @@ -106,16 +102,15 @@ class System { static uint32_t heap_start_; static int reset_counter_; uint32_t last_heartbeat_ = 0; - - // OTA - static bool upload_status_; // true if we're in the middle of a OTA firmware upload + static bool upload_status_; // true if we're in the middle of a OTA firmware upload // settings - bool system_heartbeat_; - static bool hide_led_; - uint8_t syslog_level_; - uint32_t syslog_mark_interval_; - String syslog_host_; + bool system_heartbeat_; + static bool hide_led_; + uint8_t syslog_level_; + uint32_t syslog_mark_interval_; + String syslog_host_; + static uint8_t led_gpio_; }; } // namespace emsesp diff --git a/src/uart/emsuart_esp32.cpp b/src/uart/emsuart_esp32.cpp index db9381136..f12906405 100644 --- a/src/uart/emsuart_esp32.cpp +++ b/src/uart/emsuart_esp32.cpp @@ -61,7 +61,7 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { static uint8_t length; portENTER_CRITICAL(&mux); if (EMS_UART.int_st.brk_det) { - EMS_UART.int_clr.brk_det = 1; // clear flag + EMS_UART.int_clr.brk_det = 1; // clear flag length = 0; while (EMS_UART.status.rxfifo_cnt) { uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes from fifo @@ -80,7 +80,6 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { portEXIT_CRITICAL(&mux); } - void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { if (emsTxBufLen == 0) { return; @@ -106,7 +105,7 @@ void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { /* * init UART driver */ -void EMSuart::start(const uint8_t tx_mode) { +void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio) { if (tx_mode_ != 0xFF) { // uart already initialized tx_mode_ = tx_mode; restart(); @@ -121,8 +120,9 @@ void EMSuart::start(const uint8_t tx_mode) { .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, }; + uart_param_config(EMSUART_UART, &uart_config); - uart_set_pin(EMSUART_UART, EMSUART_TXPIN, EMSUART_RXPIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + uart_set_pin(EMSUART_UART, tx_gpio, rx_gpio, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); EMS_UART.int_ena.val = 0; // disable all intr. EMS_UART.int_clr.val = 0xFFFFFFFF; // clear all intr. flags EMS_UART.idle_conf.tx_brk_num = 10; // breaklength 10 bit @@ -132,7 +132,7 @@ void EMSuart::start(const uint8_t tx_mode) { uart_isr_register(EMSUART_UART, emsuart_rx_intr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); xTaskCreate(emsuart_recvTask, "emsuart_recvTask", 2048, NULL, configMAX_PRIORITIES - 1, NULL); - timer = timerBegin(0, 80, true); // timer prescale to 1 us, countup + timer = timerBegin(0, 80, true); // timer prescale to 1 us, countup timerAttachInterrupt(timer, &emsuart_tx_timer_intr_handler, true); // Timer with edge interrupt restart(); } @@ -160,11 +160,11 @@ void EMSuart::restart() { emsTxBufIdx = 0; emsTxBufLen = 0; if (tx_mode_ > 100) { - emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ - 90); + emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ - 90); } else { - emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ + 10); + emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ + 10); } - if(tx_mode_ == EMS_TXMODE_NEW) { + if (tx_mode_ == EMS_TXMODE_NEW) { EMS_UART.conf0.txd_brk = 1; } else { EMS_UART.conf0.txd_brk = 0; diff --git a/src/uart/emsuart_esp32.h b/src/uart/emsuart_esp32.h index df29566b4..fad76233d 100644 --- a/src/uart/emsuart_esp32.h +++ b/src/uart/emsuart_esp32.h @@ -66,16 +66,6 @@ #define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080 #define EMSUART_TX_BRK_PLUS (EMSUART_TX_BIT_TIME * 11) - -// customize the GPIO pins for RX and TX here -#ifdef WEMOS_D1_32 -#define EMSUART_RXPIN 23 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty -#define EMSUART_TXPIN 5 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty -#else -#define EMSUART_RXPIN 17 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty -#define EMSUART_TXPIN 16 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty -#endif - namespace emsesp { #define EMS_TX_STATUS_OK 1 @@ -86,7 +76,7 @@ class EMSuart { EMSuart() = default; ~EMSuart() = default; - static void start(const uint8_t tx_mode); + static void start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio); static void send_poll(const uint8_t data); static void stop(); static void restart(); diff --git a/src/uart/emsuart_esp8266.cpp b/src/uart/emsuart_esp8266.cpp index 3816fed86..720a199bd 100644 --- a/src/uart/emsuart_esp8266.cpp +++ b/src/uart/emsuart_esp8266.cpp @@ -47,11 +47,11 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { if (USIS(EMSUART_UART) & ((1 << UIBD))) { // BREAK detection = End of EMS data block USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk if (emsTxBufIdx < emsTxBufLen) { // irq tx_mode is interrupted by - emsTxBufIdx = emsTxBufLen + 1; // stop tx + emsTxBufIdx = emsTxBufLen + 1; // stop tx // drop_next_rx = true; // we have trash in buffer } - USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt - length = 0; + USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt + length = 0; while ((USS(EMSUART_UART) >> USRXC) & 0x0FF) { // read fifo into buffer uint8_t rx = USF(EMSUART_UART); if (length < EMS_MAXBUFFERSIZE) { @@ -98,7 +98,7 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { timer1_write(EMSUART_TX_BRK_TIMER); } else { USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset - sending_ = false; + sending_ = false; } } @@ -113,7 +113,7 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() { /* * init UART0 driver */ -void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { +void ICACHE_FLASH_ATTR EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio) { if (tx_mode_ != 0xFF) { // it's a restart no need to configure uart tx_mode_ = tx_mode; restart(); @@ -137,7 +137,7 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); // set 9600, 8 bits, no parity check, 1 stop bit - USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); + USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1 emsuart_flush_fifos(); @@ -161,8 +161,11 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { // disable esp debug which will go to Tx and mess up the line - see https://github.com/espruino/Espruino/issues/655 system_set_os_print(0); - // swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively - system_uart_swap(); + if (tx_gpio == 1 && rx_gpio == 3) { + system_uart_de_swap(); + } else if (tx_gpio == 15 && rx_gpio == 13) { + system_uart_swap(); // swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively + } ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, nullptr); drop_next_rx = true; @@ -181,7 +184,7 @@ void ICACHE_FLASH_ATTR EMSuart::stop() { USIE(EMSUART_UART) = 0; USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear BRK bit timer1_disable(); - sending_ = false; + sending_ = false; } /* @@ -227,8 +230,8 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { emsTxBuf[i] = buf[i]; } USF(EMSUART_UART) = buf[0]; // send first byte - emsTxBufIdx = 0; - emsTxBufLen = len; + emsTxBufIdx = 0; + emsTxBufLen = len; if (tx_mode_ > 100 && len > 1) { timer1_write(EMSUART_TX_WAIT_REPLY); // large delay after first byte } else { diff --git a/src/uart/emsuart_esp8266.h b/src/uart/emsuart_esp8266.h index be4d1d9fa..0094f69df 100644 --- a/src/uart/emsuart_esp8266.h +++ b/src/uart/emsuart_esp8266.h @@ -71,7 +71,7 @@ class EMSuart { EMSuart() = default; ~EMSuart() = default; - static void ICACHE_FLASH_ATTR start(uint8_t tx_mode); + static void ICACHE_FLASH_ATTR start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio); static void ICACHE_FLASH_ATTR stop(); static void ICACHE_FLASH_ATTR restart(); static void ICACHE_FLASH_ATTR send_poll(uint8_t data); diff --git a/src/version.h b/src/version.h index f831215e6..3757861a2 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "2.0.0b13" +#define EMSESP_APP_VERSION "2.0.0b14"