-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from bcmi-labs/global-local-callbacks
Global local callbacks
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
examples/Threadsafe_Serial_GlobalPrefixSuffix/Threadsafe_Serial_GlobalPrefixSuffix.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <Arduino_ThreadsafeIO.h> | ||
|
||
/************************************************************************************** | ||
* CONSTANTS | ||
**************************************************************************************/ | ||
|
||
static size_t constexpr NUM_THREADS = 5; | ||
|
||
/************************************************************************************** | ||
* FUNCTION DECLARATION | ||
**************************************************************************************/ | ||
|
||
String serial_log_message_prefix(String const & /* msg */); | ||
String serial_log_message_suffix(String const & prefix, String const & msg); | ||
void serial_thread_func(); | ||
|
||
/************************************************************************************** | ||
* GLOBAL VARIABLES | ||
**************************************************************************************/ | ||
|
||
static char thread_name[NUM_THREADS][32]; | ||
#undef Serial | ||
#ifdef ARDUINO_PORTENTA_H7_M4 | ||
SerialDispatcher Serial(Serial1); /* No SerialUSB for Portenta H7 / M4 Core */ | ||
#else | ||
SerialDispatcher Serial(SerialUSB); | ||
#endif | ||
|
||
/************************************************************************************** | ||
* SETUP/LOOP | ||
**************************************************************************************/ | ||
|
||
void setup() | ||
{ | ||
Serial.global_prefix(serial_log_message_prefix); | ||
Serial.global_suffix(serial_log_message_suffix); | ||
|
||
/* Fire up some threads all accessing the LSM6DSOX */ | ||
for(size_t i = 0; i < NUM_THREADS; i++) | ||
{ | ||
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i); | ||
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]); | ||
t->start(serial_thread_func); | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
} | ||
|
||
/************************************************************************************** | ||
* FUNCTION DEFINITION | ||
**************************************************************************************/ | ||
|
||
String serial_log_message_prefix(String const & /* msg */) | ||
{ | ||
char msg[32] = {0}; | ||
snprintf(msg, sizeof(msg), "[%05lu] ", millis()); | ||
return String(msg); | ||
} | ||
|
||
String serial_log_message_suffix(String const & prefix, String const & msg) | ||
{ | ||
return String("\r\n"); | ||
} | ||
|
||
void serial_thread_func() | ||
{ | ||
Serial.begin(9600); | ||
|
||
for(;;) | ||
{ | ||
/* Sleep between 5 and 500 ms */ | ||
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500))); | ||
/* Print a unbroken log message including thread name and timestamp as a prefix. */ | ||
Serial.block(); | ||
Serial.print(rtos::ThisThread::get_name()); | ||
Serial.print(": "); | ||
Serial.print("Lorem ipsum ..."); | ||
Serial.unblock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters