Now in this example, the recursive command is configured, it is useful when the application requires multiple executions of the same command without calling the command in a terminal, also is used when multiple lines need to be printed, as the help
command.
This example uses two commands to switch the LED state, when a command is executed will change the LED state of a serie of LEDs by calling just the command a time in the terminal.
Example Code:
#include "CLI_command.h"
static const uint32_t Pin[] = { GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_4, GPIO_PIN_5, GPIO_PIN_6, GPIO_PIN_7 }; /* Variable that stores each pin information */
/* command definitions */
static const CLI_Command_Definition_t Serie_On = {
"SerieOn", /* Command string */
"SerieOn: Set to high the serie of leds", /* Details of command */
Serie_ON, /* Function to execute with command */
0 /* No parameters are expected */
};
static const CLI_Command_Definition_t Serie_Off = {
"SerieOff", /* Command string */
"SerieOff: Set to low the serie of leds", /* Details of command */
Serie_OFF, /* Function to execute with command */
0 /* No parameters are expected */
};
/* Command Functions */
BaseType_t Serie_ON(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
BaseType_t ReturnValue = pdFALSE; /* Return variable to command execution */
static uint8_t LED_Num = 0; /* Number of led to turn on */
if( LED_Num <= 7 ) /* Check if all the leds are turned ON */
{
HAL_GPIO_WritePin( GPIOC, Pin[LED_Num], SET ); /* Set a high value in the Pin LED */
LED_Num++; /* Increment to pass to the next LED */
strncpy( pcWriteBuffer, "LED ON", xWriteBufferLen ); /* Print a message on Terminal */
HAL_Delay( 300 ); /* Delay to observe the change */
ReturnValue = pdTRUE; /* Execute again the command function */
}
else /* If all the LEDs are processed */
{
LED_Num = 0; /* Reset LED counter */
ReturnValue = pdFALSE; /* Finish Command execution */
}
return ReturnValue; /* Return the command execution */
}
BaseType_t Serie_OFF(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
BaseType_t ReturnValue = pdFALSE; /* Return variable to command execution */
static uint8_t LED_Num = 0; /* Number of led to turn on */
if( LED_Num <= 7 ) /* Check if all the leds are turned OFF */
{
HAL_GPIO_WritePin( GPIOC, Pin[LED_Num], RESET ); /* Set a low value in the Pin LED */
LED_Num++; /* Increment the LED counter */
strncpy( pcWriteBuffer, "LED OFF", xWriteBufferLen ); /* Print a message on terminal */
HAL_Delay( 300 ); /* Delay to observe the change */
ReturnValue = pdTRUE; /* Execute again the command function */
}
else /* If all the LEDs are processed */
{
LED_Num = 0; /* Reset the LED counter */
ReturnValue = pdFALSE; /* Finish command execution */
}
return ReturnValue; /* Return the command execution */
}
/* Register the command */
void vRegisterCLICommands(void)
{
FreeRTOS_CLIRegisterCommand( &Serie_On ); /* Register the command "SerieOn" */
FreeRTOS_CLIRegisterCommand( &Serie_Off ); /* Register the command "SerieOff" */
}
Observe the command functions, now a variable ReturnValue
is used, this variable helps to execute again the command multiple times if the return value is pdTRUE
enter in a loop execution until the value changes to pdFALSE
, in the command function is used to switch the LED state of each LED returning each time the pdTRUE
value until the Serie ends, returning now the pdFALSE
value to reset the led count to start again the next execution.
CLI application:
First, the help command is executed to ensure that the commands are correctly registered. |
|
Next, the commands are executed, notice that in the first command, the LEDs are turned on. The message is printed to be more visual in the terminal of each led execution.
|
|
Finally, the command to turn off the LEDs works similarly to the first command but this time just to set a low state on each pin LED.
|