mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[ESPEasySerial] Cleanup ESPEasySerial class
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
#include <ESPeasySerial.h>
|
||||
|
||||
|
||||
#include "wrappers/ESPEasySerial_HardwareSerial.h"
|
||||
#include "wrappers/ESPEasySerial_I2C_SC16IS752.h"
|
||||
#include "wrappers/ESPEasySerial_SW_Serial.h"
|
||||
#include "wrappers/ESPEasySerial_USB_HWCDC.h"
|
||||
#include "wrappers/ESPEasySerial_USBCDC.h"
|
||||
|
||||
ESPeasySerial::ESPeasySerial(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic,
|
||||
unsigned int buffSize,
|
||||
bool forceSWserial)
|
||||
{
|
||||
ESPEasySerialConfig config;
|
||||
config.port = port;
|
||||
config.receivePin = receivePin;
|
||||
config.transmitPin = transmitPin;
|
||||
config.inverse_logic = inverse_logic;
|
||||
config.buffSize = buffSize;
|
||||
config.forceSWserial = forceSWserial;
|
||||
config.validate();
|
||||
|
||||
switch (config.port) {
|
||||
#if USES_SW_SERIAL
|
||||
case ESPEasySerialPort::software:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_SW_Serial(config);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_I2C_SC16IS752
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_I2C_SC16IS752(config);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_HWCDC
|
||||
case ESPEasySerialPort::usb_hw_cdc:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_USB_WHCDC_t(config);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_USBCDC
|
||||
case ESPEasySerialPort::usb_cdc_0:
|
||||
case ESPEasySerialPort::usb_cdc_1:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_USBCDC_t(config);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
|
||||
if (isHWserial(config.port)) {
|
||||
_serialPort = new ESPEasySerial_HardwareSerial_t(config);
|
||||
if (_serialPort != nullptr) {
|
||||
reinterpret_cast<ESPEasySerial_HardwareSerial_t*>(_serialPort)->resetConfig(config);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// FIXME TD-er: Should it call begin() ???
|
||||
if (_serialPort != nullptr) {
|
||||
// _serialPort->begin(_baud);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
ESPeasySerial::~ESPeasySerial() {
|
||||
flush();
|
||||
end();
|
||||
|
||||
if (_serialPort != nullptr) {
|
||||
delete _serialPort;
|
||||
}
|
||||
}
|
||||
|
||||
void ESPeasySerial::begin(unsigned long baud)
|
||||
{
|
||||
if (isValid()) {
|
||||
_serialPort->begin(baud);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
void ESPeasySerial::begin(unsigned long baud,
|
||||
SerialConfig config,
|
||||
SerialMode mode)
|
||||
{
|
||||
if (isValid()) {
|
||||
_serialPort->setPortConfig(baud, config, mode);
|
||||
_serialPort->begin(baud);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
void ESPeasySerial::begin(unsigned long baud, uint32_t config)
|
||||
{
|
||||
if (isValid()) {
|
||||
_serialPort->setPortConfig(baud, config);
|
||||
_serialPort->begin(baud);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void ESPeasySerial::end() {
|
||||
if (isValid()) {
|
||||
flush();
|
||||
_serialPort->end();
|
||||
}
|
||||
}
|
||||
|
||||
int ESPeasySerial::peek(void)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
return _serialPort->peek();
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(uint8_t val)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return _serialPort->write(val);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
if (!isValid() || !buffer) {
|
||||
return 0;
|
||||
}
|
||||
return _serialPort->write(buffer, size);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const char *buffer)
|
||||
{
|
||||
if (!buffer) { return 0; }
|
||||
return write(buffer, strlen_P(buffer));
|
||||
}
|
||||
|
||||
int ESPeasySerial::read(void)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
return _serialPort->read();
|
||||
}
|
||||
|
||||
int ESPeasySerial::available(void)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return _serialPort->available();
|
||||
}
|
||||
|
||||
int ESPeasySerial::availableForWrite(void)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return _serialPort->availableForWrite();
|
||||
}
|
||||
|
||||
void ESPeasySerial::flush(void)
|
||||
{
|
||||
if (isValid()) {
|
||||
_serialPort->flush();
|
||||
}
|
||||
}
|
||||
|
||||
int ESPeasySerial::baudRate(void)
|
||||
{
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return getSerialConfig().baud;
|
||||
}
|
||||
|
||||
void ESPeasySerial::setDebugOutput(bool enable)
|
||||
{
|
||||
if (isValid()) {
|
||||
_serialPort->setDebugOutput(enable);
|
||||
}
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isTxEnabled(void)
|
||||
{
|
||||
return getTxPin() != -1;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isRxEnabled(void)
|
||||
{
|
||||
return getRxPin() != -1;
|
||||
}
|
||||
|
||||
// Not supported in ESP32, since only HW serial is used.
|
||||
// Function included since it is used in some libraries.
|
||||
bool ESPeasySerial::listen() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
String ESPeasySerial::getLogString() const {
|
||||
return getSerialConfig().getLogString();
|
||||
}
|
||||
|
||||
|
||||
bool ESPeasySerial::isValid() const {
|
||||
// FIXME TD-er: Must call isValid() on the individual _serialPort types
|
||||
return _serialPort != nullptr;
|
||||
}
|
||||
@@ -1,5 +1,74 @@
|
||||
#include "ESPEasySerialConfig.h"
|
||||
|
||||
#include "ESPEasySerialType.h"
|
||||
|
||||
void ESPEasySerialConfig::validate()
|
||||
{
|
||||
port = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
#if USES_SW_SERIAL
|
||||
|
||||
if (forceSWserial) {
|
||||
# if USES_I2C_SC16IS752
|
||||
|
||||
if (port != ESPEasySerialPort::sc16is752) {
|
||||
port = ESPEasySerialPort::software;
|
||||
}
|
||||
# else // if USES_I2C_SC16IS752
|
||||
port = ESPEasySerialPort::software;
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
}
|
||||
#endif // if USES_SW_SERIAL
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
void ESPEasySerialConfig::setPortConfig(unsigned long baud,
|
||||
SerialConfig portconfig,
|
||||
SerialMode portmode)
|
||||
{
|
||||
// FIXME TD-er: Must also set baudrate?
|
||||
config = portconfig;
|
||||
mode = portmode;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
void ESPEasySerialConfig::setPortConfig(unsigned long baudrate, uint32_t portconfig)
|
||||
{
|
||||
// FIXME TD-er: Must also set baudrate?
|
||||
config = portconfig;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
String ESPEasySerialConfig::getLogString() const
|
||||
{
|
||||
String log;
|
||||
|
||||
log.reserve(48);
|
||||
log = F("ESPEasy serial: ");
|
||||
log += ESPEasySerialPort_toString(port);
|
||||
|
||||
#if USES_I2C_SC16IS752
|
||||
|
||||
if (port == ESPEasySerialPort::sc16is752) {
|
||||
log += F(": addr:");
|
||||
log += String(receivePin);
|
||||
log += F(" ch:");
|
||||
log += transmitPin == 0 ? 'A' : 'B';
|
||||
}
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
|
||||
if (useGPIOpins(port)) {
|
||||
log += F(": rx:");
|
||||
log += String(receivePin);
|
||||
log += F(" tx:");
|
||||
log += String(transmitPin);
|
||||
}
|
||||
|
||||
log += F(" baud:");
|
||||
log += String(baud);
|
||||
return log;
|
||||
}
|
||||
|
||||
#if USES_I2C_SC16IS752
|
||||
|
||||
@@ -16,3 +85,16 @@ bool ESPEasySerialConfig::getI2C_SC16IS752_Parameters(
|
||||
}
|
||||
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
|
||||
|
||||
int ESPEasySerialConfig::getReceivePin() const
|
||||
{
|
||||
if (useGPIOpins(port)) { return receivePin; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ESPEasySerialConfig::getTransmitPin() const
|
||||
{
|
||||
if (useGPIOpins(port)) { return transmitPin; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,21 @@ struct ESPEasySerialConfig {
|
||||
|
||||
ESPEasySerialConfig() = default;
|
||||
|
||||
void validate();
|
||||
|
||||
#ifdef ESP8266
|
||||
void setPortConfig(unsigned long baud,
|
||||
SerialConfig config,
|
||||
SerialMode mode);
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
void setPortConfig(unsigned long baud, uint32_t config);
|
||||
#endif
|
||||
|
||||
|
||||
String getLogString() const;
|
||||
|
||||
#if USES_I2C_SC16IS752
|
||||
|
||||
bool getI2C_SC16IS752_Parameters(ESPEasySC16IS752_Serial::I2C_address & addr,
|
||||
@@ -23,6 +38,8 @@ struct ESPEasySerialConfig {
|
||||
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
|
||||
int getReceivePin() const;
|
||||
int getTransmitPin() const;
|
||||
|
||||
ESPEasySerialPort port = ESPEasySerialPort::not_set;
|
||||
unsigned long baud = 115200;
|
||||
@@ -33,7 +50,7 @@ struct ESPEasySerialConfig {
|
||||
bool forceSWserial = false;
|
||||
unsigned long timeout_ms = 20000UL;
|
||||
|
||||
#ifdef ESP32
|
||||
#ifdef ESP32
|
||||
uint32_t config = SERIAL_8N1;
|
||||
#endif // ifdef ESP32
|
||||
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
#include "ESPEasySerialPort.h"
|
||||
|
||||
const __FlashStringHelper* ESPEasySerialPort_toString(ESPEasySerialPort serType)
|
||||
const __FlashStringHelper* ESPEasySerialPort_toString(ESPEasySerialPort port)
|
||||
{
|
||||
switch (serType) {
|
||||
switch (port) {
|
||||
case ESPEasySerialPort::not_set: break;
|
||||
#if USES_I2C_SC16IS752
|
||||
case ESPEasySerialPort::sc16is752: return F("I2C Serial");
|
||||
#endif
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
#ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0_swap: return F("HW Serial0 swap");
|
||||
#endif
|
||||
#endif // ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0: return F("HW Serial0");
|
||||
case ESPEasySerialPort::serial1: return F("HW Serial1");
|
||||
#if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2: return F("HW Serial2");
|
||||
#endif
|
||||
#endif // if HAS_SERIAL2
|
||||
#if USES_SW_SERIAL
|
||||
case ESPEasySerialPort::software: return F("SoftwareSerial");
|
||||
#endif
|
||||
#endif // if USES_SW_SERIAL
|
||||
#if USES_HWCDC
|
||||
case ESPEasySerialPort::usb_hw_cdc: return F("USB HWCDC");
|
||||
#endif
|
||||
#endif // if USES_HWCDC
|
||||
#if USES_USBCDC
|
||||
case ESPEasySerialPort::usb_cdc_0: return F("USB CDC0");
|
||||
case ESPEasySerialPort::usb_cdc_1: return F("USB CDC1");
|
||||
#endif
|
||||
#endif // if USES_USBCDC
|
||||
case ESPEasySerialPort::MAX_SERIAL_TYPE: break;
|
||||
|
||||
// Do not include "default:" to let the compiler check if we miss some case
|
||||
@@ -32,20 +32,75 @@ const __FlashStringHelper* ESPEasySerialPort_toString(ESPEasySerialPort serType)
|
||||
return F("");
|
||||
}
|
||||
|
||||
bool isHWserial(ESPEasySerialPort serType)
|
||||
bool isHWserial(ESPEasySerialPort port)
|
||||
{
|
||||
switch (serType) {
|
||||
// FIXME TD-er: Still needed?
|
||||
switch (port) {
|
||||
#ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0_swap:
|
||||
#endif
|
||||
#endif // ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial1:
|
||||
#if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2:
|
||||
#endif
|
||||
#endif // if HAS_SERIAL2
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool useGPIOpins(ESPEasySerialPort port)
|
||||
{
|
||||
switch (port) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
#ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0_swap:
|
||||
#endif // ifdef ESP8266
|
||||
case ESPEasySerialPort::serial1:
|
||||
#if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2:
|
||||
#endif // if HAS_SERIAL2
|
||||
#if USES_SW_SERIAL
|
||||
case ESPEasySerialPort::software:
|
||||
#endif // if USES_SW_SERIAL
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool validSerialPort(ESPEasySerialPort port)
|
||||
{
|
||||
switch (port) {
|
||||
#if USES_I2C_SC16IS752
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
case ESPEasySerialPort::serial0:
|
||||
#ifdef ESP8266
|
||||
case ESPEasySerialPort::serial0_swap:
|
||||
#endif // ifdef ESP8266
|
||||
case ESPEasySerialPort::serial1:
|
||||
#if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2:
|
||||
#endif // if HAS_SERIAL2
|
||||
#if USES_SW_SERIAL
|
||||
case ESPEasySerialPort::software:
|
||||
#endif // if USES_SW_SERIAL
|
||||
#if USES_HWCDC
|
||||
case ESPEasySerialPort::usb_hw_cdc:
|
||||
#endif // if USES_HWCDC
|
||||
#if USES_USBCDC
|
||||
case ESPEasySerialPort::usb_cdc_0:
|
||||
case ESPEasySerialPort::usb_cdc_1:
|
||||
#endif // if USES_USBCDC
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,35 +6,40 @@
|
||||
|
||||
// Keep value assigned as it is used in scripts and stored in the Settings.TaskDevicePort
|
||||
enum class ESPEasySerialPort : uint8_t {
|
||||
not_set = 0,
|
||||
not_set = 0,
|
||||
#if USES_I2C_SC16IS752
|
||||
sc16is752 = 1,
|
||||
#endif
|
||||
serial0 = 2,
|
||||
sc16is752 = 1,
|
||||
#endif // if USES_I2C_SC16IS752
|
||||
serial0 = 2,
|
||||
#ifdef ESP8266
|
||||
serial0_swap = 3,
|
||||
#endif
|
||||
serial1 = 4,
|
||||
#endif // ifdef ESP8266
|
||||
serial1 = 4,
|
||||
#if HAS_SERIAL2
|
||||
serial2 = 5,
|
||||
#endif
|
||||
serial2 = 5,
|
||||
#endif // if HAS_SERIAL2
|
||||
#if USES_SW_SERIAL
|
||||
software = 6,
|
||||
#endif
|
||||
software = 6,
|
||||
#endif // if USES_SW_SERIAL
|
||||
|
||||
#if USES_HWCDC
|
||||
usb_hw_cdc = 7,
|
||||
#endif
|
||||
#endif // if USES_HWCDC
|
||||
#if USES_USBCDC
|
||||
usb_cdc_0 = 8,
|
||||
usb_cdc_1 = 9,
|
||||
#endif
|
||||
usb_cdc_0 = 8,
|
||||
usb_cdc_1 = 9,
|
||||
#endif // if USES_USBCDC
|
||||
|
||||
MAX_SERIAL_TYPE
|
||||
};
|
||||
|
||||
const __FlashStringHelper* ESPEasySerialPort_toString(ESPEasySerialPort serType);
|
||||
|
||||
bool isHWserial(ESPEasySerialPort serType);
|
||||
const __FlashStringHelper* ESPEasySerialPort_toString(ESPEasySerialPort port);
|
||||
|
||||
bool isHWserial(ESPEasySerialPort port);
|
||||
|
||||
bool useGPIOpins(ESPEasySerialPort port);
|
||||
|
||||
bool validSerialPort(ESPEasySerialPort port);
|
||||
|
||||
#endif // ifndef ESPEASY_SERIAL_ESPEASYSERIALPORT_H
|
||||
|
||||
@@ -1,401 +0,0 @@
|
||||
#include <ESPeasySerial.h>
|
||||
|
||||
|
||||
// ****************************************
|
||||
// ESP32 implementation wrapper
|
||||
// Only support HW serial on Serial 0 .. 2
|
||||
// ****************************************
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
// Temporary work-around for bug in ESP32 code, where the pin matrix is not cleaned up between calling end() and begin()
|
||||
// Work-around is to keep track of the last used pins for a serial port,
|
||||
// as it is likely that a node will always use the same pins most of the time.
|
||||
// If not, a reboot may be OK to fix it.
|
||||
// Another idea is to swap pins among UART ports.
|
||||
// e.g. use the same pins on Serial1 if it was used on Serial2 before.
|
||||
|
||||
// PR to fix it: https://github.com/espressif/arduino-esp32/pull/5385
|
||||
// See: https://github.com/espressif/arduino-esp32/issues/3878
|
||||
|
||||
|
||||
static int receivePin0 = -1;
|
||||
static int transmitPin0 = -1;
|
||||
static int receivePin1 = -1;
|
||||
static int transmitPin1 = -1;
|
||||
static int receivePin2 = -1;
|
||||
static int transmitPin2 = -1;
|
||||
|
||||
bool pinsChanged(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin)
|
||||
{
|
||||
switch (port) {
|
||||
case ESPEasySerialPort::serial0: return receivePin != receivePin0 || transmitPin != transmitPin0;
|
||||
case ESPEasySerialPort::serial1: return receivePin != receivePin1 || transmitPin != transmitPin1;
|
||||
# if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2: return receivePin != receivePin2 || transmitPin != transmitPin2;
|
||||
# endif
|
||||
default:
|
||||
// No other hardware serial ports
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setPinsCache(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin)
|
||||
{
|
||||
switch (port) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
receivePin0 = receivePin;
|
||||
transmitPin0 = transmitPin;
|
||||
break;
|
||||
case ESPEasySerialPort::serial1:
|
||||
receivePin1 = receivePin;
|
||||
transmitPin1 = transmitPin;
|
||||
break;
|
||||
|
||||
# if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2:
|
||||
receivePin2 = receivePin;
|
||||
transmitPin2 = transmitPin;
|
||||
break;
|
||||
|
||||
# endif
|
||||
default:
|
||||
// No other hardware serial ports
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// End of messy work-around.
|
||||
|
||||
void ESPeasySerial::resetConfig(
|
||||
ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic,
|
||||
unsigned int buffSize)
|
||||
{
|
||||
#if USES_I2C_SC16IS752
|
||||
if (_i2cserial != nullptr) {
|
||||
_i2cserial->end();
|
||||
delete _i2cserial;
|
||||
}
|
||||
#endif
|
||||
#if USES_I2C_SC16IS752
|
||||
_i2cserial = nullptr;
|
||||
#endif
|
||||
_receivePin = receivePin;
|
||||
_transmitPin = transmitPin;
|
||||
_inverse_logic = inverse_logic;
|
||||
_buffSize = buffSize;
|
||||
|
||||
switch (port) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial1:
|
||||
# if HAS_SERIAL2
|
||||
case ESPEasySerialPort::serial2:
|
||||
# endif
|
||||
_serialtype = port;
|
||||
break;
|
||||
default:
|
||||
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
}
|
||||
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
{
|
||||
ESPEasySC16IS752_Serial::I2C_address addr = static_cast<ESPEasySC16IS752_Serial::I2C_address>(receivePin);
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch = static_cast<ESPEasySC16IS752_Serial::SC16IS752_channel>(transmitPin);
|
||||
_i2cserial = new ESPEasySC16IS752_Serial(addr, ch);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
|
||||
void ESPeasySerial::begin(unsigned long baud, uint32_t config
|
||||
, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms) {
|
||||
_baud = baud;
|
||||
|
||||
if (rxPin != -1) { _receivePin = rxPin; }
|
||||
|
||||
if (txPin != -1) { _transmitPin = txPin; }
|
||||
|
||||
_inverse_logic = invert;
|
||||
|
||||
if (!isValid()) {
|
||||
_baud = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
|
||||
if (_i2cserial != nullptr) {
|
||||
_i2cserial->begin(baud);
|
||||
}
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
} else {
|
||||
// Make sure the extra bit is set for the config. The config differs between ESP32 and ESP82xx
|
||||
config = config | 0x8000000;
|
||||
|
||||
if (isValid()) {
|
||||
// Timeout added for 1.0.1
|
||||
// See: https://github.com/espressif/arduino-esp32/commit/233d31bed22211e8c85f82bcf2492977604bbc78
|
||||
// getHW()->begin(baud, config, _receivePin, _transmitPin, invert, timeout_ms);
|
||||
if (pinsChanged(_serialtype, _receivePin, _transmitPin)) {
|
||||
setPinsCache(_serialtype, _receivePin, _transmitPin);
|
||||
// Allow to flush data from the serial buffers
|
||||
// When not opening the USB serial port, the ESP may hang at boot.
|
||||
delay(10);
|
||||
getHW()->end();
|
||||
delay(10);
|
||||
|
||||
getHW()->begin(baud, config, _receivePin, _transmitPin, _inverse_logic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ESPeasySerial::end() {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
flush();
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
_i2cserial->end();
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
} else {
|
||||
// Work-around to fix proper detach RX pin for older ESP32 core versions
|
||||
// For now do not call end()
|
||||
// getHW()->end();
|
||||
}
|
||||
}
|
||||
|
||||
HardwareSerial * ESPeasySerial::getHW() {
|
||||
switch (_serialtype) {
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) && ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
|
||||
case ESPEasySerialPort::serial0: return &Serial0;
|
||||
#else
|
||||
case ESPEasySerialPort::serial0: return &Serial;
|
||||
#endif
|
||||
case ESPEasySerialPort::serial1: return &Serial1;
|
||||
case ESPEasySerialPort::serial2:
|
||||
#if HAS_SERIAL2
|
||||
return &Serial2;
|
||||
#endif
|
||||
|
||||
default: break;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const HardwareSerial * ESPeasySerial::getHW() const {
|
||||
switch (_serialtype) {
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) && ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
|
||||
case ESPEasySerialPort::serial0: return &Serial0;
|
||||
#else
|
||||
case ESPEasySerialPort::serial0: return &Serial;
|
||||
#endif
|
||||
case ESPEasySerialPort::serial1: return &Serial1;
|
||||
case ESPEasySerialPort::serial2:
|
||||
#if HAS_SERIAL2
|
||||
return &Serial2;
|
||||
#endif
|
||||
default: break;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isValid() const {
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial1:
|
||||
return true;
|
||||
case ESPEasySerialPort::serial2:
|
||||
#if HAS_SERIAL2
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial != nullptr;
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return false;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
|
||||
// FIXME TD-er: Must perform proper check for GPIO pins here.
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int ESPeasySerial::peek(void) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial->peek();
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return false;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
return getHW()->peek();
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(uint8_t val) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial->write(val);
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return 0;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
return getHW()->write(val);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
|
||||
if (!isValid() || !buffer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial->write(buffer, size);
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return 0;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
return getHW()->write(buffer, size);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const char *buffer) {
|
||||
if (!buffer) { return 0; }
|
||||
return write(buffer, strlen_P(buffer));
|
||||
}
|
||||
|
||||
int ESPeasySerial::read(void) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial->read();
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return -1;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
return getHW()->read();
|
||||
}
|
||||
|
||||
int ESPeasySerial::available(void) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
return _i2cserial->available();
|
||||
# else // ifndef DISABLE_SC16IS752_Serial
|
||||
return 0;
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
}
|
||||
return getHW()->available();
|
||||
}
|
||||
|
||||
int ESPeasySerial::availableForWrite(void) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
// FIXME TD-er: Implement availableForWrite
|
||||
return 64; // _i2cserial->availableForWrite();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
} else {
|
||||
return getHW()->availableForWrite();
|
||||
}
|
||||
}
|
||||
|
||||
void ESPeasySerial::flush(void) {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
# ifndef DISABLE_SC16IS752_Serial
|
||||
_i2cserial->flush();
|
||||
# endif // ifndef DISABLE_SC16IS752_Serial
|
||||
} else {
|
||||
getHW()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
int ESPeasySerial::baudRate(void) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
return _baud;
|
||||
}
|
||||
return getHW()->baudRate();
|
||||
}
|
||||
|
||||
void ESPeasySerial::setDebugOutput(bool enable) {
|
||||
if (!isValid() || isI2Cserial()) {
|
||||
return;
|
||||
}
|
||||
getHW()->setDebugOutput(enable);
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isTxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
return true;
|
||||
}
|
||||
return _transmitPin != -1;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isRxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
return true;
|
||||
}
|
||||
return _receivePin != -1;
|
||||
}
|
||||
|
||||
// Not supported in ESP32, since only HW serial is used.
|
||||
// Function included since it is used in some libraries.
|
||||
bool ESPeasySerial::listen() {
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // ESP32
|
||||
@@ -1,274 +0,0 @@
|
||||
#include <ESPeasySerial.h>
|
||||
|
||||
#if !USES_SW_SERIAL && defined(ESP8266)
|
||||
|
||||
// ****************************************
|
||||
// ESP8266 implementation wrapper
|
||||
// No SoftwareSerial
|
||||
// Only support HW serial on Serial 0 .. 1
|
||||
// ****************************************
|
||||
|
||||
void ESPeasySerial::resetConfig(ESPEasySerialPort port, int receivePin, int transmitPin, bool inverse_logic, unsigned int buffSize, bool forceSWserial)
|
||||
{
|
||||
_receivePin = receivePin;
|
||||
_transmitPin = transmitPin;
|
||||
_inverse_logic = inverse_logic;
|
||||
_buffSize = buffSize;
|
||||
_forceSWserial = false;
|
||||
|
||||
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
|
||||
if (isValid()) {
|
||||
getHW()->pins(transmitPin, receivePin);
|
||||
}
|
||||
}
|
||||
|
||||
void ESPeasySerial::begin(unsigned long baud, SerialConfig config, SerialMode mode) {
|
||||
_baud = baud;
|
||||
|
||||
if (_serialtype == ESPEasySerialPort::serial0_swap) {
|
||||
// Serial.swap() should only be called here and only once.
|
||||
if (!_serial0_swap_active) {
|
||||
Serial.begin(baud, config, mode, _transmitPin);
|
||||
Serial.swap();
|
||||
_serial0_swap_active = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValid()) {
|
||||
_baud = 0;
|
||||
return;
|
||||
}
|
||||
doHWbegin(baud, config, mode);
|
||||
}
|
||||
|
||||
void ESPeasySerial::end() {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_serialtype == ESPEasySerialPort::serial0_swap) {
|
||||
if (_serial0_swap_active) {
|
||||
Serial.end();
|
||||
Serial.swap();
|
||||
_serial0_swap_active = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
getHW()->end();
|
||||
}
|
||||
|
||||
HardwareSerial * ESPeasySerial::getHW() {
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial0_swap: return &Serial;
|
||||
case ESPEasySerialPort::serial1: return &Serial1;
|
||||
case ESPEasySerialPort::software: break;
|
||||
default: break;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const HardwareSerial * ESPeasySerial::getHW() const {
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial0_swap: return &Serial;
|
||||
case ESPEasySerialPort::serial1: return &Serial1;
|
||||
case ESPEasySerialPort::software: break;
|
||||
default: break;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isValid() const {
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::serial0: return !_serial0_swap_active;
|
||||
case ESPEasySerialPort::serial0_swap: return _serial0_swap_active;
|
||||
case ESPEasySerialPort::serial1: return true; // Must also check RX pin?
|
||||
case ESPEasySerialPort::software: return false;
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int ESPeasySerial::peek(void) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
return getHW()->peek();
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(uint8_t val) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return getHW()->write(val);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
|
||||
if (!isValid() || !buffer) {
|
||||
return 0;
|
||||
}
|
||||
return getHW()->write(buffer, size);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::write(const char *buffer) {
|
||||
if (!buffer) { return 0; }
|
||||
return write(buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
int ESPeasySerial::read(void) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
return getHW()->read();
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::readBytes(char *buffer, size_t size) {
|
||||
if (!isValid() || !buffer) {
|
||||
return 0;
|
||||
}
|
||||
return getHW()->readBytes(buffer, size);
|
||||
}
|
||||
|
||||
size_t ESPeasySerial::readBytes(uint8_t *buffer, size_t size) {
|
||||
return readBytes((char *)buffer, size);
|
||||
}
|
||||
|
||||
int ESPeasySerial::available(void) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return getHW()->available();
|
||||
}
|
||||
|
||||
int ESPeasySerial::availableForWrite() {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return getHW()->availableForWrite();
|
||||
}
|
||||
|
||||
void ESPeasySerial::flush(void) {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
getHW()->flush();
|
||||
}
|
||||
|
||||
bool ESPeasySerial::overflow() {
|
||||
return hasOverrun();
|
||||
}
|
||||
|
||||
bool ESPeasySerial::hasOverrun(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// *****************************
|
||||
// HardwareSerial specific
|
||||
// *****************************
|
||||
|
||||
void ESPeasySerial::swap(uint8_t tx_pin) {
|
||||
if (isValid()) {
|
||||
switch (_serialtype) {
|
||||
case ESPEasySerialPort::serial0:
|
||||
case ESPEasySerialPort::serial0_swap:
|
||||
|
||||
// isValid() also checks for correct swap active state.
|
||||
_serial0_swap_active = !_serial0_swap_active;
|
||||
getHW()->swap(tx_pin);
|
||||
|
||||
if (_serialtype == ESPEasySerialPort::serial0) {
|
||||
_serialtype = ESPEasySerialPort::serial0_swap;
|
||||
} else {
|
||||
_serialtype = ESPEasySerialPort::serial0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ESPeasySerial::baudRate(void) {
|
||||
if (!isValid()) {
|
||||
return _baud;
|
||||
}
|
||||
return getHW()->baudRate();
|
||||
}
|
||||
|
||||
void ESPeasySerial::setDebugOutput(bool enable) {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
getHW()->setDebugOutput(enable);
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isTxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
return getHW()->isTxEnabled();
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isRxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
return getHW()->isRxEnabled();
|
||||
}
|
||||
|
||||
bool ESPeasySerial::hasRxError(void) {
|
||||
# ifdef CORE_POST_2_5_0
|
||||
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
return getHW()->hasRxError();
|
||||
# else // ifdef CORE_POST_2_5_0
|
||||
return false;
|
||||
# endif // ifdef CORE_POST_2_5_0
|
||||
}
|
||||
|
||||
void ESPeasySerial::startDetectBaudrate() {
|
||||
if (!isValid()) {
|
||||
return;
|
||||
}
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
getHW()->startDetectBaudrate();
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
|
||||
unsigned long ESPeasySerial::testBaudrate() {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return getHW()->testBaudrate();
|
||||
# else // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return 0;
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
|
||||
unsigned long ESPeasySerial::detectBaudrate(time_t timeoutMillis) {
|
||||
if (!isValid()) {
|
||||
return 0;
|
||||
}
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return getHW()->detectBaudrate(timeoutMillis);
|
||||
# else // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return 0;
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
|
||||
// *****************************
|
||||
// SoftwareSerial specific
|
||||
// *****************************
|
||||
|
||||
|
||||
bool ESPeasySerial::listen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,124 +0,0 @@
|
||||
#include <ESPeasySerial.h>
|
||||
|
||||
|
||||
#include "wrappers/ESPEasySerial_HardwareSerial.h"
|
||||
#include "wrappers/ESPEasySerial_I2C_SC16IS752.h"
|
||||
#include "wrappers/ESPEasySerial_SW_Serial.h"
|
||||
#include "wrappers/ESPEasySerial_USB_HWCDC.h"
|
||||
#include "wrappers/ESPEasySerial_USBCDC.h"
|
||||
|
||||
ESPeasySerial::ESPeasySerial(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic,
|
||||
unsigned int buffSize,
|
||||
bool forceSWserial)
|
||||
:
|
||||
_receivePin(receivePin),
|
||||
_transmitPin(transmitPin),
|
||||
_inverse_logic(inverse_logic),
|
||||
_buffSize(buffSize),
|
||||
#if USES_SW_SERIAL
|
||||
_forceSWserial(false)
|
||||
#else
|
||||
_forceSWserial(forceSWserial)
|
||||
#endif
|
||||
{
|
||||
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
#if USES_SW_SERIAL
|
||||
if (forceSWserial) {
|
||||
#if USES_I2C_SC16IS752
|
||||
if (_serialtype != ESPEasySerialPort::sc16is752) {
|
||||
_serialtype = ESPEasySerialPort::software;
|
||||
}
|
||||
#else
|
||||
_serialtype = ESPEasySerialPort::software;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
switch (_serialtype) {
|
||||
#if USES_SW_SERIAL
|
||||
case ESPEasySerialPort::software:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_SW_Serial(receivePin, transmitPin, inverse_logic);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_I2C_SC16IS752
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
{
|
||||
ESPEasySC16IS752_Serial::I2C_address addr = static_cast<ESPEasySC16IS752_Serial::I2C_address>(receivePin);
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch = static_cast<ESPEasySC16IS752_Serial::SC16IS752_channel>(transmitPin);
|
||||
|
||||
_serialPort = new ESPEasySerial_I2C_SC16IS752(addr, ch);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_HWCDC
|
||||
case ESPEasySerialPort::usb_hw_cdc:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_USB_WHCDC_t();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if USES_USBCDC
|
||||
case ESPEasySerialPort::usb_cdc_0:
|
||||
case ESPEasySerialPort::usb_cdc_1:
|
||||
{
|
||||
_serialPort = new ESPEasySerial_USBCDC_t(_serialtype);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
|
||||
if (isHWserial(_serialtype)) {
|
||||
_serialPort = new ESPEasySerial_HardwareSerial_t(_serialtype);
|
||||
if (_serialPort != nullptr) {
|
||||
_serialPort->resetConfig(_serialtype, receivePin, transmitPin, inverse_logic, buffSize);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (_serialPort != nullptr) {
|
||||
_serialPort->begin(_baud);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
ESPeasySerial::~ESPeasySerial() {
|
||||
flush();
|
||||
end();
|
||||
|
||||
if (_serialPort != nullptr) {
|
||||
delete _serialPort;
|
||||
}
|
||||
}
|
||||
|
||||
String ESPeasySerial::getLogString() const {
|
||||
String log;
|
||||
|
||||
log.reserve(48);
|
||||
log = F("ESPEasy serial: ");
|
||||
|
||||
if (isI2Cserial()) {
|
||||
log += F("I2C: addr:");
|
||||
log += String(_receivePin);
|
||||
log += F(" ch:");
|
||||
log += _transmitPin == 0 ? 'A' : 'B';
|
||||
} else {
|
||||
if (isSWserial()) {
|
||||
log += F("SW");
|
||||
} else {
|
||||
log += F("HW");
|
||||
}
|
||||
log += F(": rx:");
|
||||
log += String(_receivePin);
|
||||
log += F(" tx:");
|
||||
log += String(_transmitPin);
|
||||
}
|
||||
log += F(" baud:");
|
||||
log += String(_baud);
|
||||
return log;
|
||||
}
|
||||
@@ -36,8 +36,6 @@
|
||||
class ESPeasySerial : public Stream {
|
||||
public:
|
||||
|
||||
#ifdef ESP8266
|
||||
|
||||
// ESP82xx has 2 HW serial ports and option for several software serial ports.
|
||||
// Serial0: RX: 3 TX: 1
|
||||
// Serial0 swapped RX: 13 TX: 15
|
||||
@@ -60,45 +58,15 @@ public:
|
||||
bool forceSWserial = false);
|
||||
|
||||
// If baud rate is set to 0, it will perform an auto-detect on the baudrate
|
||||
void begin(unsigned long baud);
|
||||
#ifdef ESP8266
|
||||
void begin(unsigned long baud,
|
||||
SerialConfig config = SERIAL_8N1,
|
||||
SerialConfig config,
|
||||
SerialMode mode = SERIAL_FULL);
|
||||
#endif // ifdef ESP8266
|
||||
|
||||
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
|
||||
// ESP32 has 3 HW serial ports.
|
||||
// Serial0: RX: 3 TX: 1
|
||||
// Serial1: RX: 9 TX: 10 Defaults will never work, share pins with flash
|
||||
// Serial2: RX: 16 TX: 17
|
||||
// Pins set in the constructor will be used as override when not given when calling begin()
|
||||
// @param inverse_logic can be used to set the logic in the constructor which will then be used in the call to begin.
|
||||
// This makes the call to the constructor more in line with the constructor of SoftwareSerial.
|
||||
// buffsize is for compatibility reasons. ESP32 cannot set the buffer size.
|
||||
ESPeasySerial(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic = false,
|
||||
unsigned int buffSize = 64);
|
||||
virtual ~ESPeasySerial();
|
||||
|
||||
// Same parameters as the constructor, to allow to reconfigure the ESPEasySerial object to be another type of port.
|
||||
void resetConfig(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic = false,
|
||||
unsigned int buffSize = 64);
|
||||
|
||||
|
||||
// If baud rate is set to 0, it will perform an auto-detect on the baudrate
|
||||
void begin(unsigned long baud,
|
||||
uint32_t config = SERIAL_8N1,
|
||||
int8_t rxPin = -1,
|
||||
int8_t txPin = -1,
|
||||
bool invert = false,
|
||||
unsigned long timeout_ms = 20000UL);
|
||||
#endif // ifdef ESP32
|
||||
void begin(unsigned long baud, uint32_t config);
|
||||
#endif
|
||||
|
||||
void end();
|
||||
int peek(void);
|
||||
@@ -134,11 +102,6 @@ public:
|
||||
int baudRate(void);
|
||||
|
||||
#if defined(ESP8266)
|
||||
void swap() {
|
||||
swap(_transmitPin);
|
||||
}
|
||||
|
||||
void swap(uint8_t tx_pin);
|
||||
size_t readBytes(char *buffer,
|
||||
size_t size) override;
|
||||
size_t readBytes(uint8_t *buffer,
|
||||
@@ -164,10 +127,6 @@ public:
|
||||
bool isListening();
|
||||
bool stopListening();
|
||||
|
||||
bool serial0_swap_active() const {
|
||||
return _serial0_swap_active;
|
||||
}
|
||||
|
||||
#endif // if defined(ESP8266)
|
||||
bool listen();
|
||||
|
||||
@@ -177,49 +136,34 @@ public:
|
||||
using Print::write;
|
||||
|
||||
int getRxPin() const {
|
||||
return _receivePin;
|
||||
return getSerialConfig().receivePin;
|
||||
}
|
||||
|
||||
int getTxPin() const {
|
||||
return _transmitPin;
|
||||
return getSerialConfig().transmitPin;
|
||||
}
|
||||
|
||||
unsigned long getBaudRate() const {
|
||||
return _baud;
|
||||
}
|
||||
|
||||
bool useGPIOpins() const {
|
||||
return _serialtype != ESPEasySerialPort::sc16is752;
|
||||
bool usesGPIOpins() const {
|
||||
return useGPIOpins(getSerialConfig().port);
|
||||
}
|
||||
|
||||
ESPEasySerialPort getSerialPortType() const {
|
||||
return _serialtype;
|
||||
return getSerialConfig().port;
|
||||
}
|
||||
private:
|
||||
|
||||
const ESPEasySerial_Port_base* getHW() const;
|
||||
ESPEasySerial_Port_base * getHW();
|
||||
ESPEasySerialConfig getSerialConfig() const {
|
||||
if (_serialPort != nullptr)
|
||||
return _serialPort->getSerialConfig();
|
||||
ESPEasySerialConfig res;
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
ESPEasySerial_Port_base* _serialPort = nullptr;
|
||||
|
||||
|
||||
private:
|
||||
// Actual serial type
|
||||
ESPEasySerialPort _serialtype = ESPEasySerialPort::MAX_SERIAL_TYPE;
|
||||
int _receivePin;
|
||||
int _transmitPin;
|
||||
unsigned long _baud = 0;
|
||||
bool _inverse_logic = false;
|
||||
unsigned int _buffSize = 64;
|
||||
#ifdef ESP8266
|
||||
bool _forceSWserial = false;
|
||||
#endif
|
||||
#ifdef ESP8266
|
||||
SerialConfig _config;
|
||||
SerialMode _mode;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // ifndef ESPeasySerial_h
|
||||
|
||||
@@ -3,37 +3,44 @@
|
||||
#define DETECT_BAUDATE_TIMEOUT 250
|
||||
|
||||
|
||||
|
||||
// ****************************************
|
||||
// ESP8266 implementation wrapper
|
||||
// ****************************************
|
||||
#ifdef ESP8266
|
||||
#if false
|
||||
# ifdef ESP8266
|
||||
bool ESPeasySerial::_serial0_swap_active = false;
|
||||
|
||||
#if USES_SW_SERIAL
|
||||
# if USES_SW_SERIAL
|
||||
|
||||
void ESPeasySerial::resetConfig(ESPEasySerialPort port, int receivePin, int transmitPin, bool inverse_logic, unsigned int buffSize, bool forceSWserial)
|
||||
void ESPeasySerial::resetConfig(ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic,
|
||||
unsigned int buffSize,
|
||||
bool forceSWserial)
|
||||
{
|
||||
const bool wasValid = isValid();
|
||||
|
||||
if (_swserial != nullptr) {
|
||||
_swserial->end();
|
||||
delete _swserial;
|
||||
}
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
|
||||
if (_i2cserial != nullptr) {
|
||||
_i2cserial->end();
|
||||
delete _i2cserial;
|
||||
}
|
||||
#endif
|
||||
#if USES_I2C_SC16IS752
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
_i2cserial = nullptr;
|
||||
#endif
|
||||
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
_swserial = nullptr;
|
||||
_receivePin = receivePin;
|
||||
_transmitPin = transmitPin;
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
|
||||
_swserial = nullptr;
|
||||
_receivePin = receivePin;
|
||||
_transmitPin = transmitPin;
|
||||
_inverse_logic = inverse_logic;
|
||||
_buffSize = buffSize;
|
||||
_buffSize = buffSize;
|
||||
_forceSWserial = forceSWserial;
|
||||
|
||||
if (forceSWserial) {
|
||||
@@ -43,7 +50,7 @@ void ESPeasySerial::resetConfig(ESPEasySerialPort port, int receivePin, int tran
|
||||
default:
|
||||
_serialtype = ESPEasySerialPort::software;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (_serialtype) {
|
||||
@@ -54,12 +61,12 @@ void ESPeasySerial::resetConfig(ESPEasySerialPort port, int receivePin, int tran
|
||||
}
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
{
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
ESPEasySC16IS752_Serial::I2C_address addr = static_cast<ESPEasySC16IS752_Serial::I2C_address>(receivePin);
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch = static_cast<ESPEasySC16IS752_Serial::SC16IS752_channel>(transmitPin);
|
||||
|
||||
_i2cserial = new ESPEasySC16IS752_Serial(addr, ch);
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -69,27 +76,28 @@ void ESPeasySerial::resetConfig(ESPEasySerialPort port, int receivePin, int tran
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (wasValid && isValid()) {
|
||||
begin(_baud, _config, _mode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ESPeasySerial::begin(unsigned long baud, SerialConfig config, SerialMode mode) {
|
||||
_baud = baud;
|
||||
_baud = baud;
|
||||
_config = config;
|
||||
_mode = mode;
|
||||
_mode = mode;
|
||||
|
||||
if (isSWserial()) {
|
||||
if (_swserial != nullptr) {
|
||||
_swserial->begin(baud);
|
||||
}
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
|
||||
if (_i2cserial != nullptr) {
|
||||
_i2cserial->begin(baud);
|
||||
}
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
doHWbegin(baud, config, mode);
|
||||
}
|
||||
@@ -102,14 +110,14 @@ void ESPeasySerial::end() {
|
||||
flush();
|
||||
|
||||
if (isSWserial()) {
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
_swserial->end();
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return;
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
_i2cserial->end();
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
getHW()->end();
|
||||
}
|
||||
@@ -146,11 +154,11 @@ bool ESPeasySerial::isValid() const {
|
||||
case ESPEasySerialPort::serial1: return true; // Must also check RX pin?
|
||||
case ESPEasySerialPort::software: return _swserial != nullptr;
|
||||
case ESPEasySerialPort::sc16is752:
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial != nullptr;
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return false;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
@@ -164,11 +172,11 @@ int ESPeasySerial::peek(void) {
|
||||
if (isSWserial()) {
|
||||
return _swserial->peek();
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->peek();
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return -1;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->peek();
|
||||
}
|
||||
@@ -182,11 +190,11 @@ size_t ESPeasySerial::write(uint8_t val) {
|
||||
if (isSWserial()) {
|
||||
return _swserial->write(val);
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->write(val);
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return 0;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->write(val);
|
||||
}
|
||||
@@ -209,11 +217,11 @@ size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
|
||||
}
|
||||
return count;
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->write(buffer, size);
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return 0;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->write(buffer, size);
|
||||
}
|
||||
@@ -232,11 +240,11 @@ int ESPeasySerial::read(void) {
|
||||
if (isSWserial()) {
|
||||
return _swserial->read();
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->read();
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return -1;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->read();
|
||||
}
|
||||
@@ -260,11 +268,11 @@ size_t ESPeasySerial::readBytes(char *buffer, size_t size) {
|
||||
}
|
||||
return count;
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->readBytes(buffer, size);
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return 0;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->readBytes(buffer, size);
|
||||
}
|
||||
@@ -282,11 +290,11 @@ int ESPeasySerial::available(void) {
|
||||
if (isSWserial()) {
|
||||
return _swserial->available();
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
return _i2cserial->available();
|
||||
#else
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return 0;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->available();
|
||||
}
|
||||
@@ -301,12 +309,13 @@ int ESPeasySerial::availableForWrite() {
|
||||
// FIXME TD-er: Implement availableForWrite
|
||||
return 1;
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
|
||||
// FIXME TD-er: Implement availableForWrite
|
||||
return 4; //_i2cserial->availableForWrite();
|
||||
#else
|
||||
return 4; // _i2cserial->availableForWrite();
|
||||
# else // if USES_I2C_SC16IS752
|
||||
return 0;
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
return getHW()->availableForWrite();
|
||||
}
|
||||
@@ -320,9 +329,9 @@ void ESPeasySerial::flush(void) {
|
||||
if (isSWserial()) {
|
||||
_swserial->flush();
|
||||
} else if (isI2Cserial()) {
|
||||
#if USES_I2C_SC16IS752
|
||||
# if USES_I2C_SC16IS752
|
||||
_i2cserial->flush();
|
||||
#endif
|
||||
# endif // if USES_I2C_SC16IS752
|
||||
} else {
|
||||
getHW()->flush();
|
||||
}
|
||||
@@ -336,9 +345,9 @@ bool ESPeasySerial::hasOverrun(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
return false;
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
|
||||
if (isSWserial() || isI2Cserial()) {
|
||||
return false;
|
||||
@@ -347,7 +356,7 @@ bool ESPeasySerial::hasOverrun(void) {
|
||||
} else {
|
||||
return getHW()->hasOverrun();
|
||||
}
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
}
|
||||
|
||||
// *****************************
|
||||
@@ -377,9 +386,11 @@ bool ESPeasySerial::isTxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isSWserial()) {
|
||||
return _transmitPin != -1;
|
||||
}
|
||||
@@ -390,9 +401,11 @@ bool ESPeasySerial::isRxEnabled(void) {
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isI2Cserial()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isSWserial()) {
|
||||
return _receivePin != -1;
|
||||
}
|
||||
@@ -400,48 +413,48 @@ bool ESPeasySerial::isRxEnabled(void) {
|
||||
}
|
||||
|
||||
bool ESPeasySerial::hasRxError(void) {
|
||||
# ifdef CORE_POST_2_5_0
|
||||
# ifdef CORE_POST_2_5_0
|
||||
|
||||
if (!isValid() || isSWserial() || isI2Cserial()) {
|
||||
return false;
|
||||
}
|
||||
return getHW()->hasRxError();
|
||||
# else // ifdef CORE_POST_2_5_0
|
||||
# else // ifdef CORE_POST_2_5_0
|
||||
return false;
|
||||
# endif // ifdef CORE_POST_2_5_0
|
||||
# endif // ifdef CORE_POST_2_5_0
|
||||
}
|
||||
|
||||
void ESPeasySerial::startDetectBaudrate() {
|
||||
if (!isValid() || isSWserial() || isI2Cserial()) {
|
||||
return;
|
||||
}
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
return;
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
getHW()->startDetectBaudrate();
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
}
|
||||
|
||||
unsigned long ESPeasySerial::testBaudrate() {
|
||||
if (!isValid() || isSWserial() || isI2Cserial()) {
|
||||
return 0;
|
||||
}
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
return 0;
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
return getHW()->testBaudrate();
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
}
|
||||
|
||||
unsigned long ESPeasySerial::detectBaudrate(time_t timeoutMillis) {
|
||||
if (!isValid() || isSWserial() || isI2Cserial()) {
|
||||
return 0;
|
||||
}
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
# ifdef CORE_PRE_2_4_2
|
||||
return 0;
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
# else // ifdef CORE_PRE_2_4_2
|
||||
return getHW()->detectBaudrate(timeoutMillis);
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
# endif // ifdef CORE_PRE_2_4_2
|
||||
}
|
||||
|
||||
// *****************************
|
||||
@@ -451,35 +464,35 @@ unsigned long ESPeasySerial::detectBaudrate(time_t timeoutMillis) {
|
||||
|
||||
bool ESPeasySerial::listen() {
|
||||
if (isValid() && isSWserial()) {
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return _swserial->listen();
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::isListening() {
|
||||
if (isValid() && isSWserial()) {
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return _swserial->isListening();
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ESPeasySerial::stopListening() {
|
||||
if (isValid() && isSWserial()) {
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
return _swserial->stopListening();
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
# endif // ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // ESP8266
|
||||
# endif // ESP8266
|
||||
|
||||
|
||||
#ifdef ESP8266
|
||||
# ifdef ESP8266
|
||||
|
||||
// ****************************************
|
||||
// ESP8266 implementation wrapper
|
||||
@@ -505,4 +518,6 @@ bool ESPeasySerial::doHWbegin(unsigned long baud, SerialConfig config, SerialMod
|
||||
*/
|
||||
}
|
||||
|
||||
#endif // ifdef ESP8266
|
||||
# endif // ifdef ESP8266
|
||||
# endif // ifdef ESP8266
|
||||
#endif // if false
|
||||
|
||||
@@ -71,19 +71,22 @@ void setPinsCache(ESPEasySerialPort port,
|
||||
#endif // ifdef ESP32
|
||||
|
||||
|
||||
ESPEasySerial_HardwareSerial_t::ESPEasySerial_HardwareSerial_t(ESPEasySerialPort port)
|
||||
ESPEasySerial_HardwareSerial_t::ESPEasySerial_HardwareSerial_t(const ESPEasySerialConfig & config)
|
||||
: _serial(nullptr)
|
||||
{
|
||||
_config.port = ESPEasySerialPort::not_set;
|
||||
if (isHWserial(config.port)) {
|
||||
_config = config;
|
||||
}
|
||||
}
|
||||
|
||||
void ESPEasySerial_HardwareSerial_t::resetConfig(
|
||||
ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic,
|
||||
unsigned int buffSize)
|
||||
void ESPEasySerial_HardwareSerial_t::resetConfig(const ESPEasySerialConfig & config)
|
||||
{
|
||||
if (_config == config) return;
|
||||
if (!isHWserial(config.port)) return;
|
||||
|
||||
// First call end()
|
||||
// Then create new instance.
|
||||
|
||||
_config.receivePin = receivePin;
|
||||
_config.transmitPin = transmitPin;
|
||||
_config.inverse_logic = inverse_logic;
|
||||
|
||||
@@ -14,19 +14,14 @@
|
||||
class ESPEasySerial_HardwareSerial_t : public ESPEasySerial_Port_base {
|
||||
public:
|
||||
|
||||
ESPEasySerial_HardwareSerial_t(ESPEasySerialPort port);
|
||||
ESPEasySerial_HardwareSerial_t(const ESPEasySerialConfig & config);
|
||||
|
||||
virtual ~ESPEasySerial_HardwareSerial_t() {}
|
||||
|
||||
|
||||
// Allow for resetConfig, instead of end() and begin().
|
||||
// This can otherwise cause issues with some sensors when starting/stopping an ESPEasy task
|
||||
void resetConfig(
|
||||
ESPEasySerialPort port,
|
||||
int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic = false,
|
||||
unsigned int buffSize = 64);
|
||||
void resetConfig(const ESPEasySerialConfig & config);
|
||||
|
||||
#ifdef ESP8266
|
||||
void setSerialConfig(SerialConfig config = SERIAL_8N1,
|
||||
|
||||
@@ -4,11 +4,16 @@
|
||||
#if USES_I2C_SC16IS752
|
||||
|
||||
|
||||
ESPEasySerial_I2C_SC16IS752::ESPEasySerial_I2C_SC16IS752(ESPEasySC16IS752_Serial::I2C_address addr,
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch)
|
||||
ESPEasySerial_I2C_SC16IS752::ESPEasySerial_I2C_SC16IS752(const ESPEasySerialConfig & config)
|
||||
{
|
||||
_config.port = ESPEasySerialPort::sc16is752;
|
||||
_i2cserial = new ESPEasySC16IS752_Serial(addr, ch);
|
||||
if (config.port == ESPEasySerialPort::sc16is752) {
|
||||
ESPEasySC16IS752_Serial::I2C_address addr;
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch;
|
||||
if (config.getI2C_SC16IS752_Parameters(addr, ch)) {
|
||||
_config = config;
|
||||
_i2cserial = new ESPEasySC16IS752_Serial(addr, ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESPEasySerial_I2C_SC16IS752::~ESPEasySerial_I2C_SC16IS752()
|
||||
|
||||
@@ -18,8 +18,7 @@ public:
|
||||
// ESPEasySC16IS752_Serial::SC16IS752_channel ch = static_cast<ESPEasySC16IS752_Serial::SC16IS752_channel>(transmitPin);
|
||||
|
||||
|
||||
ESPEasySerial_I2C_SC16IS752(ESPEasySC16IS752_Serial::I2C_address addr,
|
||||
ESPEasySC16IS752_Serial::SC16IS752_channel ch);
|
||||
ESPEasySerial_I2C_SC16IS752(const ESPEasySerialConfig & config);
|
||||
|
||||
virtual ~ESPEasySerial_I2C_SC16IS752();
|
||||
|
||||
|
||||
@@ -80,13 +80,20 @@ public:
|
||||
return _config.baud;
|
||||
}
|
||||
|
||||
bool useGPIOpins() const {
|
||||
return _config.port != ESPEasySerialPort::sc16is752;
|
||||
const ESPEasySerialConfig& getSerialConfig() const {
|
||||
return _config;
|
||||
}
|
||||
|
||||
ESPEasySerialPort getSerialPortType() const {
|
||||
return _config.port;
|
||||
}
|
||||
#ifdef ESP8266
|
||||
void setPortConfig(unsigned long baud,
|
||||
SerialConfig config,
|
||||
SerialMode mode);
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
void setPortConfig(unsigned long baud, uint32_t config);
|
||||
#endif
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
@@ -4,10 +4,15 @@
|
||||
#if USES_SW_SERIAL
|
||||
|
||||
|
||||
ESPEasySerial_SW_Serial::ESPEasySerial_SW_Serial(int receivePin, int transmitPin, bool inverse_logic)
|
||||
ESPEasySerial_SW_Serial::ESPEasySerial_SW_Serial(const ESPEasySerialConfig & config)
|
||||
{
|
||||
_config.port = ESPEasySerialPort::software;
|
||||
_swserial = new ESPeasySoftwareSerial(receivePin, transmitPin, inverse_logic);
|
||||
if (config.port == ESPEasySerialPort::software) {
|
||||
_config = config;
|
||||
_swserial = new ESPeasySoftwareSerial(
|
||||
config.receivePin,
|
||||
config.transmitPin,
|
||||
config.inverse_logic);
|
||||
}
|
||||
}
|
||||
|
||||
ESPEasySerial_SW_Serial::~ESPEasySerial_SW_Serial()
|
||||
|
||||
@@ -15,9 +15,7 @@
|
||||
class ESPEasySerial_SW_Serial : public ESPEasySerial_Port_base {
|
||||
public:
|
||||
|
||||
ESPEasySerial_SW_Serial(int receivePin,
|
||||
int transmitPin,
|
||||
bool inverse_logic);
|
||||
ESPEasySerial_SW_Serial(const ESPEasySerialConfig & config);
|
||||
|
||||
virtual ~ESPEasySerial_SW_Serial();
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ static void usbcdcEventCallback(void *arg, esp_event_base_t event_base, int32_t
|
||||
}
|
||||
}
|
||||
|
||||
ESPEasySerial_USBCDC_t::ESPEasySerial_USBCDC_t(ESPEasySerialPort port)
|
||||
ESPEasySerial_USBCDC_t::ESPEasySerial_USBCDC_t(const ESPEasySerialConfig & config)
|
||||
: _serial(nullptr),
|
||||
_mustDelete(false)
|
||||
{
|
||||
@@ -94,7 +94,7 @@ ESPEasySerial_USBCDC_t::ESPEasySerial_USBCDC_t(ESPEasySerialPort port)
|
||||
}
|
||||
|
||||
if (uart_nr > 0) {
|
||||
_config.port = port;
|
||||
_config.port = config.port;
|
||||
|
||||
if ((uart_nr == 0) && (_usbcdc_serial != nullptr)) {
|
||||
_serial = _usbcdc_serial;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
class ESPEasySerial_USBCDC_t : public ESPEasySerial_Port_base {
|
||||
public:
|
||||
|
||||
ESPEasySerial_USBCDC_t(ESPEasySerialPort port);
|
||||
ESPEasySerial_USBCDC_t(const ESPEasySerialConfig & config);
|
||||
|
||||
virtual ~ESPEasySerial_USBCDC_t();
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ static void hwcdcEventCallback(void *arg, esp_event_base_t event_base, int32_t e
|
||||
}
|
||||
}
|
||||
|
||||
ESPEasySerial_USB_WHCDC_t::ESPEasySerial_USB_WHCDC_t()
|
||||
ESPEasySerial_USB_WHCDC_t::ESPEasySerial_USB_WHCDC_t(const ESPEasySerialConfig & config)
|
||||
{
|
||||
_config.port = ESPEasySerialPort::usb_hw_cdc;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
class ESPEasySerial_USB_WHCDC_t : public ESPEasySerial_Port_base {
|
||||
public:
|
||||
|
||||
ESPEasySerial_USB_WHCDC_t();
|
||||
ESPEasySerial_USB_WHCDC_t(const ESPEasySerialConfig & config);
|
||||
|
||||
virtual ~ESPEasySerial_USB_WHCDC_t() {}
|
||||
|
||||
|
||||
@@ -75,11 +75,21 @@ void Caches::updateActiveTaskUseSerial0() {
|
||||
static_cast<ESPEasySerialPort>(Settings.TaskDevicePort[task]),
|
||||
Settings.TaskDevicePin1[task],
|
||||
Settings.TaskDevicePin2[task]);
|
||||
|
||||
// FIXME TD-er: Must not check for conflict with serial0, but for conflict with ESPEasy_Console.
|
||||
#ifdef ESP32
|
||||
if (port == ESPEasySerialPort::serial0)
|
||||
{
|
||||
activeTaskUseSerial0 = true;
|
||||
}
|
||||
#endif
|
||||
#ifdef ESP8266
|
||||
if (port == ESPEasySerialPort::serial0_swap ||
|
||||
port == ESPEasySerialPort::serial0)
|
||||
{
|
||||
activeTaskUseSerial0 = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "../DataTypes/ESPEasy_plugin_functions.h"
|
||||
|
||||
#include "../ESPEasyCore/ESPEasy_USB.h"
|
||||
//#include "../ESPEasyCore/ESPEasy_USB.h"
|
||||
|
||||
#include "../Globals/Cache.h"
|
||||
#include "../Globals/Logging.h"
|
||||
|
||||
Reference in New Issue
Block a user