Fork fixing compiler warnings

Dependencies:   Buffer

Fork of BufferedSerial by Sam Grove

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Fri Jan 02 03:47:26 2015 +0000
Parent:
6:8287e83943f0
Child:
8:506247a040bc
Commit message:
made updates to enable the ability to expand the internal buffer. Additionally, used the definition to set the size of the ring buffers in the constructor initializer list. Increased the default buffer size to 512. Additional clean ups (param cks)

Changed in this revision

BufferedSerial.cpp Show annotated file Show diff for this revision Revisions of this file
BufferedSerial.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
 }
 
 
--- a/BufferedSerial.h	Tue Sep 09 20:20:38 2014 +0000
+++ b/BufferedSerial.h	Fri Jan 02 03:47:26 2015 +0000
@@ -27,8 +27,12 @@
 #include "mbed.h"
 #include "Buffer.h"
 
+// Base Class
 #define SERIAL_BASE  RawSerial
 
+// Internal buffer size
+#define BUFFEREDSERIAL_MAX_BUFFER_SIZE   512
+
 /** A serial port (UART) for communication with other serial devices
  *
  * Can be used for Full Duplex communication, or Simplex by specifying
@@ -75,6 +79,7 @@
 private:
     Buffer <char> _rxbuf;
     Buffer <char> _txbuf;
+    char          _buffer[BUFFEREDSERIAL_MAX_BUFFER_SIZE+1];
  
     void rxIrq(void);
     void txIrq(void);
@@ -84,6 +89,7 @@
     /** Create a BufferedSerial port, connected to the specified transmit and receive pins
      *  @param tx Transmit pin
      *  @param rx Receive pin
+     *  @param name optional name
      *  @note Either tx or rx may be specified as NC if unused
      */
     BufferedSerial(PinName tx, PinName rx, const char* name=NULL);