3
3
**************************************************************************************/
4
4
5
5
#include < Arduino_Threads.h>
6
-
7
- /* *************************************************************************************
8
- * CONSTANTS
9
- **************************************************************************************/
10
-
11
- static size_t constexpr NUM_THREADS = 5 ;
12
-
13
6
/* *************************************************************************************
14
7
* FUNCTION DECLARATION
15
8
**************************************************************************************/
16
9
17
10
String serial_log_message_prefix (String const & /* msg */ );
18
11
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
32
12
33
13
/* *************************************************************************************
34
14
* SETUP/LOOP
35
15
**************************************************************************************/
36
16
37
17
void setup ()
38
18
{
19
+ Serial.begin (9600 );
20
+ while (!Serial) { }
21
+
39
22
Serial.global_prefix (serial_log_message_prefix);
40
23
Serial.global_suffix (serial_log_message_suffix);
41
24
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 ();
49
28
}
50
29
51
30
void loop ()
52
31
{
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 ();
54
41
}
55
42
56
43
/* *************************************************************************************
@@ -68,20 +55,3 @@ String serial_log_message_suffix(String const & prefix, String const & msg)
68
55
{
69
56
return String (" \r\n " );
70
57
}
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