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