如何在FreeRTOS-Plus-CLI中添加自己的命令行
最近在做一个有关大气TVOC在线监测的项目,当时由于项目比较急没有来得及总结,现将在软件编程中用到的CLI命令如何添加这个知识点简单总结一下,简单的说,如何在FreeRTOS中添加CLI命令大概分为三个步骤(这里以如何通过CLI命令设置系统的时间为例来说明):
1、添加自己的数据结构,实际范例如下所示:
static const CLI_Command_Definition_t xSetting =
{
( const int8_t * ) "set", /* The command string to type. */
( const int8_t * ) "set [address] [data]: allow you to set some parameter\r\n\
[address]: rtc_year/rtc_month/rtc_day/rtc_hour/rtc_minute\r\n\
[data] : the data send to [address]",
prvSetting, /* The function to run. */
2 /* No parameters are expected. */
};
2、实现上面的数据结构后,编写数据结构中的回调函数,实际范例如下所示:
static long prvSetting(int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString)
{
static long xParameterNumber = 1;
long xParameterStringLength;
char *pcParameterString;
char tempCharaddress[40] = "";
char tempChardata[20] = "";
uint8_t temp;
( void ) pcCommandString;
( void ) xWriteBufferLen;
configASSERT( pcWriteBuffer );
memset( pcWriteBuffer, 0x00, xWriteBufferLen );
/* Obtain the parameter string. */
pcParameterString = ( char * ) FreeRTOS_CLIGetParameter
(
pcCommandString, /* The command string itself. */
xParameterNumber, /* Return the next parameter. */
&xParameterStringLength /* Store the parameter string length. */
);
if(NULL == pcParameterString )
{
return pdFALSE;
}
strncpy(tempCharaddress,pcParameterString,xParameterStringLength);//get first parameter
xParameterNumber = 2;
pcParameterString = ( char * ) FreeRTOS_CLIGetParameter
(
pcCommandString, /* The command string itself. */
xParameterNumber, /* Return the next parameter. */
&xParameterStringLength /* Store the parameter string length. */
);
if(NULL == pcParameterString )
{
return pdFALSE;
}
strncpy(tempChardata,pcParameterString,xParameterStringLength);//get second parameter
if(0 == WhetherAllNumber(pcParameterString,xParameterStringLength))
{
sprintf((char*)pcWriteBuffer, "Parameter wrong.\r\n");
xParameterNumber = 1;
return pdFALSE;
}
Set_RTC_TimeStructure.RTC_Seconds = RTC_TimeStructure.RTC_Seconds;
Set_RTC_TimeStructure.RTC_Minutes = RTC_TimeStructure.RTC_Minutes;
Set_RTC_TimeStructure.RTC_Hours = RTC_TimeStructure.RTC_Hours;
Set_RTC_DateStructure.RTC_Date = RTC_DateStructure.RTC_Date;
Set_RTC_DateStructure.RTC_Month = RTC_DateStructure.RTC_Month;
Set_RTC_DateStructure.RTC_Year = RTC_DateStructure.RTC_Year;
if(0 == strcmp("rtc_year",tempCharaddress))
{
Set_RTC_DateStructure.RTC_Year = atoi(tempChardata);
}
else if(0 == strcmp("rtc_month",tempCharaddress))
{
Set_RTC_DateStructure.RTC_Month = atoi(tempChardata);
}
else if(0 == strcmp("rtc_day",tempCharaddress))
{
Set_RTC_DateStructure.RTC_Date = atoi(tempChardata);
}
else if(0 == strcmp("rtc_hour",tempCharaddress))
{
Set_RTC_TimeStructure.RTC_Hours = atoi(tempChardata);
}
else if(0 == strcmp("rtc_minute",tempCharaddress))
{
Set_RTC_TimeStructure.RTC_Minutes = atoi(tempChardata);
}
RTC_TimeStructure.RTC_Seconds = Set_RTC_TimeStructure.RTC_Seconds;
RTC_TimeStructure.RTC_Minutes = Set_RTC_TimeStructure.RTC_Minutes;
RTC_TimeStructure.RTC_Hours = Set_RTC_TimeStructure.RTC_Hours;
RTC_DateStructure.RTC_Date = Set_RTC_DateStructure.RTC_Date;
RTC_DateStructure.RTC_Month = Set_RTC_DateStructure.RTC_Month;
RTC_DateStructure.RTC_Year = Set_RTC_DateStructure.RTC_Year;
RTC_TimeRegulate();
xParameterNumber = 1;
return pdFALSE;
}
3、注册数据结构,实际范例如下所示:
CLIRegisterSingleCommand( &xSetting);
有关以上三个函数的参数说明及更多的CIL命令实现例程可参照FreeRTOS官网,网址为:
https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_CLI/FreeRTOS_Plus_CLI_Registering_A_Command.shtml