|
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 |
| -} |
45 |
| - |
46 |
| -void loop() |
47 |
| -{ |
| 13 | + Serial.begin(9600); |
| 14 | + while (!Serial) { } |
48 | 15 |
|
| 16 | + Thread_1.start(); |
| 17 | + Thread_2.start(); |
| 18 | + Thread_3.start(); |
49 | 19 | }
|
50 | 20 |
|
51 |
| -/************************************************************************************** |
52 |
| - * FUNCTION DEFINITION |
53 |
| - **************************************************************************************/ |
54 |
| - |
55 |
| -void serial_thread_func() |
| 21 | +void loop() |
56 | 22 | {
|
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(); |
74 | 35 | }
|
0 commit comments