Skip to content

Commit b6ce537

Browse files
committed
Use Arduino_Threads for Threadsafe_Serial_ProtocolWrapping.
1 parent 70eaaf2 commit b6ce537

File tree

3 files changed

+72
-92
lines changed

3 files changed

+72
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**************************************************************************************
2+
* FUNCTION DEFINITION
3+
**************************************************************************************/
4+
5+
static String nmea_message_prefix(String const & /* msg */)
6+
{
7+
return String("$");
8+
}
9+
10+
static String nmea_message_suffix(String const & prefix, String const & msg)
11+
{
12+
/* NMEA checksum is calculated over the complete message
13+
* starting with '$' and ending with the end of the message.
14+
*/
15+
byte checksum = 0;
16+
std::for_each(msg.c_str(),
17+
msg.c_str() + msg.length(),
18+
[&checksum](char const c)
19+
{
20+
checksum ^= static_cast<uint8_t>(c);
21+
});
22+
/* Assemble the footer of the NMEA message. */
23+
char footer[16] = {0};
24+
snprintf(footer, sizeof(footer), "*%02X\r\n", checksum);
25+
return String(footer);
26+
}
27+
28+
/**************************************************************************************
29+
* SETUP/LOOP
30+
**************************************************************************************/
31+
32+
void setup()
33+
{
34+
Serial.begin(9600);
35+
36+
Serial.prefix(nmea_message_prefix);
37+
Serial.suffix(nmea_message_suffix);
38+
}
39+
40+
void loop()
41+
{
42+
/* Sleep between 5 and 500 ms */
43+
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
44+
45+
/* Print a fake NMEA GPRMC message:
46+
* $GPRMC,062101.714,A,5001.869,N,01912.114,E,955535.7,116.2,290520,000.0,W*45\r\n
47+
*/
48+
Serial.block();
49+
50+
Serial.print("GPRMC,");
51+
Serial.print(millis());
52+
Serial.print(",A,");
53+
Serial.print("5001.869,");
54+
Serial.print("N,");
55+
Serial.print("01912.114,");
56+
Serial.print("E,");
57+
Serial.print("955535.7,");
58+
Serial.print("116.2,");
59+
Serial.print("290520,");
60+
Serial.print("000.0,");
61+
Serial.print("W");
62+
63+
Serial.unblock();
64+
}

examples/Threadsafe_IO/Threadsafe_Serial_ProtocolWrapping/SharedVariables.h

Whitespace-only changes.

examples/Threadsafe_IO/Threadsafe_Serial_ProtocolWrapping/Threadsafe_Serial_ProtocolWrapping.ino

+8-92
Original file line numberDiff line numberDiff line change
@@ -4,107 +4,23 @@
44

55
#include <Arduino_Threads.h>
66

7-
/**************************************************************************************
8-
* CONSTANTS
9-
**************************************************************************************/
10-
11-
static size_t constexpr NUM_THREADS = 5;
12-
13-
/**************************************************************************************
14-
* FUNCTION DECLARATION
15-
**************************************************************************************/
16-
17-
void serial_thread_func();
18-
19-
/**************************************************************************************
20-
* GLOBAL VARIABLES
21-
**************************************************************************************/
22-
23-
static char thread_name[NUM_THREADS][32];
24-
#undef Serial
25-
#ifdef ARDUINO_PORTENTA_H7_M4
26-
SerialDispatcher Serial(Serial1); /* No SerialUSB for Portenta H7 / M4 Core */
27-
#else
28-
SerialDispatcher Serial(SerialUSB);
29-
#endif
30-
317
/**************************************************************************************
328
* SETUP/LOOP
339
**************************************************************************************/
3410

3511
void setup()
3612
{
37-
/* Fire up some threads all accessing the LSM6DSOX */
38-
for(size_t i = 0; i < NUM_THREADS; i++)
39-
{
40-
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i);
41-
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]);
42-
t->start(serial_thread_func);
43-
}
44-
}
45-
46-
void loop()
47-
{
48-
49-
}
50-
51-
/**************************************************************************************
52-
* FUNCTION DEFINITION
53-
**************************************************************************************/
13+
Serial.begin(9600);
14+
while (!Serial) { }
5415

55-
String nmea_message_prefix(String const & /* msg */)
56-
{
57-
return String("$");
16+
GPS_Thread.start();
5817
}
5918

60-
String nmea_message_suffix(String const & prefix, String const & msg)
19+
void loop()
6120
{
62-
/* NMEA checksum is calculated over the complete message
63-
* starting with '$' and ending with the end of the message.
21+
/* If we don't hand back control then the main thread
22+
* will hog the CPU and all other thread's won't get
23+
* time to be executed.
6424
*/
65-
byte checksum = 0;
66-
std::for_each(msg.c_str(),
67-
msg.c_str() + msg.length(),
68-
[&checksum](char const c)
69-
{
70-
checksum ^= static_cast<uint8_t>(c);
71-
});
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);
84-
85-
for(;;)
86-
{
87-
/* Sleep between 5 and 500 ms */
88-
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
89-
90-
/* Print a fake NMEA GPRMC message:
91-
* $GPRMC,062101.714,A,5001.869,N,01912.114,E,955535.7,116.2,290520,000.0,W*45\r\n
92-
*/
93-
Serial.block();
94-
95-
Serial.print("GPRMC,");
96-
Serial.print(millis());
97-
Serial.print(",A,");
98-
Serial.print("5001.869,");
99-
Serial.print("N,");
100-
Serial.print("01912.114,");
101-
Serial.print("E,");
102-
Serial.print("955535.7,");
103-
Serial.print("116.2,");
104-
Serial.print("290520,");
105-
Serial.print("000.0,");
106-
Serial.print("W");
107-
108-
Serial.unblock();
109-
}
25+
rtos::ThisThread::yield();
11026
}

0 commit comments

Comments
 (0)