Skip to content

Commit e06adb3

Browse files
authored
Merge pull request #39 from bcmi-labs/threads-for-examples
Use Arduino_Threads concept for `Serial` examples
2 parents 1be8f61 + b6ce537 commit e06adb3

20 files changed

+315
-252
lines changed

examples/Threadsafe_IO/Threadsafe_Serial_GlobalPrefixSuffix/SharedVariables.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
}
5+
6+
void loop()
7+
{
8+
Serial.block();
9+
Serial.println("Thread #1: Lorem ipsum ...");
10+
Serial.unblock();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
}
5+
6+
void loop()
7+
{
8+
Serial.block();
9+
Serial.println("Thread #2: Lorem ipsum ...");
10+
Serial.unblock();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
}
5+
6+
void loop()
7+
{
8+
Serial.block();
9+
Serial.println("Thread #3: Lorem ipsum ...");
10+
Serial.unblock();
11+
}

examples/Threadsafe_IO/Threadsafe_Serial_GlobalPrefixSuffix/Threadsafe_Serial_GlobalPrefixSuffix.ino

+15-45
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,41 @@
33
**************************************************************************************/
44

55
#include <Arduino_Threads.h>
6-
7-
/**************************************************************************************
8-
* CONSTANTS
9-
**************************************************************************************/
10-
11-
static size_t constexpr NUM_THREADS = 5;
12-
136
/**************************************************************************************
147
* FUNCTION DECLARATION
158
**************************************************************************************/
169

1710
String serial_log_message_prefix(String const & /* msg */);
1811
String serial_log_message_suffix(String const & prefix, String const & msg);
19-
void serial_thread_func();
20-
21-
/**************************************************************************************
22-
* GLOBAL VARIABLES
23-
**************************************************************************************/
24-
25-
static char thread_name[NUM_THREADS][32];
26-
#undef Serial
27-
#ifdef ARDUINO_PORTENTA_H7_M4
28-
SerialDispatcher Serial(Serial1); /* No SerialUSB for Portenta H7 / M4 Core */
29-
#else
30-
SerialDispatcher Serial(SerialUSB);
31-
#endif
3212

3313
/**************************************************************************************
3414
* SETUP/LOOP
3515
**************************************************************************************/
3616

3717
void setup()
3818
{
19+
Serial.begin(9600);
20+
while (!Serial) { }
21+
3922
Serial.global_prefix(serial_log_message_prefix);
4023
Serial.global_suffix(serial_log_message_suffix);
4124

42-
/* Fire up some threads all accessing the LSM6DSOX */
43-
for(size_t i = 0; i < NUM_THREADS; i++)
44-
{
45-
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i);
46-
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]);
47-
t->start(serial_thread_func);
48-
}
25+
Thread_1.start();
26+
Thread_2.start();
27+
Thread_3.start();
4928
}
5029

5130
void loop()
5231
{
53-
32+
Serial.block();
33+
Serial.println("Thread #0: Lorem ipsum ...");
34+
Serial.unblock();
35+
36+
/* If we don't hand back control then the main thread
37+
* will hog the CPU and all other thread's won't get
38+
* time to be executed.
39+
*/
40+
rtos::ThisThread::yield();
5441
}
5542

5643
/**************************************************************************************
@@ -68,20 +55,3 @@ String serial_log_message_suffix(String const & prefix, String const & msg)
6855
{
6956
return String("\r\n");
7057
}
71-
72-
void serial_thread_func()
73-
{
74-
Serial.begin(9600);
75-
76-
for(;;)
77-
{
78-
/* Sleep between 5 and 500 ms */
79-
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
80-
/* Print a unbroken log message including thread name and timestamp as a prefix. */
81-
Serial.block();
82-
Serial.print(rtos::ThisThread::get_name());
83-
Serial.print(": ");
84-
Serial.print("Lorem ipsum ...");
85-
Serial.unblock();
86-
}
87-
}
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
}

examples/Threadsafe_IO/Threadsafe_Serial_Reader/SharedVariables.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
5+
Serial.block();
6+
Serial.println("Thread #1 started.");
7+
Serial.unblock();
8+
}
9+
10+
void loop()
11+
{
12+
/* Read data from the serial interface into a String. */
13+
String serial_msg;
14+
while (Serial.available())
15+
serial_msg += (char)Serial.read();
16+
17+
/* Print thread id and chip id value to serial. */
18+
if (serial_msg.length())
19+
{
20+
Serial.block();
21+
Serial.print("[");
22+
Serial.print(millis());
23+
Serial.print("] Thread #1: ");
24+
Serial.print(serial_msg);
25+
Serial.println();
26+
Serial.unblock();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
5+
Serial.block();
6+
Serial.println("Thread #2 started.");
7+
Serial.unblock();
8+
}
9+
10+
void loop()
11+
{
12+
/* Read data from the serial interface into a String. */
13+
String serial_msg;
14+
while (Serial.available())
15+
serial_msg += (char)Serial.read();
16+
17+
/* Print thread id and chip id value to serial. */
18+
if (serial_msg.length())
19+
{
20+
Serial.block();
21+
Serial.print("[");
22+
Serial.print(millis());
23+
Serial.print("] Thread #2: ");
24+
Serial.print(serial_msg);
25+
Serial.println();
26+
Serial.unblock();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void setup()
2+
{
3+
Serial.begin(9600);
4+
5+
Serial.block();
6+
Serial.println("Thread #3 started.");
7+
Serial.unblock();
8+
}
9+
10+
void loop()
11+
{
12+
/* Read data from the serial interface into a String. */
13+
String serial_msg;
14+
while (Serial.available())
15+
serial_msg += (char)Serial.read();
16+
17+
/* Print thread id and chip id value to serial. */
18+
if (serial_msg.length())
19+
{
20+
Serial.block();
21+
Serial.print("[");
22+
Serial.print(millis());
23+
Serial.print("] Thread #3: ");
24+
Serial.print(serial_msg);
25+
Serial.println();
26+
Serial.unblock();
27+
}
28+
}

0 commit comments

Comments
 (0)