BufferedSerial.cpp
Go to the documentation of this file.
1 
23 #include "BufferedSerial.h"
24 #include <stdarg.h>
25 
26 BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name)
27  : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size))
28 {
29  RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
30  this->_buf_size = buf_size;
31  this->_tx_multiple = tx_multiple;
32  return;
33 }
34 
36 {
37  RawSerial::attach(NULL, RawSerial::RxIrq);
38  RawSerial::attach(NULL, RawSerial::TxIrq);
39 
40  return;
41 }
42 
44 {
45  return _rxbuf.available(); // note: look if things are in the buffer
46 }
47 
49 {
50  return 1; // buffer allows overwriting by design, always true
51 }
52 
54 {
55  return _rxbuf;
56 }
57 
59 {
60  _txbuf = (char)c;
62 
63  return c;
64 }
65 
66 int BufferedSerial::puts(const char *s)
67 {
68  if (s != NULL) {
69  const char* ptr = s;
70 
71  while(*(ptr) != 0) {
72  _txbuf = *(ptr++);
73  }
74  _txbuf = '\n'; // done per puts definition
76 
77  return (ptr - s) + 1;
78  }
79  return 0;
80 }
81 
82 int BufferedSerial::printf(const char* format, ...)
83 {
84  char buffer[this->_buf_size];
85  memset(buffer,0,this->_buf_size);
86  int r = 0;
87 
88  va_list arg;
89  va_start(arg, format);
90  r = vsprintf(buffer, format, arg);
91  // this may not hit the heap but should alert the user anyways
92  if(r > this->_buf_size) {
93  error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r);
94  va_end(arg);
95  return 0;
96  }
97  va_end(arg);
98  r = BufferedSerial::write(buffer, r);
99 
100  return r;
101 }
102 
103 ssize_t BufferedSerial::write(const void *s, size_t length)
104 {
105  if (s != NULL && length > 0) {
106  const char* ptr = (const char*)s;
107  const char* end = ptr + length;
108 
109  while (ptr != end) {
110  _txbuf = *(ptr++);
111  }
113 
114  return ptr - (const char*)s;
115  }
116  return 0;
117 }
118 
119 
121 {
122  // read from the peripheral and make sure something is available
123  if(serial_readable(&_serial)) {
124  _rxbuf = serial_getc(&_serial); // if so load them into a buffer
125  }
126 
127  return;
128 }
129 
131 {
132  // see if there is room in the hardware fifo and if something is in the software fifo
133  while(serial_writable(&_serial)) {
134  if(_txbuf.available()) {
135  serial_putc(&_serial, (int)_txbuf.get());
136  } else {
137  // disable the TX interrupt when there is nothing left to send
138  RawSerial::attach(NULL, RawSerial::TxIrq);
139  break;
140  }
141  }
142 
143  return;
144 }
145 
147 {
148  // if already busy then the irq will pick this up
149  if(serial_writable(&_serial)) {
150  RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq
151  BufferedSerial::txIrq(); // only write to hardware in one place
152  RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
153  }
154 
155  return;
156 }
157 
158 
virtual ssize_t write(const void *s, std::size_t length)
BufferedSerial(PinName tx, PinName rx, uint32_t buf_size=256, uint32_t tx_multiple=4, const char *name=NULL)
virtual int readable(void)
virtual int puts(const char *s)
virtual int getc(void)
virtual int putc(int c)
virtual int printf(const char *format,...)
uint32_t available(void)
Definition: Buffer.h:157
Buffer< char > _txbuf
unsigned char buffer[1]
virtual ~BufferedSerial(void)
T get(void)
Definition: Buffer.h:140
virtual int writeable(void)
uint32_t _tx_multiple
Buffer< char > _rxbuf
Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX. ...
uint32_t _buf_size


rosserial_mbed
Author(s): Gary Servin
autogenerated on Mon Jun 10 2019 14:53:26