The next example configures two commands to control an LED, this example is a basic command implementation, here just the command string is needed to execute and control all the logic in the command function.
In this example, only the CLI_command.c
file is included, use the main.c
provided on the CLI page.
Code Example:
#include "CLI_command.h"
/* command definition */
static const CLI_Command_Definition_t LED_On = {
"TurnOn", /* Command string */
"TurnOn: Turn on the LED", /* Details of command */
LED_Turn_On, /* Function to execute with command */
0 /* no parameters are expected */
};
static const CLI_Command_Definition_t LED_Off = {
"TurnOff", /* Command string */
"TurnOff: Turn off the LED", /* Details of command */
LED_Turn_Off, /* Function to execute with command */
0 /* no parameters are expected */
};
/* Command Functions */
BaseType_t LED_Turn_On(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, SET); /* Turn On the LED */
strncpy( pcWriteBuffer, "LED ON", xWriteBufferLen ); /* Print a message to indicate LED ON */
return pdFALSE; /* Ends before execution */
}
BaseType_t LED_Turn_Off(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, RESET); /* Turn Off the LED */
strncpy( pcWriteBuffer, "LED OFF", xWriteBufferLen ); /* Print a message to indicate LED OFF */
return pdFALSE; /* Ends before execution */
}
/* Function to register all the commands */
void vRegisterCLICommands(void)
{
FreeRTOS_CLIRegisterCommand( &LED_On ); /* Register the command "LED_On" */
FreeRTOS_CLIRegisterCommand( &LED_Off ); /* Register the command "LED_Off" */
}
This application configures 2 basic commands, this commands are configured to be executed without expecting any argument. Observe the command functions, this just going to print a message on the terminal and control just a configured LED, this application is not designed to control any pin, command logic just allows to control the predefined pin. This type of commands are useful in situations where just print information is needed or to execute predefined actions in the system.
CLI application:
The application starts with the welcome message, to after writing in the terminal the "help" command to print the list of the commands registered. Observe that the command prints multiple lines including the string commands and a brief explanation of each one. Notice that is the data written in the "CLI_Command_Definition_t" structure. |
|
First, the "TurnOn" command is typed. This command is just designed to control and print a message in the terminal to indicate that an established LED going to switch to a high state. |
|
Next, the "TurnOff" command is typed and executed, printing the message indicating the LED state, and also turning off the LED. |