Skip to content

Commit 07d9838

Browse files
committed
Use Arduino_Threads for Threadsafe_Serial_Write.
1 parent 1541cbc commit 07d9838

File tree

5 files changed

+60
-57
lines changed

5 files changed

+60
-57
lines changed

examples/Threadsafe_IO/Threadsafe_Serial_Writer/SharedVariables.h

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

examples/Threadsafe_IO/Threadsafe_Serial_Writer/Threadsafe_Serial_Writer.ino

+18-57
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,32 @@
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-
{
13+
Serial.begin(9600);
14+
while (!Serial) { }
4815

16+
Thread_1.start();
17+
Thread_2.start();
18+
Thread_3.start();
4919
}
5020

51-
/**************************************************************************************
52-
* FUNCTION DEFINITION
53-
**************************************************************************************/
54-
55-
void serial_thread_func()
21+
void loop()
5622
{
57-
Serial.begin(9600);
58-
59-
for(;;)
60-
{
61-
/* Sleep between 5 and 500 ms */
62-
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
63-
/* Print thread id and chip id value to serial. */
64-
char msg[64] = {0};
65-
snprintf(msg, sizeof(msg), "[%05lu] %s: Lorem ipsum ...", millis(), rtos::ThisThread::get_name());
66-
/* Every Serial.print/println() encapsulated between
67-
* block/unblock statements will only be printed after
68-
* a block statement has occurred.
69-
*/
70-
Serial.block();
71-
Serial.println(msg);
72-
Serial.unblock();
73-
}
23+
Serial.block();
24+
Serial.print("[");
25+
Serial.print(millis());
26+
Serial.print("] Thread #0: Lorem ipsum ...");
27+
Serial.println();
28+
Serial.unblock();
29+
30+
/* If we don't hand back control then the main thread
31+
* will hog the CPU and all other thread's won't get
32+
* time to be executed.
33+
*/
34+
rtos::ThisThread::yield();
7435
}

0 commit comments

Comments
 (0)