Skip to content

Commit 94c6c0f

Browse files
authored
block/unblock allows for safe aggregration of serial messages in a threaded environment. (#10)
1 parent 850046b commit 94c6c0f

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

examples/ts_serial/ts_serial.ino

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ void serial_thread_func()
6363
/* Print thread id and chip id value to serial. */
6464
char msg[64] = {0};
6565
snprintf(msg, sizeof(msg), "[%05lu] %s: Lorem ipsum ...", millis(), rtos::ThisThread::get_name());
66+
/* Every Serial.print/println() encapsulated between
67+
* block/unblock statements will only be printed after
68+
* a block statement has occurred.
69+
*/
70+
Serial.block();
6671
Serial.println(msg);
72+
Serial.unblock();
6773
}
6874
}

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ BusDevice KEYWORD1
2020

2121
transfer KEYWORD2
2222
create KEYWORD2
23+
block KEYWORD2
24+
unblock KEYWORD2
2325

2426
#######################################
2527
# Constants (LITERAL1)

src/serial/SerialDispatcher.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void SerialDispatcher::begin(unsigned long baudrate, uint16_t config)
6868
*/
6969
ThreadCustomerData data;
7070
data.thread_id = current_thread_id;
71+
data.block_tx_buffer = false;
7172
_thread_customer_list.push_back(data);
7273
}
7374
}
@@ -166,6 +167,21 @@ size_t SerialDispatcher::write(const uint8_t * data, size_t len)
166167
return bytes_written;
167168
}
168169

170+
void SerialDispatcher::block()
171+
{
172+
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
173+
auto iter = findThreadCustomerDataById(rtos::ThisThread::get_id());
174+
iter->block_tx_buffer = true;
175+
}
176+
177+
void SerialDispatcher::unblock()
178+
{
179+
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
180+
auto iter = findThreadCustomerDataById(rtos::ThisThread::get_id());
181+
iter->block_tx_buffer = false;
182+
_cond.notify_one();
183+
}
184+
169185
/**************************************************************************************
170186
* PRIVATE MEMBER FUNCTIONS
171187
**************************************************************************************/
@@ -187,9 +203,12 @@ void SerialDispatcher::threadFunc()
187203
std::end (_thread_customer_list),
188204
[this](ThreadCustomerData & d)
189205
{
190-
while(d.tx_buffer.available())
206+
if (!d.block_tx_buffer)
191207
{
192-
_serial.write(d.tx_buffer.read_char());
208+
while(d.tx_buffer.available())
209+
{
210+
_serial.write(d.tx_buffer.read_char());
211+
}
193212
}
194213
});
195214
}

src/serial/SerialDispatcher.h

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class SerialDispatcher : public arduino::HardwareSerial
5353
using Print::write;
5454
virtual operator bool() override { return _serial; }
5555

56+
void block();
57+
void unblock();
5658

5759
private:
5860

@@ -69,6 +71,7 @@ class SerialDispatcher : public arduino::HardwareSerial
6971
{
7072
osThreadId_t thread_id;
7173
arduino::RingBuffer tx_buffer;
74+
bool block_tx_buffer;
7275
} ThreadCustomerData;
7376

7477
std::list<ThreadCustomerData> _thread_customer_list;

0 commit comments

Comments
 (0)