Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Revision:
7:709130701ac7
Parent:
4:43abdd8eda40
Child:
9:5000feb4b46f
--- a/mx_cmd_buffer.h	Mon Aug 31 10:29:58 2015 +1000
+++ b/mx_cmd_buffer.h	Thu Sep 10 20:09:01 2015 +1000
@@ -70,8 +70,8 @@
 template<uint32_t BufferSize, uint8_t Commands = 16, typename CounterType = uint32_t>
 class MxCmdBuffer : public MxBuffer {
 public:
-    MxCmdBuffer() : _head(0), _tail(0), _headStartOfNxtCmd(0), _full(false),
-            _errBufFull(0), _dontSaveCurrentCommand(0), flags(0)
+    MxCmdBuffer() : _head(0), _tail(0), _full(false),
+            _errBufFull(0), _dontSaveCurrentCommand(0), _lastCharWasEOF(0), flags(0)
     {
         //flags.Val = 0;
     }
@@ -106,8 +106,14 @@
 
             //Remove all character added for this command. Restore head to last "End of Command" pointer
             if (cmdEndsBuf.isEmpty() == false) {
-                //Restore head to byte following loast "End of Command" pointer
-                _head = ((cmdEndsBuf.peekLastAdded()+1)%BufferSize);
+                //Restore head to byte following last "End of Command" pointer
+                CounterType oldHead;
+                oldHead = ((cmdEndsBuf.peekLastAdded()+1) % BufferSize);
+                //Ensure buffer not still full
+                if (oldHead != _head) {
+                    _head = oldHead;
+                    _full = false;
+                }
             }
             //If no commands in buffer, set head=tail
             else {
@@ -122,27 +128,27 @@
                 c = ';';    //Change to "end of command" character
             }
 
-            if (_dontSaveCurrentCommand == true) {
-                _dontSaveCurrentCommand = false;
-                _headStartOfNxtCmd = _head;
+            //If last character was also an "End of Command" character, ignore this one
+            if (_lastCharWasEOF == true) {
+                MXH_DEBUG_INFO("\r\nMultiple EOC");
                 return false;   //Nothing added
             }
-            else {
-                //If multiple "end of command" characters received, ignore them.
-                if (_headStartOfNxtCmd == _head) {
-                    //Nothing to do, exit
-                    MXH_DEBUG_INFO("\r\nMultiple EOC");
-                    return false;   //Nothing added
-                }
+
+            _lastCharWasEOF = true; //Remember this was an "End of Command" character
 
-                cmdEndsBuf.put(_head);  //Add pointer to "end of command" character
-                //End of command character will be added to buffer below at current _head pointer
-                MXH_DEBUG_INFO("\r\nAdded Cmd, EOC=%d", _head);
+            //Current command is now finished, so reset _dontSaveCurrentCommand
+            if (_dontSaveCurrentCommand == true) {
+                _dontSaveCurrentCommand = false;
+                return false;   //Nothing added
+            }
 
-                //Save start of next command = byte following current _head
-                _headStartOfNxtCmd = ((_head+1)%BufferSize);
-                MXH_DEBUG_INFO(" Nxt=%d", _headStartOfNxtCmd);
-            }
+            //Add pointer to "end of command" character
+            cmdEndsBuf.put(_head);
+            //End of command character will be added to buffer below at current _head pointer
+            MXH_DEBUG_INFO("\r\nAdded Cmd, EOC=%d", _head);
+        }
+        else {
+            _lastCharWasEOF = false;
         }
 
         if (_dontSaveCurrentCommand) {
@@ -534,13 +540,20 @@
      * @return Number of available bytes in buffer
      */
     CounterType getAvailable() {
-        CounterType avail;
-        if (isEmpty()) {
+        if (_head != _tail) {
+            CounterType avail;
+            avail = _head - _tail;
+            avail %= BufferSize;
+            return avail;
+        }
+
+        //Head=Tail. Can be full or empty
+        if (_full==false) {
             return 0;
         }
-        avail = _head - _tail;
-        avail %= BufferSize;
-        return avail;
+        else {
+            return BufferSize;
+        }
     }
 
 
@@ -587,7 +600,6 @@
      */
     void reset() {
         _head = 0;
-        _headStartOfNxtCmd = 0;
         _tail = 0;
         _full = false;
         _errBufFull = false;
@@ -612,11 +624,10 @@
     uint8_t _pool[BufferSize];
     volatile CounterType _head;
     volatile CounterType _tail;
-    //_head position of first character of next command = position of first byte after last "end of command" character was added
-    volatile CounterType _headStartOfNxtCmd;
     volatile bool _full;
     volatile bool _errBufFull;
     volatile bool _dontSaveCurrentCommand;
+    volatile bool _lastCharWasEOF;
 
     union Flags {
         struct {