Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Chris2016
#39174 Greetings, this may be a p.o.c. for advanced coders, I however seem to be stuck. I simply want a methodology to direct certain text in an uint8_t *buffer of size uint16_t length to different write(const uint8_t *, size_t) functions. Unfortunately, the write functions are member functions of several different classes, the Serial-Class, the Wifi-Client-Class and an asynchronous TCP-Client-Class (https://github.com/me-no-dev/ESPAsyncTCP).

All write functions share the same signature:
Code: Select allsize_t write(const uint8_t *buf, size_t len);

So I'd define my function pointer like so:
Code: Select alltypedef size_t (* writePtr) (const uint8_t *, size_t);
writePtr outputWriteChannel;

And call it like so:
Code: Select allWiFiClient testWifiClient;
// populate wifi client obviously
AsyncClient testAsyncClient;
// populate async client obviously

uint8_t buf[100];
strcpy(buf, "Hello World!");
outputWriteChannel = &Serial.write;
outputWriteChannel(buf, len); // Goes to Serial
outputWriteChannel = &testWifiClient.write;
outputWriteChannel(buf, len); // Goes to Synchronous TCP client
outputWriteChannel = &testAsyncClient.write;
outputWriteChannel(buf, len); // Goes to aSynchronous TCP client


That, however, failes at compile time. Does anybody know how I would do it correctly?
User avatar
By martinayotte
#39195 Switching output between WifiClient and Serial was easy since both was derived from Print class.
But it seems that this new ESPAsyncTCP library doesn't have its clients derived from Print class.
Maybe @me-no-dev should look to make it compatible ...