editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Thu May 23 22:08:35 2019 +0000
Revision:
0:4d7de8b5c800
Child:
1:5b33e7447601
initial commit class CmdLine from test fixture / sandbox

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 /// @todo on_immediate_0x21() on_immediate_0x7b() on_immediate_0x7d()
whismanoid 0:4d7de8b5c800 44 /// should be declared in a more extensible way
whismanoid 0:4d7de8b5c800 45 void on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 0:4d7de8b5c800 46 void on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 0:4d7de8b5c800 47 void on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 0:4d7de8b5c800 48
whismanoid 0:4d7de8b5c800 49
whismanoid 0:4d7de8b5c800 50 /**
whismanoid 0:4d7de8b5c800 51 @brief CmdLine class manages an editable serial input line buffer
whismanoid 0:4d7de8b5c800 52 */
whismanoid 0:4d7de8b5c800 53 class CmdLine
whismanoid 0:4d7de8b5c800 54 {
whismanoid 0:4d7de8b5c800 55 protected:
whismanoid 0:4d7de8b5c800 56 static const unsigned int COMMAND_BUFFER_LENGTH = 96; // buffer includes null termination
whismanoid 0:4d7de8b5c800 57 unsigned int indexOfNextEmptyCell;
whismanoid 0:4d7de8b5c800 58 char buf[COMMAND_BUFFER_LENGTH];
whismanoid 0:4d7de8b5c800 59 Stream& associatedSerialPort;
whismanoid 0:4d7de8b5c800 60 const char *name;
whismanoid 0:4d7de8b5c800 61 public:
whismanoid 0:4d7de8b5c800 62 CmdLine(Stream& AssociatedSerialPort, const char *Name);
whismanoid 0:4d7de8b5c800 63 void clear();
whismanoid 0:4d7de8b5c800 64 //void idleAppendIfReadable(); // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 65 void append(char ch); // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 66 Callback<void(CmdLine&)> onEOLcommandParser; // on CR or LF, parse the command buffer
whismanoid 0:4d7de8b5c800 67 /** serial returns reference to the associated serial port */
whismanoid 0:4d7de8b5c800 68 Stream& serial(void) const {
whismanoid 0:4d7de8b5c800 69 return associatedSerialPort;
whismanoid 0:4d7de8b5c800 70 };
whismanoid 0:4d7de8b5c800 71 /** CmdLine[index] returns the character at specified index; [0] is the first character */
whismanoid 0:4d7de8b5c800 72 char operator[] (unsigned int index) const {
whismanoid 0:4d7de8b5c800 73 if (index >= indexOfNextEmptyCell) return '\0';
whismanoid 0:4d7de8b5c800 74 return buf[index];
whismanoid 0:4d7de8b5c800 75 };
whismanoid 0:4d7de8b5c800 76 /** str returns the c-string buffer */
whismanoid 0:4d7de8b5c800 77 const char *str() const {
whismanoid 0:4d7de8b5c800 78 return buf;
whismanoid 0:4d7de8b5c800 79 };
whismanoid 0:4d7de8b5c800 80 /** nameStr returns the names of the associated serial port */
whismanoid 0:4d7de8b5c800 81 const char *nameStr() const {
whismanoid 0:4d7de8b5c800 82 return name;
whismanoid 0:4d7de8b5c800 83 };
whismanoid 0:4d7de8b5c800 84
whismanoid 0:4d7de8b5c800 85 // bool parse_and_remove_key(const char *key, int& matched_index);
whismanoid 0:4d7de8b5c800 86 bool parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen);
whismanoid 0:4d7de8b5c800 87
whismanoid 0:4d7de8b5c800 88 bool parse_frequency_Hz(const char *key, uint32_t& frequency_Hz);
whismanoid 0:4d7de8b5c800 89 bool parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask);
whismanoid 0:4d7de8b5c800 90 bool parse_byte_hex(const char *key, uint8_t& nByteVar);
whismanoid 0:4d7de8b5c800 91 bool parse_byte_dec(const char *key, uint8_t& nByteVar);
whismanoid 0:4d7de8b5c800 92 bool parse_uint16_hex(const char *key, uint16_t& uint16Var);
whismanoid 0:4d7de8b5c800 93 bool parse_int16_hex(const char *key, int16_t& int16Var);
whismanoid 0:4d7de8b5c800 94 bool parse_float(const char *key, float& floatVar);
whismanoid 0:4d7de8b5c800 95 bool parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
whismanoid 0:4d7de8b5c800 96 bool parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
whismanoid 0:4d7de8b5c800 97 };
whismanoid 0:4d7de8b5c800 98
whismanoid 0:4d7de8b5c800 99 #endif // __CmdLine_H__
whismanoid 0:4d7de8b5c800 100
whismanoid 0:4d7de8b5c800 101 // End of file
whismanoid 0:4d7de8b5c800 102