Skip to content
parse text lines and execute associated functions
C++ Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples/TextCMD_synopsis
t
Changelog
Makefile
README
TextCMD.cpp
TextCMD.h

README

NAME

    TextCMD.h - Arduino library for decode text commands

SYNOPSIS

        #include <Arduino.h>
        #include <TextCMD.h>            // https://github.com/jozef/Arduino-TextCMD
    
        cmd_dispatch commands[] = {
            { "?", &cmd_help     },
            { "*", &cmd_multiply }
        };
        TextCMD cmd((sizeof(commands)/sizeof(commands[0])),commands);
    
        void setup () {
            Serial.begin(9600);
            cmd.do_dispatch("?");
            cmd.do_dispatch("* 5 5");
        }
    
        void loop () {
            while (Serial.available()) {
                char ch = Serial.read();
                if (ch == '\b') { Serial.print(F("\b \b")); }
                else { Serial.print(ch); }
                switch (cmd.add_char(ch)) {
                    case -1: Serial.println(F("unknown command or syntax error. send '?' for help")); break;
                }
            }
            delay(100);
        }
    
        int8_t cmd_help(uint8_t argc, const char* argv[]) {
            Serial.println(F("supported commands:"));
            Serial.println(F("    * [int] [int]    - multiply two number"));
            Serial.println(F("    ?                - print this help"));
            return 0;
        }
        int8_t cmd_multiply(uint8_t argc, const char* argv[]) {
            if (argc != 3) return -1;
            Serial.println(
                String(argv[1]) + " * " + String(argv[2]) + " = "
                + String(String(argv[1]).toInt()*String(argv[2]).toInt()));
            return 0;
        }

DESCRIPTION

    Helps to parse text command lines and execute associated functions.

    Constructor needs a list of commands and associated functions. Command
    is a char* and function pointers is of type:

        int8_t (*cb)(uint8_t argc, const char* argv[])

    These callback function will get number of arguments and an array of
    these arguments as parameter. Return value is a 8bit integer. Function
    can return any value, -1 is returned by this code when there is no
    matching callback function found.

METHODS

 int8_t do_dispatch(const char* line);

    char* line passed as parameter will be split into arguments and the
    first will be matched against the list of commands. Function of the
    first matching command will be executed and its return value returned.
    Otherwise returns -1.

 int8_t add_char(const char);

    Similar as do_dispatch() but it keeps on adding charaters to a line
    buffer until new-line is reached. At that point  do_dispatch()  is
    triggered. Returns 0 in all cases but when new-line is sent, in which
    case it returns  do_dispatch()  return value.

    See "SYNOPSIS" or examples/TextCMD_synopsis/TextCMD_synopsis.ino for a
    full add_char() example of reading and echoing input from serial input.

LICENSE

    This is free software, licensed under the MIT License.

AUTHOR

        Jozef Kutej

You can’t perform that action at this time.