Skip to content

Commit aa64ae2

Browse files
committed
Provide message to be written to prefix/suffix callbacks:
* Provide message to prefix function. * Provide prefix and message to suffix function.
1 parent ad17cde commit aa64ae2

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

examples/Threadsafe_Serial_Prefix_Suffix/Threadsafe_Serial_Prefix_Suffix.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ void serial_thread_func()
5757
Serial.begin(9600);
5858

5959
char const * thread_name = rtos::ThisThread::get_name();
60-
Serial.prefix([thread_name]() -> String
60+
Serial.prefix([thread_name](String const & /* msg */) -> String
6161
{
6262
char msg[64] = {0};
6363
snprintf(msg, sizeof(msg), "[%05lu] %s ", millis(), thread_name);
6464
return String(msg);
6565
});
66-
Serial.suffix([]() -> String
66+
Serial.suffix([](String const & /* prefix */, String const & /* msg */) -> String
6767
{
6868
return String("\r\n");
6969
});

src/serial/SerialDispatcher.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,36 +236,39 @@ void SerialDispatcher::threadFunc()
236236
if (!d.tx_buffer.available())
237237
return;
238238

239+
/* Retrieve all data stored in the transmit ringbuffer
240+
* and store it into a String for usage by both suffix
241+
* prefix callback functions.
242+
*/
243+
String msg;
244+
while(d.tx_buffer.available())
245+
msg += static_cast<char>(d.tx_buffer.read_char());
246+
239247
/* The prefix callback function allows the
240248
* user to insert a custom message before
241249
* a new message is written to the serial
242250
* driver. This is useful e.g. for wrapping
243251
* protocol (e.g. the 'AT' protocol) or providing
244252
* a timestamp, a log level, ...
245253
*/
254+
String prefix;
246255
if (d.prefix_func)
247-
{
248-
String const prefix_str = d.prefix_func();
249-
_serial.write(prefix_str.c_str());
250-
}
251-
252-
/* Now it's time to actually write the message
253-
* conveyed by the user via Serial.print/println.
254-
*/
255-
while(d.tx_buffer.available())
256-
{
257-
_serial.write(d.tx_buffer.read_char());
258-
}
256+
prefix = d.prefix_func(msg);
259257

260258
/* Similar to the prefix function this callback
261259
* allows the user to specify a specific message
262260
* to be appended to each message, e.g. '\r\n'.
263261
*/
262+
String suffix;
264263
if (d.suffix_func)
265-
{
266-
String const suffix_str = d.suffix_func();
267-
_serial.write(suffix_str.c_str());
268-
}
264+
suffix = d.suffix_func(prefix, msg);
265+
266+
/* Now it's time to actually write the message
267+
* conveyed by the user via Serial.print/println.
268+
*/
269+
_serial.write(prefix.c_str());
270+
_serial.write(msg.c_str());
271+
_serial.write(suffix.c_str());
269272
});
270273
}
271274
}

src/serial/SerialDispatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class SerialDispatcher : public arduino::HardwareSerial
5959
void block();
6060
void unblock();
6161

62-
typedef std::function<String(void)> PrefixInjectorCallbackFunc;
63-
typedef PrefixInjectorCallbackFunc SuffixInjectorCallbackFunc;
62+
typedef std::function<String(String const &)> PrefixInjectorCallbackFunc;
63+
typedef std::function<String(String const &, String const &)> SuffixInjectorCallbackFunc;
6464
void prefix(PrefixInjectorCallbackFunc func);
6565
void suffix(SuffixInjectorCallbackFunc func);
6666

0 commit comments

Comments
 (0)