Skip to content

Commit d164c26

Browse files
committed
Replace inline-lamda-implementation with regular function pointers.
1 parent 4b02a9d commit d164c26

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

examples/Threadsafe_Serial_Prefix_Suffix/Threadsafe_Serial_Prefix_Suffix.ino

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,41 @@ void loop()
5252
* FUNCTION DEFINITION
5353
**************************************************************************************/
5454

55-
void serial_thread_func()
55+
String nmea_message_prefix(String const & /* msg */)
5656
{
57-
Serial.begin(9600);
57+
return String("$");
58+
}
5859

59-
Serial.prefix([](String const & /* msg */) -> String
60-
{
61-
return String("$");
62-
});
63-
Serial.suffix([](String const & prefix, String const & msg) -> String
60+
String nmea_message_suffix(String const & prefix, String const & msg)
61+
{
62+
/* NMEA checksum is calculated over the complete message
63+
* starting with '$' and ending with the end of the message.
64+
*/
65+
byte checksum = 0;
66+
std::for_each(msg.c_str(),
67+
msg.c_str() + msg.length(),
68+
[&checksum](char const c)
6469
{
65-
/* NMEA checksum is calculated over the complete message
66-
* starting with '$' and ending with the end of the message.
67-
*/
68-
byte checksum = 0;
69-
std::for_each(msg.c_str(),
70-
msg.c_str() + msg.length(),
71-
[&checksum](char const c)
72-
{
73-
checksum ^= static_cast<uint8_t>(c);
74-
});
75-
/* Assemble the footer of the NMEA message. */
76-
char footer[16] = {0};
77-
snprintf(footer, sizeof(footer), "*%02X\r\n", checksum);
78-
return String(footer);
70+
checksum ^= static_cast<uint8_t>(c);
7971
});
72+
/* Assemble the footer of the NMEA message. */
73+
char footer[16] = {0};
74+
snprintf(footer, sizeof(footer), "*%02X\r\n", checksum);
75+
return String(footer);
76+
}
77+
78+
void serial_thread_func()
79+
{
80+
Serial.begin(9600);
81+
82+
Serial.prefix(nmea_message_prefix);
83+
Serial.suffix(nmea_message_suffix);
8084

8185
for(;;)
8286
{
8387
/* Sleep between 5 and 500 ms */
8488
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
89+
8590
/* Print a fake NMEA GPRMC message:
8691
* $GPRMC,062101.714,A,5001.869,N,01912.114,E,955535.7,116.2,290520,000.0,W*45\r\n
8792
*/

0 commit comments

Comments
 (0)