BetaFlight模块设计之三十:Cli模块分析

  • Cli模块
  • Cli接口
  • Cli框架
  • Cli命令结构
  • 主要函数分析
    • cliProcess函数
    • processCharacterInteractive函数
    • processCharacter函数
  • Cli命令汇总
    • Cli串口help命令
    • Cli内部命令列表

基于BetaFlight开源代码框架简介的框架设计,逐步分析内部模块功能设计。

Cli模块

描述:Cli模块主要用于处理Cli命令,并根据命令显示系统状态或系统配置参数修改。

src/main/cli/
├── cli.c
├── cli_debug_print.h
├── cli.h
├── settings.c
└── settings.h0 directories, 5 files

注:该模块主要支持BetaFlight模块设计之二:SERIAL任务分析。

Cli接口

extern bool cliMode;                             //判断系统处于Cli模式void cliProcess(void);                           //处理Cli命令
void cliEnter(struct serialPort_s *serialPort);  //进入Cli模式
bool resetConfigToCustomDefaults(void);          //支持命令行“defaults”命令
bool hasCustomDefaults(void);                    //支持命令行“defaults”命令

Cli框架

  1. cliMode:判断当前Cli模块状态(TRUE为Cli模式;反之则不在该模式)
  2. cliEnter:触发Cli模块进入工作模式
  3. cliProcess:处理Cli命令
  4. cliExit:“CTRL + D”,退出Cli模式 //这个命令是内嵌在CliProcess函数内部

Cli命令结构

一个Cli命令通过name(命令名字),description(命令描述),args(命令参数表),cliCommand(命令函数)组成。

typedef void cliCommandFn(const char* name, char *cmdline);typedef struct {const char *name;
#ifndef MINIMAL_CLIconst char *description;const char *args;
#endifcliCommandFn *cliCommand;
} clicmd_t;

例如:help命令

CLI_COMMAND_DEF("help", "display command help", "[search string]", cliHelp),

主要函数分析

cliProcess函数

cliProcess├──> <!cliWriter>  //Cli模式下处理自定义命令或者尚未进入Cli模式,直接返回│   └──> return├──> cliWriterFlush();└──> <serialRxBytesWaiting(cliPort)>  //非阻塞式数据等待├──> c = serialRead(cliPort);└──> processCharacterInteractive(c); //交互式串口字节处理

processCharacterInteractive函数

processCharacterInteractive├──> <c == '\t' || c == '?'> //实际在配置软件里面tab可以,?无法使用│   ├──> [遍历cmdTable,匹配相近的命令(pstart, pend)]│   ├──> [如果只有一个命令,尝试补全命令]│   └──> [如果多个匹配命令,反馈命令列表]├──> <!bufferIndex && c == 4> // CTRL-D│   ├──> cliExit("", cliBuffer); //退出Cli模式│   └──> return├──> <c == 12>  // NewPage(CTRL-L) 清屏│   ├──> cliPrint("\033[2J\033[1;1H");│   └──> cliPrompt();├──> <c == 127><bufferIndex>  //回退一个字符│   ├──> cliBuffer[--bufferIndex] = 0;│   └──> cliPrint("\010 \010");└──> <other scenarios>└──> processCharacter(c);

注:ASCII表,详见链接

processCharacter函数

processCharacter├──> <bufferIndex && (c == '\n' || c == '\r')>│   ├──> cliPrintLinefeed();│   ├──> <processingCustomDefaults>  //定制命令处理场景,配合cliPrompt打印│   │   └──> cliPrint("d: ");│   ├──> [Strip comment starting with # from line]  //注释行屏蔽│   ├──> [Strip trailing whitespace] //尾部空格屏蔽│   ├──> <bufferIndex > 0)>  // Process non-empty lines│   │   ├──> [遍历列表寻找命令index]│   │   └──> [执行index命令指定的命令函数执行,异常反馈错误类型]│   ├──> memset(cliBuffer, 0, sizeof(cliBuffer));│   ├──> <!cliMode>│   │   └──> return;│   └──> cliPrompt();└──> <bufferIndex < sizeof(cliBuffer) && c >= 32 && c <= 126>├──> <!bufferIndex && c == ' '>│   └──> return // 最前面的空格忽略,直接丢弃├──> cliBuffer[bufferIndex++] = c;  //符合条件的字符保存下来,供后续处理使用└──> cliWrite(c);

Cli命令汇总

注:主要介绍BetaFlight4.3版本

Cli串口help命令

# help
adjrange - configure adjustment ranges<index> <unused> <range channel> <start> <end> <function> <select channel> [<center> <scale>]
aux - configure modes<index> <mode> <aux> <start> <end> <logic>
batch - start or end a batch of commandsstart | end
beacon - enable/disable Dshot beacon for a conditionlist<->[name]
beeper - enable/disable beeper for a conditionlist<->[name]
bind_rx - initiate binding for RX SPI or SRXL2
bl - reboot into bootloader[rom]
board_name - get / set the name of the board model[board name]
color - configure colors
defaults - reset to defaults and reboot[nosave|bare|show]
diff - list configuration changes from default[master|profile|rates|hardware|all] {defaults|bare}
dma - show/set DMA assignments<> | <device> <index> list | <device> <index> [<option>|none] | list | show
dshot_telemetry_info - display dshot telemetry info and stats
dshotprog - program DShot ESC(s)<index> <command>+
dump - dump configuration[master|profile|rates|hardware|all] {defaults|bare}
exit
feature - configure featureslist<->[name]
flash_erase - erase flash chip
flash_info - show flash chip info
flash_read<length> <address>
flash_scan - scan flash device for errors
flash_write<address> <message>
get - get variable value[name]
gpspassthrough - passthrough gps to serial
gyroregisters - dump gyro config registers contents
help - display command help[search string]
led - configure leds
manufacturer_id - get / set the id of the board manufacturer[manufacturer id]
map - configure rc channel order[<map>]
mcu_id - id of the microcontroller
mixer - configure mixerlist<name>
mmix - custom motor mixer
mode_color - configure mode and special colors
motor - get/set motor<index> [<value>]
msc - switch into msc mode[<timezone offset minutes>]
play_sound[<index>]
profile - change profile[<index>]
rateprofile - change rate profile[<index>]
rc_smoothing_info - show rc_smoothing operational settings
resource - show/set resources<> | <resource name> <index> [<pin>|none] | show [all]
rxfail - show/set rx failsafe settings
rxrange - configure rx channel ranges
save - save and reboot
sd_info - sdcard info
serial - configure serial ports
serialpassthrough - passthrough serial data data from port 1 to VCP / port 2<id1> [<baud1>] [<mode1>] [none|<dtr pinio>|reset] [<id2>] [<baud2>] [<mode2>]
servo - configure servos
set - change setting[<name>=<value>]
signature - get / set the board type signature[signature]
smix - servo mixer<rule> <servo> <source> <rate> <speed> <min> <max> <box>resetload <mixer>reverse <servo> <source> r|n
status - show status
tasks - show task stats
timer - show/set timers<> | <pin> list | <pin> [af<alternate function>|none|<option(deprecated)>] | list | show
version - show version
vtx - vtx channels on switch<index> <aux_channel> <vtx_band> <vtx_channel> <vtx_power> <start_range> <end_range>
vtx_info - vtx power config dump
vtxtable - vtx frequency table<band> <bandname> <bandletter> [FACTORY|CUSTOM] <freq> ... <freq>

Cli内部命令列表

const clicmd_t cmdTable[] = {CLI_COMMAND_DEF("adjrange", "configure adjustment ranges", "<index> <unused> <range channel> <start> <end> <function> <select channel> [<center> <scale>]", cliAdjustmentRange),CLI_COMMAND_DEF("aux", "configure modes", "<index> <mode> <aux> <start> <end> <logic>", cliAux),
#ifdef USE_CLI_BATCHCLI_COMMAND_DEF("batch", "start or end a batch of commands", "start | end", cliBatch),
#endif
#if defined(USE_BEEPER)
#if defined(USE_DSHOT)CLI_COMMAND_DEF("beacon", "enable/disable Dshot beacon for a condition", "list\r\n""\t<->[name]", cliBeacon),
#endifCLI_COMMAND_DEF("beeper", "enable/disable beeper for a condition", "list\r\n""\t<->[name]", cliBeeper),
#endif // USE_BEEPER
#if defined(USE_RX_BIND)CLI_COMMAND_DEF("bind_rx", "initiate binding for RX SPI or SRXL2", NULL, cliRxBind),
#endif
#if defined(USE_FLASH_BOOT_LOADER)CLI_COMMAND_DEF("bl", "reboot into bootloader", "[flash|rom]", cliBootloader),
#elseCLI_COMMAND_DEF("bl", "reboot into bootloader", "[rom]", cliBootloader),
#endif
#if defined(USE_BOARD_INFO)CLI_COMMAND_DEF("board_name", "get / set the name of the board model", "[board name]", cliBoardName),
#endif
#ifdef USE_LED_STRIP_STATUS_MODECLI_COMMAND_DEF("color", "configure colors", NULL, cliColor),
#endif
#if defined(USE_CUSTOM_DEFAULTS)CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "{show} {nosave} {bare} {group_id <id>}", cliDefaults),
#elseCLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "{nosave}", cliDefaults),
#endifCLI_COMMAND_DEF("diff", "list configuration changes from default", "[master|profile|rates|hardware|all] {defaults|bare}", cliDiff),
#ifdef USE_RESOURCE_MGMT#ifdef USE_DMA
#ifdef USE_DMA_SPECCLI_COMMAND_DEF("dma", "show/set DMA assignments", "<> | <device> <index> list | <device> <index> [<option>|none] | list | show", cliDma),
#elseCLI_COMMAND_DEF("dma", "show DMA assignments", "show", cliDma),
#endif
#endif#endif
#ifdef USE_DSHOT_TELEMETRYCLI_COMMAND_DEF("dshot_telemetry_info", "display dshot telemetry info and stats", NULL, cliDshotTelemetryInfo),
#endif
#ifdef USE_DSHOTCLI_COMMAND_DEF("dshotprog", "program DShot ESC(s)", "<index> <command>+", cliDshotProg),
#endifCLI_COMMAND_DEF("dump", "dump configuration","[master|profile|rates|hardware|all] {defaults|bare}", cliDump),
#ifdef USE_ESCSERIALCLI_COMMAND_DEF("escprog", "passthrough esc to serial", "<mode [sk/bl/ki/cc]> <index>", cliEscPassthrough),
#endifCLI_COMMAND_DEF("exit", NULL, NULL, cliExit),CLI_COMMAND_DEF("feature", "configure features","list\r\n""\t<->[name]", cliFeature),
#ifdef USE_FLASHFSCLI_COMMAND_DEF("flash_erase", "erase flash chip", NULL, cliFlashErase),CLI_COMMAND_DEF("flash_info", "show flash chip info", NULL, cliFlashInfo),
#ifdef USE_FLASH_TOOLSCLI_COMMAND_DEF("flash_read", NULL, "<length> <address>", cliFlashRead),CLI_COMMAND_DEF("flash_scan", "scan flash device for errors", NULL, cliFlashVerify),CLI_COMMAND_DEF("flash_write", NULL, "<address> <message>", cliFlashWrite),
#endif
#endifCLI_COMMAND_DEF("get", "get variable value", "[name]", cliGet),
#ifdef USE_GPSCLI_COMMAND_DEF("gpspassthrough", "passthrough gps to serial", NULL, cliGpsPassthrough),
#endif
#if defined(USE_GYRO_REGISTER_DUMP) && !defined(SIMULATOR_BUILD)CLI_COMMAND_DEF("gyroregisters", "dump gyro config registers contents", NULL, cliDumpGyroRegisters),
#endifCLI_COMMAND_DEF("help", "display command help", "[search string]", cliHelp),
#ifdef USE_LED_STRIP_STATUS_MODECLI_COMMAND_DEF("led", "configure leds", NULL, cliLed),
#endif
#if defined(USE_BOARD_INFO)CLI_COMMAND_DEF("manufacturer_id", "get / set the id of the board manufacturer", "[manufacturer id]", cliManufacturerId),
#endifCLI_COMMAND_DEF("map", "configure rc channel order", "[<map>]", cliMap),CLI_COMMAND_DEF("mcu_id", "id of the microcontroller", NULL, cliMcuId),
#ifndef USE_QUAD_MIXER_ONLYCLI_COMMAND_DEF("mixer", "configure mixer", "list\r\n\t<name>", cliMixer),
#endifCLI_COMMAND_DEF("mmix", "custom motor mixer", NULL, cliMotorMix),
#ifdef USE_LED_STRIP_STATUS_MODECLI_COMMAND_DEF("mode_color", "configure mode and special colors", NULL, cliModeColor),
#endifCLI_COMMAND_DEF("motor",  "get/set motor", "<index> [<value>]", cliMotor),
#ifdef USE_USB_MSC
#ifdef USE_RTC_TIMECLI_COMMAND_DEF("msc", "switch into msc mode", "[<timezone offset minutes>]", cliMsc),
#elseCLI_COMMAND_DEF("msc", "switch into msc mode", NULL, cliMsc),
#endif
#endif
#ifndef MINIMAL_CLICLI_COMMAND_DEF("play_sound", NULL, "[<index>]", cliPlaySound),
#endifCLI_COMMAND_DEF("profile", "change profile", "[<index>]", cliProfile),CLI_COMMAND_DEF("rateprofile", "change rate profile", "[<index>]", cliRateProfile),
#ifdef USE_RC_SMOOTHING_FILTERCLI_COMMAND_DEF("rc_smoothing_info", "show rc_smoothing operational settings", NULL, cliRcSmoothing),
#endif // USE_RC_SMOOTHING_FILTER
#ifdef USE_RESOURCE_MGMTCLI_COMMAND_DEF("resource", "show/set resources", "<> | <resource name> <index> [<pin>|none] | show [all]", cliResource),
#endifCLI_COMMAND_DEF("rxfail", "show/set rx failsafe settings", NULL, cliRxFailsafe),CLI_COMMAND_DEF("rxrange", "configure rx channel ranges", NULL, cliRxRange),CLI_COMMAND_DEF("save", "save and reboot", NULL, cliSave),
#ifdef USE_SDCARDCLI_COMMAND_DEF("sd_info", "sdcard info", NULL, cliSdInfo),
#endifCLI_COMMAND_DEF("serial", "configure serial ports", NULL, cliSerial),
#if defined(USE_SERIAL_PASSTHROUGH)
#if defined(USE_PINIO)CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data data from port 1 to VCP / port 2", "<id1> [<baud1>] [<mode1>] [none|<dtr pinio>|reset] [<id2>] [<baud2>] [<mode2>]", cliSerialPassthrough),
#elseCLI_COMMAND_DEF("serialpassthrough", "passthrough serial data from port 1 to VCP / port 2", "<id1> [<baud1>] [<mode1>] [none|reset] [<id2>] [<baud2>] [<mode2>]", cliSerialPassthrough),
#endif
#endif
#ifdef USE_SERVOSCLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo),
#endifCLI_COMMAND_DEF("set", "change setting", "[<name>=<value>]", cliSet),
#if defined(USE_SIGNATURE)CLI_COMMAND_DEF("signature", "get / set the board type signature", "[signature]", cliSignature),
#endif
#if defined(USE_SIMPLIFIED_TUNING)CLI_COMMAND_DEF("simplified_tuning", "applies or disables simplified tuning", "apply | disable", cliSimplifiedTuning),
#endif
#ifdef USE_SERVOSCLI_COMMAND_DEF("smix", "servo mixer", "<rule> <servo> <source> <rate> <speed> <min> <max> <box>\r\n""\treset\r\n""\tload <mixer>\r\n""\treverse <servo> <source> r|n", cliServoMix),
#endifCLI_COMMAND_DEF("status", "show status", NULL, cliStatus),CLI_COMMAND_DEF("tasks", "show task stats", NULL, cliTasks),
#ifdef USE_TIMER_MGMTCLI_COMMAND_DEF("timer", "show/set timers", "<> | <pin> list | <pin> [af<alternate function>|none|<option(deprecated)>] | list | show", cliTimer),
#endifCLI_COMMAND_DEF("version", "show version", NULL, cliVersion),
#ifdef USE_VTX_CONTROL
#ifdef MINIMAL_CLICLI_COMMAND_DEF("vtx", "vtx channels on switch", NULL, cliVtx),
#elseCLI_COMMAND_DEF("vtx", "vtx channels on switch", "<index> <aux_channel> <vtx_band> <vtx_channel> <vtx_power> <start_range> <end_range>", cliVtx),
#endif
#endif
#ifdef USE_VTX_TABLECLI_COMMAND_DEF("vtx_info", "vtx power config dump", NULL, cliVtxInfo),CLI_COMMAND_DEF("vtxtable", "vtx frequency table", "<band> <bandname> <bandletter> [FACTORY|CUSTOM] <freq> ... <freq>\r\n", cliVtxTable),
#endif
};

BetaFlight模块设计之三十:Cli模块分析相关推荐

  1. BetaFlight模块设计之三十五:RSSI信号强度链路稳定性分析

    BetaFlight模块设计之三十五:RSSI信号强度&链路稳定性分析 1. RSSI信号强度 1.1 RSSI Value 1.2 RSSI dBm Value 2. 链路稳定性 3. RS ...

  2. BetaFlight模块设计之三十二:MSP协议模块分析

    BetaFlight模块设计之三十二:MSP协议模块分析 1. MSP协议模块 1.1 MSP描述 1.2 MSP版本优缺点 1.3 MSP代码资源 2. MSP报文解析 2.1 MSP收包状态机 2 ...

  3. BetaFlight模块设计之三十四:OSD模块分析

    BetaFlight模块设计之三十四:OSD模块分析 1. OSD模块 1.1 osd状态机子模块 1.2 osd_warnings检查子模块 1.3 osd_elements子模块 2. OSD设备 ...

  4. PX4模块设计之三十五:MulticopterAttitudeControl模块

    PX4模块设计之三十五:MulticopterAttitudeControl模块 1. MulticopterAttitudeControl模块简介 2. 模块入口函数 2.1 主入口mc_att_c ...

  5. PX4模块设计之三十八:Navigator模块

    PX4模块设计之三十八:Navigator模块 1. Navigator模块简介 2. 模块入口函数 2.1 主入口navigator_main 2.2 自定义子命令custom_command 3. ...

  6. PX4模块设计之三十四:ControlAllocator模块

    PX4模块设计之三十四:ControlAllocator模块 1. ControlAllocator模块简介 2. 模块入口函数 2.1 主入口control_allocator_main 2.2 自 ...

  7. PX4模块设计之四十四: bmp280模块

    int bmp280_main(int argc, char *argv[]); PX4模块设计之四十四: bmp280模块 1. bmp280模块简介 2. 模块入口函数 2.1 主入口bmp280 ...

  8. PX4模块设计之三十一:ManualControl模块

    PX4模块设计之三十一:ManualControl模块 1. ManualControl模块简介 2. 模块入口函数 2.1 主入口manual_control_main 2.2 自定义子命令cust ...

  9. PX4模块设计之三十三:Sensors模块

    PX4模块设计之三十三:Sensors模块 1. Sensors模块简介 2. 模块入口函数 2.1 主入口sensors_main 2.2 自定义子命令custom_command 2.3 模块状态 ...

最新文章

  1. 详解物联网的中间件:为何说它是不可或缺的?
  2. 驾照考试(科目三-大路)
  3. C++对象模型探索 / 子类的内存布局
  4. 6工程文件夹作用_MCUXpresso IDE下SDK工程导入与workspace管理机制
  5. Log保存文件-Android
  6. .NET Core 中生成验证码
  7. 八类网线和七类网线的区别_什么是七类网线?七类网线水晶头如何制作?
  8. MySQL 8.0 error 2059: Authentication plugin 'caching_sha2_password' cannot be loaded
  9. javascript中为某个对象(控件)绑定事件的几种方法
  10. OpenShift 4 之 GitOps(7)用ArgoCD部署Pacman应用集群
  11. MySQL的DDL、DML、DCL、TCL什么意思?
  12. 翻牌游戏如何打乱牌面java_如何游戏4Bet底池?
  13. oracle_分区表的新增、修改、删除、合并。普通表转分区表方法
  14. 据说:一个线程性能相当于30%核心
  15. 一淘商品类目预测[转载]
  16. iOS平台上aa(见缝插针)游戏的简易实现
  17. Java中字节流和字符流的read()方法为什么返回的值是int类型
  18. 【linux】什么是栈回溯
  19. 计算机控制电机启动接线图,电机控制线路图大全(上下)
  20. 【NOI2015】小园丁与老司机

热门文章

  1. 6大中文分词工具测试比较
  2. 2022年焊工考试电焊工考试模拟试题卷及答案
  3. 网站安全检测之信息收集类工具
  4. mysql安装过程中root口令_MySQL5.7安装过程并重置root密码的方法(shell 脚本)
  5. 华为云桌面公有云版正式商用背后的五大黑科技
  6. linux的多进程等待,等待进程结束wait()和waitpid()函数
  7. 学人工智能必看!具体职业细分
  8. 监控建设05dingtalk搭建
  9. Win10系统出现edge主页被劫持篡改的问题
  10. 带id的市和区/县 json数据