1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-14 06:23:07 +01:00

allow to start m5 atom s3 without USB device connected

This commit is contained in:
andreas
2024-03-23 20:21:21 +01:00
parent 5b0b8ba799
commit d27e811317
5 changed files with 103 additions and 30 deletions

View File

@@ -40,7 +40,7 @@ class GwSerialStream: public Stream{
GwSerial::GwSerial(GwLog *logger, HardwareSerial *s, int id,bool allowRead):serial(s)
GwSerial::GwSerial(GwLog *logger, Stream *s, int id,bool allowRead):serial(s)
{
LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p id %d",this,id);
this->id=id;
@@ -54,10 +54,6 @@ GwSerial::GwSerial(GwLog *logger, HardwareSerial *s, int id,bool allowRead):seri
this->readBuffer=new GwBuffer(logger, GwBuffer::RX_BUFFER_SIZE,bufName+"rd");
}
buffer->reset("init");
serial->onReceiveError([this](hardwareSerial_error_t err){
GwLog *logger=this->logger;
LOG_DEBUG(GwLog::ERROR,"serial error on id %d: %d",this->id,(int)err);
});
initialized=true;
}
GwSerial::~GwSerial()
@@ -119,11 +115,21 @@ void GwSerial::readMessages(GwMessageFetcher *writer){
writer->handleBuffer(readBuffer);
}
void GwSerial::flush(){
if (! isInitialized()) return;
while (write() == GwBuffer::AGAIN){
vTaskDelay(1);
bool GwSerial::flush(long max){
if (! isInitialized()) return false;
if (! availableWrite) {
if ( serial->availableForWrite() < 1){
return false;
}
availableWrite=true;
}
auto start=millis();
while (millis() < (start+max)){
if (write() != GwBuffer::AGAIN) return true;
vTaskDelay(1);
}
availableWrite=(serial->availableForWrite() > 0);
return false;
}
Stream * GwSerial::getStream(bool partialWrite){
return new GwSerialStream(this,partialWrite);

View File

@@ -16,17 +16,19 @@ class GwSerial : public GwChannelInterface{
int id=-1;
int overflows=0;
size_t enqueue(const uint8_t *data, size_t len,bool partial=false);
HardwareSerial *serial;
Stream *serial;
bool availableWrite=false; //if this is false we will wait for availabkleWrite until we flush again
public:
static const int bufferSize=200;
GwSerial(GwLog *logger,HardwareSerial *stream,int id,bool allowRead=true);
GwSerial(GwLog *logger,Stream *stream,int id,bool allowRead=true);
~GwSerial();
bool isInitialized();
virtual size_t sendToClients(const char *buf,int sourceId,bool partial=false);
virtual void loop(bool handleRead=true,bool handleWrite=true);
virtual void readMessages(GwMessageFetcher *writer);
void flush();
bool flush(long millis=200);
virtual Stream *getStream(bool partialWrites);
bool getAvailableWrite(){return availableWrite;}
friend GwSerialStream;
};
#endif