editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Thu Jun 06 01:50:32 2019 +0000
Revision:
2:0f702da53f2a
Parent:
1:5b33e7447601
Child:
4:700b71cd3bd2
add parse_uint16_dec parse_int16_dec functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whismanoid 0:4d7de8b5c800 1 // /*******************************************************************************
whismanoid 0:4d7de8b5c800 2 // * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
whismanoid 0:4d7de8b5c800 3 // *
whismanoid 0:4d7de8b5c800 4 // * Permission is hereby granted, free of charge, to any person obtaining a
whismanoid 0:4d7de8b5c800 5 // * copy of this software and associated documentation files (the "Software"),
whismanoid 0:4d7de8b5c800 6 // * to deal in the Software without restriction, including without limitation
whismanoid 0:4d7de8b5c800 7 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
whismanoid 0:4d7de8b5c800 8 // * and/or sell copies of the Software, and to permit persons to whom the
whismanoid 0:4d7de8b5c800 9 // * Software is furnished to do so, subject to the following conditions:
whismanoid 0:4d7de8b5c800 10 // *
whismanoid 0:4d7de8b5c800 11 // * The above copyright notice and this permission notice shall be included
whismanoid 0:4d7de8b5c800 12 // * in all copies or substantial portions of the Software.
whismanoid 0:4d7de8b5c800 13 // *
whismanoid 0:4d7de8b5c800 14 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
whismanoid 0:4d7de8b5c800 15 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
whismanoid 0:4d7de8b5c800 16 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
whismanoid 0:4d7de8b5c800 17 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
whismanoid 0:4d7de8b5c800 18 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
whismanoid 0:4d7de8b5c800 19 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
whismanoid 0:4d7de8b5c800 20 // * OTHER DEALINGS IN THE SOFTWARE.
whismanoid 0:4d7de8b5c800 21 // *
whismanoid 0:4d7de8b5c800 22 // * Except as contained in this notice, the name of Maxim Integrated
whismanoid 0:4d7de8b5c800 23 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
whismanoid 0:4d7de8b5c800 24 // * Products, Inc. Branding Policy.
whismanoid 0:4d7de8b5c800 25 // *
whismanoid 0:4d7de8b5c800 26 // * The mere transfer of this software does not imply any licenses
whismanoid 0:4d7de8b5c800 27 // * of trade secrets, proprietary technology, copyrights, patents,
whismanoid 0:4d7de8b5c800 28 // * trademarks, maskwork rights, or any other form of intellectual
whismanoid 0:4d7de8b5c800 29 // * property whatsoever. Maxim Integrated Products, Inc. retains all
whismanoid 0:4d7de8b5c800 30 // * ownership rights.
whismanoid 0:4d7de8b5c800 31 // *******************************************************************************
whismanoid 0:4d7de8b5c800 32 // */
whismanoid 0:4d7de8b5c800 33 // *********************************************************************
whismanoid 0:4d7de8b5c800 34 // @file CmdLine.h
whismanoid 0:4d7de8b5c800 35 // *********************************************************************
whismanoid 0:4d7de8b5c800 36
whismanoid 0:4d7de8b5c800 37 // Prevent multiple declaration
whismanoid 0:4d7de8b5c800 38 #ifndef __CmdLine_H__
whismanoid 0:4d7de8b5c800 39 #define __CmdLine_H__
whismanoid 0:4d7de8b5c800 40
whismanoid 0:4d7de8b5c800 41 #include "mbed.h"
whismanoid 0:4d7de8b5c800 42
whismanoid 0:4d7de8b5c800 43 /**
whismanoid 0:4d7de8b5c800 44 @brief CmdLine class manages an editable serial input line buffer
whismanoid 0:4d7de8b5c800 45 */
whismanoid 0:4d7de8b5c800 46 class CmdLine
whismanoid 0:4d7de8b5c800 47 {
whismanoid 1:5b33e7447601 48 /// @page Immediate Immediate command character
whismanoid 1:5b33e7447601 49 /// Class CmdLine supports immeidate command character handler functions.
whismanoid 1:5b33e7447601 50 /// Immediate command characters get handled immediately,
whismanoid 1:5b33e7447601 51 /// without waiting for End Of Line CR or LF,
whismanoid 1:5b33e7447601 52 /// and without appearing in the command buffer.
whismanoid 1:5b33e7447601 53 /// Avoid using characters that may appear in other commands,
whismanoid 1:5b33e7447601 54 /// such as 0-9 A-Z a-z and some punctuation %*+-./=
whismanoid 1:5b33e7447601 55 /// so these 25 characters are available: !"#$&'(),:;<>?@[\]^_`{|}~
whismanoid 2:0f702da53f2a 56 /// @see on_immediate_0x21 handler for Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 2:0f702da53f2a 57 /// @see on_immediate_0x22 handler for Unicode (U+0022) " QUOTATION MARK
whismanoid 2:0f702da53f2a 58 /// @see on_immediate_0x23 handler for Unicode (U+0023) # NUMBER SIGN = pound sign, hash, crosshatch
whismanoid 2:0f702da53f2a 59 /// @see on_immediate_0x24 handler for Unicode (U+0024) $ DOLLAR SIGN
whismanoid 2:0f702da53f2a 60 /// @see on_immediate_0x26 handler for Unicode (U+0026) & AMPERSAND
whismanoid 2:0f702da53f2a 61 /// @see on_immediate_0x27 handler for Unicode (U+0027) ' APOSTROPHE
whismanoid 2:0f702da53f2a 62 /// @see on_immediate_0x28 handler for Unicode (U+0028) ( LEFT PARENTHESIS
whismanoid 2:0f702da53f2a 63 /// @see on_immediate_0x29 handler for Unicode (U+0029) ) RIGHT PARENTHESIS
whismanoid 2:0f702da53f2a 64 /// @see on_immediate_0x2c handler for Unicode (U+002C) , COMMA
whismanoid 2:0f702da53f2a 65 /// @see on_immediate_0x3a handler for Unicode (U+003A) : COLON
whismanoid 2:0f702da53f2a 66 /// @see on_immediate_0x3b handler for Unicode (U+003B) ; SEMICOLON
whismanoid 2:0f702da53f2a 67 /// @see on_immediate_0x3c handler for Unicode (U+003C) < LESS-THAN SIGN
whismanoid 2:0f702da53f2a 68 /// @see on_immediate_0x3e handler for Unicode (U+003E) > GREATER-THAN SIGN
whismanoid 2:0f702da53f2a 69 /// @see on_immediate_0x3f handler for Unicode (U+003F) ? QUESTION MARK
whismanoid 2:0f702da53f2a 70 /// @see on_immediate_0x40 handler for Unicode (U+0040) @ COMMERCIAL AT = at sign
whismanoid 2:0f702da53f2a 71 /// @see on_immediate_0x5b handler for Unicode (U+005B) [ LEFT SQUARE BRACKET
whismanoid 2:0f702da53f2a 72 /// @see on_immediate_0x5c handler for Unicode (U+005C) \ REVERSE SOLIDUS
whismanoid 2:0f702da53f2a 73 /// @see on_immediate_0x5d handler for Unicode (U+005D) ] RIGHT SQUARE BRACKET
whismanoid 2:0f702da53f2a 74 /// @see on_immediate_0x5e handler for Unicode (U+005E) ^ CIRCUMFLEX ACCENT
whismanoid 2:0f702da53f2a 75 /// @see on_immediate_0x5f handler for Unicode (U+005F) _ LOW LINE =SPACING UNDERSCORE
whismanoid 2:0f702da53f2a 76 /// @see on_immediate_0x60 handler for Unicode (U+0060) ` GRAVE ACCENT (also called backtick)
whismanoid 2:0f702da53f2a 77 /// @see on_immediate_0x7b handler for Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 2:0f702da53f2a 78 /// @see on_immediate_0x7c handler for Unicode (U+007C) | VERTICAL LINE
whismanoid 2:0f702da53f2a 79 /// @see on_immediate_0x7d handler for Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 2:0f702da53f2a 80 /// @see on_immediate_0x7e handler for Unicode (U+007E) ~ TILDE
whismanoid 1:5b33e7447601 81
whismanoid 0:4d7de8b5c800 82 protected:
whismanoid 0:4d7de8b5c800 83 static const unsigned int COMMAND_BUFFER_LENGTH = 96; // buffer includes null termination
whismanoid 0:4d7de8b5c800 84 unsigned int indexOfNextEmptyCell;
whismanoid 0:4d7de8b5c800 85 char buf[COMMAND_BUFFER_LENGTH];
whismanoid 0:4d7de8b5c800 86 Stream& associatedSerialPort;
whismanoid 0:4d7de8b5c800 87 const char *name;
whismanoid 1:5b33e7447601 88
whismanoid 0:4d7de8b5c800 89 public:
whismanoid 0:4d7de8b5c800 90 CmdLine(Stream& AssociatedSerialPort, const char *Name);
whismanoid 1:5b33e7447601 91
whismanoid 0:4d7de8b5c800 92 void clear();
whismanoid 1:5b33e7447601 93
whismanoid 0:4d7de8b5c800 94 //void idleAppendIfReadable(); // append ch to buf, unless BS or EOL or other editing character
whismanoid 1:5b33e7447601 95
whismanoid 0:4d7de8b5c800 96 void append(char ch); // append ch to buf, unless BS or EOL or other editing character
whismanoid 1:5b33e7447601 97
whismanoid 1:5b33e7447601 98 Callback<void(CmdLine&)> onEOLcommandParser; //!< optional immediate handler for End Of Line CR or LF, parse the command buffer
whismanoid 1:5b33e7447601 99
whismanoid 1:5b33e7447601 100 Callback<void(void)> on_immediate_0x21; //!< optional @ref Immediate handler for Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 101 Callback<void(void)> on_immediate_0x22; //!< optional @ref Immediate handler for Unicode (U+0022) " QUOTATION MARK
whismanoid 1:5b33e7447601 102 Callback<void(void)> on_immediate_0x23; //!< optional @ref Immediate handler for Unicode (U+0023) # NUMBER SIGN = pound sign, hash, crosshatch
whismanoid 1:5b33e7447601 103 Callback<void(void)> on_immediate_0x24; //!< optional @ref Immediate handler for Unicode (U+0024) $ DOLLAR SIGN
whismanoid 1:5b33e7447601 104 Callback<void(void)> on_immediate_0x26; //!< optional @ref Immediate handler for Unicode (U+0026) & AMPERSAND
whismanoid 1:5b33e7447601 105 Callback<void(void)> on_immediate_0x27; //!< optional @ref Immediate handler for Unicode (U+0027) ' APOSTROPHE
whismanoid 1:5b33e7447601 106 Callback<void(void)> on_immediate_0x28; //!< optional @ref Immediate handler for Unicode (U+0028) ( LEFT PARENTHESIS
whismanoid 1:5b33e7447601 107 Callback<void(void)> on_immediate_0x29; //!< optional @ref Immediate handler for Unicode (U+0029) ) RIGHT PARENTHESIS
whismanoid 1:5b33e7447601 108 Callback<void(void)> on_immediate_0x2c; //!< optional @ref Immediate handler for Unicode (U+002C) , COMMA
whismanoid 1:5b33e7447601 109 Callback<void(void)> on_immediate_0x3a; //!< optional @ref Immediate handler for Unicode (U+003A) : COLON
whismanoid 1:5b33e7447601 110 Callback<void(void)> on_immediate_0x3b; //!< optional @ref Immediate handler for Unicode (U+003B) ; SEMICOLON
whismanoid 1:5b33e7447601 111 Callback<void(void)> on_immediate_0x3c; //!< optional @ref Immediate handler for Unicode (U+003C) < LESS-THAN SIGN
whismanoid 1:5b33e7447601 112 Callback<void(void)> on_immediate_0x3e; //!< optional @ref Immediate handler for Unicode (U+003E) > GREATER-THAN SIGN
whismanoid 1:5b33e7447601 113 Callback<void(void)> on_immediate_0x3f; //!< optional @ref Immediate handler for Unicode (U+003F) ? QUESTION MARK
whismanoid 1:5b33e7447601 114 Callback<void(void)> on_immediate_0x40; //!< optional @ref Immediate handler for Unicode (U+0040) @ COMMERCIAL AT = at sign
whismanoid 1:5b33e7447601 115 Callback<void(void)> on_immediate_0x5b; //!< optional @ref Immediate handler for Unicode (U+005B) [ LEFT SQUARE BRACKET
whismanoid 1:5b33e7447601 116 Callback<void(void)> on_immediate_0x5c; //!< optional @ref Immediate handler for Unicode (U+005C) \ REVERSE SOLIDUS
whismanoid 1:5b33e7447601 117 Callback<void(void)> on_immediate_0x5d; //!< optional @ref Immediate handler for Unicode (U+005D) ] RIGHT SQUARE BRACKET
whismanoid 1:5b33e7447601 118 Callback<void(void)> on_immediate_0x5e; //!< optional @ref Immediate handler for Unicode (U+005E) ^ CIRCUMFLEX ACCENT
whismanoid 1:5b33e7447601 119 Callback<void(void)> on_immediate_0x5f; //!< optional @ref Immediate handler for Unicode (U+005F) _ LOW LINE =SPACING UNDERSCORE
whismanoid 1:5b33e7447601 120 Callback<void(void)> on_immediate_0x60; //!< optional @ref Immediate handler for Unicode (U+0060) ` GRAVE ACCENT (also called backtick)
whismanoid 1:5b33e7447601 121 Callback<void(void)> on_immediate_0x7b; //!< optional @ref Immediate handler for Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 122 Callback<void(void)> on_immediate_0x7c; //!< optional @ref Immediate handler for Unicode (U+007C) | VERTICAL LINE
whismanoid 1:5b33e7447601 123 Callback<void(void)> on_immediate_0x7d; //!< optional @ref Immediate handler for Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 124 Callback<void(void)> on_immediate_0x7e; //!< optional @ref Immediate handler for Unicode (U+007E) ~ TILDE
whismanoid 1:5b33e7447601 125
whismanoid 0:4d7de8b5c800 126 /** serial returns reference to the associated serial port */
whismanoid 0:4d7de8b5c800 127 Stream& serial(void) const {
whismanoid 0:4d7de8b5c800 128 return associatedSerialPort;
whismanoid 0:4d7de8b5c800 129 };
whismanoid 1:5b33e7447601 130
whismanoid 0:4d7de8b5c800 131 /** CmdLine[index] returns the character at specified index; [0] is the first character */
whismanoid 0:4d7de8b5c800 132 char operator[] (unsigned int index) const {
whismanoid 0:4d7de8b5c800 133 if (index >= indexOfNextEmptyCell) return '\0';
whismanoid 0:4d7de8b5c800 134 return buf[index];
whismanoid 0:4d7de8b5c800 135 };
whismanoid 1:5b33e7447601 136
whismanoid 0:4d7de8b5c800 137 /** str returns the c-string buffer */
whismanoid 0:4d7de8b5c800 138 const char *str() const {
whismanoid 0:4d7de8b5c800 139 return buf;
whismanoid 0:4d7de8b5c800 140 };
whismanoid 1:5b33e7447601 141
whismanoid 0:4d7de8b5c800 142 /** nameStr returns the names of the associated serial port */
whismanoid 0:4d7de8b5c800 143 const char *nameStr() const {
whismanoid 0:4d7de8b5c800 144 return name;
whismanoid 0:4d7de8b5c800 145 };
whismanoid 0:4d7de8b5c800 146
whismanoid 0:4d7de8b5c800 147 // bool parse_and_remove_key(const char *key, int& matched_index);
whismanoid 0:4d7de8b5c800 148 bool parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen);
whismanoid 0:4d7de8b5c800 149
whismanoid 0:4d7de8b5c800 150 bool parse_frequency_Hz(const char *key, uint32_t& frequency_Hz);
whismanoid 0:4d7de8b5c800 151 bool parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask);
whismanoid 0:4d7de8b5c800 152 bool parse_byte_hex(const char *key, uint8_t& nByteVar);
whismanoid 0:4d7de8b5c800 153 bool parse_byte_dec(const char *key, uint8_t& nByteVar);
whismanoid 0:4d7de8b5c800 154 bool parse_uint16_hex(const char *key, uint16_t& uint16Var);
whismanoid 2:0f702da53f2a 155 bool parse_uint16_dec(const char *key, uint16_t& uint16Var);
whismanoid 0:4d7de8b5c800 156 bool parse_int16_hex(const char *key, int16_t& int16Var);
whismanoid 2:0f702da53f2a 157 bool parse_int16_dec(const char *key, int16_t& int16Var);
whismanoid 0:4d7de8b5c800 158 bool parse_float(const char *key, float& floatVar);
whismanoid 0:4d7de8b5c800 159 bool parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
whismanoid 0:4d7de8b5c800 160 bool parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
whismanoid 0:4d7de8b5c800 161 };
whismanoid 0:4d7de8b5c800 162
whismanoid 0:4d7de8b5c800 163 #endif // __CmdLine_H__
whismanoid 0:4d7de8b5c800 164
whismanoid 0:4d7de8b5c800 165 // End of file