|
| 1 | +//MF_Serial wrapper around SerialUART with task based TX buffer |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "hal_RP2040.h" |
| 6 | +#include "../MF_Serial.h" |
| 7 | +#include <stream_buffer.h> |
| 8 | + |
| 9 | +void MF_SerialUART_task( void * pvParameters ); |
| 10 | + |
| 11 | +class MF_SerialUART : public MF_Serial { |
| 12 | + public: |
| 13 | + SerialUART *_serial; |
| 14 | + StreamBufferHandle_t xStreamBuffer = NULL; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + MF_SerialUART(SerialUART *_serial, const char* taskname) { |
| 19 | + this->_serial = _serial; |
| 20 | + |
| 21 | + xStreamBuffer = xStreamBufferCreate(256, 1); // length, triggerlevel |
| 22 | + |
| 23 | + xTaskCreate( MF_SerialUART_task, // The function that implements the task |
| 24 | + taskname, // Human readable name for the task |
| 25 | + configMINIMAL_STACK_SIZE, // Stack size (in words!) |
| 26 | + (void*)this, // Task parameter |
| 27 | + uxTaskPriorityGet(NULL), // The priority at which the task is created |
| 28 | + NULL ); // No use for the task handle |
| 29 | + } |
| 30 | + |
| 31 | + void begin(int baud) override { |
| 32 | + _serial->begin(baud); |
| 33 | + } |
| 34 | + |
| 35 | + int read(uint8_t *buf, int len) override { |
| 36 | + return _serial->readBytes(buf, len); |
| 37 | + } |
| 38 | + |
| 39 | + int available() override { |
| 40 | + return _serial->available(); |
| 41 | + } |
| 42 | + |
| 43 | + int availableForWrite() override { |
| 44 | + return xStreamBufferSpacesAvailable(xStreamBuffer); |
| 45 | + } |
| 46 | + |
| 47 | + int write(uint8_t *buf, int len) override { |
| 48 | + if(availableForWrite() < len) return 0; |
| 49 | + return xStreamBufferSend( xStreamBuffer, (const void *)buf, len, 0 ); //0 = no wait |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +void MF_SerialUART_task( void * pvParameters ) { |
| 54 | + MF_SerialUART *self = (MF_SerialUART*) pvParameters; |
| 55 | + uint8_t b; |
| 56 | + for( ;; ) { |
| 57 | + xStreamBufferReceive( self->xStreamBuffer, &b, 1, portMAX_DELAY ); |
| 58 | + self->_serial->write(b); |
| 59 | + } |
| 60 | +} |
0 commit comments