Benjamin Hepp / BufferedSerial

Dependencies:   Buffer

Fork of BufferedSerial by Sam Grove

Revision:
13:b4080afc8cd5
Parent:
12:c7947d444267
Child:
14:2150f1edc9bc
diff -r c7947d444267 -r b4080afc8cd5 BufferedSerial.cpp
--- a/BufferedSerial.cpp	Sat Feb 13 14:14:50 2016 +0000
+++ b/BufferedSerial.cpp	Sat Feb 13 17:10:39 2016 +0000
@@ -53,7 +53,7 @@
 
 int BufferedSerial::writeable(void)
 {
-    _buffered_bytes < this->_buf_size;
+    return _buffered_bytes < this->_buf_size;
 }
 
 int BufferedSerial::getc(void)
@@ -112,6 +112,23 @@
     return r;
 }
 
+int BufferedSerial::printf(const char* format, va_list args)
+{
+    char buffer[this->_buf_size];
+    memset(buffer, 0, this->_buf_size);
+    int r = 0;
+
+    r = vsnprintf(buffer, this->_buf_size, format, args);
+    // this may not hit the heap but should alert the user anyways
+    if(r > this->_buf_size) {
+        error("%s %d buffer to small (buf_size: %d, required: %d)!\r\n", __FILE__, __LINE__, this->_buf_size, r);
+        return 0;
+    }
+    r = BufferedSerial::write(buffer, r);
+
+    return r;
+}
+
 ssize_t BufferedSerial::write(const void *s, size_t length)
 {
     if (s != NULL && length > 0) {
@@ -143,11 +160,9 @@
 void BufferedSerial::txIrq(void)
 {
     // see if there is room in the hardware fifo and if something is in the software fifo
-    while (serial_writable(&_serial)) {
-        if (_txbuf.available()) {
-            serial_putc(&_serial, (int)_txbuf.get());
-            --_buffered_bytes;
-        }
+    while (serial_writable(&_serial) && _txbuf.available()) {
+        serial_putc(&_serial, (int)_txbuf.get());
+        --_buffered_bytes;
     }
 }