Skip to content

Commit

Permalink
Merge pull request #27 from bcmi-labs/global-local-callbacks
Browse files Browse the repository at this point in the history
Global local callbacks
  • Loading branch information
aentinger committed Oct 1, 2021
2 parents fd2bfa1 + 31840d7 commit 28522d6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
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();
}
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ block KEYWORD2
unblock KEYWORD2
prefix KEYWORD2
suffix KEYWORD2
global_prefix KEYWORD2
global_suffix KEYWORD2
spi KEYWORD2
wire KEYWORD2
read KEYWORD2
Expand Down
26 changes: 26 additions & 0 deletions src/serial/SerialDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ SerialDispatcher::SerialDispatcher(arduino::HardwareSerial & serial)
, _thread(osPriorityRealtime, 4096, nullptr, "SerialDispatcher")
, _has_tread_started{false}
, _terminate_thread{false}
, _global_prefix_callback{nullptr}
, _global_suffix_callback{nullptr}
{

}
Expand Down Expand Up @@ -202,6 +204,18 @@ void SerialDispatcher::suffix(SuffixInjectorCallbackFunc func)
iter->suffix_func = func;
}

void SerialDispatcher::global_prefix(PrefixInjectorCallbackFunc func)
{
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
_global_prefix_callback = func;
}

void SerialDispatcher::global_suffix(SuffixInjectorCallbackFunc func)
{
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
_global_suffix_callback = func;
}

/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
Expand Down Expand Up @@ -254,6 +268,12 @@ void SerialDispatcher::threadFunc()
String prefix;
if (d.prefix_func)
prefix = d.prefix_func(msg);
/* A prefix callback function defined per thread
* takes precedence over a globally defined prefix
* callback function.
*/
else if (_global_prefix_callback)
prefix = _global_prefix_callback(msg);

/* Similar to the prefix function this callback
* allows the user to specify a specific message
Expand All @@ -262,6 +282,12 @@ void SerialDispatcher::threadFunc()
String suffix;
if (d.suffix_func)
suffix = d.suffix_func(prefix, msg);
/* A suffix callback function defined per thread
* takes precedence over a globally defined suffix
* callback function.
*/
else if (_global_suffix_callback)
suffix = _global_suffix_callback(prefix, msg);

/* Now it's time to actually write the message
* conveyed by the user via Serial.print/println.
Expand Down
5 changes: 5 additions & 0 deletions src/serial/SerialDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class SerialDispatcher : public arduino::HardwareSerial
typedef std::function<String(String const &, String const &)> SuffixInjectorCallbackFunc;
void prefix(PrefixInjectorCallbackFunc func);
void suffix(SuffixInjectorCallbackFunc func);
void global_prefix(PrefixInjectorCallbackFunc func);
void global_suffix(SuffixInjectorCallbackFunc func);


private:
Expand All @@ -76,6 +78,9 @@ class SerialDispatcher : public arduino::HardwareSerial
bool _has_tread_started;
bool _terminate_thread;

PrefixInjectorCallbackFunc _global_prefix_callback;
SuffixInjectorCallbackFunc _global_suffix_callback;

static int constexpr THREADSAFE_SERIAL_TRANSMIT_RINGBUFFER_SIZE = 128;
typedef arduino::RingBufferN<THREADSAFE_SERIAL_TRANSMIT_RINGBUFFER_SIZE> SerialTransmitRingbuffer;

Expand Down

0 comments on commit 28522d6

Please sign in to comment.