MODSERIAL with support for more devices

Dependents:   1D-Pong BMT-K9_encoder BMT-K9-Regelaar programma_filter ... more

Check the cookbook page for more information: https://mbed.org/cookbook/MODSERIAL

Did you add a device? Please send a pull request so we can keep everything in one library instead of many copies. In that case also send a PM, since currently mbed does not inform of new pull requests. I will then also add you to the developers of this library so you can do other changes directly.

Revision:
37:31db07ebcb68
Parent:
36:a2ceec937db7
Child:
38:a04506a9a55b
--- a/RESIZE.cpp	Thu Dec 11 05:25:58 2014 +0000
+++ b/RESIZE.cpp	Thu Dec 11 20:24:15 2014 +0000
@@ -55,69 +55,82 @@
 int 
 MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
 {
-    if (size <= buffer_count[type]) {
+    // is new buffer is big enough?
+    if (size <= buffer_count[type])
         return BufferOversize;
-    }
+    
+    // allocate new buffer
+    char * newBuffer = (char*)malloc(size);
     
-    char *s = (char *)malloc(size);
-    
-    if (s == (char *)NULL) {
-        if (memory_check) {
+    // allocation failed?
+    if (newBuffer == (char*)NULL)
+    {
+        if (memory_check)
             error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
-        }
+            
         return NoMemory;
     }
     
-    int c, new_in = 0;
+    // copy old buffer content to new one
+    moveRingBuffer(newBuffer, type);
     
-    do {
-        c = __getc(false);
-        if (c != -1) s[new_in++] = (char)c;
-        if (new_in >= size) new_in = 0;
-    }
-    while (c != -1);
+    // free old buffer and reset ring buffer cursor
+    free((char*)buffer[type]);
+        
+    buffer[type]      = newBuffer;
+    buffer_size[type] = size;
+    buffer_in[type]   = buffer_count[type];
+    buffer_out[type]  = 0;
     
-    free((char *)buffer[type]);
-    buffer[type]      = s;
-    buffer_in[type]   = new_in;
-    buffer_out[type]  = 0;
-    return Ok;        
+    return Ok;
 }
 
 int 
 MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
 {
-    char *s = (char *)malloc(size);
+    // allocate new buffer
+    char * newBuffer = (char*)malloc(size);
     
-    if (s == (char *)NULL) {
-        if (memory_check) {
+    // allocation failed?
+    if (newBuffer == (char*)NULL)
+    {
+        if (memory_check)
             error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
-        }
+            
         return NoMemory;
     }
     
-    if (buffer_count[type] == 0) { // Current buffer empty?
-        free((char *)buffer[type]);
-        buffer[type]      = s;
-        buffer_in[type]   = 0;
-        buffer_out[type]  = 0;
-    }        
-    else { // Copy the current contents into the new buffer.
-        int c, new_in = 0;
-        do {
-            c = __getc(false);
-            if (c != -1) s[new_in++] = (char)c;
-            if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure.
-        }
-        while (c != -1);
-        free((char *)buffer[type]);
-        buffer[type]      = s;
-        buffer_in[type]   = new_in;
-        buffer_out[type]  = 0;
-    }
+    // copy old buffer content to new one
+    moveRingBuffer(newBuffer, type);
     
+    // free old buffer and reset ring buffer cursor
+    free((char*)buffer[type]);
+        
+    buffer[type]      = newBuffer;
     buffer_size[type] = size;
+    buffer_in[type]   = buffer_count[type];
+    buffer_out[type]  = 0;
+    
     return Ok;
 }
 
+void MODSERIAL::moveRingBuffer(char * newBuffer, IrqType type)
+{
+    // copy old buffer content to new one
+    if(buffer_in[type] > buffer_out[type])      
+    {   // content in the middle of the ring buffer
+        memcpy(&newBuffer[0], (char*)buffer[type], buffer_count[type]);
+    }  
+    else if(buffer_in[type] < buffer_out[type]) 
+    {   // content split, free space in the middle
+        // copy last part of the old buffer
+        int end_count= buffer_size[type] - buffer_out[type];
+        memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], end_count);
+        
+        // copy first part of old buffer
+        memcpy(&newBuffer[end_count], (char*)buffer[type], buffer_in[type]);
+    }
+    // else old buffer empty
+}
+
 }; // namespace AjK ends