editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

CmdLine.h

Committer:
whismanoid
Date:
2019-05-23
Revision:
0:4d7de8b5c800
Child:
1:5b33e7447601

File content as of revision 0:4d7de8b5c800:

// /*******************************************************************************
// * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
// *
// * Permission is hereby granted, free of charge, to any person obtaining a
// * copy of this software and associated documentation files (the "Software"),
// * to deal in the Software without restriction, including without limitation
// * the rights to use, copy, modify, merge, publish, distribute, sublicense,
// * and/or sell copies of the Software, and to permit persons to whom the
// * Software is furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included
// * in all copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
// * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// * OTHER DEALINGS IN THE SOFTWARE.
// *
// * Except as contained in this notice, the name of Maxim Integrated
// * Products, Inc. shall not be used except as stated in the Maxim Integrated
// * Products, Inc. Branding Policy.
// *
// * The mere transfer of this software does not imply any licenses
// * of trade secrets, proprietary technology, copyrights, patents,
// * trademarks, maskwork rights, or any other form of intellectual
// * property whatsoever. Maxim Integrated Products, Inc. retains all
// * ownership rights.
// *******************************************************************************
// */
// *********************************************************************
// @file CmdLine.h
// *********************************************************************

// Prevent multiple declaration
#ifndef __CmdLine_H__
#define __CmdLine_H__

#include "mbed.h"

/// @todo on_immediate_0x21() on_immediate_0x7b() on_immediate_0x7d()
/// should be declared in a more extensible way
void on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
void on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
void on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET


/**
 @brief CmdLine class manages an editable serial input line buffer
 */
class CmdLine
{
protected:
    static const unsigned int COMMAND_BUFFER_LENGTH = 96; // buffer includes null termination
    unsigned int indexOfNextEmptyCell;
    char buf[COMMAND_BUFFER_LENGTH];
    Stream& associatedSerialPort;
    const char *name;
public:
    CmdLine(Stream& AssociatedSerialPort, const char *Name);
    void clear();
    //void idleAppendIfReadable(); // append ch to buf, unless BS or EOL or other editing character
    void append(char ch); // append ch to buf, unless BS or EOL or other editing character
    Callback<void(CmdLine&)> onEOLcommandParser; // on CR or LF, parse the command buffer
    /** serial returns reference to the associated serial port */
    Stream& serial(void) const {
        return associatedSerialPort;
    };
    /** CmdLine[index] returns the character at specified index; [0] is the first character */
    char operator[] (unsigned int index) const {
        if (index >= indexOfNextEmptyCell) return '\0';
        return buf[index];
    };
    /** str returns the c-string buffer */
    const char *str() const {
        return buf;
    };
    /** nameStr returns the names of the associated serial port */
    const char *nameStr() const {
        return name;
    };

    // bool parse_and_remove_key(const char *key, int& matched_index);
    bool parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen);

    bool parse_frequency_Hz(const char *key, uint32_t& frequency_Hz);
    bool parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask);
    bool parse_byte_hex(const char *key, uint8_t& nByteVar);
    bool parse_byte_dec(const char *key, uint8_t& nByteVar);
    bool parse_uint16_hex(const char *key, uint16_t& uint16Var);
    bool parse_int16_hex(const char *key, int16_t& int16Var);
    bool parse_float(const char *key, float& floatVar);
    bool parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
    bool parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize);
};

#endif // __CmdLine_H__

// End of file