Skip to content

Commit 70eaaf2

Browse files
committed
Use Arduino_Threads for Threadsafe_Serial_GlobalPrefixSuffix.
1 parent 01c27b0 commit 70eaaf2

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
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-
}

0 commit comments

Comments
 (0)