mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
(v2) add GPIOs to WebUI as configurable options (LED, Rx, Tx, Dallas sensor) #466
This commit is contained in:
@@ -76,6 +76,54 @@ function EMSESPSettingsControllerForm(props: EMSESPSettingsControllerFormProps)
|
|||||||
<MenuItem value={0x0F}>Time Module (0x0F)</MenuItem>
|
<MenuItem value={0x0F}>Time Module (0x0F)</MenuItem>
|
||||||
<MenuItem value={0x12}>Alarm Module (0x12)</MenuItem>
|
<MenuItem value={0x12}>Alarm Module (0x12)</MenuItem>
|
||||||
</SelectValidator>
|
</SelectValidator>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
|
||||||
|
errorMessages={['Rx GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
|
||||||
|
name="rx_gpio"
|
||||||
|
label="Rx GPIO pin"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.rx_gpio}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('rx_gpio')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
|
||||||
|
errorMessages={['Tx GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
|
||||||
|
name="tx_gpio"
|
||||||
|
label="Tx GPIO pin"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.tx_gpio}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('tx_gpio')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
|
||||||
|
errorMessages={['LED GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
|
||||||
|
name="led_gpio"
|
||||||
|
label="LED GPIO pin (0=none)"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.led_gpio}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('led_gpio')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
|
<TextValidator
|
||||||
|
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
|
||||||
|
errorMessages={['Dallas GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
|
||||||
|
name="dallas_gpio"
|
||||||
|
label="Dallas GPIO pin (0=none)"
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
value={data.dallas_gpio}
|
||||||
|
type="number"
|
||||||
|
onChange={handleValueChange('dallas_gpio')}
|
||||||
|
margin="normal"
|
||||||
|
/>
|
||||||
<BlockFormControlLabel
|
<BlockFormControlLabel
|
||||||
control={
|
control={
|
||||||
<Checkbox
|
<Checkbox
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ export interface EMSESPSettings {
|
|||||||
shower_timer: boolean;
|
shower_timer: boolean;
|
||||||
shower_alert: boolean;
|
shower_alert: boolean;
|
||||||
hide_led: boolean;
|
hide_led: boolean;
|
||||||
|
rx_gpio: number;
|
||||||
|
tx_gpio : number;
|
||||||
|
dallas_gpio : number;
|
||||||
|
led_gpio : number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum busConnectionStatus {
|
export enum busConnectionStatus {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace emsesp {
|
|||||||
/*
|
/*
|
||||||
* init UART0 driver
|
* init UART0 driver
|
||||||
*/
|
*/
|
||||||
void EMSuart::start(uint8_t tx_mode) {
|
void EMSuart::start(uint8_t tx_mode, uint8_t rx_gpio, uint8_t tx_gpio) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class EMSuart {
|
|||||||
EMSuart() = default;
|
EMSuart() = default;
|
||||||
~EMSuart() = default;
|
~EMSuart() = default;
|
||||||
|
|
||||||
static void start(uint8_t tx_mode);
|
static void start(uint8_t tx_mode, uint8_t rx_gpio, uint8_t tx_gpio);
|
||||||
static void stop();
|
static void stop();
|
||||||
static void restart();
|
static void restart();
|
||||||
static void send_poll(uint8_t data);
|
static void send_poll(uint8_t data);
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ void EMSESPSettings::read(EMSESPSettings & settings, JsonObject & root) {
|
|||||||
root["shower_timer"] = settings.shower_timer;
|
root["shower_timer"] = settings.shower_timer;
|
||||||
root["shower_alert"] = settings.shower_alert;
|
root["shower_alert"] = settings.shower_alert;
|
||||||
root["hide_led"] = settings.hide_led;
|
root["hide_led"] = settings.hide_led;
|
||||||
|
root["rx_gpio"] = settings.rx_gpio;
|
||||||
|
root["tx_gpio"] = settings.tx_gpio;
|
||||||
|
root["dallas_gpio"] = settings.dallas_gpio;
|
||||||
|
root["led_gpio"] = settings.led_gpio;
|
||||||
}
|
}
|
||||||
|
|
||||||
StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & settings) {
|
StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & settings) {
|
||||||
@@ -49,6 +53,10 @@ StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & set
|
|||||||
settings.shower_timer = root["shower_timer"] | EMSESP_DEFAULT_SHOWER_TIMER;
|
settings.shower_timer = root["shower_timer"] | EMSESP_DEFAULT_SHOWER_TIMER;
|
||||||
settings.shower_alert = root["shower_alert"] | EMSESP_DEFAULT_SHOWER_ALERT;
|
settings.shower_alert = root["shower_alert"] | EMSESP_DEFAULT_SHOWER_ALERT;
|
||||||
settings.hide_led = root["hide_led"] | EMSESP_DEFAULT_HIDE_LED;
|
settings.hide_led = root["hide_led"] | EMSESP_DEFAULT_HIDE_LED;
|
||||||
|
settings.rx_gpio = root["rx_gpio"] | EMSESP_DEFAULT_RX_GPIO;
|
||||||
|
settings.tx_gpio = root["tx_gpio"] | EMSESP_DEFAULT_TX_GPIO;
|
||||||
|
settings.dallas_gpio = root["dallas_gpio"] | EMSESP_DEFAULT_DALLAS_GPIO;
|
||||||
|
settings.led_gpio = root["led_gpio"] | EMSESP_DEFAULT_LED_GPIO;
|
||||||
|
|
||||||
return StateUpdateResult::CHANGED;
|
return StateUpdateResult::CHANGED;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,25 @@
|
|||||||
#define EMSESP_DEFAULT_SHOWER_ALERT false
|
#define EMSESP_DEFAULT_SHOWER_ALERT false
|
||||||
#define EMSESP_DEFAULT_HIDE_LED false
|
#define EMSESP_DEFAULT_HIDE_LED false
|
||||||
|
|
||||||
|
// Default GPIO PIN definitions
|
||||||
|
#if defined(ESP8266)
|
||||||
|
#define EMSESP_DEFAULT_RX_GPIO 13 // UART0, swapped
|
||||||
|
#define EMSESP_DEFAULT_TX_GPIO 15 // UART0, swapped
|
||||||
|
#define EMSESP_DEFAULT_DALLAS_GPIO 14 // D5
|
||||||
|
#define EMSESP_DEFAULT_LED_GPIO 2 // onboard LED
|
||||||
|
#elif defined(ESP32)
|
||||||
|
#define EMSESP_DEFAULT_RX_GPIO 23 // D7 on Wemos D1-32, OR 17 for UART2 on Lolin D32
|
||||||
|
#define EMSESP_DEFAULT_TX_GPIO 5 // D8 on Wemos D1-32, OR 16 for UART2 on Lolin D32
|
||||||
|
#define EMSESP_DEFAULT_DALLAS_GPIO 18 // 18 on Wemos D1-32, 14 on LOLIN D32
|
||||||
|
#define EMSESP_DEFAULT_LED_GPIO 2 // 2 on Wemos D1-32, 5 on LOLIN D32
|
||||||
|
#else
|
||||||
|
// for standalone
|
||||||
|
#define EMSESP_DEFAULT_RX_GPIO 0
|
||||||
|
#define EMSESP_DEFAULT_TX_GPIO 0
|
||||||
|
#define EMSESP_DEFAULT_DALLAS_GPIO 0
|
||||||
|
#define EMSESP_DEFAULT_LED_GPIO 0
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
enum MQTT_format : uint8_t { SINGLE = 1, NESTED, HA, CUSTOM };
|
enum MQTT_format : uint8_t { SINGLE = 1, NESTED, HA, CUSTOM };
|
||||||
@@ -50,6 +69,10 @@ class EMSESPSettings {
|
|||||||
int8_t syslog_level; // uuid::log::Level
|
int8_t syslog_level; // uuid::log::Level
|
||||||
uint32_t syslog_mark_interval;
|
uint32_t syslog_mark_interval;
|
||||||
String syslog_host;
|
String syslog_host;
|
||||||
|
uint8_t rx_gpio;
|
||||||
|
uint8_t tx_gpio;
|
||||||
|
uint8_t dallas_gpio;
|
||||||
|
uint8_t led_gpio;
|
||||||
|
|
||||||
static void read(EMSESPSettings & settings, JsonObject & root);
|
static void read(EMSESPSettings & settings, JsonObject & root);
|
||||||
static StateUpdateResult update(JsonObject & root, EMSESPSettings & settings);
|
static StateUpdateResult update(JsonObject & root, EMSESPSettings & settings);
|
||||||
|
|||||||
@@ -142,14 +142,15 @@ void EMSESP::watch_id(uint16_t watch_id) {
|
|||||||
// resets all counters and bumps the UART
|
// resets all counters and bumps the UART
|
||||||
// this is called when the tx_mode is persisted in the FS either via Web UI or the console
|
// this is called when the tx_mode is persisted in the FS either via Web UI or the console
|
||||||
void EMSESP::init_tx() {
|
void EMSESP::init_tx() {
|
||||||
// get the tx_mode
|
|
||||||
uint8_t tx_mode;
|
uint8_t tx_mode;
|
||||||
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { tx_mode = settings.tx_mode; });
|
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) {
|
||||||
|
tx_mode = settings.tx_mode;
|
||||||
|
|
||||||
#ifndef EMSESP_FORCE_SERIAL
|
#ifndef EMSESP_FORCE_SERIAL
|
||||||
EMSuart::stop();
|
EMSuart::stop();
|
||||||
EMSuart::start(tx_mode);
|
EMSuart::start(tx_mode, settings.rx_gpio, settings.tx_gpio);
|
||||||
#endif
|
#endif
|
||||||
|
});
|
||||||
|
|
||||||
txservice_.start(); // sends out request to EMS bus for all devices
|
txservice_.start(); // sends out request to EMS bus for all devices
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,9 @@ void Sensors::start() {
|
|||||||
reload();
|
reload();
|
||||||
|
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
bus_.begin(SENSOR_GPIO);
|
if (dallas_gpio_) {
|
||||||
|
bus_.begin(dallas_gpio_);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +49,8 @@ void Sensors::reload() {
|
|||||||
mqtt_format_ = settings.mqtt_format; // single, nested or ha
|
mqtt_format_ = settings.mqtt_format; // single, nested or ha
|
||||||
});
|
});
|
||||||
|
|
||||||
|
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { dallas_gpio_ = settings.dallas_gpio; });
|
||||||
|
|
||||||
if (mqtt_format_ == MQTT_format::HA) {
|
if (mqtt_format_ == MQTT_format::HA) {
|
||||||
for (uint8_t i = 0; i < MAX_SENSORS; registered_ha_[i++] = false)
|
for (uint8_t i = 0; i < MAX_SENSORS; registered_ha_[i++] = false)
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -64,16 +64,6 @@ class Sensors {
|
|||||||
const std::vector<Device> devices() const;
|
const std::vector<Device> devices() const;
|
||||||
|
|
||||||
private:
|
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;
|
static constexpr uint8_t MAX_SENSORS = 20;
|
||||||
|
|
||||||
enum class State { IDLE, READING, SCANNING };
|
enum class State { IDLE, READING, SCANNING };
|
||||||
@@ -117,7 +107,8 @@ class Sensors {
|
|||||||
bool registered_ha_[MAX_SENSORS];
|
bool registered_ha_[MAX_SENSORS];
|
||||||
|
|
||||||
uint8_t mqtt_format_;
|
uint8_t mqtt_format_;
|
||||||
uint8_t retrycnt_ = 0;
|
uint8_t retrycnt_ = 0;
|
||||||
|
uint8_t dallas_gpio_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ uint32_t System::heap_start_ = 0;
|
|||||||
int System::reset_counter_ = 0;
|
int System::reset_counter_ = 0;
|
||||||
bool System::upload_status_ = false;
|
bool System::upload_status_ = false;
|
||||||
bool System::hide_led_ = false;
|
bool System::hide_led_ = false;
|
||||||
|
uint8_t System::led_gpio_ = 0;
|
||||||
|
|
||||||
// send on/off to a gpio pin
|
// send on/off to a gpio pin
|
||||||
// value: true = HIGH, false = LOW
|
// value: true = HIGH, false = LOW
|
||||||
@@ -155,9 +156,10 @@ void System::start() {
|
|||||||
void System::set_led() {
|
void System::set_led() {
|
||||||
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) {
|
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) {
|
||||||
hide_led_ = settings.hide_led;
|
hide_led_ = settings.hide_led;
|
||||||
if (LED_GPIO) {
|
led_gpio_ = settings.led_gpio;
|
||||||
pinMode(LED_GPIO, OUTPUT); // LED_GPIO 0 means disabled
|
if (led_gpio_) {
|
||||||
digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
|
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 it was unhealthy but now we're better, make sure the LED is solid again cos we've been healed
|
||||||
if (!system_healthy_) {
|
if (!system_healthy_) {
|
||||||
system_healthy_ = true;
|
system_healthy_ = true;
|
||||||
if (LED_GPIO) {
|
if (led_gpio_) {
|
||||||
digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
|
digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -275,7 +277,7 @@ void System::system_check() {
|
|||||||
|
|
||||||
// flashes the LED
|
// flashes the LED
|
||||||
void System::led_monitor() {
|
void System::led_monitor() {
|
||||||
if (!LED_GPIO) {
|
if (!led_gpio_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +288,7 @@ void System::led_monitor() {
|
|||||||
|
|
||||||
// if bus_not_connected or network not connected, start flashing
|
// if bus_not_connected or network not connected, start flashing
|
||||||
if (!system_healthy_) {
|
if (!system_healthy_) {
|
||||||
digitalWrite(LED_GPIO, !digitalRead(LED_GPIO));
|
digitalWrite(led_gpio_, !digitalRead(led_gpio_));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/system.h
27
src/system.h
@@ -76,20 +76,16 @@ class System {
|
|||||||
// internal LED
|
// internal LED
|
||||||
#ifndef EMSESP_NO_LED
|
#ifndef EMSESP_NO_LED
|
||||||
#if defined(ESP8266)
|
#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)
|
#elif defined(ESP32)
|
||||||
#ifdef WEMOS_D1_32
|
#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
|
#else
|
||||||
static constexpr uint8_t LED_GPIO = 5;
|
static constexpr uint8_t LED_ON = LOW;
|
||||||
static constexpr uint8_t LED_ON = LOW;
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#else
|
#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
|
#endif
|
||||||
|
|
||||||
void led_monitor();
|
void led_monitor();
|
||||||
@@ -106,16 +102,15 @@ class System {
|
|||||||
static uint32_t heap_start_;
|
static uint32_t heap_start_;
|
||||||
static int reset_counter_;
|
static int reset_counter_;
|
||||||
uint32_t last_heartbeat_ = 0;
|
uint32_t last_heartbeat_ = 0;
|
||||||
|
static bool upload_status_; // true if we're in the middle of a OTA firmware upload
|
||||||
// OTA
|
|
||||||
static bool upload_status_; // true if we're in the middle of a OTA firmware upload
|
|
||||||
|
|
||||||
// settings
|
// settings
|
||||||
bool system_heartbeat_;
|
bool system_heartbeat_;
|
||||||
static bool hide_led_;
|
static bool hide_led_;
|
||||||
uint8_t syslog_level_;
|
uint8_t syslog_level_;
|
||||||
uint32_t syslog_mark_interval_;
|
uint32_t syslog_mark_interval_;
|
||||||
String syslog_host_;
|
String syslog_host_;
|
||||||
|
static uint8_t led_gpio_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
|||||||
static uint8_t length;
|
static uint8_t length;
|
||||||
portENTER_CRITICAL(&mux);
|
portENTER_CRITICAL(&mux);
|
||||||
if (EMS_UART.int_st.brk_det) {
|
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;
|
length = 0;
|
||||||
while (EMS_UART.status.rxfifo_cnt) {
|
while (EMS_UART.status.rxfifo_cnt) {
|
||||||
uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes from fifo
|
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);
|
portEXIT_CRITICAL(&mux);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
||||||
if (emsTxBufLen == 0) {
|
if (emsTxBufLen == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -106,7 +105,7 @@ void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
|||||||
/*
|
/*
|
||||||
* init UART driver
|
* 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
|
if (tx_mode_ != 0xFF) { // uart already initialized
|
||||||
tx_mode_ = tx_mode;
|
tx_mode_ = tx_mode;
|
||||||
restart();
|
restart();
|
||||||
@@ -121,8 +120,9 @@ void EMSuart::start(const uint8_t tx_mode) {
|
|||||||
.stop_bits = UART_STOP_BITS_1,
|
.stop_bits = UART_STOP_BITS_1,
|
||||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||||
};
|
};
|
||||||
|
|
||||||
uart_param_config(EMSUART_UART, &uart_config);
|
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_ena.val = 0; // disable all intr.
|
||||||
EMS_UART.int_clr.val = 0xFFFFFFFF; // clear all intr. flags
|
EMS_UART.int_clr.val = 0xFFFFFFFF; // clear all intr. flags
|
||||||
EMS_UART.idle_conf.tx_brk_num = 10; // breaklength 10 bit
|
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);
|
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);
|
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
|
timerAttachInterrupt(timer, &emsuart_tx_timer_intr_handler, true); // Timer with edge interrupt
|
||||||
restart();
|
restart();
|
||||||
}
|
}
|
||||||
@@ -160,11 +160,11 @@ void EMSuart::restart() {
|
|||||||
emsTxBufIdx = 0;
|
emsTxBufIdx = 0;
|
||||||
emsTxBufLen = 0;
|
emsTxBufLen = 0;
|
||||||
if (tx_mode_ > 100) {
|
if (tx_mode_ > 100) {
|
||||||
emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ - 90);
|
emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ - 90);
|
||||||
} else {
|
} 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;
|
EMS_UART.conf0.txd_brk = 1;
|
||||||
} else {
|
} else {
|
||||||
EMS_UART.conf0.txd_brk = 0;
|
EMS_UART.conf0.txd_brk = 0;
|
||||||
|
|||||||
@@ -66,16 +66,6 @@
|
|||||||
#define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080
|
#define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080
|
||||||
#define EMSUART_TX_BRK_PLUS (EMSUART_TX_BIT_TIME * 11)
|
#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 {
|
namespace emsesp {
|
||||||
|
|
||||||
#define EMS_TX_STATUS_OK 1
|
#define EMS_TX_STATUS_OK 1
|
||||||
@@ -86,7 +76,7 @@ class EMSuart {
|
|||||||
EMSuart() = default;
|
EMSuart() = default;
|
||||||
~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 send_poll(const uint8_t data);
|
||||||
static void stop();
|
static void stop();
|
||||||
static void restart();
|
static void restart();
|
||||||
|
|||||||
@@ -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
|
if (USIS(EMSUART_UART) & ((1 << UIBD))) { // BREAK detection = End of EMS data block
|
||||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||||
if (emsTxBufIdx < emsTxBufLen) { // irq tx_mode is interrupted by <brk>
|
if (emsTxBufIdx < emsTxBufLen) { // irq tx_mode is interrupted by <brk>
|
||||||
emsTxBufIdx = emsTxBufLen + 1; // stop tx
|
emsTxBufIdx = emsTxBufLen + 1; // stop tx
|
||||||
// drop_next_rx = true; // we have trash in buffer
|
// drop_next_rx = true; // we have trash in buffer
|
||||||
}
|
}
|
||||||
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||||
length = 0;
|
length = 0;
|
||||||
while ((USS(EMSUART_UART) >> USRXC) & 0x0FF) { // read fifo into buffer
|
while ((USS(EMSUART_UART) >> USRXC) & 0x0FF) { // read fifo into buffer
|
||||||
uint8_t rx = USF(EMSUART_UART);
|
uint8_t rx = USF(EMSUART_UART);
|
||||||
if (length < EMS_MAXBUFFERSIZE) {
|
if (length < EMS_MAXBUFFERSIZE) {
|
||||||
@@ -98,7 +98,7 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
|||||||
timer1_write(EMSUART_TX_BRK_TIMER);
|
timer1_write(EMSUART_TX_BRK_TIMER);
|
||||||
} else {
|
} else {
|
||||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset <BRK>
|
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset <BRK>
|
||||||
sending_ = false;
|
sending_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() {
|
|||||||
/*
|
/*
|
||||||
* init UART0 driver
|
* 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
|
if (tx_mode_ != 0xFF) { // it's a restart no need to configure uart
|
||||||
tx_mode_ = tx_mode;
|
tx_mode_ = tx_mode;
|
||||||
restart();
|
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);
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
|
||||||
|
|
||||||
// set 9600, 8 bits, no parity check, 1 stop bit
|
// 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
|
USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1
|
||||||
emsuart_flush_fifos();
|
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
|
// 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);
|
system_set_os_print(0);
|
||||||
|
|
||||||
// swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively
|
if (tx_gpio == 1 && rx_gpio == 3) {
|
||||||
system_uart_swap();
|
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);
|
ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, nullptr);
|
||||||
drop_next_rx = true;
|
drop_next_rx = true;
|
||||||
@@ -181,7 +184,7 @@ void ICACHE_FLASH_ATTR EMSuart::stop() {
|
|||||||
USIE(EMSUART_UART) = 0;
|
USIE(EMSUART_UART) = 0;
|
||||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear BRK bit
|
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear BRK bit
|
||||||
timer1_disable();
|
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];
|
emsTxBuf[i] = buf[i];
|
||||||
}
|
}
|
||||||
USF(EMSUART_UART) = buf[0]; // send first byte
|
USF(EMSUART_UART) = buf[0]; // send first byte
|
||||||
emsTxBufIdx = 0;
|
emsTxBufIdx = 0;
|
||||||
emsTxBufLen = len;
|
emsTxBufLen = len;
|
||||||
if (tx_mode_ > 100 && len > 1) {
|
if (tx_mode_ > 100 && len > 1) {
|
||||||
timer1_write(EMSUART_TX_WAIT_REPLY); // large delay after first byte
|
timer1_write(EMSUART_TX_WAIT_REPLY); // large delay after first byte
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class EMSuart {
|
|||||||
EMSuart() = default;
|
EMSuart() = default;
|
||||||
~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 stop();
|
||||||
static void ICACHE_FLASH_ATTR restart();
|
static void ICACHE_FLASH_ATTR restart();
|
||||||
static void ICACHE_FLASH_ATTR send_poll(uint8_t data);
|
static void ICACHE_FLASH_ATTR send_poll(uint8_t data);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
#define EMSESP_APP_VERSION "2.0.0b13"
|
#define EMSESP_APP_VERSION "2.0.0b14"
|
||||||
|
|||||||
Reference in New Issue
Block a user