Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others
mx_cmd_buffer.h@13:328bfac0e686, 2015-10-21 (annotated)
- Committer:
- modtronix-com
- Date:
- Wed Oct 21 14:53:20 2015 +1100
- Revision:
- 13:328bfac0e686
- Parent:
- 9:5000feb4b46f
- Child:
- 17:86034c970ea0
Many improvements, and added dependency for modtronix_config.h file.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
modtronix | 4:43abdd8eda40 | 1 | /** |
modtronix | 4:43abdd8eda40 | 2 | * File: mx_ascii_cmd_buffer.h |
modtronix | 4:43abdd8eda40 | 3 | * |
modtronix | 4:43abdd8eda40 | 4 | * Author: Modtronix Engineering - www.modtronix.com |
modtronix | 4:43abdd8eda40 | 5 | * |
modtronix | 4:43abdd8eda40 | 6 | * Description: |
modtronix | 4:43abdd8eda40 | 7 | * |
modtronix | 4:43abdd8eda40 | 8 | * Software License Agreement: |
modtronix | 4:43abdd8eda40 | 9 | * This software has been written or modified by Modtronix Engineering. The code |
modtronix | 4:43abdd8eda40 | 10 | * may be modified and can be used free of charge for commercial and non commercial |
modtronix | 4:43abdd8eda40 | 11 | * applications. If this is modified software, any license conditions from original |
modtronix | 4:43abdd8eda40 | 12 | * software also apply. Any redistribution must include reference to 'Modtronix |
modtronix | 4:43abdd8eda40 | 13 | * Engineering' and web link(www.modtronix.com) in the file header. |
modtronix | 4:43abdd8eda40 | 14 | * |
modtronix | 4:43abdd8eda40 | 15 | * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, |
modtronix | 4:43abdd8eda40 | 16 | * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
modtronix | 4:43abdd8eda40 | 17 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE |
modtronix | 4:43abdd8eda40 | 18 | * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
modtronix | 4:43abdd8eda40 | 19 | * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
modtronix | 4:43abdd8eda40 | 20 | */ |
modtronix | 4:43abdd8eda40 | 21 | #ifndef SRC_MX_CMD_BUFFER_H_ |
modtronix | 4:43abdd8eda40 | 22 | #define SRC_MX_CMD_BUFFER_H_ |
modtronix | 4:43abdd8eda40 | 23 | |
modtronix | 4:43abdd8eda40 | 24 | // This file contains an ASCII command buffer. It is used to store ASCII commands. |
modtronix-com |
13:328bfac0e686 | 25 | // |
modtronix | 4:43abdd8eda40 | 26 | // Each command ends with a ';' character. CR(0x0a='\r') and LF(0x0d='\n') are converted to ';' characters. |
modtronix-com |
13:328bfac0e686 | 27 | // |
modtronix | 4:43abdd8eda40 | 28 | // A ASCII command is an ASCII Formatted String, with Escape Sequences (Control Characters). |
modtronix-com |
13:328bfac0e686 | 29 | // It uses 2 upper case characters to represent a single hex character, and can have embedded strings (enclosed |
modtronix-com |
13:328bfac0e686 | 30 | // within single quotation marks = ') and "Control Characters" (Lower case characters 'a' to 'z') |
modtronix-com |
13:328bfac0e686 | 31 | // |
modtronix-com |
13:328bfac0e686 | 32 | // It has the following format: |
modtronix | 4:43abdd8eda40 | 33 | // HH = Two upper case characters representing a single byte (hex value). |
modtronix-com |
13:328bfac0e686 | 34 | // XHH = **Not Implemented Yet** Same as HH |
modtronix-com |
13:328bfac0e686 | 35 | // NDD = **Not Implemented Yet** Decimal number. For example "N100" = decimal 100 t0='500' |
modtronix-com |
13:328bfac0e686 | 36 | // c = **Not Implemented Yet** Lower case character 'a' to 'z' represents a "control character". |
modtronix-com |
13:328bfac0e686 | 37 | // ' = Enclose string with single quotes. A double ' character will not end the string, but represents a |
modtronix-com |
13:328bfac0e686 | 38 | // single ' character. |
modtronix | 4:43abdd8eda40 | 39 | // ^^ = Two "escape characters" represents a single '^' character. |
modtronix | 4:43abdd8eda40 | 40 | // ^x = Use this format to represent a "control character" that is not lower. Lower case characters do not have |
modtronix | 4:43abdd8eda40 | 41 | // to be escaped. Currently not used, but reserved for future use! |
modtronix-com |
13:328bfac0e686 | 42 | // |
modtronix-com |
13:328bfac0e686 | 43 | // |
modtronix-com |
13:328bfac0e686 | 44 | // ===== Examples when NOT using "Escape Character" ===== |
modtronix-com |
13:328bfac0e686 | 45 | // 0156A9BC; is decoded as: |
modtronix-com |
13:328bfac0e686 | 46 | // 0x01, 0x56, 0xA9, 0xBC |
modtronix-com |
13:328bfac0e686 | 47 | // |
modtronix-com |
13:328bfac0e686 | 48 | // BC56'Hello'; is decoded as: |
modtronix-com |
13:328bfac0e686 | 49 | // 0xBC, 0x56, H, e, l, l, o |
modtronix-com |
13:328bfac0e686 | 50 | // |
modtronix-com |
13:328bfac0e686 | 51 | // A1'Hi^'; is decoded as: |
modtronix-com |
13:328bfac0e686 | 52 | // 0xA1, H, i, ^ |
modtronix | 4:43abdd8eda40 | 53 | // |
modtronix-com |
13:328bfac0e686 | 54 | // s6A52p; is decoded as("lower case" control characters NOT supported without "Escape Character"): |
modtronix-com |
13:328bfac0e686 | 55 | // 0x6A, 0x52 |
modtronix-com |
13:328bfac0e686 | 56 | // |
modtronix-com |
13:328bfac0e686 | 57 | // |
modtronix-com |
13:328bfac0e686 | 58 | // ===== Examples when using "Escape Character" '^' ===== |
modtronix-com |
13:328bfac0e686 | 59 | // s6A52p; is decoded as: |
modtronix-com |
13:328bfac0e686 | 60 | // ^, s, 0x6A, 0x52, ^, p |
modtronix-com |
13:328bfac0e686 | 61 | // |
modtronix-com |
13:328bfac0e686 | 62 | // s50'Hi'p; is decoded as: |
modtronix-com |
13:328bfac0e686 | 63 | // ^, s, 0x50, H, i, ^, p |
modtronix-com |
13:328bfac0e686 | 64 | // |
modtronix-com |
13:328bfac0e686 | 65 | // 'Hi^'; is decoded as: |
modtronix-com |
13:328bfac0e686 | 66 | // H, i, ^, ^ |
modtronix | 4:43abdd8eda40 | 67 | |
modtronix | 4:43abdd8eda40 | 68 | #include "mx_buffer_base.h" |
modtronix | 4:43abdd8eda40 | 69 | #include "mx_circular_buffer.h" |
modtronix | 4:43abdd8eda40 | 70 | |
modtronix-com |
13:328bfac0e686 | 71 | |
modtronix-com |
13:328bfac0e686 | 72 | // Debugging ////////////////////////////////////////////////////////////////// |
modtronix-com |
13:328bfac0e686 | 73 | // To enable debug output from this file, define MX_DEBUG and DEBUG_ENABLE_MX_CMD_BUFFER before |
modtronix-com |
13:328bfac0e686 | 74 | // including this file. |
modtronix-com |
13:328bfac0e686 | 75 | // |
modtronix | 4:43abdd8eda40 | 76 | //Defines for MXH_DEBUG - debugging for include file |
modtronix-com |
13:328bfac0e686 | 77 | #if !defined(DEBUG_ENABLE_MX_CMD_BUFFER) |
modtronix-com |
13:328bfac0e686 | 78 | #define DEBUG_ENABLE_MX_CMD_BUFFER 0 |
modtronix-com |
13:328bfac0e686 | 79 | #endif |
modtronix-com |
13:328bfac0e686 | 80 | #if !defined(DEBUG_ENABLE_INFO_MX_CMD_BUFFER) |
modtronix-com |
13:328bfac0e686 | 81 | #define DEBUG_ENABLE_INFO_MX_CMD_BUFFER 0 |
modtronix-com |
13:328bfac0e686 | 82 | #endif |
modtronix | 4:43abdd8eda40 | 83 | |
modtronix | 4:43abdd8eda40 | 84 | #if !defined(MXH_DEBUG) |
modtronix-com |
13:328bfac0e686 | 85 | #if defined(MX_DEBUG) && (DEBUG_ENABLE_MX_CMD_BUFFER==1) |
modtronix-com |
13:328bfac0e686 | 86 | #define MXH_DEBUG MX_DEBUG |
modtronix-com |
13:328bfac0e686 | 87 | #else |
modtronix-com |
13:328bfac0e686 | 88 | #define MXH_DEBUG(format, args...) ((void)0) |
modtronix-com |
13:328bfac0e686 | 89 | #endif |
modtronix-com |
13:328bfac0e686 | 90 | #endif |
modtronix-com |
13:328bfac0e686 | 91 | |
modtronix-com |
13:328bfac0e686 | 92 | #if !defined(MXH_DEBUG_INFO) |
modtronix-com |
13:328bfac0e686 | 93 | #if defined(MX_DEBUG) && (DEBUG_ENABLE_MX_CMD_BUFFER==1) && (DEBUG_ENABLE_INFO_MX_CMD_BUFFER==1) |
modtronix-com |
13:328bfac0e686 | 94 | #define MXH_DEBUG_INFO MX_DEBUG |
modtronix | 4:43abdd8eda40 | 95 | #else |
modtronix | 4:43abdd8eda40 | 96 | #define MXH_DEBUG_INFO(format, args...) ((void)0) |
modtronix | 4:43abdd8eda40 | 97 | #endif |
modtronix-com |
13:328bfac0e686 | 98 | #endif |
modtronix | 4:43abdd8eda40 | 99 | |
modtronix | 4:43abdd8eda40 | 100 | |
modtronix | 4:43abdd8eda40 | 101 | /** Templated Circular buffer class |
modtronix | 4:43abdd8eda40 | 102 | */ |
modtronix | 4:43abdd8eda40 | 103 | template<uint32_t BufferSize, uint8_t Commands = 16, typename CounterType = uint32_t> |
modtronix | 4:43abdd8eda40 | 104 | class MxCmdBuffer : public MxBuffer { |
modtronix | 4:43abdd8eda40 | 105 | public: |
modtronix-com |
7:709130701ac7 | 106 | MxCmdBuffer() : _head(0), _tail(0), _full(false), |
modtronix-com |
7:709130701ac7 | 107 | _errBufFull(0), _dontSaveCurrentCommand(0), _lastCharWasEOF(0), flags(0) |
modtronix | 4:43abdd8eda40 | 108 | { |
modtronix | 4:43abdd8eda40 | 109 | //flags.Val = 0; |
modtronix | 4:43abdd8eda40 | 110 | } |
modtronix | 4:43abdd8eda40 | 111 | |
modtronix | 4:43abdd8eda40 | 112 | ~MxCmdBuffer() { |
modtronix | 4:43abdd8eda40 | 113 | } |
modtronix | 4:43abdd8eda40 | 114 | |
modtronix | 4:43abdd8eda40 | 115 | /** Adds a byte to the current command in the buffer. This function checks for the "End of Command" character, |
modtronix | 4:43abdd8eda40 | 116 | * and if found, increments the "command count" register indicating how many commands this buffer has. |
modtronix | 4:43abdd8eda40 | 117 | * |
modtronix | 4:43abdd8eda40 | 118 | * If the buffer becomes full before the "End of Command" character is reached, all bytes added for current |
modtronix | 4:43abdd8eda40 | 119 | * command are removed. And, all following bytes added until the next "End of Command" character will be ignored. |
modtronix | 4:43abdd8eda40 | 120 | * |
modtronix | 4:43abdd8eda40 | 121 | * The checkBufferFullError() function can be used to check if this function caused an error, and command was |
modtronix | 4:43abdd8eda40 | 122 | * not added to buffer. |
modtronix | 4:43abdd8eda40 | 123 | * |
modtronix | 4:43abdd8eda40 | 124 | * @param data Data to be pushed to the buffer |
modtronix | 4:43abdd8eda40 | 125 | * |
modtronix | 4:43abdd8eda40 | 126 | * @return true if character added to buffer, else false. Important to note that all characters added to |
modtronix | 4:43abdd8eda40 | 127 | * current command CAN BE REMOVED if buffer gets full before "End of Command" added to buffer! |
modtronix | 4:43abdd8eda40 | 128 | */ |
modtronix | 4:43abdd8eda40 | 129 | bool put(const uint8_t& data) { |
modtronix | 4:43abdd8eda40 | 130 | uint8_t c = data; |
modtronix | 4:43abdd8eda40 | 131 | |
modtronix | 4:43abdd8eda40 | 132 | //Check if buffer full! If so: |
modtronix | 4:43abdd8eda40 | 133 | // - All data for this command added till now is lost |
modtronix | 4:43abdd8eda40 | 134 | // - All future data added for this command is ignored, until next "End of command" character is received |
modtronix | 4:43abdd8eda40 | 135 | if (isFull() && (_dontSaveCurrentCommand==false)) { |
modtronix | 4:43abdd8eda40 | 136 | _dontSaveCurrentCommand = true; |
modtronix | 4:43abdd8eda40 | 137 | _errBufFull = true; |
modtronix | 4:43abdd8eda40 | 138 | MXH_DEBUG("\r\nBuffer full, cmd LOST!"); |
modtronix | 4:43abdd8eda40 | 139 | |
modtronix | 4:43abdd8eda40 | 140 | //Remove all character added for this command. Restore head to last "End of Command" pointer |
modtronix | 4:43abdd8eda40 | 141 | if (cmdEndsBuf.isEmpty() == false) { |
modtronix-com |
7:709130701ac7 | 142 | //Restore head to byte following last "End of Command" pointer |
modtronix-com |
7:709130701ac7 | 143 | CounterType oldHead; |
modtronix-com |
7:709130701ac7 | 144 | oldHead = ((cmdEndsBuf.peekLastAdded()+1) % BufferSize); |
modtronix-com |
7:709130701ac7 | 145 | //Ensure buffer not still full |
modtronix-com |
7:709130701ac7 | 146 | if (oldHead != _head) { |
modtronix-com |
7:709130701ac7 | 147 | _head = oldHead; |
modtronix-com |
7:709130701ac7 | 148 | _full = false; |
modtronix-com |
7:709130701ac7 | 149 | } |
modtronix | 4:43abdd8eda40 | 150 | } |
modtronix | 4:43abdd8eda40 | 151 | //If no commands in buffer, set head=tail |
modtronix | 4:43abdd8eda40 | 152 | else { |
modtronix | 4:43abdd8eda40 | 153 | _head = _tail; |
modtronix | 4:43abdd8eda40 | 154 | _full = false; |
modtronix | 4:43abdd8eda40 | 155 | } |
modtronix | 4:43abdd8eda40 | 156 | } |
modtronix | 4:43abdd8eda40 | 157 | |
modtronix | 4:43abdd8eda40 | 158 | //Check if "End of Command" byte. A command string is terminated with a ';', CR(0x0a='\r') or LF(0x0d='\n') character |
modtronix | 4:43abdd8eda40 | 159 | if ((c == ';') || (c == 0x0d) || (c == 0x0a)) { |
modtronix | 4:43abdd8eda40 | 160 | if (flags.bits.replaceCrLfWithEoc) { |
modtronix | 4:43abdd8eda40 | 161 | c = ';'; //Change to "end of command" character |
modtronix | 4:43abdd8eda40 | 162 | } |
modtronix | 4:43abdd8eda40 | 163 | |
modtronix-com |
7:709130701ac7 | 164 | //If last character was also an "End of Command" character, ignore this one |
modtronix-com |
7:709130701ac7 | 165 | if (_lastCharWasEOF == true) { |
modtronix-com |
7:709130701ac7 | 166 | MXH_DEBUG_INFO("\r\nMultiple EOC"); |
modtronix | 4:43abdd8eda40 | 167 | return false; //Nothing added |
modtronix | 4:43abdd8eda40 | 168 | } |
modtronix-com |
7:709130701ac7 | 169 | |
modtronix-com |
7:709130701ac7 | 170 | _lastCharWasEOF = true; //Remember this was an "End of Command" character |
modtronix | 4:43abdd8eda40 | 171 | |
modtronix-com |
7:709130701ac7 | 172 | //Current command is now finished, so reset _dontSaveCurrentCommand |
modtronix-com |
7:709130701ac7 | 173 | if (_dontSaveCurrentCommand == true) { |
modtronix-com |
7:709130701ac7 | 174 | _dontSaveCurrentCommand = false; |
modtronix-com |
7:709130701ac7 | 175 | return false; //Nothing added |
modtronix-com |
7:709130701ac7 | 176 | } |
modtronix | 4:43abdd8eda40 | 177 | |
modtronix-com |
7:709130701ac7 | 178 | //Add pointer to "end of command" character |
modtronix-com |
7:709130701ac7 | 179 | cmdEndsBuf.put(_head); |
modtronix-com |
7:709130701ac7 | 180 | //End of command character will be added to buffer below at current _head pointer |
modtronix-com |
7:709130701ac7 | 181 | MXH_DEBUG_INFO("\r\nAdded Cmd, EOC=%d", _head); |
modtronix-com |
7:709130701ac7 | 182 | } |
modtronix-com |
7:709130701ac7 | 183 | else { |
modtronix-com |
7:709130701ac7 | 184 | _lastCharWasEOF = false; |
modtronix | 4:43abdd8eda40 | 185 | } |
modtronix | 4:43abdd8eda40 | 186 | |
modtronix | 4:43abdd8eda40 | 187 | if (_dontSaveCurrentCommand) { |
modtronix | 4:43abdd8eda40 | 188 | return false; |
modtronix | 4:43abdd8eda40 | 189 | } |
modtronix | 4:43abdd8eda40 | 190 | |
modtronix | 4:43abdd8eda40 | 191 | //Add byte to buffer |
modtronix | 4:43abdd8eda40 | 192 | _pool[_head++] = c; |
modtronix | 4:43abdd8eda40 | 193 | _head %= BufferSize; |
modtronix | 4:43abdd8eda40 | 194 | if (_head == _tail) { |
modtronix | 4:43abdd8eda40 | 195 | _full = true; |
modtronix | 4:43abdd8eda40 | 196 | } |
modtronix | 4:43abdd8eda40 | 197 | |
modtronix | 4:43abdd8eda40 | 198 | return true; |
modtronix | 4:43abdd8eda40 | 199 | } |
modtronix | 4:43abdd8eda40 | 200 | |
modtronix | 4:43abdd8eda40 | 201 | /** Adds given NULL terminated string to the buffer. This function checks for the "End of Command" character, |
modtronix | 4:43abdd8eda40 | 202 | * and if found, increments the "command count" register indicating how many commands this buffer has. |
modtronix | 4:43abdd8eda40 | 203 | * |
modtronix | 4:43abdd8eda40 | 204 | * If the buffer becomes full before the "End of Command" character is reached, all bytes added for current |
modtronix | 4:43abdd8eda40 | 205 | * command are removed. And, all following bytes added until the next "End of Command" character will be ignored. |
modtronix | 4:43abdd8eda40 | 206 | * |
modtronix | 4:43abdd8eda40 | 207 | * The checkBufferFullError() function can be used to check if this function caused an error, and command was |
modtronix | 4:43abdd8eda40 | 208 | * not added to buffer. |
modtronix | 4:43abdd8eda40 | 209 | * |
modtronix | 4:43abdd8eda40 | 210 | * @param buf Source buffer containing array to add to buffer |
modtronix | 4:43abdd8eda40 | 211 | * @param bufSize Size of array to add to buffer |
modtronix | 4:43abdd8eda40 | 212 | * |
modtronix | 4:43abdd8eda40 | 213 | * @return Returns true if something added to buffer, else false. Important to note that all characters added to |
modtronix | 4:43abdd8eda40 | 214 | * current command CAN BE REMOVED if buffer gets full before "End of Command" added to buffer! |
modtronix | 4:43abdd8eda40 | 215 | */ |
modtronix | 4:43abdd8eda40 | 216 | bool put(const char* str) { |
modtronix | 4:43abdd8eda40 | 217 | bool retVal = 0; |
modtronix | 4:43abdd8eda40 | 218 | |
modtronix | 4:43abdd8eda40 | 219 | //DO NOT DO this check here! This check MUST be done by put() function, because if buffer becomes full before |
modtronix | 4:43abdd8eda40 | 220 | //"End of Command" character received, the put() function will remove all current command characters added! |
modtronix | 4:43abdd8eda40 | 221 | //if (isFull() == true) { |
modtronix | 4:43abdd8eda40 | 222 | // return false; |
modtronix | 4:43abdd8eda40 | 223 | //} |
modtronix | 4:43abdd8eda40 | 224 | |
modtronix | 4:43abdd8eda40 | 225 | //Add whole string to buffer. DO NOT check isFull() in next line, MUST be checked in put() below!!!! |
modtronix | 4:43abdd8eda40 | 226 | while((*str != 0) /*&& (isFull()==false)*/) { |
modtronix | 4:43abdd8eda40 | 227 | retVal = retVal | put((uint8_t)(*str++)); |
modtronix | 4:43abdd8eda40 | 228 | } |
modtronix | 4:43abdd8eda40 | 229 | return retVal; |
modtronix | 4:43abdd8eda40 | 230 | } |
modtronix | 4:43abdd8eda40 | 231 | |
modtronix | 4:43abdd8eda40 | 232 | |
modtronix | 4:43abdd8eda40 | 233 | /** Adds given array to the buffer. This function checks for the "End of Command" character, |
modtronix | 4:43abdd8eda40 | 234 | * and if found, increments the "command count" register indicating how many commands this buffer has. |
modtronix | 4:43abdd8eda40 | 235 | * |
modtronix | 4:43abdd8eda40 | 236 | * If the buffer becomes full before the "End of Command" character is reached, all bytes added for current |
modtronix | 4:43abdd8eda40 | 237 | * command are removed. And, all following bytes added until the next "End of Command" character will be ignored. |
modtronix | 4:43abdd8eda40 | 238 | * |
modtronix | 4:43abdd8eda40 | 239 | * The checkBufferFullError() function can be used to check if this function caused an error, and command was |
modtronix | 4:43abdd8eda40 | 240 | * not added to buffer. |
modtronix | 4:43abdd8eda40 | 241 | |
modtronix | 4:43abdd8eda40 | 242 | * @param buf Source buffer containing array to add to buffer |
modtronix | 4:43abdd8eda40 | 243 | * @param bufSize Size of array to add to buffer |
modtronix | 4:43abdd8eda40 | 244 | * |
modtronix | 4:43abdd8eda40 | 245 | * @return Returns true if something added to buffer, else false. Important to note that all characters added to |
modtronix | 4:43abdd8eda40 | 246 | * current command CAN BE REMOVED if buffer gets full before "End of Command" added to buffer! |
modtronix | 4:43abdd8eda40 | 247 | */ |
modtronix | 4:43abdd8eda40 | 248 | bool putArray(uint8_t* buf, uint16_t bufSize) { |
modtronix | 4:43abdd8eda40 | 249 | bool retVal = 0; |
modtronix | 4:43abdd8eda40 | 250 | int i; |
modtronix | 4:43abdd8eda40 | 251 | |
modtronix | 4:43abdd8eda40 | 252 | //DO NOT DO this check here! This check MUST be done by put() function, because if buffer becomes full before |
modtronix | 4:43abdd8eda40 | 253 | //"End of Command" character received, the put() function will remove all current command characters added! |
modtronix | 4:43abdd8eda40 | 254 | //if (getFree() < bufSize) { |
modtronix | 4:43abdd8eda40 | 255 | // return 0; |
modtronix | 4:43abdd8eda40 | 256 | //} |
modtronix | 4:43abdd8eda40 | 257 | |
modtronix | 4:43abdd8eda40 | 258 | for(i=0; i<bufSize; i++) { |
modtronix | 4:43abdd8eda40 | 259 | retVal = retVal | put(buf[i]); |
modtronix | 4:43abdd8eda40 | 260 | } |
modtronix | 4:43abdd8eda40 | 261 | return retVal; |
modtronix | 4:43abdd8eda40 | 262 | } |
modtronix | 4:43abdd8eda40 | 263 | |
modtronix | 4:43abdd8eda40 | 264 | /** Gets and object from the buffer. Ensure buffer is NOT empty before calling this function! |
modtronix | 4:43abdd8eda40 | 265 | * |
modtronix | 4:43abdd8eda40 | 266 | * @return Read data |
modtronix | 4:43abdd8eda40 | 267 | */ |
modtronix | 4:43abdd8eda40 | 268 | uint8_t get() { |
modtronix | 4:43abdd8eda40 | 269 | if (!isEmpty()) { |
modtronix | 4:43abdd8eda40 | 270 | uint8_t retData; |
modtronix | 4:43abdd8eda40 | 271 | retData = _pool[_tail++]; |
modtronix | 4:43abdd8eda40 | 272 | _tail %= BufferSize; |
modtronix | 4:43abdd8eda40 | 273 | _full = false; |
modtronix | 4:43abdd8eda40 | 274 | return retData; |
modtronix | 4:43abdd8eda40 | 275 | } |
modtronix | 4:43abdd8eda40 | 276 | return 0; |
modtronix | 4:43abdd8eda40 | 277 | } |
modtronix | 4:43abdd8eda40 | 278 | |
modtronix | 4:43abdd8eda40 | 279 | /** Gets and object from the buffer. Returns true if OK, else false |
modtronix | 4:43abdd8eda40 | 280 | * |
modtronix | 4:43abdd8eda40 | 281 | * @param data Variable to put read data into |
modtronix | 4:43abdd8eda40 | 282 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
modtronix | 4:43abdd8eda40 | 283 | */ |
modtronix | 4:43abdd8eda40 | 284 | bool getAndCheck(uint8_t& data) { |
modtronix | 4:43abdd8eda40 | 285 | if (!isEmpty()) { |
modtronix | 4:43abdd8eda40 | 286 | data = _pool[_tail++]; |
modtronix | 4:43abdd8eda40 | 287 | _tail %= BufferSize; |
modtronix | 4:43abdd8eda40 | 288 | _full = false; |
modtronix | 4:43abdd8eda40 | 289 | return true; |
modtronix | 4:43abdd8eda40 | 290 | } |
modtronix | 4:43abdd8eda40 | 291 | return false; |
modtronix | 4:43abdd8eda40 | 292 | } |
modtronix | 4:43abdd8eda40 | 293 | |
modtronix | 4:43abdd8eda40 | 294 | |
modtronix | 4:43abdd8eda40 | 295 | /** Gets and object from the buffer, but do NOT remove it. Ensure buffer is NOT empty before calling |
modtronix | 4:43abdd8eda40 | 296 | * this function! If buffer is empty, will return an undefined value. |
modtronix | 4:43abdd8eda40 | 297 | * |
modtronix | 4:43abdd8eda40 | 298 | * @return Read data |
modtronix | 4:43abdd8eda40 | 299 | */ |
modtronix | 4:43abdd8eda40 | 300 | uint8_t peek() { |
modtronix | 4:43abdd8eda40 | 301 | return _pool[_tail]; |
modtronix | 4:43abdd8eda40 | 302 | } |
modtronix | 4:43abdd8eda40 | 303 | |
modtronix | 4:43abdd8eda40 | 304 | /** Gets an object from the buffer at given offset, but do NOT remove it. Given offset is a value from |
modtronix | 4:43abdd8eda40 | 305 | * 0 to n. Ensure buffer has as many objects as the offset requested! For example, if buffer has 5 objects |
modtronix | 4:43abdd8eda40 | 306 | * available, given offset can be a value from 0 to 4. |
modtronix | 4:43abdd8eda40 | 307 | * |
modtronix | 4:43abdd8eda40 | 308 | * @param offset Offset of requested object. A value from 0-n, where (n+1) = available objects = getAvailable() |
modtronix | 4:43abdd8eda40 | 309 | * @return Object at given offset |
modtronix | 4:43abdd8eda40 | 310 | */ |
modtronix | 4:43abdd8eda40 | 311 | uint8_t peekAt(CounterType offset) { |
modtronix | 4:43abdd8eda40 | 312 | return _pool[(offset+_tail)%BufferSize]; |
modtronix | 4:43abdd8eda40 | 313 | } |
modtronix | 4:43abdd8eda40 | 314 | |
modtronix | 4:43abdd8eda40 | 315 | /** Gets the last object added to the buffer, but do NOT remove it. |
modtronix | 4:43abdd8eda40 | 316 | * |
modtronix | 4:43abdd8eda40 | 317 | * @return Object at given offset |
modtronix | 4:43abdd8eda40 | 318 | */ |
modtronix | 4:43abdd8eda40 | 319 | uint8_t peekLastAdded() { |
modtronix | 4:43abdd8eda40 | 320 | return _pool[(_head-1)%BufferSize]; |
modtronix | 4:43abdd8eda40 | 321 | } |
modtronix | 4:43abdd8eda40 | 322 | |
modtronix | 4:43abdd8eda40 | 323 | /** Gets and array of given size, and write it to given buffer. |
modtronix | 4:43abdd8eda40 | 324 | * Nothing is removed from buffer |
modtronix | 4:43abdd8eda40 | 325 | * |
modtronix | 4:43abdd8eda40 | 326 | * @param buf Destination buffer to array to |
modtronix | 4:43abdd8eda40 | 327 | * @param bufSize Maximum size to write to destination buffer |
modtronix | 4:43abdd8eda40 | 328 | * @param lenReq Requested length |
modtronix | 4:43abdd8eda40 | 329 | * |
modtronix | 4:43abdd8eda40 | 330 | * |
modtronix | 4:43abdd8eda40 | 331 | * @return Number of bytes written to given buffer |
modtronix | 4:43abdd8eda40 | 332 | */ |
modtronix | 4:43abdd8eda40 | 333 | uint16_t peekArray(uint8_t* buf, uint16_t bufSize, CounterType lenReq) { |
modtronix | 4:43abdd8eda40 | 334 | uint16_t lenWritten=0; |
modtronix | 4:43abdd8eda40 | 335 | CounterType currTail; |
modtronix | 4:43abdd8eda40 | 336 | currTail = _tail; |
modtronix | 4:43abdd8eda40 | 337 | |
modtronix | 4:43abdd8eda40 | 338 | //Some checks |
modtronix | 4:43abdd8eda40 | 339 | if (isEmpty() || (bufSize==0) || (lenReq==0)) { |
modtronix | 4:43abdd8eda40 | 340 | return 0; |
modtronix | 4:43abdd8eda40 | 341 | } |
modtronix | 4:43abdd8eda40 | 342 | |
modtronix | 4:43abdd8eda40 | 343 | do { |
modtronix | 4:43abdd8eda40 | 344 | buf[lenWritten++] = _pool[currTail]; |
modtronix | 4:43abdd8eda40 | 345 | currTail = ((currTail+1)%BufferSize); |
modtronix | 4:43abdd8eda40 | 346 | } while((lenWritten<bufSize) && (lenWritten<lenReq)); |
modtronix | 4:43abdd8eda40 | 347 | |
modtronix | 4:43abdd8eda40 | 348 | |
modtronix | 4:43abdd8eda40 | 349 | return lenWritten; |
modtronix | 4:43abdd8eda40 | 350 | } |
modtronix | 4:43abdd8eda40 | 351 | |
modtronix | 4:43abdd8eda40 | 352 | /** Check if the buffer has a complete command |
modtronix | 4:43abdd8eda40 | 353 | * |
modtronix | 4:43abdd8eda40 | 354 | * @return True if the buffer has a command, false if not |
modtronix | 4:43abdd8eda40 | 355 | */ |
modtronix | 4:43abdd8eda40 | 356 | bool hasCommand() { |
modtronix | 4:43abdd8eda40 | 357 | return !cmdEndsBuf.isEmpty(); |
modtronix | 4:43abdd8eda40 | 358 | } |
modtronix | 4:43abdd8eda40 | 359 | |
modtronix | 4:43abdd8eda40 | 360 | /** Get length of next command in buffer, excluding "end of command" character! For example, |
modtronix | 4:43abdd8eda40 | 361 | * the command "r=100;" will return 5. |
modtronix | 4:43abdd8eda40 | 362 | * @return Length of next command in buffer |
modtronix | 4:43abdd8eda40 | 363 | */ |
modtronix | 4:43abdd8eda40 | 364 | uint8_t getCommandLength() { |
modtronix | 4:43abdd8eda40 | 365 | CounterType offsetEOC; |
modtronix | 4:43abdd8eda40 | 366 | |
modtronix | 4:43abdd8eda40 | 367 | if (cmdEndsBuf.isEmpty()) { |
modtronix | 4:43abdd8eda40 | 368 | return 0; |
modtronix | 4:43abdd8eda40 | 369 | } |
modtronix | 4:43abdd8eda40 | 370 | |
modtronix | 4:43abdd8eda40 | 371 | //Get offset of "end of command" character of current command in buffer |
modtronix | 4:43abdd8eda40 | 372 | offsetEOC = cmdEndsBuf.peek(); |
modtronix | 4:43abdd8eda40 | 373 | |
modtronix | 4:43abdd8eda40 | 374 | return ((offsetEOC-_tail) % BufferSize); |
modtronix | 4:43abdd8eda40 | 375 | } |
modtronix | 4:43abdd8eda40 | 376 | |
modtronix | 4:43abdd8eda40 | 377 | /** Get number of commands waiting in buffer |
modtronix | 4:43abdd8eda40 | 378 | * @return Number of commands waiting in buffer |
modtronix | 4:43abdd8eda40 | 379 | */ |
modtronix | 4:43abdd8eda40 | 380 | uint8_t getCommandsAvailable() { |
modtronix | 4:43abdd8eda40 | 381 | return cmdEndsBuf.getAvailable(); |
modtronix | 4:43abdd8eda40 | 382 | } |
modtronix | 4:43abdd8eda40 | 383 | |
modtronix | 4:43abdd8eda40 | 384 | /** If current command has "name=value" format, the 'name' part is returned. |
modtronix | 4:43abdd8eda40 | 385 | * Else, the whole command is returned(excluding a possible trailing '=' character). |
modtronix | 4:43abdd8eda40 | 386 | * Returned string is NULL terminated by default. |
modtronix | 4:43abdd8eda40 | 387 | * Nothing is removed from the buffer! |
modtronix | 4:43abdd8eda40 | 388 | * |
modtronix | 4:43abdd8eda40 | 389 | * If true is returned in the "isNameValue" parameter, this is a "name=value" command, with a name part of at |
modtronix | 4:43abdd8eda40 | 390 | * lease 1 character long. The offset of the 'value' part will be the returned |
modtronix | 4:43abdd8eda40 | 391 | * |
modtronix | 4:43abdd8eda40 | 392 | * If false is returned in the "isNameValue" parameter, there is no 'value' part of at least 1 character. There could however |
modtronix | 4:43abdd8eda40 | 393 | * still be a '=' character with no 'value' following it. Any possible trailing '=' character is removed string returned in 'buf'. |
modtronix | 4:43abdd8eda40 | 394 | * |
modtronix | 4:43abdd8eda40 | 395 | * @param buf Destination buffer to write string to |
modtronix | 4:43abdd8eda40 | 396 | * |
modtronix | 4:43abdd8eda40 | 397 | * @param bufSize Maximum size to write to destination buffer |
modtronix | 4:43abdd8eda40 | 398 | * |
modtronix | 4:43abdd8eda40 | 399 | * @param isNameValue Returns true if a '=' character was found -AND- at least 1 char following it. This indicates |
modtronix | 4:43abdd8eda40 | 400 | * this is a "name=value" command, and has a value part(of at least 1 character) following the '='. |
modtronix | 4:43abdd8eda40 | 401 | * First character of 'value' is value returned by this function + 1! |
modtronix | 4:43abdd8eda40 | 402 | * |
modtronix | 4:43abdd8eda40 | 403 | * @return Size in bytes of string returned in 'buf' parameter(is 'name' if isNameValue=true). |
modtronix | 4:43abdd8eda40 | 404 | * If true is returned in 'isNameValue', 'buf' contains the 'name' part of a 'name=value' command. This returned offset points to '='. |
modtronix | 4:43abdd8eda40 | 405 | * If false is returned in 'isNameValue', 'buf' contains the whole command string(excluding possible trailing '=' character). |
modtronix | 4:43abdd8eda40 | 406 | */ |
modtronix | 4:43abdd8eda40 | 407 | uint16_t getCommandName(uint8_t* buf, uint16_t bufSize, bool& isNameValue, bool nullTerminate = true) { |
modtronix | 4:43abdd8eda40 | 408 | CounterType offsetEOC; |
modtronix | 4:43abdd8eda40 | 409 | CounterType currTail; |
modtronix | 4:43abdd8eda40 | 410 | uint16_t nameLen = 0; |
modtronix | 4:43abdd8eda40 | 411 | isNameValue=false; |
modtronix | 4:43abdd8eda40 | 412 | |
modtronix | 4:43abdd8eda40 | 413 | //Some checks |
modtronix | 4:43abdd8eda40 | 414 | if (cmdEndsBuf.isEmpty() || (bufSize==0) || (_pool[_tail]=='=')) { |
modtronix | 4:43abdd8eda40 | 415 | return 0; |
modtronix | 4:43abdd8eda40 | 416 | } |
modtronix | 4:43abdd8eda40 | 417 | offsetEOC = cmdEndsBuf.peek(); |
modtronix | 4:43abdd8eda40 | 418 | currTail = _tail; |
modtronix | 4:43abdd8eda40 | 419 | |
modtronix | 4:43abdd8eda40 | 420 | //Copy current command string to given buffer, until '=' reached |
modtronix | 4:43abdd8eda40 | 421 | do { |
modtronix | 4:43abdd8eda40 | 422 | buf[nameLen++] = _pool[currTail]; |
modtronix | 4:43abdd8eda40 | 423 | currTail = ((currTail+1)%BufferSize); |
modtronix | 4:43abdd8eda40 | 424 | //If next character is '=' |
modtronix | 4:43abdd8eda40 | 425 | if(_pool[currTail] == '=') { |
modtronix | 4:43abdd8eda40 | 426 | //Check at least 1 character following '=' |
modtronix | 4:43abdd8eda40 | 427 | //Get pointer of character following '=', and ensure it is not "end of command"(offsetEOC). Meaning there is |
modtronix | 4:43abdd8eda40 | 428 | //still at least 1 more character following '=' |
modtronix | 4:43abdd8eda40 | 429 | if(((currTail+1)%BufferSize) != offsetEOC) { |
modtronix | 4:43abdd8eda40 | 430 | isNameValue = true; |
modtronix | 4:43abdd8eda40 | 431 | } |
modtronix | 4:43abdd8eda40 | 432 | break; |
modtronix | 4:43abdd8eda40 | 433 | } |
modtronix | 4:43abdd8eda40 | 434 | } while((currTail!=offsetEOC) && (nameLen<bufSize)); |
modtronix | 4:43abdd8eda40 | 435 | |
modtronix | 4:43abdd8eda40 | 436 | |
modtronix | 4:43abdd8eda40 | 437 | if (nullTerminate) { |
modtronix | 4:43abdd8eda40 | 438 | if(nameLen<bufSize) { |
modtronix | 4:43abdd8eda40 | 439 | buf[nameLen] = 0; |
modtronix | 4:43abdd8eda40 | 440 | } |
modtronix | 4:43abdd8eda40 | 441 | buf[bufSize-1] = 0; //Null terminate last position of buffer in case it filled up |
modtronix | 4:43abdd8eda40 | 442 | } |
modtronix | 4:43abdd8eda40 | 443 | |
modtronix | 4:43abdd8eda40 | 444 | return nameLen; |
modtronix | 4:43abdd8eda40 | 445 | } |
modtronix | 4:43abdd8eda40 | 446 | |
modtronix | 4:43abdd8eda40 | 447 | /** For commands with "name=value" format, returns the 'value' part. |
modtronix | 4:43abdd8eda40 | 448 | * Returned string is NULL terminated by default. |
modtronix | 4:43abdd8eda40 | 449 | * Nothing is removed from the buffer! |
modtronix | 4:43abdd8eda40 | 450 | * |
modtronix | 4:43abdd8eda40 | 451 | * To optimize this function, call getCommandName() before calling this function. The getCommandName() return value(+1) |
modtronix | 4:43abdd8eda40 | 452 | * can be used as 'offset' parameter to this function. This will save this function from searching for '=' character. |
modtronix | 4:43abdd8eda40 | 453 | * |
modtronix | 4:43abdd8eda40 | 454 | * |
modtronix | 4:43abdd8eda40 | 455 | * @param buf Destination buffer to write string to |
modtronix | 4:43abdd8eda40 | 456 | * @param bufSize Maximum size to write to destination buffer |
modtronix | 4:43abdd8eda40 | 457 | * @param offset Gives offset of value string if known. This can be value(+1) returned by |
modtronix | 4:43abdd8eda40 | 458 | * getCommandName() function. Use -1 if unknown. |
modtronix | 4:43abdd8eda40 | 459 | * |
modtronix | 4:43abdd8eda40 | 460 | * @return Size in bytes of returned string |
modtronix | 4:43abdd8eda40 | 461 | */ |
modtronix | 4:43abdd8eda40 | 462 | uint16_t getCommandValue(uint8_t* buf, uint16_t bufSize, uint16_t offset = -1, bool nullTerminate = true) { |
modtronix | 4:43abdd8eda40 | 463 | CounterType offsetEOC; |
modtronix | 4:43abdd8eda40 | 464 | CounterType currTail; |
modtronix | 4:43abdd8eda40 | 465 | uint16_t valueLen = 0; |
modtronix | 4:43abdd8eda40 | 466 | bool foundEq=false; //'=' character found |
modtronix | 4:43abdd8eda40 | 467 | |
modtronix | 4:43abdd8eda40 | 468 | //Some checks |
modtronix | 4:43abdd8eda40 | 469 | if (cmdEndsBuf.isEmpty() || (bufSize==0)) { |
modtronix | 4:43abdd8eda40 | 470 | return 0; |
modtronix | 4:43abdd8eda40 | 471 | } |
modtronix | 4:43abdd8eda40 | 472 | offsetEOC = cmdEndsBuf.peek(); //offset of "end of command" character |
modtronix | 4:43abdd8eda40 | 473 | |
modtronix | 4:43abdd8eda40 | 474 | currTail = _tail; |
modtronix | 4:43abdd8eda40 | 475 | |
modtronix | 4:43abdd8eda40 | 476 | //If offset was given, it will point to first character of value string |
modtronix | 4:43abdd8eda40 | 477 | if (offset != -1) { |
modtronix | 4:43abdd8eda40 | 478 | //Check offset point somewhere inside current command! Where ((offsetEOC-_tail) % BufferSize) = command length |
modtronix | 4:43abdd8eda40 | 479 | if (offset < ((offsetEOC-_tail)%BufferSize)) { |
modtronix | 4:43abdd8eda40 | 480 | currTail = ((currTail + offset)%BufferSize); //Add offset to tail |
modtronix | 4:43abdd8eda40 | 481 | |
modtronix | 4:43abdd8eda40 | 482 | //If given offset was for first character of 'value', it will NOT point to '='. It will be next character. |
modtronix | 4:43abdd8eda40 | 483 | if(_pool[currTail] != '=') { |
modtronix | 4:43abdd8eda40 | 484 | //Points to character after '=', indicate '=' has already been found. |
modtronix | 4:43abdd8eda40 | 485 | foundEq=true; |
modtronix | 4:43abdd8eda40 | 486 | } |
modtronix | 4:43abdd8eda40 | 487 | //ELSE, assume offset for for '=' character. It will be found in step below |
modtronix | 4:43abdd8eda40 | 488 | } |
modtronix | 4:43abdd8eda40 | 489 | else { |
modtronix | 4:43abdd8eda40 | 490 | //currTail = _tail; //Ignore given offset, and search whole command for '=' |
modtronix | 4:43abdd8eda40 | 491 | MXH_DEBUG("\r\ngetCommandValue() Offset error!"); |
modtronix | 4:43abdd8eda40 | 492 | } |
modtronix | 4:43abdd8eda40 | 493 | } |
modtronix | 4:43abdd8eda40 | 494 | |
modtronix | 4:43abdd8eda40 | 495 | do { |
modtronix | 4:43abdd8eda40 | 496 | if (foundEq) { |
modtronix | 4:43abdd8eda40 | 497 | buf[valueLen++] = _pool[currTail]; |
modtronix | 4:43abdd8eda40 | 498 | } |
modtronix | 4:43abdd8eda40 | 499 | else { |
modtronix | 4:43abdd8eda40 | 500 | if(_pool[currTail] == '=') { |
modtronix | 4:43abdd8eda40 | 501 | foundEq=true; |
modtronix | 4:43abdd8eda40 | 502 | } |
modtronix | 4:43abdd8eda40 | 503 | } |
modtronix | 4:43abdd8eda40 | 504 | currTail = ((currTail+1)%BufferSize); |
modtronix | 4:43abdd8eda40 | 505 | } while((currTail != offsetEOC) && (valueLen<bufSize)); |
modtronix | 4:43abdd8eda40 | 506 | |
modtronix | 4:43abdd8eda40 | 507 | if (nullTerminate) { |
modtronix | 4:43abdd8eda40 | 508 | if(valueLen<bufSize) { |
modtronix | 4:43abdd8eda40 | 509 | buf[valueLen] = 0; |
modtronix | 4:43abdd8eda40 | 510 | } |
modtronix | 4:43abdd8eda40 | 511 | buf[bufSize-1] = 0; //Null terminate last position of buffer in case it filled up |
modtronix | 4:43abdd8eda40 | 512 | } |
modtronix | 4:43abdd8eda40 | 513 | |
modtronix | 4:43abdd8eda40 | 514 | return valueLen; |
modtronix | 4:43abdd8eda40 | 515 | } |
modtronix | 4:43abdd8eda40 | 516 | |
modtronix | 4:43abdd8eda40 | 517 | /** Search current command for given character |
modtronix | 4:43abdd8eda40 | 518 | * |
modtronix | 4:43abdd8eda40 | 519 | * @param offset Returns offset of found character. If NULL, this parameter is not used. |
modtronix | 4:43abdd8eda40 | 520 | * |
modtronix | 4:43abdd8eda40 | 521 | * @return Returns true if found, else false |
modtronix | 4:43abdd8eda40 | 522 | */ |
modtronix | 4:43abdd8eda40 | 523 | bool search(uint8_t c, uint16_t* offsetFound) { |
modtronix | 4:43abdd8eda40 | 524 | CounterType offsetEOF; |
modtronix | 4:43abdd8eda40 | 525 | CounterType currTail; |
modtronix | 4:43abdd8eda40 | 526 | |
modtronix | 4:43abdd8eda40 | 527 | //Get command length |
modtronix | 4:43abdd8eda40 | 528 | if (cmdEndsBuf.isEmpty()) { |
modtronix | 4:43abdd8eda40 | 529 | return false; |
modtronix | 4:43abdd8eda40 | 530 | } |
modtronix | 4:43abdd8eda40 | 531 | offsetEOF = cmdEndsBuf.peek(); |
modtronix | 4:43abdd8eda40 | 532 | currTail = _tail; |
modtronix | 4:43abdd8eda40 | 533 | do { |
modtronix | 4:43abdd8eda40 | 534 | if(_pool[currTail] == c) { |
modtronix | 4:43abdd8eda40 | 535 | if (offsetFound!=NULL) { |
modtronix | 4:43abdd8eda40 | 536 | *offsetFound = (uint16_t)currTail; |
modtronix | 4:43abdd8eda40 | 537 | } |
modtronix | 4:43abdd8eda40 | 538 | return true; |
modtronix | 4:43abdd8eda40 | 539 | } |
modtronix | 4:43abdd8eda40 | 540 | currTail = ((currTail+1)%BufferSize); |
modtronix | 4:43abdd8eda40 | 541 | } while(currTail != offsetEOF); |
modtronix | 4:43abdd8eda40 | 542 | |
modtronix | 4:43abdd8eda40 | 543 | // CounterType i; |
modtronix | 4:43abdd8eda40 | 544 | // CounterType cmdLen; |
modtronix | 4:43abdd8eda40 | 545 | // cmdLen = (offsetEOF-_tail) % BufferSize; |
modtronix | 4:43abdd8eda40 | 546 | // for (i=0; i<cmdLen; i++) { |
modtronix | 4:43abdd8eda40 | 547 | // if(_pool[(i+_tail)%BufferSize] == c) { |
modtronix | 4:43abdd8eda40 | 548 | // //if(peekAt(i)==c) { |
modtronix | 4:43abdd8eda40 | 549 | // offsetFound = (uint16_t)i; |
modtronix | 4:43abdd8eda40 | 550 | // return true; |
modtronix | 4:43abdd8eda40 | 551 | // } |
modtronix | 4:43abdd8eda40 | 552 | // } |
modtronix | 4:43abdd8eda40 | 553 | return false; |
modtronix | 4:43abdd8eda40 | 554 | } |
modtronix | 4:43abdd8eda40 | 555 | |
modtronix | 4:43abdd8eda40 | 556 | /** Check if the buffer is empty |
modtronix | 4:43abdd8eda40 | 557 | * |
modtronix | 4:43abdd8eda40 | 558 | * @return True if the buffer is empty, false if not |
modtronix | 4:43abdd8eda40 | 559 | */ |
modtronix | 4:43abdd8eda40 | 560 | bool isEmpty() { |
modtronix | 4:43abdd8eda40 | 561 | return (_head == _tail) && !_full; |
modtronix | 4:43abdd8eda40 | 562 | } |
modtronix | 4:43abdd8eda40 | 563 | |
modtronix | 4:43abdd8eda40 | 564 | /** Check if the buffer is full |
modtronix | 4:43abdd8eda40 | 565 | * |
modtronix | 4:43abdd8eda40 | 566 | * @return True if the buffer is full, false if not |
modtronix | 4:43abdd8eda40 | 567 | */ |
modtronix | 4:43abdd8eda40 | 568 | bool isFull() { |
modtronix | 4:43abdd8eda40 | 569 | return _full; |
modtronix | 4:43abdd8eda40 | 570 | } |
modtronix | 4:43abdd8eda40 | 571 | |
modtronix | 4:43abdd8eda40 | 572 | /** Get number of available bytes in buffer |
modtronix | 4:43abdd8eda40 | 573 | * @return Number of available bytes in buffer |
modtronix | 4:43abdd8eda40 | 574 | */ |
modtronix | 4:43abdd8eda40 | 575 | CounterType getAvailable() { |
modtronix-com |
7:709130701ac7 | 576 | if (_head != _tail) { |
modtronix-com |
7:709130701ac7 | 577 | CounterType avail; |
modtronix-com |
7:709130701ac7 | 578 | avail = _head - _tail; |
modtronix-com |
7:709130701ac7 | 579 | avail %= BufferSize; |
modtronix-com |
7:709130701ac7 | 580 | return avail; |
modtronix-com |
7:709130701ac7 | 581 | } |
modtronix-com |
7:709130701ac7 | 582 | |
modtronix-com |
7:709130701ac7 | 583 | //Head=Tail. Can be full or empty |
modtronix-com |
7:709130701ac7 | 584 | if (_full==false) { |
modtronix | 4:43abdd8eda40 | 585 | return 0; |
modtronix | 4:43abdd8eda40 | 586 | } |
modtronix-com |
7:709130701ac7 | 587 | else { |
modtronix-com |
7:709130701ac7 | 588 | return BufferSize; |
modtronix-com |
7:709130701ac7 | 589 | } |
modtronix | 4:43abdd8eda40 | 590 | } |
modtronix | 4:43abdd8eda40 | 591 | |
modtronix | 4:43abdd8eda40 | 592 | |
modtronix | 4:43abdd8eda40 | 593 | /** Get number of free bytes in buffer available for writing data to. |
modtronix | 4:43abdd8eda40 | 594 | * @return Number of free bytes in buffer available for writing data to. |
modtronix | 4:43abdd8eda40 | 595 | */ |
modtronix | 4:43abdd8eda40 | 596 | CounterType getFree() { |
modtronix | 4:43abdd8eda40 | 597 | CounterType free; |
modtronix | 4:43abdd8eda40 | 598 | //Full |
modtronix | 4:43abdd8eda40 | 599 | if (_full==true) { |
modtronix | 4:43abdd8eda40 | 600 | return 0; |
modtronix | 4:43abdd8eda40 | 601 | } |
modtronix | 4:43abdd8eda40 | 602 | //Empty |
modtronix | 4:43abdd8eda40 | 603 | if(_head == _tail) { |
modtronix | 4:43abdd8eda40 | 604 | return BufferSize; |
modtronix | 4:43abdd8eda40 | 605 | } |
modtronix | 4:43abdd8eda40 | 606 | free = _tail - _head; |
modtronix | 4:43abdd8eda40 | 607 | free %= BufferSize; |
modtronix | 4:43abdd8eda40 | 608 | return free; |
modtronix | 4:43abdd8eda40 | 609 | } |
modtronix | 4:43abdd8eda40 | 610 | |
modtronix | 4:43abdd8eda40 | 611 | /** Replaces any LF or CR characters with an "End of Command" character = ';' |
modtronix | 4:43abdd8eda40 | 612 | */ |
modtronix | 4:43abdd8eda40 | 613 | void enableReplaceCrLfWithEoc() { |
modtronix | 4:43abdd8eda40 | 614 | flags.bits.replaceCrLfWithEoc = true; |
modtronix | 4:43abdd8eda40 | 615 | } |
modtronix | 4:43abdd8eda40 | 616 | |
modtronix | 4:43abdd8eda40 | 617 | /** Do NOT replaces LF and CR characters with an "End of Command" character = ';' |
modtronix | 4:43abdd8eda40 | 618 | */ |
modtronix | 4:43abdd8eda40 | 619 | void disableReplaceCrLfWithEoc() { |
modtronix | 4:43abdd8eda40 | 620 | flags.bits.replaceCrLfWithEoc = false; |
modtronix | 4:43abdd8eda40 | 621 | } |
modtronix | 4:43abdd8eda40 | 622 | |
modtronix | 4:43abdd8eda40 | 623 | /** Check if an overwrite error occurred. It resets the error flag if it was set |
modtronix | 4:43abdd8eda40 | 624 | * @return True if overwrite error occurred since last time this function was called, else false |
modtronix | 4:43abdd8eda40 | 625 | */ |
modtronix | 4:43abdd8eda40 | 626 | bool checkBufferFullError() { |
modtronix | 4:43abdd8eda40 | 627 | bool retVal = _errBufFull; |
modtronix | 4:43abdd8eda40 | 628 | _errBufFull = false; |
modtronix | 4:43abdd8eda40 | 629 | return retVal; |
modtronix | 4:43abdd8eda40 | 630 | } |
modtronix | 4:43abdd8eda40 | 631 | |
modtronix | 4:43abdd8eda40 | 632 | /** Reset the buffer |
modtronix | 4:43abdd8eda40 | 633 | */ |
modtronix | 4:43abdd8eda40 | 634 | void reset() { |
modtronix | 4:43abdd8eda40 | 635 | _head = 0; |
modtronix | 4:43abdd8eda40 | 636 | _tail = 0; |
modtronix | 4:43abdd8eda40 | 637 | _full = false; |
modtronix | 4:43abdd8eda40 | 638 | _errBufFull = false; |
modtronix | 4:43abdd8eda40 | 639 | _dontSaveCurrentCommand = false; |
modtronix | 4:43abdd8eda40 | 640 | } |
modtronix | 4:43abdd8eda40 | 641 | |
modtronix | 4:43abdd8eda40 | 642 | /** Remove a command from buffer |
modtronix | 4:43abdd8eda40 | 643 | */ |
modtronix | 4:43abdd8eda40 | 644 | void removeCommand() { |
modtronix | 4:43abdd8eda40 | 645 | if (cmdEndsBuf.isEmpty()) { |
modtronix | 4:43abdd8eda40 | 646 | return; |
modtronix | 4:43abdd8eda40 | 647 | } |
modtronix | 4:43abdd8eda40 | 648 | |
modtronix | 4:43abdd8eda40 | 649 | //MXH_DEBUG("\r\nRemoving Cmd"); |
modtronix | 4:43abdd8eda40 | 650 | |
modtronix | 4:43abdd8eda40 | 651 | //Set tail = "end of command" character + 1. This is first character of next command |
modtronix | 4:43abdd8eda40 | 652 | _tail = (cmdEndsBuf.get()+1) % BufferSize; |
modtronix | 4:43abdd8eda40 | 653 | _full = false; |
modtronix | 4:43abdd8eda40 | 654 | } |
modtronix | 4:43abdd8eda40 | 655 | |
modtronix | 4:43abdd8eda40 | 656 | private: |
modtronix | 4:43abdd8eda40 | 657 | uint8_t _pool[BufferSize]; |
modtronix | 4:43abdd8eda40 | 658 | volatile CounterType _head; |
modtronix | 4:43abdd8eda40 | 659 | volatile CounterType _tail; |
modtronix | 4:43abdd8eda40 | 660 | volatile bool _full; |
modtronix | 4:43abdd8eda40 | 661 | volatile bool _errBufFull; |
modtronix | 4:43abdd8eda40 | 662 | volatile bool _dontSaveCurrentCommand; |
modtronix-com |
7:709130701ac7 | 663 | volatile bool _lastCharWasEOF; |
modtronix | 4:43abdd8eda40 | 664 | |
modtronix | 4:43abdd8eda40 | 665 | union Flags { |
modtronix | 4:43abdd8eda40 | 666 | struct { |
modtronix | 4:43abdd8eda40 | 667 | uint8_t replaceCrLfWithEoc : 1; //Replace CR and LF with "End of Command" character = ';' |
modtronix | 4:43abdd8eda40 | 668 | } bits; |
modtronix | 4:43abdd8eda40 | 669 | uint8_t Val; |
modtronix | 4:43abdd8eda40 | 670 | //Union constructor. Used in initialization list of this class. |
modtronix | 4:43abdd8eda40 | 671 | Flags(uint8_t v) : Val(v) {} |
modtronix | 4:43abdd8eda40 | 672 | } flags; |
modtronix | 4:43abdd8eda40 | 673 | |
modtronix | 4:43abdd8eda40 | 674 | public: |
modtronix | 4:43abdd8eda40 | 675 | //Buffer for storing "end of command" locations |
modtronix | 4:43abdd8eda40 | 676 | MxCircularBuffer <uint16_t, Commands, CounterType> cmdEndsBuf; |
modtronix | 4:43abdd8eda40 | 677 | //MxCircularBuffer <uint16_t, Commands, uint16_t> cmdEndsBuf; //Creates larger code |
modtronix | 4:43abdd8eda40 | 678 | }; |
modtronix | 4:43abdd8eda40 | 679 | |
modtronix-com |
9:5000feb4b46f | 680 | #if defined(MXH_DEBUG) |
modtronix-com |
13:328bfac0e686 | 681 | #undef MXH_DEBUG |
modtronix-com |
9:5000feb4b46f | 682 | #endif |
modtronix-com |
9:5000feb4b46f | 683 | #if defined(MXH_DEBUG_INFO) |
modtronix-com |
13:328bfac0e686 | 684 | #undef MXH_DEBUG_INFO |
modtronix-com |
9:5000feb4b46f | 685 | #endif |
modtronix | 4:43abdd8eda40 | 686 | |
modtronix | 4:43abdd8eda40 | 687 | |
modtronix | 4:43abdd8eda40 | 688 | #endif /* SRC_MX_CMD_BUFFER_H_ */ |