|
4 | 4 |
|
5 | 5 | #include <Arduino_Threads.h>
|
6 | 6 |
|
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 |
| - |
31 | 7 | /**************************************************************************************
|
32 | 8 | * SETUP/LOOP
|
33 | 9 | **************************************************************************************/
|
34 | 10 |
|
35 | 11 | void setup()
|
36 | 12 | {
|
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) { } |
45 | 15 |
|
46 |
| -void loop() |
47 |
| -{ |
| 16 | + Thread_1.start(); |
| 17 | + Thread_2.start(); |
| 18 | + Thread_3.start(); |
48 | 19 |
|
| 20 | + Serial.block(); |
| 21 | + Serial.println("Thread #0 started."); |
| 22 | + Serial.unblock(); |
49 | 23 | }
|
50 | 24 |
|
51 |
| -/************************************************************************************** |
52 |
| - * FUNCTION DEFINITION |
53 |
| - **************************************************************************************/ |
54 |
| - |
55 |
| -void serial_thread_func() |
| 25 | +void loop() |
56 | 26 | {
|
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(); |
58 | 31 |
|
59 |
| - for(;;) |
| 32 | + /* Print thread id and chip id value to serial. */ |
| 33 | + if (serial_msg.length()) |
60 | 34 | {
|
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(); |
78 | 42 | }
|
| 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(); |
79 | 49 | }
|
0 commit comments