The third argument (blinking frequency) is used directly in the calculation and not just to to define what function to call. The cmd_led() function is the only one that uses three arguments. Type \"help led\" to see how to use the LED command.") Line_string = Serial.readStringUntil("\n") The memset functions reset the line string and the args list to zero. execute() – this calls the correct function based on the user’s input.parse_line() – this divides the input into arguments and stores them in the args list.read_line() – this waits for input from the user and stores it in the line string. Three important functions are called within my_cli(). Memset(args, 0, sizeof(args) * MAX_NUM_ARGS * ARG_BUF_SIZE) Serial.println("Type \"help\" to see a list of commands.") Serial.println("Welcome to this simple Arduino command line interface (CLI).") The cli_init() function which is called in setup() just displays a short welcome message while my_cli() is where the the magic starts to happen. When the user enters a command, the program will compare it to the strings in commands_str and call the function in commands_func with the same index. Int num_commands = sizeof(commands_str) / sizeof(char *) List of functions pointers corresponding to each command Int blink_cycles = 10 //How many times the LED will blink #define MAX_NUM_ARGS 8 //Maximum number of arguments #define ARG_BUF_SIZE 64 //Maximum argument string length #define LINE_BUF_SIZE 128 //Maximum input string length Let’s start with the global variables, defines and the setup() and loop() functions. The code itself is just over 200 lines where many are just pure printing functions, so the core functionality is not that complex. We’ve also included a help command which explains what each command does as well as an exit command which just puts the program in a while(1) state. You can either turn it on, off or having it blink 10 times at a given frequency. The primary purpose of this specific code is to control the on-board LED tied to pin 13 on the Arduino. But first we’ll briefly explain what our program does. We’re basically going to go quickly go through the code in chunks in this post. The purpose of a CLI like this on an Arduino is to have the ability to send text commands to it during runtime to execute certain tasks, such as controlling a servo, displaying text on a screen or launching a rocket. What our CLI looks like in Arduino’s serial monitor. This means that several tasks can’t run simultaneously and that one task has to finish before starting a new one. There are ways around this, but we’ve kept everything sequential for simplicity’s sake in this example. Having a CLI on an embedded system like the one in this blog post is quite different from a shell in Linux for instance since you usually don’t have an OS to back you up with multiprocessing and all that jazz. We recommend you read Brennan’s tutorial regardless since it contains a lot of important info about this topic and programming in general which we won’t mention here. CLI) for the Arduino than what we did in the serial input post. We briefly touched upon this topic in our post about Arduino serial input, but inspired by this excellent tutorial by Stephen Brennan we thought it would be fun to create a more rigid command line interface (aka.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |