Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: BufferSerial.cpp
- Revision:
- 1:476b874003d8
- Parent:
- 0:07be23bfcae4
- Child:
- 2:304d0e762287
--- a/BufferSerial.cpp Tue Nov 10 08:56:55 2020 +0000
+++ b/BufferSerial.cpp Mon Nov 16 08:33:33 2020 +0000
@@ -1,26 +1,66 @@
#include "BufferSerial.h"
-BufferSerial::BufferSerial(PinName tx, PinName rx) :
- serial(tx,rx)
+BufferSerial::BufferSerial(PinName tx, PinName rx,int baud,int timeout)
+ :RawSerial(tx,rx,baud)
{
-
// initialize functions
- _rx_buffer_head(0);
- _rx_buhher_tail(0);
+ _rx_buffer_head = 0;
+ _rx_buffer_tail = 0;
+ // time out
+ timer_ms = timeout;
+ timer.start();
// attach
- serial.attach(this,&BufferSerial::serial_rx, Serial::RxIrq);
+ RawSerial::attach(callback(this,&BufferSerial::serial_rx), Serial::RxIrq);
+}
+
+int BufferSerial::available(void)
+{
+ return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
+}
+
+int BufferSerial::read(void)
+{
+ if (_rx_buffer_head == _rx_buffer_tail) {
+ return -1;
+ } else {
+ unsigned char c = _rx_buffer[_rx_buffer_tail];
+ _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
+ return c;
+ }
}
-int BufferSerial::available(void){
-
+size_t BufferSerial::readBytesUntil(char charactor,char *buffer,int length)
+{
+ size_t index = 0;
+ while (index < length) {
+ int c = timedRead();
+ if (c < 0 || c == charactor) break;
+ *buffer++ = (char)c;
+ index++;
+ }
+ return index;
}
-unsigned char BufferSirial::read(void){
-
+void BufferSerial::serial_rx(void)
+{
+ while(RawSerial::readable()) {
+ unsigned char c = RawSerial::getc();
+ rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
+ if (i != _rx_buffer_tail) {
+ _rx_buffer[_rx_buffer_head] = c;
+ _rx_buffer_head = i;
+ }
+ }
}
-void BufferSerial::erial_rx(){
- while(serial.readable()){
-
- }
+int BufferSerial::timedRead()
+{
+ int c;
+ timer.reset();
+ _startMillis = timer.read_ms();
+ do {
+ c = read();
+ if (c >= 0) return c;
+ } while(timer.read_ms() - _startMillis < _timeout);
+ return -1; // -1 indicates timeout
}
\ No newline at end of file