intermediate: use the arduino hwserial as idf does not receive data
This commit is contained in:
parent
9174f7d086
commit
bec155fb4d
|
@ -1,30 +1,34 @@
|
||||||
#include "GwSerial.h"
|
#include "GwSerial.h"
|
||||||
class SerialWriter : public GwBufferWriter{
|
class SerialWriter : public GwBufferWriter{
|
||||||
private:
|
private:
|
||||||
uart_port_t num;
|
HardwareSerial *serial;
|
||||||
public:
|
public:
|
||||||
SerialWriter(uart_port_t num){
|
SerialWriter(HardwareSerial *serial){
|
||||||
this->num=num;
|
this->serial=serial;
|
||||||
}
|
}
|
||||||
virtual ~SerialWriter(){}
|
virtual ~SerialWriter(){}
|
||||||
virtual int write(const uint8_t *buffer,size_t len){
|
virtual int write(const uint8_t *buffer,size_t len){
|
||||||
return uart_tx_chars(num,(const char *)buffer,len);
|
size_t numWrite=serial->availableForWrite();
|
||||||
|
if (numWrite < len) len=numWrite;
|
||||||
|
if (!len) return 0;
|
||||||
|
return serial->write(buffer,len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
GwSerial::GwSerial(GwLog *logger, uart_port_t num, int id,bool allowRead)
|
GwSerial::GwSerial(GwLog *logger, int num, int id,bool allowRead)
|
||||||
{
|
{
|
||||||
LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p port %d",this,(int)num);
|
LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p port %d",this,(int)num);
|
||||||
this->id=id;
|
this->id=id;
|
||||||
this->logger = logger;
|
this->logger = logger;
|
||||||
this->num = num;
|
this->num = num;
|
||||||
this->buffer = new GwBuffer(logger,GwBuffer::TX_BUFFER_SIZE);
|
this->buffer = new GwBuffer(logger,GwBuffer::TX_BUFFER_SIZE);
|
||||||
this->writer = new SerialWriter(num);
|
|
||||||
this->allowRead=allowRead;
|
this->allowRead=allowRead;
|
||||||
if (allowRead){
|
if (allowRead){
|
||||||
this->readBuffer=new GwBuffer(logger, GwBuffer::RX_BUFFER_SIZE);
|
this->readBuffer=new GwBuffer(logger, GwBuffer::RX_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
this->serial=new HardwareSerial(num);
|
||||||
|
this->writer = new SerialWriter(serial);
|
||||||
|
|
||||||
}
|
}
|
||||||
GwSerial::~GwSerial()
|
GwSerial::~GwSerial()
|
||||||
|
@ -32,32 +36,14 @@ GwSerial::~GwSerial()
|
||||||
delete buffer;
|
delete buffer;
|
||||||
delete writer;
|
delete writer;
|
||||||
if (readBuffer) delete readBuffer;
|
if (readBuffer) delete readBuffer;
|
||||||
|
delete serial;
|
||||||
}
|
}
|
||||||
int GwSerial::setup(int baud, int rxpin, int txpin)
|
int GwSerial::setup(int baud, int rxpin, int txpin)
|
||||||
{
|
{
|
||||||
uart_config_t config = {
|
serial->begin(baud,SERIAL_8N1,rxpin,txpin);
|
||||||
.baud_rate = baud,
|
|
||||||
.data_bits = UART_DATA_8_BITS,
|
|
||||||
.parity = UART_PARITY_DISABLE,
|
|
||||||
.stop_bits = UART_STOP_BITS_1,
|
|
||||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
||||||
};
|
|
||||||
if (uart_driver_install(num, bufferSize, 0, 0, NULL, 0) != ESP_OK)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
// Configure UART parameters
|
|
||||||
if (uart_param_config(num, &config) != ESP_OK)
|
|
||||||
{
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (uart_set_pin(num, txpin, rxpin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK)
|
|
||||||
{
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
buffer->reset();
|
buffer->reset();
|
||||||
initialized = true;
|
initialized = true;
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
bool GwSerial::isInitialized() { return initialized; }
|
bool GwSerial::isInitialized() { return initialized; }
|
||||||
size_t GwSerial::enqueue(const uint8_t *data, size_t len)
|
size_t GwSerial::enqueue(const uint8_t *data, size_t len)
|
||||||
|
@ -83,9 +69,13 @@ void GwSerial::loop(bool handleRead){
|
||||||
if (! isInitialized()) return;
|
if (! isInitialized()) return;
|
||||||
if (! handleRead) return;
|
if (! handleRead) return;
|
||||||
char buffer[10];
|
char buffer[10];
|
||||||
int rt=uart_read_bytes(num,(uint8_t *)(&buffer),10,0);
|
size_t available=serial->available();
|
||||||
|
if (! available) return;
|
||||||
|
if (available > 10) available=10;
|
||||||
|
int rt=serial->readBytes(buffer,available);
|
||||||
if (allowRead && rt > 0){
|
if (allowRead && rt > 0){
|
||||||
readBuffer->addData((uint8_t *)(&buffer),rt,true);
|
size_t wr=readBuffer->addData((uint8_t *)(&buffer),rt,true);
|
||||||
|
LOG_DEBUG(GwLog::DEBUG+2,"GwSerial read %d bytes, to buffer %d bytes",rt,wr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool GwSerial::readMessages(GwBufferWriter *writer){
|
bool GwSerial::readMessages(GwBufferWriter *writer){
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#ifndef _GWSERIAL_H
|
#ifndef _GWSERIAL_H
|
||||||
#define _GWSERIAL_H
|
#define _GWSERIAL_H
|
||||||
#include "driver/uart.h"
|
#include "HardwareSerial.h"
|
||||||
#include "GwLog.h"
|
#include "GwLog.h"
|
||||||
#include "GwBuffer.h"
|
#include "GwBuffer.h"
|
||||||
class SerialWriter;
|
class SerialWriter;
|
||||||
|
@ -10,16 +10,17 @@ class GwSerial{
|
||||||
GwBuffer *readBuffer=NULL;
|
GwBuffer *readBuffer=NULL;
|
||||||
GwLog *logger;
|
GwLog *logger;
|
||||||
SerialWriter *writer;
|
SerialWriter *writer;
|
||||||
uart_port_t num;
|
int num;
|
||||||
bool initialized=false;
|
bool initialized=false;
|
||||||
bool allowRead=true;
|
bool allowRead=true;
|
||||||
GwBuffer::WriteStatus write();
|
GwBuffer::WriteStatus write();
|
||||||
int id=-1;
|
int id=-1;
|
||||||
int overflows=0;
|
int overflows=0;
|
||||||
size_t enqueue(const uint8_t *data, size_t len);
|
size_t enqueue(const uint8_t *data, size_t len);
|
||||||
|
HardwareSerial *serial;
|
||||||
public:
|
public:
|
||||||
static const int bufferSize=200;
|
static const int bufferSize=200;
|
||||||
GwSerial(GwLog *logger,uart_port_t num,int id,bool allowRead=true);
|
GwSerial(GwLog *logger,int num,int id,bool allowRead=true);
|
||||||
~GwSerial();
|
~GwSerial();
|
||||||
int setup(int baud,int rxpin,int txpin);
|
int setup(int baud,int rxpin,int txpin);
|
||||||
bool isInitialized();
|
bool isInitialized();
|
||||||
|
|
19
src/main.cpp
19
src/main.cpp
|
@ -92,7 +92,7 @@ GwConfigInterface *receiveSerial=config.getConfigItem(config.receiveSerial,true)
|
||||||
GwConfigInterface *sendSerial=config.getConfigItem(config.sendSerial,true);
|
GwConfigInterface *sendSerial=config.getConfigItem(config.sendSerial,true);
|
||||||
GwConfigInterface *n2kFromSerial=config.getConfigItem(config.serialToN2k,true);
|
GwConfigInterface *n2kFromSerial=config.getConfigItem(config.serialToN2k,true);
|
||||||
|
|
||||||
GwSerial *usbSerial = new GwSerial(NULL, UART_NUM_0, USB_CHANNEL_ID);
|
GwSerial *usbSerial = new GwSerial(NULL, 0, USB_CHANNEL_ID);
|
||||||
GwSerial *serial1=NULL;
|
GwSerial *serial1=NULL;
|
||||||
|
|
||||||
|
|
||||||
|
@ -275,8 +275,8 @@ void setup() {
|
||||||
}
|
}
|
||||||
logger.logDebug(GwLog::LOG,"config: %s", config.toString().c_str());
|
logger.logDebug(GwLog::LOG,"config: %s", config.toString().c_str());
|
||||||
#ifdef GWSERIAL_MODE
|
#ifdef GWSERIAL_MODE
|
||||||
int serialrx=UART_PIN_NO_CHANGE;
|
int serialrx=-1;
|
||||||
int serialtx=UART_PIN_NO_CHANGE;
|
int serialtx=-1;
|
||||||
#ifdef GWSERIAL_TX
|
#ifdef GWSERIAL_TX
|
||||||
serialtx=GWSERIAL_TX;
|
serialtx=GWSERIAL_TX;
|
||||||
#endif
|
#endif
|
||||||
|
@ -289,17 +289,18 @@ void setup() {
|
||||||
if (serialMode != String("UNI")){
|
if (serialMode != String("UNI")){
|
||||||
serialDirection=String("");
|
serialDirection=String("");
|
||||||
}
|
}
|
||||||
if (serialDirection == "receive" || serialDirection == "off") serialtx=UART_PIN_NO_CHANGE;
|
//if (serialDirection == "receive" || serialDirection == "off") serialtx=UART_PIN_NO_CHANGE;
|
||||||
if (serialDirection == "send" || serialDirection == "off") serialrx=UART_PIN_NO_CHANGE;
|
//if (serialDirection == "send" || serialDirection == "off") serialrx=UART_PIN_NO_CHANGE;
|
||||||
logger.logDebug(GwLog::DEBUG,"serial set up: mode=%s,direction=%s,rx=%d,tx=%d",
|
logger.logDebug(GwLog::DEBUG,"serial set up: mode=%s,direction=%s,rx=%d,tx=%d",
|
||||||
serialMode.c_str(),serialDirection.c_str(),serialrx,serialtx
|
serialMode.c_str(),serialDirection.c_str(),serialrx,serialtx
|
||||||
);
|
);
|
||||||
if (serialtx != UART_PIN_NO_CHANGE || serialrx != UART_PIN_NO_CHANGE){
|
if (serialtx != -1 || serialrx != -1){
|
||||||
logger.logDebug(GwLog::LOG,"creating serial interface rx=%d, tx=%d",serialrx,serialtx);
|
logger.logDebug(GwLog::LOG,"creating serial interface rx=%d, tx=%d",serialrx,serialtx);
|
||||||
serial1=new GwSerial(&logger,UART_NUM_1,SERIAL1_CHANNEL_ID,true);
|
serial1=new GwSerial(&logger,1,SERIAL1_CHANNEL_ID,true);
|
||||||
}
|
}
|
||||||
if (serial1){
|
if (serial1){
|
||||||
serial1->setup(config.getInt(config.serialBaud,115200),serialrx,serialtx);
|
int rt=serial1->setup(config.getInt(config.serialBaud,115200),serialrx,serialtx);
|
||||||
|
logger.logDebug(GwLog::LOG,"starting serial returns %d",rt);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -442,7 +443,7 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg, int sourceId) {
|
||||||
void handleReceivedNmeaMessage(const char *buf, int sourceId){
|
void handleReceivedNmeaMessage(const char *buf, int sourceId){
|
||||||
if ((sourceId == USB_CHANNEL_ID && n2kFromUSB->asBoolean())||
|
if ((sourceId == USB_CHANNEL_ID && n2kFromUSB->asBoolean())||
|
||||||
(sourceId >= MIN_TCP_CHANNEL_ID && n2kFromTCP->asBoolean())||
|
(sourceId >= MIN_TCP_CHANNEL_ID && n2kFromTCP->asBoolean())||
|
||||||
(sourceId == SERIAL1_CHANNEL_ID && n2kFromSerial)
|
(sourceId == SERIAL1_CHANNEL_ID && n2kFromSerial->asBoolean())
|
||||||
)
|
)
|
||||||
toN2KConverter->parseAndSend(buf,sourceId);
|
toN2KConverter->parseAndSend(buf,sourceId);
|
||||||
sendBufferToChannels(buf,sourceId);
|
sendBufferToChannels(buf,sourceId);
|
||||||
|
|
Loading…
Reference in New Issue