Hi
Creating an external component that reads some data from a UART. Data is stored in a buffer. The size of the buffer is configurable in YAML. So nothing special:
In YAML:
- platform: MyTextSensor
name: testsensor
buffersize: 4096
in sensor.py:
async def to_code(config):
...
cg.add(var.set_buffersize(buffersize))
In the C++ code:
void set_buffersize(uint16_t value) { this->maxbuffer_ = value; }
And then allocate the buffer on the heap.
But what if I would like the buffer be defined statically and not on the heap (should be possible, because all info is known at compile time). Then I would write it as below
class MyTextSensor : public text_sensor::TextSensor, public Component, public uart::UARTDevice {
public:
void setup() override;
void loop() override;
protected:
uint16_t maxbuffer_ = 10;
char buffer_[10];
But now the set_buffersize
function is not the way to initialize the buffersize to 4096. It should be written directly in the code. So the code to be compiled has to be altered and looks like:
class MyTextSensor : public text_sensor::TextSensor, public Component, public uart::UARTDevice {
public:
void setup() override;
void loop() override;
protected:
uint16_t maxbuffer_ = 4096;
char buffer_[4096];
Is this possible?