Skip to content

Commit 01c27b0

Browse files
committed
Use Arduino_Threads for Threadsafe_Serial_Reader.
1 parent 07d9838 commit 01c27b0

File tree

5 files changed

+112
-58
lines changed

5 files changed

+112
-58
lines changed

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

examples/Threadsafe_IO/Threadsafe_Serial_Reader/Threadsafe_Serial_Reader.ino

+28-58
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,46 @@
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-
}
13+
Serial.begin(9600);
14+
while (!Serial) { }
4515

46-
void loop()
47-
{
16+
Thread_1.start();
17+
Thread_2.start();
18+
Thread_3.start();
4819

20+
Serial.block();
21+
Serial.println("Thread #0 started.");
22+
Serial.unblock();
4923
}
5024

51-
/**************************************************************************************
52-
* FUNCTION DEFINITION
53-
**************************************************************************************/
54-
55-
void serial_thread_func()
25+
void loop()
5626
{
57-
Serial.begin(9600);
27+
/* Read data from the serial interface into a String. */
28+
String serial_msg;
29+
while (Serial.available())
30+
serial_msg += (char)Serial.read();
5831

59-
for(;;)
32+
/* Print thread id and chip id value to serial. */
33+
if (serial_msg.length())
6034
{
61-
/* Sleep between 5 and 500 ms */
62-
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
63-
64-
/* Read data from the serial interface into a String. */
65-
String serial_msg;
66-
while (Serial.available())
67-
serial_msg += (char)Serial.read();
68-
69-
/* Print thread id and chip id value to serial. */
70-
if (serial_msg.length())
71-
{
72-
char msg[64] = {0};
73-
snprintf(msg, sizeof(msg), "[%05lu] %s: %s ...", millis(), rtos::ThisThread::get_name(), serial_msg.c_str());
74-
Serial.block();
75-
Serial.println(msg);
76-
Serial.unblock();
77-
}
35+
Serial.block();
36+
Serial.print("[");
37+
Serial.print(millis());
38+
Serial.print("] Thread #0: ");
39+
Serial.print(serial_msg);
40+
Serial.println();
41+
Serial.unblock();
7842
}
43+
44+
/* If we don't hand back control then the main thread
45+
* will hog the CPU and all other thread's won't get
46+
* time to be executed.
47+
*/
48+
rtos::ThisThread::yield();
7949
}

0 commit comments

Comments
 (0)