|
| 1 | +/************************************************************************************** |
| 2 | + * INCLUDE |
| 3 | + **************************************************************************************/ |
| 4 | + |
| 5 | +#include <Arduino_ThreadsafeIO.h> |
| 6 | + |
| 7 | +/************************************************************************************** |
| 8 | + * CONSTANTS |
| 9 | + **************************************************************************************/ |
| 10 | + |
| 11 | +static size_t constexpr NUM_THREADS = 5; |
| 12 | + |
| 13 | +/************************************************************************************** |
| 14 | + * FUNCTION DECLARATION |
| 15 | + **************************************************************************************/ |
| 16 | + |
| 17 | +String serial_log_message_prefix(String const & /* msg */); |
| 18 | +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 | + |
| 33 | +/************************************************************************************** |
| 34 | + * SETUP/LOOP |
| 35 | + **************************************************************************************/ |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.global_prefix(serial_log_message_prefix); |
| 40 | + Serial.global_suffix(serial_log_message_suffix); |
| 41 | + |
| 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 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void loop() |
| 52 | +{ |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +/************************************************************************************** |
| 57 | + * FUNCTION DEFINITION |
| 58 | + **************************************************************************************/ |
| 59 | + |
| 60 | +String serial_log_message_prefix(String const & /* msg */) |
| 61 | +{ |
| 62 | + char msg[32] = {0}; |
| 63 | + snprintf(msg, sizeof(msg), "[%05lu] ", millis()); |
| 64 | + return String(msg); |
| 65 | +} |
| 66 | + |
| 67 | +String serial_log_message_suffix(String const & prefix, String const & msg) |
| 68 | +{ |
| 69 | + return String("\r\n"); |
| 70 | +} |
| 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