Fixed a bug which caused printing of floats larger than 100 in the CLI to crash
[betaflight.git] / src / main / io / serial_cli.c
blob8f009328f4ef9546d903e67dac8401359f71ad6c
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <math.h>
24 #include <ctype.h>
26 #include "platform.h"
27 #include "scheduler.h"
28 #include "version.h"
30 #include "build_config.h"
32 #include "common/axis.h"
33 #include "common/maths.h"
34 #include "common/color.h"
35 #include "common/typeconversion.h"
37 #include "drivers/system.h"
39 #include "drivers/sensor.h"
40 #include "drivers/accgyro.h"
41 #include "drivers/compass.h"
43 #include "drivers/serial.h"
44 #include "drivers/bus_i2c.h"
45 #include "drivers/gpio.h"
46 #include "drivers/timer.h"
47 #include "drivers/pwm_rx.h"
48 #include "drivers/sdcard.h"
49 #include "drivers/gyro_sync.h"
51 #include "drivers/buf_writer.h"
53 #include "io/escservo.h"
54 #include "io/gps.h"
55 #include "io/gimbal.h"
56 #include "io/rc_controls.h"
57 #include "io/serial.h"
58 #include "io/ledstrip.h"
59 #include "io/flashfs.h"
60 #include "io/beeper.h"
61 #include "io/asyncfatfs/asyncfatfs.h"
63 #include "rx/rx.h"
64 #include "rx/spektrum.h"
66 #include "sensors/battery.h"
67 #include "sensors/boardalignment.h"
68 #include "sensors/sensors.h"
69 #include "sensors/acceleration.h"
70 #include "sensors/gyro.h"
71 #include "sensors/compass.h"
72 #include "sensors/barometer.h"
74 #include "flight/pid.h"
75 #include "flight/imu.h"
76 #include "flight/mixer.h"
77 #include "flight/navigation.h"
78 #include "flight/failsafe.h"
80 #include "telemetry/telemetry.h"
81 #include "telemetry/frsky.h"
83 #include "config/runtime_config.h"
84 #include "config/config.h"
85 #include "config/config_profile.h"
86 #include "config/config_master.h"
88 #include "common/printf.h"
90 #include "serial_cli.h"
92 // FIXME remove this for targets that don't need a CLI. Perhaps use a no-op macro when USE_CLI is not enabled
93 // signal that we're in cli mode
94 uint8_t cliMode = 0;
96 #ifdef USE_CLI
98 extern uint16_t cycleTime; // FIXME dependency on mw.c
100 void gpsEnablePassthrough(serialPort_t *gpsPassthroughPort);
102 static serialPort_t *cliPort;
103 static bufWriter_t *cliWriter;
104 static uint8_t cliWriteBuffer[sizeof(*cliWriter) + 16];
106 static void cliAux(char *cmdline);
107 static void cliRxFail(char *cmdline);
108 static void cliAdjustmentRange(char *cmdline);
109 static void cliMotorMix(char *cmdline);
110 static void cliDefaults(char *cmdline);
111 static void cliDump(char *cmdLine);
112 void cliDumpProfile(uint8_t profileIndex);
113 void cliDumpRateProfile(uint8_t rateProfileIndex) ;
114 static void cliExit(char *cmdline);
115 static void cliFeature(char *cmdline);
116 static void cliMotor(char *cmdline);
117 static void cliPlaySound(char *cmdline);
118 static void cliProfile(char *cmdline);
119 static void cliRateProfile(char *cmdline);
120 static void cliReboot(void);
121 static void cliSave(char *cmdline);
122 static void cliSerial(char *cmdline);
123 #ifndef SKIP_SERIAL_PASSTHROUGH
124 static void cliSerialPassthrough(char *cmdline);
125 #endif
127 #ifdef USE_SERVOS
128 static void cliServo(char *cmdline);
129 static void cliServoMix(char *cmdline);
130 #endif
132 static void cliSet(char *cmdline);
133 static void cliGet(char *cmdline);
134 static void cliStatus(char *cmdline);
135 #ifndef SKIP_TASK_STATISTICS
136 static void cliTasks(char *cmdline);
137 #endif
138 static void cliVersion(char *cmdline);
139 static void cliRxRange(char *cmdline);
141 #ifdef GPS
142 static void cliGpsPassthrough(char *cmdline);
143 #endif
145 static void cliHelp(char *cmdline);
146 static void cliMap(char *cmdline);
148 #ifdef LED_STRIP
149 static void cliLed(char *cmdline);
150 static void cliColor(char *cmdline);
151 #endif
153 #ifndef USE_QUAD_MIXER_ONLY
154 static void cliMixer(char *cmdline);
155 #endif
157 #ifdef USE_FLASHFS
158 static void cliFlashInfo(char *cmdline);
159 static void cliFlashErase(char *cmdline);
160 #ifdef USE_FLASH_TOOLS
161 static void cliFlashWrite(char *cmdline);
162 static void cliFlashRead(char *cmdline);
163 #endif
164 #endif
166 #ifdef USE_SDCARD
167 static void cliSdInfo(char *cmdline);
168 #endif
170 #ifdef BEEPER
171 static void cliBeeper(char *cmdline);
172 #endif
174 // buffer
175 static char cliBuffer[48];
176 static uint32_t bufferIndex = 0;
178 #ifndef USE_QUAD_MIXER_ONLY
179 // sync this with mixerMode_e
180 static const char * const mixerNames[] = {
181 "TRI", "QUADP", "QUADX", "BI",
182 "GIMBAL", "Y6", "HEX6",
183 "FLYING_WING", "Y4", "HEX6X", "OCTOX8", "OCTOFLATP", "OCTOFLATX",
184 "AIRPLANE", "HELI_120_CCPM", "HELI_90_DEG", "VTAIL4",
185 "HEX6H", "PPM_TO_SERVO", "DUALCOPTER", "SINGLECOPTER",
186 "ATAIL4", "CUSTOM", "CUSTOMAIRPLANE", "CUSTOMTRI", "QUADX1234", NULL
188 #endif
190 // sync this with features_e
191 static const char * const featureNames[] = {
192 "RX_PPM", "VBAT", "INFLIGHT_ACC_CAL", "RX_SERIAL", "MOTOR_STOP",
193 "SERVO_TILT", "SOFTSERIAL", "GPS", "FAILSAFE",
194 "SONAR", "TELEMETRY", "CURRENT_METER", "3D", "RX_PARALLEL_PWM",
195 "RX_MSP", "RSSI_ADC", "LED_STRIP", "DISPLAY", "ONESHOT125",
196 "BLACKBOX", "CHANNEL_FORWARDING", "TRANSPONDER", NULL
199 // sync this with rxFailsafeChannelMode_e
200 static const char rxFailsafeModeCharacters[] = "ahs";
202 static const rxFailsafeChannelMode_e rxFailsafeModesTable[RX_FAILSAFE_TYPE_COUNT][RX_FAILSAFE_MODE_COUNT] = {
203 { RX_FAILSAFE_MODE_AUTO, RX_FAILSAFE_MODE_HOLD, RX_FAILSAFE_MODE_INVALID },
204 { RX_FAILSAFE_MODE_INVALID, RX_FAILSAFE_MODE_HOLD, RX_FAILSAFE_MODE_SET }
207 #ifndef CJMCU
208 // sync this with sensors_e
209 static const char * const sensorTypeNames[] = {
210 "GYRO", "ACC", "BARO", "MAG", "SONAR", "GPS", "GPS+MAG", NULL
213 #define SENSOR_NAMES_MASK (SENSOR_GYRO | SENSOR_ACC | SENSOR_BARO | SENSOR_MAG)
215 static const char * const sensorHardwareNames[4][11] = {
216 { "", "None", "MPU6050", "L3G4200D", "MPU3050", "L3GD20", "MPU6000", "MPU6500", "FAKE", NULL },
217 { "", "None", "ADXL345", "MPU6050", "MMA845x", "BMA280", "LSM303DLHC", "MPU6000", "MPU6500", "FAKE", NULL },
218 { "", "None", "BMP085", "MS5611", "BMP280", NULL },
219 { "", "None", "HMC5883", "AK8975", "AK8963", NULL }
221 #endif
223 typedef struct {
224 const char *name;
225 #ifndef SKIP_CLI_COMMAND_HELP
226 const char *description;
227 const char *args;
228 #endif
229 void (*func)(char *cmdline);
230 } clicmd_t;
232 #ifndef SKIP_CLI_COMMAND_HELP
233 #define CLI_COMMAND_DEF(name, description, args, method) \
235 name , \
236 description , \
237 args , \
238 method \
240 #else
241 #define CLI_COMMAND_DEF(name, description, args, method) \
243 name, \
244 method \
246 #endif
248 // should be sorted a..z for bsearch()
249 const clicmd_t cmdTable[] = {
250 CLI_COMMAND_DEF("adjrange", "configure adjustment ranges", NULL, cliAdjustmentRange),
251 CLI_COMMAND_DEF("aux", "configure modes", NULL, cliAux),
252 #ifdef LED_STRIP
253 CLI_COMMAND_DEF("color", "configure colors", NULL, cliColor),
254 #endif
255 CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", NULL, cliDefaults),
256 CLI_COMMAND_DEF("dump", "dump configuration",
257 "[master|profile]", cliDump),
258 CLI_COMMAND_DEF("exit", NULL, NULL, cliExit),
259 CLI_COMMAND_DEF("feature", "configure features",
260 "list\r\n"
261 "\t<+|->[name]", cliFeature),
262 #ifdef USE_FLASHFS
263 CLI_COMMAND_DEF("flash_erase", "erase flash chip", NULL, cliFlashErase),
264 CLI_COMMAND_DEF("flash_info", "show flash chip info", NULL, cliFlashInfo),
265 #ifdef USE_FLASH_TOOLS
266 CLI_COMMAND_DEF("flash_read", NULL, "<length> <address>", cliFlashRead),
267 CLI_COMMAND_DEF("flash_write", NULL, "<address> <message>", cliFlashWrite),
268 #endif
269 #endif
270 CLI_COMMAND_DEF("get", "get variable value",
271 "[name]", cliGet),
272 #ifdef GPS
273 CLI_COMMAND_DEF("gpspassthrough", "passthrough gps to serial", NULL, cliGpsPassthrough),
274 #endif
275 CLI_COMMAND_DEF("help", NULL, NULL, cliHelp),
276 #ifdef LED_STRIP
277 CLI_COMMAND_DEF("led", "configure leds", NULL, cliLed),
278 #endif
279 CLI_COMMAND_DEF("map", "configure rc channel order",
280 "[<map>]", cliMap),
281 #ifndef USE_QUAD_MIXER_ONLY
282 CLI_COMMAND_DEF("mixer", "configure mixer",
283 "list\r\n"
284 "\t<name>", cliMixer),
285 #endif
286 CLI_COMMAND_DEF("mmix", "custom motor mixer", NULL, cliMotorMix),
287 CLI_COMMAND_DEF("motor", "get/set motor",
288 "<index> [<value>]", cliMotor),
289 CLI_COMMAND_DEF("play_sound", NULL,
290 "[<index>]\r\n", cliPlaySound),
291 CLI_COMMAND_DEF("profile", "change profile",
292 "[<index>]", cliProfile),
293 CLI_COMMAND_DEF("rateprofile", "change rate profile", "[<index>]", cliRateProfile),
294 CLI_COMMAND_DEF("rxrange", "configure rx channel ranges", NULL, cliRxRange),
295 CLI_COMMAND_DEF("rxfail", "show/set rx failsafe settings", NULL, cliRxFail),
296 CLI_COMMAND_DEF("save", "save and reboot", NULL, cliSave),
297 CLI_COMMAND_DEF("serial", "configure serial ports", NULL, cliSerial),
298 #ifndef SKIP_SERIAL_PASSTHROUGH
299 CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data to port",
300 "<id> [baud] [mode] : passthrough to serial",
301 cliSerialPassthrough),
302 #endif
303 #ifdef USE_SERVOS
304 CLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo),
305 #endif
306 CLI_COMMAND_DEF("set", "change setting",
307 "[<name>=<value>]", cliSet),
308 #ifdef USE_SERVOS
309 CLI_COMMAND_DEF("smix", "servo mixer",
310 "<rule> <servo> <source> <rate> <speed> <min> <max> <box>\r\n"
311 "\treset\r\n"
312 "\tload <mixer>\r\n"
313 "\treverse <servo> <source> r|n", cliServoMix),
314 #endif
315 #ifdef USE_SDCARD
316 CLI_COMMAND_DEF("sd_info", "sdcard info", NULL, cliSdInfo),
317 #endif
318 CLI_COMMAND_DEF("status", "show status", NULL, cliStatus),
319 #ifndef SKIP_TASK_STATISTICS
320 CLI_COMMAND_DEF("tasks", "show task stats", NULL, cliTasks),
321 #endif
322 CLI_COMMAND_DEF("version", "show version", NULL, cliVersion),
323 #ifdef BEEPER
324 CLI_COMMAND_DEF("beeper", "turn on/off beeper", "list\r\n"
325 "\t<+|->[name]", cliBeeper),
326 #endif
328 #define CMD_COUNT (sizeof(cmdTable) / sizeof(clicmd_t))
330 static const char * const lookupTableOffOn[] = {
331 "OFF", "ON"
334 static const char * const lookupTableUnit[] = {
335 "IMPERIAL", "METRIC"
338 static const char * const lookupTableAlignment[] = {
339 "DEFAULT",
340 "CW0",
341 "CW90",
342 "CW180",
343 "CW270",
344 "CW0FLIP",
345 "CW90FLIP",
346 "CW180FLIP",
347 "CW270FLIP"
350 #ifdef GPS
351 static const char * const lookupTableGPSProvider[] = {
352 "NMEA", "UBLOX"
355 static const char * const lookupTableGPSSBASMode[] = {
356 "AUTO", "EGNOS", "WAAS", "MSAS", "GAGAN"
358 #endif
360 static const char * const lookupTableCurrentSensor[] = {
361 "NONE", "ADC", "VIRTUAL"
364 static const char * const lookupTableGimbalMode[] = {
365 "NORMAL", "MIXTILT"
368 static const char * const lookupTableBlackboxDevice[] = {
369 "SERIAL", "SPIFLASH", "SDCARD"
373 static const char * const lookupTablePidController[] = {
374 "MW23", "MWREWRITE", "LUX"
377 static const char * const lookupTableSerialRX[] = {
378 "SPEK1024",
379 "SPEK2048",
380 "SBUS",
381 "SUMD",
382 "SUMH",
383 "XB-B",
384 "XB-B-RJ01",
385 "IBUS",
386 "JETIEXBUS"
389 static const char * const lookupTableGyroLpf[] = {
390 "OFF",
391 "188HZ",
392 "98HZ",
393 "42HZ",
394 "20HZ",
395 "10HZ",
396 "5HZ",
397 "EXPERIMENTAL"
400 static const char * const lookupTableAccHardware[] = {
401 "AUTO",
402 "NONE",
403 "ADXL345",
404 "MPU6050",
405 "MMA8452",
406 "BMA280",
407 "LSM303DLHC",
408 "MPU6000",
409 "MPU6500",
410 "FAKE"
413 static const char * const lookupTableBaroHardware[] = {
414 "AUTO",
415 "NONE",
416 "BMP085",
417 "MS5611",
418 "BMP280"
421 static const char * const lookupTableMagHardware[] = {
422 "AUTO",
423 "NONE",
424 "HMC5883",
425 "AK8975",
426 "AK8963"
429 static const char * const lookupDeltaMethod[] = {
430 "ERROR", "MEASUREMENT"
433 static const char * const lookupTableDebug[] = {
434 "NONE",
435 "CYCLETIME",
436 "BATTERY",
437 "GYRO",
438 "ACCELEROMETER",
439 "MIXER",
440 "AIRMODE"
443 typedef struct lookupTableEntry_s {
444 const char * const *values;
445 const uint8_t valueCount;
446 } lookupTableEntry_t;
448 typedef enum {
449 TABLE_OFF_ON = 0,
450 TABLE_UNIT,
451 TABLE_ALIGNMENT,
452 #ifdef GPS
453 TABLE_GPS_PROVIDER,
454 TABLE_GPS_SBAS_MODE,
455 #endif
456 #ifdef BLACKBOX
457 TABLE_BLACKBOX_DEVICE,
458 #endif
459 TABLE_CURRENT_SENSOR,
460 TABLE_GIMBAL_MODE,
461 TABLE_PID_CONTROLLER,
462 TABLE_SERIAL_RX,
463 TABLE_GYRO_LPF,
464 TABLE_ACC_HARDWARE,
465 TABLE_BARO_HARDWARE,
466 TABLE_MAG_HARDWARE,
467 TABLE_DELTA_METHOD,
468 TABLE_DEBUG,
469 } lookupTableIndex_e;
471 static const lookupTableEntry_t lookupTables[] = {
472 { lookupTableOffOn, sizeof(lookupTableOffOn) / sizeof(char *) },
473 { lookupTableUnit, sizeof(lookupTableUnit) / sizeof(char *) },
474 { lookupTableAlignment, sizeof(lookupTableAlignment) / sizeof(char *) },
475 #ifdef GPS
476 { lookupTableGPSProvider, sizeof(lookupTableGPSProvider) / sizeof(char *) },
477 { lookupTableGPSSBASMode, sizeof(lookupTableGPSSBASMode) / sizeof(char *) },
478 #endif
479 #ifdef BLACKBOX
480 { lookupTableBlackboxDevice, sizeof(lookupTableBlackboxDevice) / sizeof(char *) },
481 #endif
482 { lookupTableCurrentSensor, sizeof(lookupTableCurrentSensor) / sizeof(char *) },
483 { lookupTableGimbalMode, sizeof(lookupTableGimbalMode) / sizeof(char *) },
484 { lookupTablePidController, sizeof(lookupTablePidController) / sizeof(char *) },
485 { lookupTableSerialRX, sizeof(lookupTableSerialRX) / sizeof(char *) },
486 { lookupTableGyroLpf, sizeof(lookupTableGyroLpf) / sizeof(char *) },
487 { lookupTableAccHardware, sizeof(lookupTableAccHardware) / sizeof(char *) },
488 { lookupTableBaroHardware, sizeof(lookupTableBaroHardware) / sizeof(char *) },
489 { lookupTableMagHardware, sizeof(lookupTableMagHardware) / sizeof(char *) },
490 { lookupDeltaMethod, sizeof(lookupDeltaMethod) / sizeof(char *) },
491 { lookupTableDebug, sizeof(lookupTableDebug) / sizeof(char *) }
494 #define VALUE_TYPE_OFFSET 0
495 #define VALUE_SECTION_OFFSET 4
496 #define VALUE_MODE_OFFSET 6
498 typedef enum {
499 // value type
500 VAR_UINT8 = (0 << VALUE_TYPE_OFFSET),
501 VAR_INT8 = (1 << VALUE_TYPE_OFFSET),
502 VAR_UINT16 = (2 << VALUE_TYPE_OFFSET),
503 VAR_INT16 = (3 << VALUE_TYPE_OFFSET),
504 VAR_UINT32 = (4 << VALUE_TYPE_OFFSET),
505 VAR_FLOAT = (5 << VALUE_TYPE_OFFSET),
507 // value section
508 MASTER_VALUE = (0 << VALUE_SECTION_OFFSET),
509 PROFILE_VALUE = (1 << VALUE_SECTION_OFFSET),
510 PROFILE_RATE_VALUE = (2 << VALUE_SECTION_OFFSET),
511 // value mode
512 MODE_DIRECT = (0 << VALUE_MODE_OFFSET),
513 MODE_LOOKUP = (1 << VALUE_MODE_OFFSET)
514 } cliValueFlag_e;
516 #define VALUE_TYPE_MASK (0x0F)
517 #define VALUE_SECTION_MASK (0x30)
518 #define VALUE_MODE_MASK (0xC0)
520 typedef struct cliMinMaxConfig_s {
521 const int32_t min;
522 const int32_t max;
523 } cliMinMaxConfig_t;
525 typedef struct cliLookupTableConfig_s {
526 const lookupTableIndex_e tableIndex;
527 } cliLookupTableConfig_t;
529 typedef union {
530 cliLookupTableConfig_t lookup;
531 cliMinMaxConfig_t minmax;
533 } cliValueConfig_t;
535 typedef struct {
536 const char *name;
537 const uint8_t type; // see cliValueFlag_e
538 void *ptr;
539 const cliValueConfig_t config;
540 } clivalue_t;
542 const clivalue_t valueTable[] = {
543 // { "emf_avoidance", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.emf_avoidance, .config.lookup = { TABLE_OFF_ON } },
545 { "mid_rc", VAR_UINT16 | MASTER_VALUE, &masterConfig.rxConfig.midrc, .config.minmax = { 1200, 1700 } },
546 { "min_check", VAR_UINT16 | MASTER_VALUE, &masterConfig.rxConfig.mincheck, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
547 { "max_check", VAR_UINT16 | MASTER_VALUE, &masterConfig.rxConfig.maxcheck, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
548 { "rssi_channel", VAR_INT8 | MASTER_VALUE, &masterConfig.rxConfig.rssi_channel, .config.minmax = { 0, MAX_SUPPORTED_RC_CHANNEL_COUNT } },
549 { "rssi_scale", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.rssi_scale, .config.minmax = { RSSI_SCALE_MIN, RSSI_SCALE_MAX } },
550 { "rssi_ppm_invert", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.rxConfig.rssi_ppm_invert, .config.lookup = { TABLE_OFF_ON } },
551 { "input_filtering_mode", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.inputFilteringMode, .config.lookup = { TABLE_OFF_ON } },
552 { "rc_smoothing", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.rxConfig.rcSmoothing, .config.lookup = { TABLE_OFF_ON } },
553 { "roll_yaw_cam_mix_degrees", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.fpvCamAngleDegrees, .config.minmax = { 0, 50 } },
554 { "max_aux_channels", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.max_aux_channel, .config.minmax = { 0, 13 } },
555 { "debug_mode", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.debug_mode, .config.lookup = { TABLE_DEBUG } },
557 { "min_throttle", VAR_UINT16 | MASTER_VALUE, &masterConfig.escAndServoConfig.minthrottle, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
558 { "max_throttle", VAR_UINT16 | MASTER_VALUE, &masterConfig.escAndServoConfig.maxthrottle, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
559 { "min_command", VAR_UINT16 | MASTER_VALUE, &masterConfig.escAndServoConfig.mincommand, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
560 { "servo_center_pulse", VAR_UINT16 | MASTER_VALUE, &masterConfig.escAndServoConfig.servoCenterPulse, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
562 { "3d_deadband_low", VAR_UINT16 | MASTER_VALUE, &masterConfig.flight3DConfig.deadband3d_low, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } }, // FIXME upper limit should match code in the mixer, 1500 currently
563 { "3d_deadband_high", VAR_UINT16 | MASTER_VALUE, &masterConfig.flight3DConfig.deadband3d_high, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } }, // FIXME lower limit should match code in the mixer, 1500 currently,
564 { "3d_neutral", VAR_UINT16 | MASTER_VALUE, &masterConfig.flight3DConfig.neutral3d, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
565 { "3d_deadband_throttle", VAR_UINT16 | MASTER_VALUE, &masterConfig.flight3DConfig.deadband3d_throttle, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } },
567 { "use_oneshot42", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.use_oneshot42, .config.lookup = { TABLE_OFF_ON } },
568 { "use_multishot", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.use_multiShot, .config.lookup = { TABLE_OFF_ON } },
569 #ifdef CC3D
570 { "enable_buzzer_p6", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.use_buzzer_p6, .config.lookup = { TABLE_OFF_ON } },
571 #endif
572 { "motor_pwm_rate", VAR_UINT16 | MASTER_VALUE, &masterConfig.motor_pwm_rate, .config.minmax = { 300, 32000 } },
573 { "servo_pwm_rate", VAR_UINT16 | MASTER_VALUE, &masterConfig.servo_pwm_rate, .config.minmax = { 50, 498 } },
575 { "disarm_kill_switch", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.disarm_kill_switch, .config.lookup = { TABLE_OFF_ON } },
576 { "gyro_cal_on_first_arm", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gyro_cal_on_first_arm, .config.lookup = { TABLE_OFF_ON } },
577 { "auto_disarm_delay", VAR_UINT8 | MASTER_VALUE, &masterConfig.auto_disarm_delay, .config.minmax = { 0, 60 } },
578 { "small_angle", VAR_UINT8 | MASTER_VALUE, &masterConfig.small_angle, .config.minmax = { 0, 180 } },
580 { "fixedwing_althold_dir", VAR_INT8 | MASTER_VALUE, &masterConfig.airplaneConfig.fixedwing_althold_dir, .config.minmax = { -1, 1 } },
582 { "reboot_character", VAR_UINT8 | MASTER_VALUE, &masterConfig.serialConfig.reboot_character, .config.minmax = { 48, 126 } },
584 #ifdef GPS
585 { "gps_provider", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gpsConfig.provider, .config.lookup = { TABLE_GPS_PROVIDER } },
586 { "gps_sbas_mode", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gpsConfig.sbasMode, .config.lookup = { TABLE_GPS_SBAS_MODE } },
587 { "gps_auto_config", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gpsConfig.autoConfig, .config.lookup = { TABLE_OFF_ON } },
588 { "gps_auto_baud", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gpsConfig.autoBaud, .config.lookup = { TABLE_OFF_ON } },
590 { "gps_pos_p", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDPOS], .config.minmax = { 0, 200 } },
591 { "gps_pos_i", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDPOS], .config.minmax = { 0, 200 } },
592 { "gps_pos_d", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDPOS], .config.minmax = { 0, 200 } },
593 { "gps_posr_p", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDPOSR], .config.minmax = { 0, 200 } },
594 { "gps_posr_i", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDPOSR], .config.minmax = { 0, 200 } },
595 { "gps_posr_d", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDPOSR], .config.minmax = { 0, 200 } },
596 { "gps_nav_p", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDNAVR], .config.minmax = { 0, 200 } },
597 { "gps_nav_i", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDNAVR], .config.minmax = { 0, 200 } },
598 { "gps_nav_d", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDNAVR], .config.minmax = { 0, 200 } },
599 { "gps_wp_radius", VAR_UINT16 | MASTER_VALUE, &masterConfig.gpsProfile.gps_wp_radius, .config.minmax = { 0, 2000 } },
600 { "nav_controls_heading", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gpsProfile.nav_controls_heading, .config.lookup = { TABLE_OFF_ON } },
601 { "nav_speed_min", VAR_UINT16 | MASTER_VALUE, &masterConfig.gpsProfile.nav_speed_min, .config.minmax = { 10, 2000 } },
602 { "nav_speed_max", VAR_UINT16 | MASTER_VALUE, &masterConfig.gpsProfile.nav_speed_max, .config.minmax = { 10, 2000 } },
603 { "nav_slew_rate", VAR_UINT8 | MASTER_VALUE, &masterConfig.gpsProfile.nav_slew_rate, .config.minmax = { 0, 100 } },
604 #endif
605 #ifdef GTUNE
606 { "gtune_loP_rll", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_lolimP[FD_ROLL], .config.minmax = { 10, 200 } },
607 { "gtune_loP_ptch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_lolimP[FD_PITCH], .config.minmax = { 10, 200 } },
608 { "gtune_loP_yw", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_lolimP[FD_YAW], .config.minmax = { 10, 200 } },
609 { "gtune_hiP_rll", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_hilimP[FD_ROLL], .config.minmax = { 0, 200 } },
610 { "gtune_hiP_ptch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_hilimP[FD_PITCH], .config.minmax = { 0, 200 } },
611 { "gtune_hiP_yw", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_hilimP[FD_YAW], .config.minmax = { 0, 200 } },
612 { "gtune_pwr", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_pwr, .config.minmax = { 0, 10 } },
613 { "gtune_settle_time", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_settle_time, .config.minmax = { 200, 1000 } },
614 { "gtune_average_cycles", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.gtune_average_cycles, .config.minmax = { 8, 128 } },
615 #endif
617 { "serialrx_provider", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.rxConfig.serialrx_provider, .config.lookup = { TABLE_SERIAL_RX } },
618 { "sbus_inversion", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.rxConfig.sbus_inversion, .config.lookup = { TABLE_OFF_ON } },
619 { "spektrum_sat_bind", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.spektrum_sat_bind, .config.minmax = { SPEKTRUM_SAT_BIND_DISABLED, SPEKTRUM_SAT_BIND_MAX} },
620 { "spektrum_sat_bind_autoreset",VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.spektrum_sat_bind_autoreset, .config.minmax = { 0, 1} },
622 { "telemetry_switch", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.telemetryConfig.telemetry_switch, .config.lookup = { TABLE_OFF_ON } },
623 { "telemetry_inversion", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.telemetryConfig.telemetry_inversion, .config.lookup = { TABLE_OFF_ON } },
624 { "frsky_default_lattitude", VAR_FLOAT | MASTER_VALUE, &masterConfig.telemetryConfig.gpsNoFixLatitude, .config.minmax = { -90.0, 90.0 } },
625 { "frsky_default_longitude", VAR_FLOAT | MASTER_VALUE, &masterConfig.telemetryConfig.gpsNoFixLongitude, .config.minmax = { -180.0, 180.0 } },
626 { "frsky_coordinates_format", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.frsky_coordinate_format, .config.minmax = { 0, FRSKY_FORMAT_NMEA } },
627 { "frsky_unit", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.telemetryConfig.frsky_unit, .config.lookup = { TABLE_UNIT } },
628 { "frsky_vfas_precision", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.frsky_vfas_precision, .config.minmax = { FRSKY_VFAS_PRECISION_LOW, FRSKY_VFAS_PRECISION_HIGH } },
629 { "frsky_vfas_cell_voltage", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.telemetryConfig.frsky_vfas_cell_voltage, .config.lookup = { TABLE_OFF_ON } },
630 { "hott_alarm_sound_interval", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.hottAlarmSoundInterval, .config.minmax = { 0, 120 } },
632 { "battery_capacity", VAR_UINT16 | MASTER_VALUE, &masterConfig.batteryConfig.batteryCapacity, .config.minmax = { 0, 20000 } },
633 { "vbat_scale", VAR_UINT8 | MASTER_VALUE, &masterConfig.batteryConfig.vbatscale, .config.minmax = { VBAT_SCALE_MIN, VBAT_SCALE_MAX } },
634 { "vbat_max_cell_voltage", VAR_UINT8 | MASTER_VALUE, &masterConfig.batteryConfig.vbatmaxcellvoltage, .config.minmax = { 10, 50 } },
635 { "vbat_min_cell_voltage", VAR_UINT8 | MASTER_VALUE, &masterConfig.batteryConfig.vbatmincellvoltage, .config.minmax = { 10, 50 } },
636 { "vbat_warning_cell_voltage", VAR_UINT8 | MASTER_VALUE, &masterConfig.batteryConfig.vbatwarningcellvoltage, .config.minmax = { 10, 50 } },
637 { "vbat_hysteresis", VAR_UINT8 | MASTER_VALUE, &masterConfig.batteryConfig.vbathysteresis, .config.minmax = { 0, 250 } },
638 { "vbat_pid_compensation", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.batteryConfig.vbatPidCompensation, .config.lookup = { TABLE_OFF_ON } },
639 { "current_meter_scale", VAR_INT16 | MASTER_VALUE, &masterConfig.batteryConfig.currentMeterScale, .config.minmax = { -10000, 10000 } },
640 { "current_meter_offset", VAR_UINT16 | MASTER_VALUE, &masterConfig.batteryConfig.currentMeterOffset, .config.minmax = { 0, 3300 } },
641 { "multiwii_current_meter_output", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.batteryConfig.multiwiiCurrentMeterOutput, .config.lookup = { TABLE_OFF_ON } },
642 { "current_meter_type", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.batteryConfig.currentMeterType, .config.lookup = { TABLE_CURRENT_SENSOR } },
644 { "align_gyro", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.sensorAlignmentConfig.gyro_align, .config.lookup = { TABLE_ALIGNMENT } },
645 { "align_acc", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.sensorAlignmentConfig.acc_align, .config.lookup = { TABLE_ALIGNMENT } },
646 { "align_mag", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.sensorAlignmentConfig.mag_align, .config.lookup = { TABLE_ALIGNMENT } },
648 { "align_board_roll", VAR_INT16 | MASTER_VALUE, &masterConfig.boardAlignment.rollDegrees, .config.minmax = { -180, 360 } },
649 { "align_board_pitch", VAR_INT16 | MASTER_VALUE, &masterConfig.boardAlignment.pitchDegrees, .config.minmax = { -180, 360 } },
650 { "align_board_yaw", VAR_INT16 | MASTER_VALUE, &masterConfig.boardAlignment.yawDegrees, .config.minmax = { -180, 360 } },
652 { "max_angle_inclination", VAR_UINT16 | MASTER_VALUE, &masterConfig.max_angle_inclination, .config.minmax = { 100, 900 } },
654 { "gyro_lpf", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gyro_lpf, .config.lookup = { TABLE_GYRO_LPF } },
655 { "gyro_sync_denom", VAR_UINT8 | MASTER_VALUE, &masterConfig.gyro_sync_denom, .config.minmax = { 1, 8 } },
656 { "gyro_lowpass_hz", VAR_FLOAT | MASTER_VALUE, &masterConfig.gyro_soft_lpf_hz, .config.minmax = { 0, 500 } },
657 { "moron_threshold", VAR_UINT8 | MASTER_VALUE, &masterConfig.gyroConfig.gyroMovementCalibrationThreshold, .config.minmax = { 0, 128 } },
658 { "imu_dcm_kp", VAR_UINT16 | MASTER_VALUE, &masterConfig.dcm_kp, .config.minmax = { 0, 50000 } },
659 { "imu_dcm_ki", VAR_UINT16 | MASTER_VALUE, &masterConfig.dcm_ki, .config.minmax = { 0, 50000 } },
661 { "alt_hold_deadband", VAR_UINT8 | MASTER_VALUE, &masterConfig.rcControlsConfig.alt_hold_deadband, .config.minmax = { 1, 250 } },
662 { "alt_hold_fast_change", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.rcControlsConfig.alt_hold_fast_change, .config.lookup = { TABLE_OFF_ON } },
663 { "deadband", VAR_UINT8 | MASTER_VALUE, &masterConfig.rcControlsConfig.deadband, .config.minmax = { 0, 32 } },
664 { "yaw_deadband", VAR_UINT8 | MASTER_VALUE, &masterConfig.rcControlsConfig.yaw_deadband, .config.minmax = { 0, 100 } },
666 { "throttle_correction_value", VAR_UINT8 | MASTER_VALUE, &masterConfig.throttle_correction_value, .config.minmax = { 0, 150 } },
667 { "throttle_correction_angle", VAR_UINT16 | MASTER_VALUE, &masterConfig.throttle_correction_angle, .config.minmax = { 1, 900 } },
669 { "yaw_control_direction", VAR_INT8 | MASTER_VALUE, &masterConfig.yaw_control_direction, .config.minmax = { -1, 1 } },
671 { "yaw_motor_direction", VAR_INT8 | MASTER_VALUE, &masterConfig.mixerConfig.yaw_motor_direction, .config.minmax = { -1, 1 } },
672 { "yaw_jump_prevention_limit", VAR_UINT16 | MASTER_VALUE, &masterConfig.mixerConfig.yaw_jump_prevention_limit, .config.minmax = { YAW_JUMP_PREVENTION_LIMIT_LOW, YAW_JUMP_PREVENTION_LIMIT_HIGH } },
673 { "yaw_p_limit", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.yaw_p_limit, .config.minmax = { YAW_P_LIMIT_MIN, YAW_P_LIMIT_MAX } },
674 #ifdef USE_SERVOS
675 { "tri_unarmed_servo", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.tri_unarmed_servo, .config.lookup = { TABLE_OFF_ON } },
676 { "servo_lowpass_freq", VAR_INT16 | MASTER_VALUE, &masterConfig.mixerConfig.servo_lowpass_freq, .config.minmax = { 10, 400} },
677 { "servo_lowpass_enable", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.servo_lowpass_enable, .config.lookup = { TABLE_OFF_ON } },
678 #endif
680 { "rc_rate", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rcRate8, .config.minmax = { 0, 250 } },
681 { "rc_expo", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rcExpo8, .config.minmax = { 0, 100 } },
682 { "rc_yaw_expo", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rcYawExpo8, .config.minmax = { 0, 100 } },
683 { "thr_mid", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].thrMid8, .config.minmax = { 0, 100 } },
684 { "thr_expo", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].thrExpo8, .config.minmax = { 0, 100 } },
685 { "roll_rate", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rates[FD_ROLL], .config.minmax = { 0, CONTROL_RATE_CONFIG_ROLL_PITCH_RATE_MAX } },
686 { "pitch_rate", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rates[FD_PITCH], .config.minmax = { 0, CONTROL_RATE_CONFIG_ROLL_PITCH_RATE_MAX } },
687 { "yaw_rate", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].rates[FD_YAW], .config.minmax = { 0, CONTROL_RATE_CONFIG_YAW_RATE_MAX } },
688 { "tpa_rate", VAR_UINT8 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].dynThrPID, .config.minmax = { 0, CONTROL_RATE_CONFIG_TPA_MAX} },
689 { "tpa_breakpoint", VAR_UINT16 | PROFILE_RATE_VALUE, &masterConfig.profile[0].controlRateProfile[0].tpa_breakpoint, .config.minmax = { PWM_RANGE_MIN, PWM_RANGE_MAX} },
690 { "super_expo_factor", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.superExpoFactor, .config.minmax = {1, 100 } },
692 { "failsafe_delay", VAR_UINT8 | MASTER_VALUE, &masterConfig.failsafeConfig.failsafe_delay, .config.minmax = { 0, 200 } },
693 { "failsafe_off_delay", VAR_UINT8 | MASTER_VALUE, &masterConfig.failsafeConfig.failsafe_off_delay, .config.minmax = { 0, 200 } },
694 { "failsafe_throttle", VAR_UINT16 | MASTER_VALUE, &masterConfig.failsafeConfig.failsafe_throttle, .config.minmax = { PWM_RANGE_MIN, PWM_RANGE_MAX } },
695 { "failsafe_kill_switch", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.failsafeConfig.failsafe_kill_switch, .config.lookup = { TABLE_OFF_ON } },
696 { "failsafe_throttle_low_delay",VAR_UINT16 | MASTER_VALUE, &masterConfig.failsafeConfig.failsafe_throttle_low_delay, .config.minmax = { 0, 300 } },
697 { "failsafe_procedure", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.failsafeConfig.failsafe_procedure, .config.lookup = { TABLE_OFF_ON } },
699 { "rx_min_usec", VAR_UINT16 | MASTER_VALUE, &masterConfig.rxConfig.rx_min_usec, .config.minmax = { PWM_PULSE_MIN, PWM_PULSE_MAX } },
700 { "rx_max_usec", VAR_UINT16 | MASTER_VALUE, &masterConfig.rxConfig.rx_max_usec, .config.minmax = { PWM_PULSE_MIN, PWM_PULSE_MAX } },
702 #ifdef USE_SERVOS
703 { "gimbal_mode", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.gimbalConfig.mode, .config.lookup = { TABLE_GIMBAL_MODE } },
704 #endif
706 { "acc_hardware", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.acc_hardware, .config.lookup = { TABLE_ACC_HARDWARE } },
707 { "acc_lpf_hz", VAR_FLOAT | MASTER_VALUE, &masterConfig.acc_lpf_hz, .config.minmax = { 0, 400 } },
708 { "accxy_deadband", VAR_UINT8 | MASTER_VALUE, &masterConfig.accDeadband.xy, .config.minmax = { 0, 100 } },
709 { "accz_deadband", VAR_UINT8 | MASTER_VALUE, &masterConfig.accDeadband.z, .config.minmax = { 0, 100 } },
710 { "acc_unarmedcal", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.acc_unarmedcal, .config.lookup = { TABLE_OFF_ON } },
711 { "acc_trim_pitch", VAR_INT16 | MASTER_VALUE, &masterConfig.accelerometerTrims.values.pitch, .config.minmax = { -300, 300 } },
712 { "acc_trim_roll", VAR_INT16 | MASTER_VALUE, &masterConfig.accelerometerTrims.values.roll, .config.minmax = { -300, 300 } },
714 { "baro_tab_size", VAR_UINT8 | MASTER_VALUE, &masterConfig.barometerConfig.baro_sample_count, .config.minmax = { 0, BARO_SAMPLE_COUNT_MAX } },
715 { "baro_noise_lpf", VAR_FLOAT | MASTER_VALUE, &masterConfig.barometerConfig.baro_noise_lpf, .config.minmax = { 0 , 1 } },
716 { "baro_cf_vel", VAR_FLOAT | MASTER_VALUE, &masterConfig.barometerConfig.baro_cf_vel, .config.minmax = { 0 , 1 } },
717 { "baro_cf_alt", VAR_FLOAT | MASTER_VALUE, &masterConfig.barometerConfig.baro_cf_alt, .config.minmax = { 0 , 1 } },
718 { "baro_hardware", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.baro_hardware, .config.lookup = { TABLE_BARO_HARDWARE } },
720 { "mag_hardware", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mag_hardware, .config.lookup = { TABLE_MAG_HARDWARE } },
721 { "mag_declination", VAR_INT16 | MASTER_VALUE, &masterConfig.mag_declination, .config.minmax = { -18000, 18000 } },
722 { "pid_delta_method", VAR_UINT8 | PROFILE_VALUE | MODE_LOOKUP, &masterConfig.profile[0].pidProfile.deltaMethod, .config.lookup = { TABLE_DELTA_METHOD } },
723 { "dterm_lowpass_hz", VAR_FLOAT | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.dterm_lpf_hz, .config.minmax = {0, 500 } },
724 { "dterm_average_count", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.dterm_average_count, .config.minmax = {0, 12 } },
725 { "iterm_reset_degrees", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.rollPitchItermResetRate, .config.minmax = {50, 1000 } },
726 { "yaw_iterm_reset_degrees", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.yawItermResetRate, .config.minmax = {25, 1000 } },
727 { "yaw_lowpass_hz", VAR_FLOAT | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.yaw_lpf_hz, .config.minmax = {0, 500 } },
728 { "pid_process_denom", VAR_UINT8 | MASTER_VALUE, &masterConfig.pid_process_denom, .config.minmax = { 1, 8 } },
730 { "pid_controller", VAR_UINT8 | PROFILE_VALUE | MODE_LOOKUP, &masterConfig.profile[0].pidProfile.pidController, .config.lookup = { TABLE_PID_CONTROLLER } },
732 { "p_pitch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PITCH], .config.minmax = { 0, 200 } },
733 { "i_pitch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PITCH], .config.minmax = { 0, 200 } },
734 { "d_pitch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PITCH], .config.minmax = { 0, 200 } },
735 { "p_roll", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[ROLL], .config.minmax = { 0, 200 } },
736 { "i_roll", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[ROLL], .config.minmax = { 0, 200 } },
737 { "d_roll", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[ROLL], .config.minmax = { 0, 200 } },
738 { "p_yaw", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[YAW], .config.minmax = { 0, 200 } },
739 { "i_yaw", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[YAW], .config.minmax = { 0, 200 } },
740 { "d_yaw", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[YAW], .config.minmax = { 0, 200 } },
742 { "p_alt", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDALT], .config.minmax = { 0, 200 } },
743 { "i_alt", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDALT], .config.minmax = { 0, 200 } },
744 { "d_alt", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDALT], .config.minmax = { 0, 200 } },
746 { "p_level", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDLEVEL], .config.minmax = { 0, 200 } },
747 { "i_level", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDLEVEL], .config.minmax = { 0, 200 } },
748 { "d_level", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDLEVEL], .config.minmax = { 0, 200 } },
750 { "p_vel", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PIDVEL], .config.minmax = { 0, 200 } },
751 { "i_vel", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PIDVEL], .config.minmax = { 0, 200 } },
752 { "d_vel", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.D8[PIDVEL], .config.minmax = { 0, 200 } },
754 #ifdef BLACKBOX
755 { "blackbox_rate_num", VAR_UINT8 | MASTER_VALUE, &masterConfig.blackbox_rate_num, .config.minmax = { 1, 32 } },
756 { "blackbox_rate_denom", VAR_UINT8 | MASTER_VALUE, &masterConfig.blackbox_rate_denom, .config.minmax = { 1, 32 } },
757 { "blackbox_device", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.blackbox_device, .config.lookup = { TABLE_BLACKBOX_DEVICE } },
758 #endif
760 { "magzero_x", VAR_INT16 | MASTER_VALUE, &masterConfig.magZero.raw[X], .config.minmax = { -32768, 32767 } },
761 { "magzero_y", VAR_INT16 | MASTER_VALUE, &masterConfig.magZero.raw[Y], .config.minmax = { -32768, 32767 } },
762 { "magzero_z", VAR_INT16 | MASTER_VALUE, &masterConfig.magZero.raw[Z], .config.minmax = { -32768, 32767 } },
765 #define VALUE_COUNT (sizeof(valueTable) / sizeof(clivalue_t))
768 typedef union {
769 int32_t int_value;
770 float float_value;
771 } int_float_value_t;
773 static void cliSetVar(const clivalue_t *var, const int_float_value_t value);
774 static void cliPrintVar(const clivalue_t *var, uint32_t full);
775 static void cliPrintVarRange(const clivalue_t *var);
776 static void cliPrint(const char *str);
777 static void cliPrintf(const char *fmt, ...);
778 static void cliWrite(uint8_t ch);
780 static void cliPrompt(void)
782 cliPrint("\r\n# ");
783 bufWriterFlush(cliWriter);
786 static void cliShowParseError(void)
788 cliPrint("Parse error\r\n");
791 static void cliShowArgumentRangeError(char *name, int min, int max)
793 cliPrintf("%s must be between %d and %d\r\n", name, min, max);
796 static char *processChannelRangeArgs(char *ptr, channelRange_t *range, uint8_t *validArgumentCount)
798 int val;
800 for (int argIndex = 0; argIndex < 2; argIndex++) {
801 ptr = strchr(ptr, ' ');
802 if (ptr) {
803 val = atoi(++ptr);
804 val = CHANNEL_VALUE_TO_STEP(val);
805 if (val >= MIN_MODE_RANGE_STEP && val <= MAX_MODE_RANGE_STEP) {
806 if (argIndex == 0) {
807 range->startStep = val;
808 } else {
809 range->endStep = val;
811 (*validArgumentCount)++;
816 return ptr;
819 // Check if a string's length is zero
820 static bool isEmpty(const char *string)
822 return *string == '\0';
825 static void cliRxFail(char *cmdline)
827 uint8_t channel;
828 char buf[3];
830 if (isEmpty(cmdline)) {
831 // print out rxConfig failsafe settings
832 for (channel = 0; channel < MAX_SUPPORTED_RC_CHANNEL_COUNT; channel++) {
833 cliRxFail(itoa(channel, buf, 10));
835 } else {
836 char *ptr = cmdline;
837 channel = atoi(ptr++);
838 if ((channel < MAX_SUPPORTED_RC_CHANNEL_COUNT)) {
840 rxFailsafeChannelConfiguration_t *channelFailsafeConfiguration = &masterConfig.rxConfig.failsafe_channel_configurations[channel];
842 uint16_t value;
843 rxFailsafeChannelType_e type = (channel < NON_AUX_CHANNEL_COUNT) ? RX_FAILSAFE_TYPE_FLIGHT : RX_FAILSAFE_TYPE_AUX;
844 rxFailsafeChannelMode_e mode = channelFailsafeConfiguration->mode;
845 bool requireValue = channelFailsafeConfiguration->mode == RX_FAILSAFE_MODE_SET;
847 ptr = strchr(ptr, ' ');
848 if (ptr) {
849 char *p = strchr(rxFailsafeModeCharacters, *(++ptr));
850 if (p) {
851 uint8_t requestedMode = p - rxFailsafeModeCharacters;
852 mode = rxFailsafeModesTable[type][requestedMode];
853 } else {
854 mode = RX_FAILSAFE_MODE_INVALID;
856 if (mode == RX_FAILSAFE_MODE_INVALID) {
857 cliShowParseError();
858 return;
861 requireValue = mode == RX_FAILSAFE_MODE_SET;
863 ptr = strchr(ptr, ' ');
864 if (ptr) {
865 if (!requireValue) {
866 cliShowParseError();
867 return;
869 value = atoi(++ptr);
870 value = CHANNEL_VALUE_TO_RXFAIL_STEP(value);
871 if (value > MAX_RXFAIL_RANGE_STEP) {
872 cliPrint("Value out of range\r\n");
873 return;
876 channelFailsafeConfiguration->step = value;
877 } else if (requireValue) {
878 cliShowParseError();
879 return;
881 channelFailsafeConfiguration->mode = mode;
885 char modeCharacter = rxFailsafeModeCharacters[channelFailsafeConfiguration->mode];
887 // triple use of cliPrintf below
888 // 1. acknowledge interpretation on command,
889 // 2. query current setting on single item,
890 // 3. recursive use for full list.
892 if (requireValue) {
893 cliPrintf("rxfail %u %c %d\r\n",
894 channel,
895 modeCharacter,
896 RXFAIL_STEP_TO_CHANNEL_VALUE(channelFailsafeConfiguration->step)
898 } else {
899 cliPrintf("rxfail %u %c\r\n",
900 channel,
901 modeCharacter
904 } else {
905 cliShowArgumentRangeError("channel", 0, MAX_SUPPORTED_RC_CHANNEL_COUNT - 1);
910 static void cliAux(char *cmdline)
912 int i, val = 0;
913 char *ptr;
915 if (isEmpty(cmdline)) {
916 // print out aux channel settings
917 for (i = 0; i < MAX_MODE_ACTIVATION_CONDITION_COUNT; i++) {
918 modeActivationCondition_t *mac = &masterConfig.modeActivationConditions[i];
919 cliPrintf("aux %u %u %u %u %u\r\n",
921 mac->modeId,
922 mac->auxChannelIndex,
923 MODE_STEP_TO_CHANNEL_VALUE(mac->range.startStep),
924 MODE_STEP_TO_CHANNEL_VALUE(mac->range.endStep)
927 } else {
928 ptr = cmdline;
929 i = atoi(ptr++);
930 if (i < MAX_MODE_ACTIVATION_CONDITION_COUNT) {
931 modeActivationCondition_t *mac = &masterConfig.modeActivationConditions[i];
932 uint8_t validArgumentCount = 0;
933 ptr = strchr(ptr, ' ');
934 if (ptr) {
935 val = atoi(++ptr);
936 if (val >= 0 && val < CHECKBOX_ITEM_COUNT) {
937 mac->modeId = val;
938 validArgumentCount++;
941 ptr = strchr(ptr, ' ');
942 if (ptr) {
943 val = atoi(++ptr);
944 if (val >= 0 && val < MAX_AUX_CHANNEL_COUNT) {
945 mac->auxChannelIndex = val;
946 validArgumentCount++;
949 ptr = processChannelRangeArgs(ptr, &mac->range, &validArgumentCount);
951 if (validArgumentCount != 4) {
952 memset(mac, 0, sizeof(modeActivationCondition_t));
954 } else {
955 cliShowArgumentRangeError("index", 0, MAX_MODE_ACTIVATION_CONDITION_COUNT - 1);
960 static void cliSerial(char *cmdline)
962 int i, val;
963 char *ptr;
965 if (isEmpty(cmdline)) {
966 for (i = 0; i < SERIAL_PORT_COUNT; i++) {
967 if (!serialIsPortAvailable(masterConfig.serialConfig.portConfigs[i].identifier)) {
968 continue;
970 cliPrintf("serial %d %d %ld %ld %ld %ld\r\n" ,
971 masterConfig.serialConfig.portConfigs[i].identifier,
972 masterConfig.serialConfig.portConfigs[i].functionMask,
973 baudRates[masterConfig.serialConfig.portConfigs[i].msp_baudrateIndex],
974 baudRates[masterConfig.serialConfig.portConfigs[i].gps_baudrateIndex],
975 baudRates[masterConfig.serialConfig.portConfigs[i].telemetry_baudrateIndex],
976 baudRates[masterConfig.serialConfig.portConfigs[i].blackbox_baudrateIndex]
979 return;
982 serialPortConfig_t portConfig;
983 memset(&portConfig, 0 , sizeof(portConfig));
985 serialPortConfig_t *currentConfig;
987 uint8_t validArgumentCount = 0;
989 ptr = cmdline;
991 val = atoi(ptr++);
992 currentConfig = serialFindPortConfiguration(val);
993 if (currentConfig) {
994 portConfig.identifier = val;
995 validArgumentCount++;
998 ptr = strchr(ptr, ' ');
999 if (ptr) {
1000 val = atoi(++ptr);
1001 portConfig.functionMask = val & 0xFFFF;
1002 validArgumentCount++;
1005 for (i = 0; i < 4; i ++) {
1006 ptr = strchr(ptr, ' ');
1007 if (!ptr) {
1008 break;
1011 val = atoi(++ptr);
1013 uint8_t baudRateIndex = lookupBaudRateIndex(val);
1014 if (baudRates[baudRateIndex] != (uint32_t) val) {
1015 break;
1018 switch(i) {
1019 case 0:
1020 if (baudRateIndex < BAUD_9600 || baudRateIndex > BAUD_115200) {
1021 continue;
1023 portConfig.msp_baudrateIndex = baudRateIndex;
1024 break;
1025 case 1:
1026 if (baudRateIndex < BAUD_9600 || baudRateIndex > BAUD_115200) {
1027 continue;
1029 portConfig.gps_baudrateIndex = baudRateIndex;
1030 break;
1031 case 2:
1032 if (baudRateIndex != BAUD_AUTO && baudRateIndex > BAUD_115200) {
1033 continue;
1035 portConfig.telemetry_baudrateIndex = baudRateIndex;
1036 break;
1037 case 3:
1038 if (baudRateIndex < BAUD_19200 || baudRateIndex > BAUD_250000) {
1039 continue;
1041 portConfig.blackbox_baudrateIndex = baudRateIndex;
1042 break;
1045 validArgumentCount++;
1048 if (validArgumentCount < 6) {
1049 cliShowParseError();
1050 return;
1053 memcpy(currentConfig, &portConfig, sizeof(portConfig));
1057 #ifndef SKIP_SERIAL_PASSTHROUGH
1058 static void cliSerialPassthrough(char *cmdline)
1060 if (isEmpty(cmdline)) {
1061 cliShowParseError();
1062 return;
1065 int id = -1;
1066 uint32_t baud = 0;
1067 unsigned mode = 0;
1068 char* tok = strtok(cmdline, " ");
1069 int index = 0;
1071 while (tok != NULL) {
1072 switch(index) {
1073 case 0:
1074 id = atoi(tok);
1075 break;
1076 case 1:
1077 baud = atoi(tok);
1078 break;
1079 case 2:
1080 if (strstr(tok, "rx") || strstr(tok, "RX"))
1081 mode |= MODE_RX;
1082 if (strstr(tok, "tx") || strstr(tok, "TX"))
1083 mode |= MODE_TX;
1084 break;
1086 index++;
1087 tok = strtok(NULL, " ");
1090 serialPort_t *passThroughPort;
1091 serialPortUsage_t *passThroughPortUsage = findSerialPortUsageByIdentifier(id);
1092 if (!passThroughPortUsage || passThroughPortUsage->serialPort == NULL) {
1093 if (!baud) {
1094 printf("Port %d is not open, you must specify baud\r\n", id);
1095 return;
1097 if (!mode)
1098 mode = MODE_RXTX;
1100 passThroughPort = openSerialPort(id, FUNCTION_PASSTHROUGH, NULL,
1101 baud, mode,
1102 SERIAL_NOT_INVERTED);
1103 if (!passThroughPort) {
1104 printf("Port %d could not be opened\r\n", id);
1105 return;
1107 printf("Port %d opened, baud=%d\r\n", id, baud);
1108 } else {
1109 passThroughPort = passThroughPortUsage->serialPort;
1110 // If the user supplied a mode, override the port's mode, otherwise
1111 // leave the mode unchanged. serialPassthrough() handles one-way ports.
1112 printf("Port %d already open\r\n", id);
1113 if (mode && passThroughPort->mode != mode) {
1114 printf("Adjusting mode from configured value %d to %d\r\n",
1115 passThroughPort->mode, mode);
1116 serialSetMode(passThroughPort, mode);
1120 printf("Relaying data to device on port %d, Reset your board to exit "
1121 "serial passthrough mode.\r\n");
1123 serialPassthrough(cliPort, passThroughPort, NULL, NULL);
1125 #endif
1127 static void cliAdjustmentRange(char *cmdline)
1129 int i, val = 0;
1130 char *ptr;
1132 if (isEmpty(cmdline)) {
1133 // print out adjustment ranges channel settings
1134 for (i = 0; i < MAX_ADJUSTMENT_RANGE_COUNT; i++) {
1135 adjustmentRange_t *ar = &masterConfig.adjustmentRanges[i];
1136 cliPrintf("adjrange %u %u %u %u %u %u %u\r\n",
1138 ar->adjustmentIndex,
1139 ar->auxChannelIndex,
1140 MODE_STEP_TO_CHANNEL_VALUE(ar->range.startStep),
1141 MODE_STEP_TO_CHANNEL_VALUE(ar->range.endStep),
1142 ar->adjustmentFunction,
1143 ar->auxSwitchChannelIndex
1146 } else {
1147 ptr = cmdline;
1148 i = atoi(ptr++);
1149 if (i < MAX_ADJUSTMENT_RANGE_COUNT) {
1150 adjustmentRange_t *ar = &masterConfig.adjustmentRanges[i];
1151 uint8_t validArgumentCount = 0;
1153 ptr = strchr(ptr, ' ');
1154 if (ptr) {
1155 val = atoi(++ptr);
1156 if (val >= 0 && val < MAX_SIMULTANEOUS_ADJUSTMENT_COUNT) {
1157 ar->adjustmentIndex = val;
1158 validArgumentCount++;
1161 ptr = strchr(ptr, ' ');
1162 if (ptr) {
1163 val = atoi(++ptr);
1164 if (val >= 0 && val < MAX_AUX_CHANNEL_COUNT) {
1165 ar->auxChannelIndex = val;
1166 validArgumentCount++;
1170 ptr = processChannelRangeArgs(ptr, &ar->range, &validArgumentCount);
1172 ptr = strchr(ptr, ' ');
1173 if (ptr) {
1174 val = atoi(++ptr);
1175 if (val >= 0 && val < ADJUSTMENT_FUNCTION_COUNT) {
1176 ar->adjustmentFunction = val;
1177 validArgumentCount++;
1180 ptr = strchr(ptr, ' ');
1181 if (ptr) {
1182 val = atoi(++ptr);
1183 if (val >= 0 && val < MAX_AUX_CHANNEL_COUNT) {
1184 ar->auxSwitchChannelIndex = val;
1185 validArgumentCount++;
1189 if (validArgumentCount != 6) {
1190 memset(ar, 0, sizeof(adjustmentRange_t));
1191 cliShowParseError();
1193 } else {
1194 cliShowArgumentRangeError("index", 0, MAX_ADJUSTMENT_RANGE_COUNT - 1);
1199 static void cliMotorMix(char *cmdline)
1201 #ifdef USE_QUAD_MIXER_ONLY
1202 UNUSED(cmdline);
1203 #else
1204 int i, check = 0;
1205 int num_motors = 0;
1206 uint8_t len;
1207 char buf[16];
1208 char *ptr;
1210 if (isEmpty(cmdline)) {
1211 cliPrint("Motor\tThr\tRoll\tPitch\tYaw\r\n");
1212 for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
1213 if (masterConfig.customMotorMixer[i].throttle == 0.0f)
1214 break;
1215 num_motors++;
1216 cliPrintf("#%d:\t", i);
1217 cliPrintf("%s\t", ftoa(masterConfig.customMotorMixer[i].throttle, buf));
1218 cliPrintf("%s\t", ftoa(masterConfig.customMotorMixer[i].roll, buf));
1219 cliPrintf("%s\t", ftoa(masterConfig.customMotorMixer[i].pitch, buf));
1220 cliPrintf("%s\r\n", ftoa(masterConfig.customMotorMixer[i].yaw, buf));
1222 return;
1223 } else if (strncasecmp(cmdline, "reset", 5) == 0) {
1224 // erase custom mixer
1225 for (i = 0; i < MAX_SUPPORTED_MOTORS; i++)
1226 masterConfig.customMotorMixer[i].throttle = 0.0f;
1227 } else if (strncasecmp(cmdline, "load", 4) == 0) {
1228 ptr = strchr(cmdline, ' ');
1229 if (ptr) {
1230 len = strlen(++ptr);
1231 for (i = 0; ; i++) {
1232 if (mixerNames[i] == NULL) {
1233 cliPrint("Invalid name\r\n");
1234 break;
1236 if (strncasecmp(ptr, mixerNames[i], len) == 0) {
1237 mixerLoadMix(i, masterConfig.customMotorMixer);
1238 cliPrintf("Loaded %s\r\n", mixerNames[i]);
1239 cliMotorMix("");
1240 break;
1244 } else {
1245 ptr = cmdline;
1246 i = atoi(ptr); // get motor number
1247 if (i < MAX_SUPPORTED_MOTORS) {
1248 ptr = strchr(ptr, ' ');
1249 if (ptr) {
1250 masterConfig.customMotorMixer[i].throttle = fastA2F(++ptr);
1251 check++;
1253 ptr = strchr(ptr, ' ');
1254 if (ptr) {
1255 masterConfig.customMotorMixer[i].roll = fastA2F(++ptr);
1256 check++;
1258 ptr = strchr(ptr, ' ');
1259 if (ptr) {
1260 masterConfig.customMotorMixer[i].pitch = fastA2F(++ptr);
1261 check++;
1263 ptr = strchr(ptr, ' ');
1264 if (ptr) {
1265 masterConfig.customMotorMixer[i].yaw = fastA2F(++ptr);
1266 check++;
1268 if (check != 4) {
1269 cliShowParseError();
1270 } else {
1271 cliMotorMix("");
1273 } else {
1274 cliShowArgumentRangeError("index", 0, MAX_SUPPORTED_MOTORS - 1);
1277 #endif
1280 static void cliRxRange(char *cmdline)
1282 int i, validArgumentCount = 0;
1283 char *ptr;
1285 if (isEmpty(cmdline)) {
1286 for (i = 0; i < NON_AUX_CHANNEL_COUNT; i++) {
1287 rxChannelRangeConfiguration_t *channelRangeConfiguration = &masterConfig.rxConfig.channelRanges[i];
1288 cliPrintf("rxrange %u %u %u\r\n", i, channelRangeConfiguration->min, channelRangeConfiguration->max);
1290 } else if (strcasecmp(cmdline, "reset") == 0) {
1291 resetAllRxChannelRangeConfigurations(masterConfig.rxConfig.channelRanges);
1292 } else {
1293 ptr = cmdline;
1294 i = atoi(ptr);
1295 if (i >= 0 && i < NON_AUX_CHANNEL_COUNT) {
1296 int rangeMin, rangeMax;
1298 ptr = strchr(ptr, ' ');
1299 if (ptr) {
1300 rangeMin = atoi(++ptr);
1301 validArgumentCount++;
1304 ptr = strchr(ptr, ' ');
1305 if (ptr) {
1306 rangeMax = atoi(++ptr);
1307 validArgumentCount++;
1310 if (validArgumentCount != 2) {
1311 cliShowParseError();
1312 } else if (rangeMin < PWM_PULSE_MIN || rangeMin > PWM_PULSE_MAX || rangeMax < PWM_PULSE_MIN || rangeMax > PWM_PULSE_MAX) {
1313 cliShowParseError();
1314 } else {
1315 rxChannelRangeConfiguration_t *channelRangeConfiguration = &masterConfig.rxConfig.channelRanges[i];
1316 channelRangeConfiguration->min = rangeMin;
1317 channelRangeConfiguration->max = rangeMax;
1319 } else {
1320 cliShowArgumentRangeError("channel", 0, NON_AUX_CHANNEL_COUNT - 1);
1325 #ifdef LED_STRIP
1326 static void cliLed(char *cmdline)
1328 int i;
1329 char *ptr;
1330 char ledConfigBuffer[20];
1332 if (isEmpty(cmdline)) {
1333 for (i = 0; i < MAX_LED_STRIP_LENGTH; i++) {
1334 generateLedConfig(i, ledConfigBuffer, sizeof(ledConfigBuffer));
1335 cliPrintf("led %u %s\r\n", i, ledConfigBuffer);
1337 } else {
1338 ptr = cmdline;
1339 i = atoi(ptr);
1340 if (i < MAX_LED_STRIP_LENGTH) {
1341 ptr = strchr(cmdline, ' ');
1342 if (!parseLedStripConfig(i, ++ptr)) {
1343 cliShowParseError();
1345 } else {
1346 cliShowArgumentRangeError("index", 0, MAX_LED_STRIP_LENGTH - 1);
1351 static void cliColor(char *cmdline)
1353 int i;
1354 char *ptr;
1356 if (isEmpty(cmdline)) {
1357 for (i = 0; i < CONFIGURABLE_COLOR_COUNT; i++) {
1358 cliPrintf("color %u %d,%u,%u\r\n",
1360 masterConfig.colors[i].h,
1361 masterConfig.colors[i].s,
1362 masterConfig.colors[i].v
1365 } else {
1366 ptr = cmdline;
1367 i = atoi(ptr);
1368 if (i < CONFIGURABLE_COLOR_COUNT) {
1369 ptr = strchr(cmdline, ' ');
1370 if (!parseColor(i, ++ptr)) {
1371 cliShowParseError();
1373 } else {
1374 cliShowArgumentRangeError("index", 0, CONFIGURABLE_COLOR_COUNT - 1);
1378 #endif
1380 #ifdef USE_SERVOS
1381 static void cliServo(char *cmdline)
1383 enum { SERVO_ARGUMENT_COUNT = 8 };
1384 int16_t arguments[SERVO_ARGUMENT_COUNT];
1386 servoParam_t *servo;
1388 int i;
1389 char *ptr;
1391 if (isEmpty(cmdline)) {
1392 // print out servo settings
1393 for (i = 0; i < MAX_SUPPORTED_SERVOS; i++) {
1394 servo = &masterConfig.servoConf[i];
1396 cliPrintf("servo %u %d %d %d %d %d %d %d\r\n",
1398 servo->min,
1399 servo->max,
1400 servo->middle,
1401 servo->angleAtMin,
1402 servo->angleAtMax,
1403 servo->rate,
1404 servo->forwardFromChannel
1407 } else {
1408 int validArgumentCount = 0;
1410 ptr = cmdline;
1412 // Command line is integers (possibly negative) separated by spaces, no other characters allowed.
1414 // If command line doesn't fit the format, don't modify the config
1415 while (*ptr) {
1416 if (*ptr == '-' || (*ptr >= '0' && *ptr <= '9')) {
1417 if (validArgumentCount >= SERVO_ARGUMENT_COUNT) {
1418 cliShowParseError();
1419 return;
1422 arguments[validArgumentCount++] = atoi(ptr);
1424 do {
1425 ptr++;
1426 } while (*ptr >= '0' && *ptr <= '9');
1427 } else if (*ptr == ' ') {
1428 ptr++;
1429 } else {
1430 cliShowParseError();
1431 return;
1435 enum {INDEX = 0, MIN, MAX, MIDDLE, ANGLE_AT_MIN, ANGLE_AT_MAX, RATE, FORWARD};
1437 i = arguments[INDEX];
1439 // Check we got the right number of args and the servo index is correct (don't validate the other values)
1440 if (validArgumentCount != SERVO_ARGUMENT_COUNT || i < 0 || i >= MAX_SUPPORTED_SERVOS) {
1441 cliShowParseError();
1442 return;
1445 servo = &masterConfig.servoConf[i];
1447 if (
1448 arguments[MIN] < PWM_PULSE_MIN || arguments[MIN] > PWM_PULSE_MAX ||
1449 arguments[MAX] < PWM_PULSE_MIN || arguments[MAX] > PWM_PULSE_MAX ||
1450 arguments[MIDDLE] < arguments[MIN] || arguments[MIDDLE] > arguments[MAX] ||
1451 arguments[MIN] > arguments[MAX] || arguments[MAX] < arguments[MIN] ||
1452 arguments[RATE] < -100 || arguments[RATE] > 100 ||
1453 arguments[FORWARD] >= MAX_SUPPORTED_RC_CHANNEL_COUNT ||
1454 arguments[ANGLE_AT_MIN] < 0 || arguments[ANGLE_AT_MIN] > 180 ||
1455 arguments[ANGLE_AT_MAX] < 0 || arguments[ANGLE_AT_MAX] > 180
1457 cliShowParseError();
1458 return;
1461 servo->min = arguments[1];
1462 servo->max = arguments[2];
1463 servo->middle = arguments[3];
1464 servo->angleAtMin = arguments[4];
1465 servo->angleAtMax = arguments[5];
1466 servo->rate = arguments[6];
1467 servo->forwardFromChannel = arguments[7];
1470 #endif
1472 #ifdef USE_SERVOS
1473 static void cliServoMix(char *cmdline)
1475 int i;
1476 uint8_t len;
1477 char *ptr;
1478 int args[8], check = 0;
1479 len = strlen(cmdline);
1481 if (len == 0) {
1483 cliPrint("Rule\tServo\tSource\tRate\tSpeed\tMin\tMax\tBox\r\n");
1485 for (i = 0; i < MAX_SERVO_RULES; i++) {
1486 if (masterConfig.customServoMixer[i].rate == 0)
1487 break;
1489 cliPrintf("#%d:\t%d\t%d\t%d\t%d\t%d\t%d\t%d\r\n",
1491 masterConfig.customServoMixer[i].targetChannel,
1492 masterConfig.customServoMixer[i].inputSource,
1493 masterConfig.customServoMixer[i].rate,
1494 masterConfig.customServoMixer[i].speed,
1495 masterConfig.customServoMixer[i].min,
1496 masterConfig.customServoMixer[i].max,
1497 masterConfig.customServoMixer[i].box
1500 cliPrintf("\r\n");
1501 return;
1502 } else if (strncasecmp(cmdline, "reset", 5) == 0) {
1503 // erase custom mixer
1504 memset(masterConfig.customServoMixer, 0, sizeof(masterConfig.customServoMixer));
1505 for (i = 0; i < MAX_SUPPORTED_SERVOS; i++) {
1506 masterConfig.servoConf[i].reversedSources = 0;
1508 } else if (strncasecmp(cmdline, "load", 4) == 0) {
1509 ptr = strchr(cmdline, ' ');
1510 if (ptr) {
1511 len = strlen(++ptr);
1512 for (i = 0; ; i++) {
1513 if (mixerNames[i] == NULL) {
1514 cliPrintf("Invalid name\r\n");
1515 break;
1517 if (strncasecmp(ptr, mixerNames[i], len) == 0) {
1518 servoMixerLoadMix(i, masterConfig.customServoMixer);
1519 cliPrintf("Loaded %s\r\n", mixerNames[i]);
1520 cliServoMix("");
1521 break;
1525 } else if (strncasecmp(cmdline, "reverse", 7) == 0) {
1526 enum {SERVO = 0, INPUT, REVERSE, ARGS_COUNT};
1527 int servoIndex, inputSource;
1528 ptr = strchr(cmdline, ' ');
1530 len = strlen(ptr);
1531 if (len == 0) {
1532 cliPrintf("s");
1533 for (inputSource = 0; inputSource < INPUT_SOURCE_COUNT; inputSource++)
1534 cliPrintf("\ti%d", inputSource);
1535 cliPrintf("\r\n");
1537 for (servoIndex = 0; servoIndex < MAX_SUPPORTED_SERVOS; servoIndex++) {
1538 cliPrintf("%d", servoIndex);
1539 for (inputSource = 0; inputSource < INPUT_SOURCE_COUNT; inputSource++)
1540 cliPrintf("\t%s ", (masterConfig.servoConf[servoIndex].reversedSources & (1 << inputSource)) ? "r" : "n");
1541 cliPrintf("\r\n");
1543 return;
1546 ptr = strtok(ptr, " ");
1547 while (ptr != NULL && check < ARGS_COUNT - 1) {
1548 args[check++] = atoi(ptr);
1549 ptr = strtok(NULL, " ");
1552 if (ptr == NULL || check != ARGS_COUNT - 1) {
1553 cliShowParseError();
1554 return;
1557 if (args[SERVO] >= 0 && args[SERVO] < MAX_SUPPORTED_SERVOS
1558 && args[INPUT] >= 0 && args[INPUT] < INPUT_SOURCE_COUNT
1559 && (*ptr == 'r' || *ptr == 'n')) {
1560 if (*ptr == 'r')
1561 masterConfig.servoConf[args[SERVO]].reversedSources |= 1 << args[INPUT];
1562 else
1563 masterConfig.servoConf[args[SERVO]].reversedSources &= ~(1 << args[INPUT]);
1564 } else
1565 cliShowParseError();
1567 cliServoMix("reverse");
1568 } else {
1569 enum {RULE = 0, TARGET, INPUT, RATE, SPEED, MIN, MAX, BOX, ARGS_COUNT};
1570 ptr = strtok(cmdline, " ");
1571 while (ptr != NULL && check < ARGS_COUNT) {
1572 args[check++] = atoi(ptr);
1573 ptr = strtok(NULL, " ");
1576 if (ptr != NULL || check != ARGS_COUNT) {
1577 cliShowParseError();
1578 return;
1581 i = args[RULE];
1582 if (i >= 0 && i < MAX_SERVO_RULES &&
1583 args[TARGET] >= 0 && args[TARGET] < MAX_SUPPORTED_SERVOS &&
1584 args[INPUT] >= 0 && args[INPUT] < INPUT_SOURCE_COUNT &&
1585 args[RATE] >= -100 && args[RATE] <= 100 &&
1586 args[SPEED] >= 0 && args[SPEED] <= MAX_SERVO_SPEED &&
1587 args[MIN] >= 0 && args[MIN] <= 100 &&
1588 args[MAX] >= 0 && args[MAX] <= 100 && args[MIN] < args[MAX] &&
1589 args[BOX] >= 0 && args[BOX] <= MAX_SERVO_BOXES) {
1590 masterConfig.customServoMixer[i].targetChannel = args[TARGET];
1591 masterConfig.customServoMixer[i].inputSource = args[INPUT];
1592 masterConfig.customServoMixer[i].rate = args[RATE];
1593 masterConfig.customServoMixer[i].speed = args[SPEED];
1594 masterConfig.customServoMixer[i].min = args[MIN];
1595 masterConfig.customServoMixer[i].max = args[MAX];
1596 masterConfig.customServoMixer[i].box = args[BOX];
1597 cliServoMix("");
1598 } else {
1599 cliShowParseError();
1603 #endif
1605 #ifdef USE_SDCARD
1607 static void cliWriteBytes(const uint8_t *buffer, int count)
1609 while (count > 0) {
1610 cliWrite(*buffer);
1611 buffer++;
1612 count--;
1616 static void cliSdInfo(char *cmdline) {
1617 UNUSED(cmdline);
1619 cliPrint("SD card: ");
1621 if (!sdcard_isInserted()) {
1622 cliPrint("None inserted\r\n");
1623 return;
1626 if (!sdcard_isInitialized()) {
1627 cliPrint("Startup failed\r\n");
1628 return;
1631 const sdcardMetadata_t *metadata = sdcard_getMetadata();
1633 cliPrintf("Manufacturer 0x%x, %ukB, %02d/%04d, v%d.%d, '",
1634 metadata->manufacturerID,
1635 metadata->numBlocks / 2, /* One block is half a kB */
1636 metadata->productionMonth,
1637 metadata->productionYear,
1638 metadata->productRevisionMajor,
1639 metadata->productRevisionMinor
1642 cliWriteBytes((uint8_t*)metadata->productName, sizeof(metadata->productName));
1644 cliPrint("'\r\n" "Filesystem: ");
1646 switch (afatfs_getFilesystemState()) {
1647 case AFATFS_FILESYSTEM_STATE_READY:
1648 cliPrint("Ready");
1649 break;
1650 case AFATFS_FILESYSTEM_STATE_INITIALIZATION:
1651 cliPrint("Initializing");
1652 break;
1653 case AFATFS_FILESYSTEM_STATE_UNKNOWN:
1654 case AFATFS_FILESYSTEM_STATE_FATAL:
1655 cliPrint("Fatal");
1657 switch (afatfs_getLastError()) {
1658 case AFATFS_ERROR_BAD_MBR:
1659 cliPrint(" - no FAT MBR partitions");
1660 break;
1661 case AFATFS_ERROR_BAD_FILESYSTEM_HEADER:
1662 cliPrint(" - bad FAT header");
1663 break;
1664 case AFATFS_ERROR_GENERIC:
1665 case AFATFS_ERROR_NONE:
1666 ; // Nothing more detailed to print
1667 break;
1670 cliPrint("\r\n");
1671 break;
1675 #endif
1677 #ifdef USE_FLASHFS
1679 static void cliFlashInfo(char *cmdline)
1681 const flashGeometry_t *layout = flashfsGetGeometry();
1683 UNUSED(cmdline);
1685 cliPrintf("Flash sectors=%u, sectorSize=%u, pagesPerSector=%u, pageSize=%u, totalSize=%u, usedSize=%u\r\n",
1686 layout->sectors, layout->sectorSize, layout->pagesPerSector, layout->pageSize, layout->totalSize, flashfsGetOffset());
1689 static void cliFlashErase(char *cmdline)
1691 UNUSED(cmdline);
1693 cliPrintf("Erasing...\r\n");
1694 flashfsEraseCompletely();
1696 while (!flashfsIsReady()) {
1697 delay(100);
1700 cliPrintf("Done.\r\n");
1703 #ifdef USE_FLASH_TOOLS
1705 static void cliFlashWrite(char *cmdline)
1707 uint32_t address = atoi(cmdline);
1708 char *text = strchr(cmdline, ' ');
1710 if (!text) {
1711 cliShowParseError();
1712 } else {
1713 flashfsSeekAbs(address);
1714 flashfsWrite((uint8_t*)text, strlen(text), true);
1715 flashfsFlushSync();
1717 cliPrintf("Wrote %u bytes at %u.\r\n", strlen(text), address);
1721 static void cliFlashRead(char *cmdline)
1723 uint32_t address = atoi(cmdline);
1724 uint32_t length;
1725 int i;
1727 uint8_t buffer[32];
1729 char *nextArg = strchr(cmdline, ' ');
1731 if (!nextArg) {
1732 cliShowParseError();
1733 } else {
1734 length = atoi(nextArg);
1736 cliPrintf("Reading %u bytes at %u:\r\n", length, address);
1738 while (length > 0) {
1739 int bytesRead;
1741 bytesRead = flashfsReadAbs(address, buffer, length < sizeof(buffer) ? length : sizeof(buffer));
1743 for (i = 0; i < bytesRead; i++) {
1744 cliWrite(buffer[i]);
1747 length -= bytesRead;
1748 address += bytesRead;
1750 if (bytesRead == 0) {
1751 //Assume we reached the end of the volume or something fatal happened
1752 break;
1755 cliPrintf("\r\n");
1759 #endif
1760 #endif
1762 static void dumpValues(uint16_t valueSection)
1764 uint32_t i;
1765 const clivalue_t *value;
1766 for (i = 0; i < VALUE_COUNT; i++) {
1767 value = &valueTable[i];
1769 if ((value->type & VALUE_SECTION_MASK) != valueSection) {
1770 continue;
1773 cliPrintf("set %s = ", valueTable[i].name);
1774 cliPrintVar(value, 0);
1775 cliPrint("\r\n");
1779 typedef enum {
1780 DUMP_MASTER = (1 << 0),
1781 DUMP_PROFILE = (1 << 1),
1782 DUMP_RATES = (1 << 2),
1783 DUMP_ALL = (1 << 3),
1784 } dumpFlags_e;
1787 static const char* const sectionBreak = "\r\n";
1789 #define printSectionBreak() cliPrintf((char *)sectionBreak)
1791 static void cliDump(char *cmdline)
1793 unsigned int i;
1794 char buf[16];
1795 uint32_t mask;
1797 #ifndef USE_QUAD_MIXER_ONLY
1798 float thr, roll, pitch, yaw;
1799 #endif
1801 uint8_t dumpMask = DUMP_MASTER;
1802 if (strcasecmp(cmdline, "master") == 0) {
1803 dumpMask = DUMP_MASTER; // only
1805 if (strcasecmp(cmdline, "profile") == 0) {
1806 dumpMask = DUMP_PROFILE; // only
1808 if (strcasecmp(cmdline, "rates") == 0) {
1809 dumpMask = DUMP_RATES;
1812 if (strcasecmp(cmdline, "all") == 0) {
1813 dumpMask = DUMP_ALL; // All profiles and rates
1816 if ((dumpMask & DUMP_MASTER) || (dumpMask & DUMP_ALL)) {
1818 cliPrint("\r\n# version\r\n");
1819 cliVersion(NULL);
1821 cliPrint("\r\n# dump master\r\n");
1822 cliPrint("\r\n# mixer\r\n");
1824 #ifndef USE_QUAD_MIXER_ONLY
1825 cliPrintf("mixer %s\r\n", mixerNames[masterConfig.mixerMode - 1]);
1827 cliPrintf("mmix reset\r\n");
1829 for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
1830 if (masterConfig.customMotorMixer[i].throttle == 0.0f)
1831 break;
1832 thr = masterConfig.customMotorMixer[i].throttle;
1833 roll = masterConfig.customMotorMixer[i].roll;
1834 pitch = masterConfig.customMotorMixer[i].pitch;
1835 yaw = masterConfig.customMotorMixer[i].yaw;
1836 cliPrintf("mmix %d", i);
1837 if (thr < 0)
1838 cliWrite(' ');
1839 cliPrintf("%s", ftoa(thr, buf));
1840 if (roll < 0)
1841 cliWrite(' ');
1842 cliPrintf("%s", ftoa(roll, buf));
1843 if (pitch < 0)
1844 cliWrite(' ');
1845 cliPrintf("%s", ftoa(pitch, buf));
1846 if (yaw < 0)
1847 cliWrite(' ');
1848 cliPrintf("%s\r\n", ftoa(yaw, buf));
1851 #ifdef USE_SERVOS
1852 // print custom servo mixer if exists
1853 cliPrintf("smix reset\r\n");
1855 for (i = 0; i < MAX_SERVO_RULES; i++) {
1857 if (masterConfig.customServoMixer[i].rate == 0)
1858 break;
1860 cliPrintf("smix %d %d %d %d %d %d %d %d\r\n",
1862 masterConfig.customServoMixer[i].targetChannel,
1863 masterConfig.customServoMixer[i].inputSource,
1864 masterConfig.customServoMixer[i].rate,
1865 masterConfig.customServoMixer[i].speed,
1866 masterConfig.customServoMixer[i].min,
1867 masterConfig.customServoMixer[i].max,
1868 masterConfig.customServoMixer[i].box
1872 #endif
1873 #endif
1875 cliPrint("\r\n\r\n# feature\r\n");
1877 mask = featureMask();
1878 for (i = 0; ; i++) { // disable all feature first
1879 if (featureNames[i] == NULL)
1880 break;
1881 cliPrintf("feature -%s\r\n", featureNames[i]);
1883 for (i = 0; ; i++) { // reenable what we want.
1884 if (featureNames[i] == NULL)
1885 break;
1886 if (mask & (1 << i))
1887 cliPrintf("feature %s\r\n", featureNames[i]);
1891 #ifdef BEEPER
1892 cliPrint("\r\n\r\n# beeper\r\n");
1894 uint8_t beeperCount = beeperTableEntryCount();
1895 mask = getBeeperOffMask();
1896 for (int i = 0; i < (beeperCount-2); i++) {
1897 if (mask & (1 << i))
1898 cliPrintf("beeper -%s\r\n", beeperNameForTableIndex(i));
1899 else
1900 cliPrintf("beeper %s\r\n", beeperNameForTableIndex(i));
1902 #endif
1905 cliPrint("\r\n\r\n# map\r\n");
1907 for (i = 0; i < 8; i++)
1908 buf[masterConfig.rxConfig.rcmap[i]] = rcChannelLetters[i];
1909 buf[i] = '\0';
1910 cliPrintf("map %s\r\n", buf);
1912 cliPrint("\r\n\r\n# serial\r\n");
1913 cliSerial("");
1915 #ifdef LED_STRIP
1916 cliPrint("\r\n\r\n# led\r\n");
1917 cliLed("");
1919 cliPrint("\r\n\r\n# color\r\n");
1920 cliColor("");
1921 #endif
1923 cliPrint("\r\n# aux\r\n");
1925 cliAux("");
1927 cliPrint("\r\n# adjrange\r\n");
1929 cliAdjustmentRange("");
1931 cliPrintf("\r\n# rxrange\r\n");
1933 cliRxRange("");
1935 #ifdef USE_SERVOS
1936 cliPrint("\r\n# servo\r\n");
1938 cliServo("");
1940 // print servo directions
1941 unsigned int channel;
1943 for (i = 0; i < MAX_SUPPORTED_SERVOS; i++) {
1944 for (channel = 0; channel < INPUT_SOURCE_COUNT; channel++) {
1945 if (servoDirection(i, channel) < 0) {
1946 cliPrintf("smix reverse %d %d r\r\n", i , channel);
1950 #endif
1952 printSectionBreak();
1953 dumpValues(MASTER_VALUE);
1955 cliPrint("\r\n# rxfail\r\n");
1956 cliRxFail("");
1958 if (dumpMask & DUMP_ALL) {
1959 uint8_t activeProfile = masterConfig.current_profile_index;
1960 uint8_t currentRateIndex = currentProfile->activeRateProfile;
1961 uint8_t profileCount;
1962 uint8_t rateCount;
1963 for (profileCount=0; profileCount<MAX_PROFILE_COUNT;profileCount++) {
1964 cliDumpProfile(profileCount);
1965 for (rateCount=0; rateCount<MAX_RATEPROFILES; rateCount++)
1966 cliDumpRateProfile(rateCount);
1969 changeProfile(activeProfile);
1970 changeControlRateProfile(currentRateIndex);
1971 } else {
1972 cliDumpProfile(masterConfig.current_profile_index);
1973 cliDumpRateProfile(currentProfile->activeRateProfile);
1977 if (dumpMask & DUMP_PROFILE) {
1978 cliDumpProfile(masterConfig.current_profile_index);
1981 if (dumpMask & DUMP_RATES) {
1982 cliDumpRateProfile(currentProfile->activeRateProfile);
1987 void cliDumpProfile(uint8_t profileIndex) {
1988 if (profileIndex >= MAX_PROFILE_COUNT) // Faulty values
1989 return;
1991 changeProfile(profileIndex);
1992 cliPrint("\r\n# profile\r\n");
1993 cliProfile("");
1994 cliPrintf("############################# PROFILE VALUES ####################################\r\n");
1995 cliProfile("");
1996 printSectionBreak();
1997 dumpValues(PROFILE_VALUE);
1999 cliRateProfile("");
2001 void cliDumpRateProfile(uint8_t rateProfileIndex) {
2002 if (rateProfileIndex >= MAX_RATEPROFILES) // Faulty values
2003 return;
2005 changeControlRateProfile(rateProfileIndex);
2006 cliPrint("\r\n# rateprofile\r\n");
2007 cliRateProfile("");
2008 printSectionBreak();
2010 dumpValues(PROFILE_RATE_VALUE);
2013 void cliEnter(serialPort_t *serialPort)
2015 cliMode = 1;
2016 cliPort = serialPort;
2017 setPrintfSerialPort(cliPort);
2018 cliWriter = bufWriterInit(cliWriteBuffer, sizeof(cliWriteBuffer),
2019 (bufWrite_t)serialWriteBufShim, serialPort);
2021 cliPrint("\r\nEntering CLI Mode, type 'exit' to return, or 'help'\r\n");
2022 cliPrompt();
2023 ENABLE_ARMING_FLAG(PREVENT_ARMING);
2026 static void cliExit(char *cmdline)
2028 UNUSED(cmdline);
2030 cliPrint("\r\nLeaving CLI mode, unsaved changes lost.\r\n");
2031 bufWriterFlush(cliWriter);
2033 *cliBuffer = '\0';
2034 bufferIndex = 0;
2035 cliMode = 0;
2036 // incase a motor was left running during motortest, clear it here
2037 mixerResetDisarmedMotors();
2038 cliReboot();
2040 cliWriter = NULL;
2043 static void cliFeature(char *cmdline)
2045 uint32_t i;
2046 uint32_t len;
2047 uint32_t mask;
2049 len = strlen(cmdline);
2050 mask = featureMask();
2052 if (len == 0) {
2053 cliPrint("Enabled: ");
2054 for (i = 0; ; i++) {
2055 if (featureNames[i] == NULL)
2056 break;
2057 if (mask & (1 << i))
2058 cliPrintf("%s ", featureNames[i]);
2060 cliPrint("\r\n");
2061 } else if (strncasecmp(cmdline, "list", len) == 0) {
2062 cliPrint("Available: ");
2063 for (i = 0; ; i++) {
2064 if (featureNames[i] == NULL)
2065 break;
2066 cliPrintf("%s ", featureNames[i]);
2068 cliPrint("\r\n");
2069 return;
2070 } else {
2071 bool remove = false;
2072 if (cmdline[0] == '-') {
2073 // remove feature
2074 remove = true;
2075 cmdline++; // skip over -
2076 len--;
2079 for (i = 0; ; i++) {
2080 if (featureNames[i] == NULL) {
2081 cliPrint("Invalid name\r\n");
2082 break;
2085 if (strncasecmp(cmdline, featureNames[i], len) == 0) {
2087 mask = 1 << i;
2088 #ifndef GPS
2089 if (mask & FEATURE_GPS) {
2090 cliPrint("unavailable\r\n");
2091 break;
2093 #endif
2094 #ifndef SONAR
2095 if (mask & FEATURE_SONAR) {
2096 cliPrint("unavailable\r\n");
2097 break;
2099 #endif
2100 if (remove) {
2101 featureClear(mask);
2102 cliPrint("Disabled");
2103 } else {
2104 featureSet(mask);
2105 cliPrint("Enabled");
2107 cliPrintf(" %s\r\n", featureNames[i]);
2108 break;
2114 #ifdef BEEPER
2115 static void cliBeeper(char *cmdline)
2117 uint32_t i;
2118 uint32_t len = strlen(cmdline);;
2119 uint8_t beeperCount = beeperTableEntryCount();
2120 uint32_t mask = getBeeperOffMask();
2122 if (len == 0) {
2123 cliPrintf("Disabled:");
2124 for (int i = 0; ; i++) {
2125 if (i == beeperCount-2){
2126 if (mask == 0)
2127 cliPrint(" none");
2128 break;
2130 if (mask & (1 << i))
2131 cliPrintf(" %s", beeperNameForTableIndex(i));
2133 cliPrint("\r\n");
2134 } else if (strncasecmp(cmdline, "list", len) == 0) {
2135 cliPrint("Available:");
2136 for (i = 0; i < beeperCount; i++)
2137 cliPrintf(" %s", beeperNameForTableIndex(i));
2138 cliPrint("\r\n");
2139 return;
2140 } else {
2141 bool remove = false;
2142 if (cmdline[0] == '-') {
2143 remove = true; // this is for beeper OFF condition
2144 cmdline++;
2145 len--;
2148 for (i = 0; ; i++) {
2149 if (i == beeperCount) {
2150 cliPrint("Invalid name\r\n");
2151 break;
2153 if (strncasecmp(cmdline, beeperNameForTableIndex(i), len) == 0) {
2154 if (remove) { // beeper off
2155 if (i == BEEPER_ALL-1)
2156 beeperOffSetAll(beeperCount-2);
2157 else
2158 if (i == BEEPER_PREFERENCE-1)
2159 setBeeperOffMask(getPreferedBeeperOffMask());
2160 else {
2161 mask = 1 << i;
2162 beeperOffSet(mask);
2164 cliPrint("Disabled");
2166 else { // beeper on
2167 if (i == BEEPER_ALL-1)
2168 beeperOffClearAll();
2169 else
2170 if (i == BEEPER_PREFERENCE-1)
2171 setPreferedBeeperOffMask(getBeeperOffMask());
2172 else {
2173 mask = 1 << i;
2174 beeperOffClear(mask);
2176 cliPrint("Enabled");
2178 cliPrintf(" %s\r\n", beeperNameForTableIndex(i));
2179 break;
2184 #endif
2187 #ifdef GPS
2188 static void cliGpsPassthrough(char *cmdline)
2190 UNUSED(cmdline);
2192 gpsEnablePassthrough(cliPort);
2194 #endif
2196 static void cliHelp(char *cmdline)
2198 uint32_t i = 0;
2200 UNUSED(cmdline);
2202 for (i = 0; i < CMD_COUNT; i++) {
2203 cliPrint(cmdTable[i].name);
2204 #ifndef SKIP_CLI_COMMAND_HELP
2205 if (cmdTable[i].description) {
2206 cliPrintf(" - %s", cmdTable[i].description);
2208 if (cmdTable[i].args) {
2209 cliPrintf("\r\n\t%s", cmdTable[i].args);
2211 #endif
2212 cliPrint("\r\n");
2216 static void cliMap(char *cmdline)
2218 uint32_t len;
2219 uint32_t i;
2220 char out[9];
2222 len = strlen(cmdline);
2224 if (len == 8) {
2225 // uppercase it
2226 for (i = 0; i < 8; i++)
2227 cmdline[i] = toupper((unsigned char)cmdline[i]);
2228 for (i = 0; i < 8; i++) {
2229 if (strchr(rcChannelLetters, cmdline[i]) && !strchr(cmdline + i + 1, cmdline[i]))
2230 continue;
2231 cliShowParseError();
2232 return;
2234 parseRcChannels(cmdline, &masterConfig.rxConfig);
2236 cliPrint("Map: ");
2237 for (i = 0; i < 8; i++)
2238 out[masterConfig.rxConfig.rcmap[i]] = rcChannelLetters[i];
2239 out[i] = '\0';
2240 cliPrintf("%s\r\n", out);
2243 #ifndef USE_QUAD_MIXER_ONLY
2244 static void cliMixer(char *cmdline)
2246 int i;
2247 int len;
2249 len = strlen(cmdline);
2251 if (len == 0) {
2252 cliPrintf("Mixer: %s\r\n", mixerNames[masterConfig.mixerMode - 1]);
2253 return;
2254 } else if (strncasecmp(cmdline, "list", len) == 0) {
2255 cliPrint("Available mixers: ");
2256 for (i = 0; ; i++) {
2257 if (mixerNames[i] == NULL)
2258 break;
2259 cliPrintf("%s ", mixerNames[i]);
2261 cliPrint("\r\n");
2262 return;
2265 for (i = 0; ; i++) {
2266 if (mixerNames[i] == NULL) {
2267 cliPrint("Invalid name\r\n");
2268 return;
2270 if (strncasecmp(cmdline, mixerNames[i], len) == 0) {
2271 masterConfig.mixerMode = i + 1;
2272 break;
2276 cliMixer("");
2278 #endif
2280 static void cliMotor(char *cmdline)
2282 int motor_index = 0;
2283 int motor_value = 0;
2284 int index = 0;
2285 char *pch = NULL;
2286 char *saveptr;
2288 if (isEmpty(cmdline)) {
2289 cliShowParseError();
2290 return;
2293 pch = strtok_r(cmdline, " ", &saveptr);
2294 while (pch != NULL) {
2295 switch (index) {
2296 case 0:
2297 motor_index = atoi(pch);
2298 break;
2299 case 1:
2300 motor_value = atoi(pch);
2301 break;
2303 index++;
2304 pch = strtok_r(NULL, " ", &saveptr);
2307 if (motor_index < 0 || motor_index >= MAX_SUPPORTED_MOTORS) {
2308 cliShowArgumentRangeError("index", 0, MAX_SUPPORTED_MOTORS - 1);
2309 return;
2312 if (index == 2) {
2313 if (motor_value < PWM_RANGE_MIN || motor_value > PWM_RANGE_MAX) {
2314 cliShowArgumentRangeError("value", 1000, 2000);
2315 return;
2316 } else {
2317 motor_disarmed[motor_index] = motor_value;
2321 cliPrintf("motor %d: %d\r\n", motor_index, motor_disarmed[motor_index]);
2324 static void cliPlaySound(char *cmdline)
2326 #if FLASH_SIZE <= 64
2327 UNUSED(cmdline);
2328 #else
2329 int i;
2330 const char *name;
2331 static int lastSoundIdx = -1;
2333 if (isEmpty(cmdline)) {
2334 i = lastSoundIdx + 1; //next sound index
2335 if ((name=beeperNameForTableIndex(i)) == NULL) {
2336 while (true) { //no name for index; try next one
2337 if (++i >= beeperTableEntryCount())
2338 i = 0; //if end then wrap around to first entry
2339 if ((name=beeperNameForTableIndex(i)) != NULL)
2340 break; //if name OK then play sound below
2341 if (i == lastSoundIdx + 1) { //prevent infinite loop
2342 cliPrintf("Error playing sound\r\n");
2343 return;
2347 } else { //index value was given
2348 i = atoi(cmdline);
2349 if ((name=beeperNameForTableIndex(i)) == NULL) {
2350 cliPrintf("No sound for index %d\r\n", i);
2351 return;
2354 lastSoundIdx = i;
2355 beeperSilence();
2356 cliPrintf("Playing sound %d: %s\r\n", i, name);
2357 beeper(beeperModeForTableIndex(i));
2358 #endif
2361 static void cliProfile(char *cmdline)
2363 int i;
2365 if (isEmpty(cmdline)) {
2366 cliPrintf("profile %d\r\n", getCurrentProfile());
2367 return;
2368 } else {
2369 i = atoi(cmdline);
2370 if (i >= 0 && i < MAX_PROFILE_COUNT) {
2371 masterConfig.current_profile_index = i;
2372 writeEEPROM();
2373 readEEPROM();
2374 cliProfile("");
2379 static void cliRateProfile(char *cmdline) {
2380 int i;
2382 if (isEmpty(cmdline)) {
2383 cliPrintf("rateprofile %d\r\n", getCurrentControlRateProfile());
2384 return;
2385 } else {
2386 i = atoi(cmdline);
2387 if (i >= 0 && i < MAX_RATEPROFILES) {
2388 changeControlRateProfile(i);
2389 cliRateProfile("");
2394 static void cliReboot(void) {
2395 cliPrint("\r\nRebooting");
2396 bufWriterFlush(cliWriter);
2397 waitForSerialPortToFinishTransmitting(cliPort);
2398 stopMotors();
2399 handleOneshotFeatureChangeOnRestart();
2400 systemReset();
2403 static void cliSave(char *cmdline)
2405 UNUSED(cmdline);
2407 cliPrint("Saving");
2408 //copyCurrentProfileToProfileSlot(masterConfig.current_profile_index);
2409 writeEEPROM();
2410 cliReboot();
2413 static void cliDefaults(char *cmdline)
2415 UNUSED(cmdline);
2417 cliPrint("Resetting to defaults");
2418 resetEEPROM();
2419 cliReboot();
2422 static void cliPrint(const char *str)
2424 while (*str)
2425 bufWriterAppend(cliWriter, *str++);
2428 static void cliPutp(void *p, char ch)
2430 bufWriterAppend(p, ch);
2433 static void cliPrintf(const char *fmt, ...)
2435 va_list va;
2436 va_start(va, fmt);
2437 tfp_format(cliWriter, cliPutp, fmt, va);
2438 va_end(va);
2441 static void cliWrite(uint8_t ch)
2443 bufWriterAppend(cliWriter, ch);
2446 static void cliPrintVar(const clivalue_t *var, uint32_t full)
2448 int32_t value = 0;
2449 char buf[13];
2451 void *ptr = var->ptr;
2452 if ((var->type & VALUE_SECTION_MASK) == PROFILE_VALUE) {
2453 ptr = ((uint8_t *)ptr) + (sizeof(profile_t) * masterConfig.current_profile_index);
2456 if ((var->type & VALUE_SECTION_MASK) == PROFILE_RATE_VALUE) {
2457 ptr = ((uint8_t *)ptr) + (sizeof(profile_t) * masterConfig.current_profile_index) + (sizeof(controlRateConfig_t) * getCurrentControlRateProfile());
2460 switch (var->type & VALUE_TYPE_MASK) {
2461 case VAR_UINT8:
2462 value = *(uint8_t *)ptr;
2463 break;
2465 case VAR_INT8:
2466 value = *(int8_t *)ptr;
2467 break;
2469 case VAR_UINT16:
2470 value = *(uint16_t *)ptr;
2471 break;
2473 case VAR_INT16:
2474 value = *(int16_t *)ptr;
2475 break;
2477 case VAR_UINT32:
2478 value = *(uint32_t *)ptr;
2479 break;
2481 case VAR_FLOAT:
2482 cliPrintf("%s", ftoa(*(float *)ptr, buf));
2483 if (full && (var->type & VALUE_MODE_MASK) == MODE_DIRECT) {
2484 cliPrintf(" %s", ftoa((float)var->config.minmax.min, buf));
2485 cliPrintf(" %s", ftoa((float)var->config.minmax.max, buf));
2487 return; // return from case for float only
2490 switch(var->type & VALUE_MODE_MASK) {
2491 case MODE_DIRECT:
2492 cliPrintf("%d", value);
2493 if (full) {
2494 cliPrintf(" %d %d", var->config.minmax.min, var->config.minmax.max);
2496 break;
2497 case MODE_LOOKUP:
2498 cliPrintf(lookupTables[var->config.lookup.tableIndex].values[value]);
2499 break;
2502 static void cliPrintVarRange(const clivalue_t *var)
2504 switch (var->type & VALUE_MODE_MASK) {
2505 case (MODE_DIRECT): {
2506 cliPrintf("Allowed range: %d - %d\n", var->config.minmax.min, var->config.minmax.max);
2508 break;
2509 case (MODE_LOOKUP): {
2510 const lookupTableEntry_t *tableEntry = &lookupTables[var->config.lookup.tableIndex];
2511 cliPrint("Allowed values:");
2512 uint8_t i;
2513 for (i = 0; i < tableEntry->valueCount ; i++) {
2514 if (i > 0)
2515 cliPrint(",");
2516 cliPrintf(" %s", tableEntry->values[i]);
2518 cliPrint("\n");
2520 break;
2523 static void cliSetVar(const clivalue_t *var, const int_float_value_t value)
2525 void *ptr = var->ptr;
2526 if ((var->type & VALUE_SECTION_MASK) == PROFILE_VALUE) {
2527 ptr = ((uint8_t *)ptr) + (sizeof(profile_t) * masterConfig.current_profile_index);
2529 if ((var->type & VALUE_SECTION_MASK) == PROFILE_RATE_VALUE) {
2530 ptr = ((uint8_t *)ptr) + (sizeof(profile_t) * masterConfig.current_profile_index) + (sizeof(controlRateConfig_t) * getCurrentControlRateProfile());
2533 switch (var->type & VALUE_TYPE_MASK) {
2534 case VAR_UINT8:
2535 case VAR_INT8:
2536 *(int8_t *)ptr = value.int_value;
2537 break;
2539 case VAR_UINT16:
2540 case VAR_INT16:
2541 *(int16_t *)ptr = value.int_value;
2542 break;
2544 case VAR_UINT32:
2545 *(uint32_t *)ptr = value.int_value;
2546 break;
2548 case VAR_FLOAT:
2549 *(float *)ptr = (float)value.float_value;
2550 break;
2554 static void cliSet(char *cmdline)
2556 uint32_t i;
2557 uint32_t len;
2558 const clivalue_t *val;
2559 char *eqptr = NULL;
2561 len = strlen(cmdline);
2563 if (len == 0 || (len == 1 && cmdline[0] == '*')) {
2564 cliPrint("Current settings: \r\n");
2565 for (i = 0; i < VALUE_COUNT; i++) {
2566 val = &valueTable[i];
2567 cliPrintf("%s = ", valueTable[i].name);
2568 cliPrintVar(val, len); // when len is 1 (when * is passed as argument), it will print min/max values as well, for gui
2569 cliPrint("\r\n");
2571 } else if ((eqptr = strstr(cmdline, "=")) != NULL) {
2572 // has equals
2574 char *lastNonSpaceCharacter = eqptr;
2575 while (*(lastNonSpaceCharacter - 1) == ' ') {
2576 lastNonSpaceCharacter--;
2578 uint8_t variableNameLength = lastNonSpaceCharacter - cmdline;
2580 // skip the '=' and any ' ' characters
2581 eqptr++;
2582 while (*(eqptr) == ' ') {
2583 eqptr++;
2586 for (i = 0; i < VALUE_COUNT; i++) {
2587 val = &valueTable[i];
2588 // ensure exact match when setting to prevent setting variables with shorter names
2589 if (strncasecmp(cmdline, valueTable[i].name, strlen(valueTable[i].name)) == 0 && variableNameLength == strlen(valueTable[i].name)) {
2591 bool changeValue = false;
2592 int_float_value_t tmp;
2593 switch (valueTable[i].type & VALUE_MODE_MASK) {
2594 case MODE_DIRECT: {
2595 int32_t value = 0;
2596 float valuef = 0;
2598 value = atoi(eqptr);
2599 valuef = fastA2F(eqptr);
2601 if (valuef >= valueTable[i].config.minmax.min && valuef <= valueTable[i].config.minmax.max) { // note: compare float value
2603 if ((valueTable[i].type & VALUE_TYPE_MASK) == VAR_FLOAT)
2604 tmp.float_value = valuef;
2605 else
2606 tmp.int_value = value;
2608 changeValue = true;
2611 break;
2612 case MODE_LOOKUP: {
2613 const lookupTableEntry_t *tableEntry = &lookupTables[valueTable[i].config.lookup.tableIndex];
2614 bool matched = false;
2615 for (uint8_t tableValueIndex = 0; tableValueIndex < tableEntry->valueCount && !matched; tableValueIndex++) {
2616 matched = strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
2618 if (matched) {
2619 tmp.int_value = tableValueIndex;
2620 changeValue = true;
2624 break;
2627 if (changeValue) {
2628 cliSetVar(val, tmp);
2630 cliPrintf("%s set to ", valueTable[i].name);
2631 cliPrintVar(val, 0);
2632 } else {
2633 cliPrint("Invalid value\r\n");
2634 cliPrintVarRange(val);
2637 return;
2640 cliPrint("Invalid name\r\n");
2641 } else {
2642 // no equals, check for matching variables.
2643 cliGet(cmdline);
2647 static void cliGet(char *cmdline)
2649 uint32_t i;
2650 const clivalue_t *val;
2651 int matchedCommands = 0;
2653 for (i = 0; i < VALUE_COUNT; i++) {
2654 if (strstr(valueTable[i].name, cmdline)) {
2655 val = &valueTable[i];
2656 cliPrintf("%s = ", valueTable[i].name);
2657 cliPrintVar(val, 0);
2658 cliPrint("\n");
2659 cliPrintVarRange(val);
2660 cliPrint("\r\n");
2662 matchedCommands++;
2667 if (matchedCommands) {
2668 return;
2671 cliPrint("Invalid name\r\n");
2674 static void cliStatus(char *cmdline)
2676 UNUSED(cmdline);
2678 cliPrintf("System Uptime: %d seconds, Voltage: %d * 0.1V (%dS battery - %s), CPU:%d%%\r\n",
2679 millis() / 1000,
2680 vbat,
2681 batteryCellCount,
2682 getBatteryStateString(),
2683 constrain(averageSystemLoadPercent, 0, 100)
2686 cliPrintf("CPU Clock=%dMHz", (SystemCoreClock / 1000000));
2688 #ifndef CJMCU
2689 uint8_t i;
2690 uint32_t mask;
2691 uint32_t detectedSensorsMask = sensorsMask();
2693 for (i = 0; ; i++) {
2695 if (sensorTypeNames[i] == NULL)
2696 break;
2698 mask = (1 << i);
2699 if ((detectedSensorsMask & mask) && (mask & SENSOR_NAMES_MASK)) {
2700 const char *sensorHardware;
2701 uint8_t sensorHardwareIndex = detectedSensors[i];
2702 sensorHardware = sensorHardwareNames[i][sensorHardwareIndex];
2704 cliPrintf(", %s=%s", sensorTypeNames[i], sensorHardware);
2706 if (mask == SENSOR_ACC && acc.revisionCode) {
2707 cliPrintf(".%c", acc.revisionCode);
2711 #endif
2712 cliPrint("\r\n");
2714 #ifdef USE_I2C
2715 uint16_t i2cErrorCounter = i2cGetErrorCounter();
2716 #else
2717 uint16_t i2cErrorCounter = 0;
2718 #endif
2720 cliPrintf("Cycle Time: %d, I2C Errors: %d, config size: %d\r\n", cycleTime, i2cErrorCounter, sizeof(master_t));
2723 #ifndef SKIP_TASK_STATISTICS
2724 static void cliTasks(char *cmdline)
2726 UNUSED(cmdline);
2728 cfTaskId_e taskId;
2729 cfTaskInfo_t taskInfo;
2731 cliPrintf("Task list:\r\n");
2732 for (taskId = 0; taskId < TASK_COUNT; taskId++) {
2733 getTaskInfo(taskId, &taskInfo);
2734 if (taskInfo.isEnabled) {
2735 uint16_t taskFrequency;
2736 uint16_t subTaskFrequency;
2738 uint32_t taskTotalTime = taskInfo.totalExecutionTime / 1000;
2740 if (taskId == TASK_GYROPID) {
2741 subTaskFrequency = (uint16_t)(1.0f / ((float)cycleTime * 0.000001f));
2742 if (masterConfig.pid_process_denom > 1) {
2743 taskFrequency = subTaskFrequency / masterConfig.pid_process_denom;
2744 cliPrintf("%d - (%s) ", taskId, taskInfo.taskName);
2745 } else {
2746 taskFrequency = subTaskFrequency;
2747 cliPrintf("%d - (%s/%s) ", taskId, taskInfo.subTaskName, taskInfo.taskName);
2749 } else {
2750 taskFrequency = (uint16_t)(1.0f / ((float)taskInfo.latestDeltaTime * 0.000001f));
2751 cliPrintf("%d - (%s) ", taskId, taskInfo.taskName);
2754 cliPrintf("max: %dus, avg: %dus, rate: %dhz, total: ", taskInfo.maxExecutionTime, taskInfo.averageExecutionTime, taskFrequency);
2756 if (taskTotalTime >= 1000) {
2757 cliPrintf("%dsec", taskTotalTime / 1000);
2758 } else {
2759 cliPrintf("%dms", taskTotalTime);
2762 if (taskId == TASK_GYROPID && masterConfig.pid_process_denom > 1) cliPrintf("\r\n- - (%s) rate: %dhz", taskInfo.subTaskName, subTaskFrequency);
2763 cliPrintf("\r\n", taskTotalTime);
2767 #endif
2769 static void cliVersion(char *cmdline)
2771 UNUSED(cmdline);
2773 cliPrintf("# BetaFlight/%s %s %s / %s (%s)",
2774 targetName,
2775 FC_VERSION_STRING,
2776 buildDate,
2777 buildTime,
2778 shortGitRevision
2782 void cliProcess(void)
2784 if (!cliWriter) {
2785 return;
2788 // Be a little bit tricky. Flush the last inputs buffer, if any.
2789 bufWriterFlush(cliWriter);
2791 while (serialRxBytesWaiting(cliPort)) {
2792 uint8_t c = serialRead(cliPort);
2793 if (c == '\t' || c == '?') {
2794 // do tab completion
2795 const clicmd_t *cmd, *pstart = NULL, *pend = NULL;
2796 uint32_t i = bufferIndex;
2797 for (cmd = cmdTable; cmd < cmdTable + CMD_COUNT; cmd++) {
2798 if (bufferIndex && (strncasecmp(cliBuffer, cmd->name, bufferIndex) != 0))
2799 continue;
2800 if (!pstart)
2801 pstart = cmd;
2802 pend = cmd;
2804 if (pstart) { /* Buffer matches one or more commands */
2805 for (; ; bufferIndex++) {
2806 if (pstart->name[bufferIndex] != pend->name[bufferIndex])
2807 break;
2808 if (!pstart->name[bufferIndex] && bufferIndex < sizeof(cliBuffer) - 2) {
2809 /* Unambiguous -- append a space */
2810 cliBuffer[bufferIndex++] = ' ';
2811 cliBuffer[bufferIndex] = '\0';
2812 break;
2814 cliBuffer[bufferIndex] = pstart->name[bufferIndex];
2817 if (!bufferIndex || pstart != pend) {
2818 /* Print list of ambiguous matches */
2819 cliPrint("\r\033[K");
2820 for (cmd = pstart; cmd <= pend; cmd++) {
2821 cliPrint(cmd->name);
2822 cliWrite('\t');
2824 cliPrompt();
2825 i = 0; /* Redraw prompt */
2827 for (; i < bufferIndex; i++)
2828 cliWrite(cliBuffer[i]);
2829 } else if (!bufferIndex && c == 4) { // CTRL-D
2830 cliExit(cliBuffer);
2831 return;
2832 } else if (c == 12) { // NewPage / CTRL-L
2833 // clear screen
2834 cliPrint("\033[2J\033[1;1H");
2835 cliPrompt();
2836 } else if (bufferIndex && (c == '\n' || c == '\r')) {
2837 // enter pressed
2838 cliPrint("\r\n");
2840 // Strip comment starting with # from line
2841 char *p = cliBuffer;
2842 p = strchr(p, '#');
2843 if (NULL != p) {
2844 bufferIndex = (uint32_t)(p - cliBuffer);
2847 // Strip trailing whitespace
2848 while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') {
2849 bufferIndex--;
2852 // Process non-empty lines
2853 if (bufferIndex > 0) {
2854 cliBuffer[bufferIndex] = 0; // null terminate
2856 const clicmd_t *cmd;
2857 for (cmd = cmdTable; cmd < cmdTable + CMD_COUNT; cmd++) {
2858 if(!strncasecmp(cliBuffer, cmd->name, strlen(cmd->name)) // command names match
2859 && !isalnum((unsigned)cliBuffer[strlen(cmd->name)])) // next characted in bufffer is not alphanumeric (command is correctly terminated)
2860 break;
2862 if(cmd < cmdTable + CMD_COUNT)
2863 cmd->func(cliBuffer + strlen(cmd->name) + 1);
2864 else
2865 cliPrint("Unknown command, try 'help'");
2866 bufferIndex = 0;
2869 memset(cliBuffer, 0, sizeof(cliBuffer));
2871 // 'exit' will reset this flag, so we don't need to print prompt again
2872 if (!cliMode)
2873 return;
2875 cliPrompt();
2876 } else if (c == 127) {
2877 // backspace
2878 if (bufferIndex) {
2879 cliBuffer[--bufferIndex] = 0;
2880 cliPrint("\010 \010");
2882 } else if (bufferIndex < sizeof(cliBuffer) && c >= 32 && c <= 126) {
2883 if (!bufferIndex && c == ' ')
2884 continue; // Ignore leading spaces
2885 cliBuffer[bufferIndex++] = c;
2886 cliWrite(c);
2891 void cliInit(serialConfig_t *serialConfig)
2893 UNUSED(serialConfig);
2895 #endif