diff --git a/cmdproc.cpp b/cmdproc.cpp deleted file mode 100644 index 22c3ce1..0000000 --- a/cmdproc.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include - -#include "cmdproc.h" - - -static const cmd_t *find_cmd(const cmd_t * commands, const char *name) -{ - const cmd_t *cmd; - for (cmd = commands; cmd->cmd != NULL; cmd++) { - if (strcmp(name, cmd->name) == 0) { - return cmd; - } - } - return NULL; -} - -static int split(char *input, char *args[], int maxargs) -{ - int argc = 0; - char *next = strtok(input, " "); - while ((next != NULL) && (argc < maxargs)) { - args[argc++] = next; - next = strtok(NULL, " "); - } - return argc; -} - -int cmd_process(const cmd_t * commands, char *line) -{ - char *argv[CMD_MAX_ARGS]; - - // parse line - int argc = split(line, argv, CMD_MAX_ARGS); - if (argc == 0) { - // no command present - return CMD_NO_CMD; - } - // find matching entry - const cmd_t *cmd = find_cmd(commands, argv[0]); - if (cmd == NULL) { - // no command found - return CMD_UNKNOWN; - } - // execute - int res = cmd->cmd(argc, argv); - return res; -} diff --git a/cmdproc.h b/cmdproc.h deleted file mode 100644 index fb32128..0000000 --- a/cmdproc.h +++ /dev/null @@ -1,25 +0,0 @@ -// maximum number of arguments (including command itself) -#define CMD_MAX_ARGS 50 - -// default result codes -#define CMD_OK 0 -#define CMD_NO_CMD -0x7F00 -#define CMD_UNKNOWN -0x7F01 - -typedef int (cmd_fn)(int argc, char *argv[]); - -// command table entry -typedef struct { - const char *name; - cmd_fn *cmd; - const char *help; -} cmd_t; - -/** - * Parses the given line into arguments, matches it with a command and executes the command. - * @param[in] commands command table - * @param[in] line the line to parse - * @return the result of command execution - */ -int cmd_process(const cmd_t *commands, char *line); - diff --git a/editline.cpp b/editline.cpp deleted file mode 100644 index cc111f4..0000000 --- a/editline.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "editline.h" - -#define BELL 0x07 -#define BS 0x08 -#define LF 0x0A -#define CR 0x0D - -static char *line; -static int size; -static int pos = 0; - -/** - * Initializes the edit buffer. - * - * @param buffer the edit buffer - * @param bufsize the size of the edit buffer - */ -void EditInit(char *buffer, int bufsize) -{ - line = buffer; - size = bufsize; -} - -/** - * Processes a character into an edit buffer, returns true - * if a full line has been received - * - * @param cin the character to process - * @param cout the output character - * @return true if a full line was entered, false otherwise - */ -bool EditLine(char cin, char *cout) -{ - *cout = cin; // echo by default - switch (cin) { // carriage return is ignored - case '\r': - break; - case '\n': // end-of-line - line[pos] = '\0'; - pos = 0; - return true; - case 0x7F: - case 0x08: // backspace - if (pos > 0) { - pos--; - } - break; - default: - if (pos < (size - 1)) { // store char as long as there is space to do so - line[pos++] = cin; - } else { - *cout = 0x07; // bell - } - break; - } - return false; -} - diff --git a/editline.h b/editline.h deleted file mode 100644 index d122dc1..0000000 --- a/editline.h +++ /dev/null @@ -1,6 +0,0 @@ -#include - -void EditInit(char *buf, int bufsize); -bool EditLine(char cin, char *cout); - - diff --git a/platformio.ini b/platformio.ini index 7ea8354..a47a2df 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,5 +21,6 @@ upload_speed = 1000000 lib_deps = fastled/FastLED@3.6.0 tzapu/WifiManager@^0.16.0 bblanchon/ArduinoJson@^6.20.1 + https://github.com/bertrik/minishell diff --git a/stofananas.ino b/stofananas.ino index 67d0bf0..1aa883e 100644 --- a/stofananas.ino +++ b/stofananas.ino @@ -11,9 +11,7 @@ #include #include #include - -#include "cmdproc.h" -#include "editline.h" +#include #include @@ -41,6 +39,7 @@ static savedata_t savedata; static WiFiManager wifiManager; static WiFiClient wifiClient; +static MiniShell shell(&Serial); static char espid[64]; static CRGB leds1[1]; @@ -130,7 +129,7 @@ static int do_get(int argc, char *argv[]) printf("fetch_pm failed!\n"); return -1; } - return CMD_OK; + return 0; } static int do_config(int argc, char *argv[]) @@ -154,7 +153,7 @@ static int do_config(int argc, char *argv[]) printf("config.rgb = %s\n", savedata.hasRgbLed ? "true" : "false"); printf("config.magic = %08X\n", savedata.magic); - return CMD_OK; + return 0; } static CRGB interpolate(float pm, const pmlevel_t table[]) @@ -182,7 +181,7 @@ static int do_pm(int argc, char *argv[]) float pm = atoi(argv[1]); set_led(interpolate(pm, pmlevels)); - return CMD_OK; + return 0; } static bool geolocate(float &latitude, float &longitude, float &accuracy) @@ -253,13 +252,13 @@ static int do_geolocate(int argc, char *argv[]) printf("Latitude = %f, Longitude = %f, Accuracy = %f\n", latitude, longitude, accuracy); printf("https://google.com/maps/place/%f,%f\n", latitude, longitude); - return CMD_OK; + return 0; } static int do_reboot(int argc, char *argv[]) { ESP.restart(); - return CMD_OK; + return 0; } static int do_error(int argc, char *argv[]) @@ -268,7 +267,7 @@ static int do_error(int argc, char *argv[]) num_fetch_failures = atoi(argv[1]); } printf("fetch failures:%d\n", num_fetch_failures); - return CMD_OK; + return 0; } static int do_led(int argc, char *argv[]) @@ -279,7 +278,7 @@ static int do_led(int argc, char *argv[]) int rgb = strtoul(argv[1], NULL, 16); set_led(rgb); - return CMD_OK; + return 0; } const cmd_t commands[] = { @@ -297,7 +296,7 @@ const cmd_t commands[] = { static int do_help(int argc, char *argv[]) { show_help(commands); - return CMD_OK; + return 0; } static void animate(void) @@ -329,7 +328,6 @@ void setup(void) Serial.begin(115200); printf("\nESP-STOFANANAS\n"); - EditInit(line, sizeof(line)); // init config EEPROM.begin(sizeof(savedata)); @@ -392,29 +390,7 @@ void loop(void) FastLED.showColor(color, flash ? 200 : 255); } - // parse command line - while (Serial.available()) { - char c = Serial.read(); - bool haveLine = EditLine(c, &c); - Serial.write(c); - if (haveLine) { - int result = cmd_process(commands, line); - switch (result) { - case CMD_OK: - printf("OK\n"); - break; - case CMD_NO_CMD: - break; - case CMD_UNKNOWN: - printf("Unknown command, available commands:\n"); - show_help(commands); - break; - default: - printf("%d\n", result); - break; - } - printf(">"); - } - } + // command line processing + shell.process(">", commands); }