|
| 1 | +/** |
| 2 | + * @file examples/arduino/basic_usage_callbacks/src/main.ino |
| 3 | + * @author Jose Miguel Rios Rubio <jrios.github@gmail.com> |
| 4 | + * @date 09-07-2022 |
| 5 | + * @version 1.0.0 |
| 6 | + * |
| 7 | + * @section DESCRIPTION |
| 8 | + * |
| 9 | + * MINBASECLI library basic usage example for Arduino Framework that shows the |
| 10 | + * usage of the CLI to setup and handle commands callbacks. |
| 11 | + * |
| 12 | + * @section LICENSE |
| 13 | + * |
| 14 | + * Copyright (c) 2021 Jose Miguel Rios Rubio. All right reserved. |
| 15 | + * |
| 16 | + * This library is free software; you can redistribute it and/or |
| 17 | + * modify it under the terms of the GNU Lesser General Public |
| 18 | + * License as published by the Free Software Foundation; either |
| 19 | + * version 2.1 of the License, or (at your option) any later version. |
| 20 | + * |
| 21 | + * This library is distributed in the hope that it will be useful, |
| 22 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | + * Lesser General Public License for more details. |
| 25 | + * |
| 26 | + * You should have received a copy of the GNU Lesser General Public |
| 27 | + * License along with this library; if not, write to the Free Software |
| 28 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 | + */ |
| 30 | + |
| 31 | +/*****************************************************************************/ |
| 32 | + |
| 33 | +/* Libraries */ |
| 34 | + |
| 35 | +// Device Framework libraries |
| 36 | +#include <Arduino.h> |
| 37 | + |
| 38 | +// Custom libraries |
| 39 | +#include <minbasecli.h> |
| 40 | + |
| 41 | +/*****************************************************************************/ |
| 42 | + |
| 43 | +/* Defines, Macros, Constants and Types */ |
| 44 | + |
| 45 | +// Serial Speed |
| 46 | +#define SERIAL_BAUDS 115200 |
| 47 | + |
| 48 | +// LED to be controlled thorugh CLI command |
| 49 | +#if defined(LED_BUILTIN) |
| 50 | + #define COMMAND_LED LED_BUILTIN |
| 51 | +#else |
| 52 | + #define COMMAND_LED 13 |
| 53 | +#endif |
| 54 | + |
| 55 | +// Current Firmware Application Version |
| 56 | +#define FW_APP_VERSION "1.0.0" |
| 57 | + |
| 58 | +/*****************************************************************************/ |
| 59 | + |
| 60 | +/* Global Elements */ |
| 61 | + |
| 62 | +static MINBASECLI Cli; |
| 63 | + |
| 64 | +static volatile bool exit = false; |
| 65 | + |
| 66 | +/*****************************************************************************/ |
| 67 | + |
| 68 | +/* Function Prototypes */ |
| 69 | + |
| 70 | +// CLI command "help" callback function |
| 71 | +void cmd_help(int argc, char* argv[]); |
| 72 | + |
| 73 | +// CLI command "led" callback function |
| 74 | +void cmd_led(int argc, char* argv[]); |
| 75 | + |
| 76 | +// CLI command "version" callback function |
| 77 | +void cmd_version(int argc, char* argv[]); |
| 78 | + |
| 79 | +// CLI command "exit" callback function |
| 80 | +void cmd_exit(int argc, char* argv[]); |
| 81 | + |
| 82 | +/*****************************************************************************/ |
| 83 | + |
| 84 | +/* Setup & Loop */ |
| 85 | + |
| 86 | +void setup() |
| 87 | +{ |
| 88 | + // LED Init |
| 89 | + digitalWrite(COMMAND_LED, LOW); |
| 90 | + pinMode(COMMAND_LED, OUTPUT); |
| 91 | + |
| 92 | + // Serial Init |
| 93 | + Serial.begin(SERIAL_BAUDS); |
| 94 | + |
| 95 | + // CLI init to use Serial as interface |
| 96 | + Cli.setup(&Serial); |
| 97 | + |
| 98 | + // Add commands and bind callbacks to them |
| 99 | + Cli.add_cmd("led", &cmd_test, "led [on/off], Turn LED ON or OFF.."); |
| 100 | + Cli.add_cmd("version", &cmd_version, "Shows current firmware version."); |
| 101 | + Cli.add_cmd("exit", &cmd_exit, "Exit and close the program."); |
| 102 | + |
| 103 | + // The "help" command is already builtin and available from the CLI, and it |
| 104 | + // will shows added command descriptions, but you can setup a custom one |
| 105 | + Cli.add_cmd("help", &cmd_help, "Shows program help information."); |
| 106 | + |
| 107 | + Cli.printf("\nCommand Line Interface is ready\n\n"); |
| 108 | +} |
| 109 | + |
| 110 | +void loop() |
| 111 | +{ |
| 112 | + // Check and Handle CLI commands |
| 113 | + Cli.run(); |
| 114 | + |
| 115 | + // System delay |
| 116 | + delay(10); |
| 117 | +} |
| 118 | + |
| 119 | +/*****************************************************************************/ |
| 120 | + |
| 121 | +/* CLI Commands Callbacks */ |
| 122 | + |
| 123 | +void cmd_help(int argc, char* argv[]) |
| 124 | +{ |
| 125 | + // Show some Info text |
| 126 | + Cli.printf("\nCustom Help Command\n"); |
| 127 | + Cli.printf("MINBASECLI basic_usage_callbacks %s\n", FW_APP_VERSION); |
| 128 | + |
| 129 | + // Call the builtin "help" function to show added command descriptions |
| 130 | + Cli.cmd_help(argc, argv); |
| 131 | +} |
| 132 | + |
| 133 | +void cmd_led(int argc, char* argv[]) |
| 134 | +{ |
| 135 | + bool invalid_argv = false; |
| 136 | + |
| 137 | + if(argc == 0) |
| 138 | + invalid_argv = true; |
| 139 | + else |
| 140 | + { |
| 141 | + char* test_mode = argv[0]; |
| 142 | + if(strcmp(test_mode, "on") == 0) |
| 143 | + { |
| 144 | + Cli.printf("Turning LED ON.\n"); |
| 145 | + digitalWrite(COMMAND_LED, HIGH); |
| 146 | + } |
| 147 | + else if(strcmp(test_mode, "off") == 0) |
| 148 | + { |
| 149 | + Cli.printf("Turning LED OFF.\n"); |
| 150 | + digitalWrite(COMMAND_LED, LOW); |
| 151 | + } |
| 152 | + else |
| 153 | + invalid_argv = true; |
| 154 | + } |
| 155 | + |
| 156 | + if(invalid_argv) |
| 157 | + Cli.printf("led command needs \"on\" or \"off\" arg.\n"); |
| 158 | + |
| 159 | + Cli.printf("\n"); |
| 160 | +} |
| 161 | + |
| 162 | +void cmd_version(int argc, char* argv[]) |
| 163 | +{ |
| 164 | + Cli.printf("FW App Version: %s\n", FW_APP_VERSION); |
| 165 | +} |
0 commit comments