|
| 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 | +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 | +/************************************************************************************** |
| 32 | + * SETUP/LOOP |
| 33 | + **************************************************************************************/ |
| 34 | + |
| 35 | +void setup() |
| 36 | +{ |
| 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 | +{ |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +/************************************************************************************** |
| 52 | + * FUNCTION DEFINITION |
| 53 | + **************************************************************************************/ |
| 54 | + |
| 55 | +void serial_thread_func() |
| 56 | +{ |
| 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 | + |
| 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 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments