test

Dependencies:   Buffer

Fork of BufferedSerial by Sam Grove

Revision:
7:6fa214b41d73
Parent:
6:8287e83943f0
Child:
8:506247a040bc
--- a/BufferedSerial.cpp	Tue Sep 09 20:20:38 2014 +0000
+++ b/BufferedSerial.cpp	Fri Jan 02 03:47:26 2015 +0000
@@ -24,10 +24,10 @@
 #include <stdarg.h>
 
 BufferedSerial::BufferedSerial(PinName tx, PinName rx, const char* name)
-    : SERIAL_BASE(tx, rx)
+    : SERIAL_BASE(tx, rx), _rxbuf(BUFFEREDSERIAL_MAX_BUFFER_SIZE), _txbuf(BUFFEREDSERIAL_MAX_BUFFER_SIZE)
 {
     SERIAL_BASE::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
-
+    
     return;
 }
 
@@ -64,46 +64,54 @@
 
 int BufferedSerial::puts(const char *s)
 {
-    const char* ptr = s;
-
-    while(*(ptr) != 0) {
-        _txbuf = *(ptr++);
+    if (s != NULL) {
+        const char* ptr = s;
+    
+        while(*(ptr) != 0) {
+            _txbuf = *(ptr++);
+        }
+        _txbuf = '\n';  // done per puts definition
+        BufferedSerial::prime();
+    
+        return (ptr - s) + 1;
     }
-    _txbuf = '\n';  // done per puts definition
-    BufferedSerial::prime();
-
-    return (ptr - s) + 1;
+    return 0;
 }
 
 int BufferedSerial::printf(const char* format, ...)
 {
-    char buf[256] = {0};
+    memset(this->_buffer,0,BUFFEREDSERIAL_MAX_BUFFER_SIZE+1);
     int r = 0;
 
     va_list arg;
     va_start(arg, format);
-    r = vsprintf(buf, format, arg);
+    r = vsprintf(this->_buffer, format, arg);
     // this may not hit the heap but should alert the user anyways
-    if(r > sizeof(buf)) {
-        error("%s %d buffer overwrite!\n", __FILE__, __LINE__);
+    if(r > BUFFEREDSERIAL_MAX_BUFFER_SIZE) {
+        error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,BUFFEREDSERIAL_MAX_BUFFER_SIZE,r);
+        va_end(arg);
+        return 0;
     }
     va_end(arg);
-    r = BufferedSerial::write(buf, r);
+    r = BufferedSerial::write(this->_buffer, r);
 
     return r;
 }
 
 ssize_t BufferedSerial::write(const void *s, size_t length)
 {
-    const char* ptr = (const char*)s;
-    const char* end = ptr + length;
-
-    while (ptr != end) {
-        _txbuf = *(ptr++);
+    if (s != NULL && length > 0) {
+        const char* ptr = (const char*)s;
+        const char* end = ptr + length;
+    
+        while (ptr != end) {
+            _txbuf = *(ptr++);
+        }
+        BufferedSerial::prime();
+    
+        return ptr - (const char*)s;
     }
-    BufferedSerial::prime();
-
-    return ptr - (const char*)s;
+    return 0;
 }