1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-15 04:15:44 +03:00

Reduce code size of serial_cli by 104 bytes with some small tweaks:

Avoid calling strlen() just to check if a string is empty
Shorter error messages and reuse messages for cliServo
Use cliPrint rather than printf when not using placeholders
This commit is contained in:
Nicholas Sherlock 2015-02-18 13:07:26 +13:00
parent 716ee9f5c4
commit 155a369491

View file

@ -190,7 +190,7 @@ const clicmd_t cmdTable[] = {
{ "profile", "index (0 to 2)", cliProfile }, { "profile", "index (0 to 2)", cliProfile },
{ "rateprofile", "index (0 to 2)", cliRateProfile }, { "rateprofile", "index (0 to 2)", cliRateProfile },
{ "save", "save and reboot", cliSave }, { "save", "save and reboot", cliSave },
{ "servo", "get/set servo configuration", cliServo }, { "servo", "servo config", cliServo },
{ "set", "name=value or blank or * for list", cliSet }, { "set", "name=value or blank or * for list", cliSet },
{ "status", "show system status", cliStatus }, { "status", "show system status", cliStatus },
{ "version", "", cliVersion }, { "version", "", cliVersion },
@ -470,14 +470,18 @@ static char *processChannelRangeArgs(char *ptr, channelRange_t *range, uint8_t *
return ptr; return ptr;
} }
// Check if a string's length is zero
static bool isEmpty(const char *string)
{
return *string == '\0';
}
static void cliAux(char *cmdline) static void cliAux(char *cmdline)
{ {
int i, val = 0; int i, val = 0;
uint8_t len;
char *ptr; char *ptr;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
// print out aux channel settings // print out aux channel settings
for (i = 0; i < MAX_MODE_ACTIVATION_CONDITION_COUNT; i++) { for (i = 0; i < MAX_MODE_ACTIVATION_CONDITION_COUNT; i++) {
modeActivationCondition_t *mac = &currentProfile->modeActivationConditions[i]; modeActivationCondition_t *mac = &currentProfile->modeActivationConditions[i];
@ -526,11 +530,9 @@ static void cliAux(char *cmdline)
static void cliAdjustmentRange(char *cmdline) static void cliAdjustmentRange(char *cmdline)
{ {
int i, val = 0; int i, val = 0;
uint8_t len;
char *ptr; char *ptr;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
// print out adjustment ranges channel settings // print out adjustment ranges channel settings
for (i = 0; i < MAX_ADJUSTMENT_RANGE_COUNT; i++) { for (i = 0; i < MAX_ADJUSTMENT_RANGE_COUNT; i++) {
adjustmentRange_t *ar = &currentProfile->adjustmentRanges[i]; adjustmentRange_t *ar = &currentProfile->adjustmentRanges[i];
@ -605,9 +607,7 @@ static void cliCMix(char *cmdline)
float mixsum[3]; float mixsum[3];
char *ptr; char *ptr;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
cliPrint("Custom mixer: \r\nMotor\tThr\tRoll\tPitch\tYaw\r\n"); cliPrint("Custom mixer: \r\nMotor\tThr\tRoll\tPitch\tYaw\r\n");
for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) { for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
if (masterConfig.customMixer[i].throttle == 0.0f) if (masterConfig.customMixer[i].throttle == 0.0f)
@ -691,12 +691,10 @@ static void cliCMix(char *cmdline)
static void cliLed(char *cmdline) static void cliLed(char *cmdline)
{ {
int i; int i;
uint8_t len;
char *ptr; char *ptr;
char ledConfigBuffer[20]; char ledConfigBuffer[20];
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
for (i = 0; i < MAX_LED_STRIP_LENGTH; i++) { for (i = 0; i < MAX_LED_STRIP_LENGTH; i++) {
generateLedConfig(i, ledConfigBuffer, sizeof(ledConfigBuffer)); generateLedConfig(i, ledConfigBuffer, sizeof(ledConfigBuffer));
printf("led %u %s\r\n", i, ledConfigBuffer); printf("led %u %s\r\n", i, ledConfigBuffer);
@ -707,7 +705,7 @@ static void cliLed(char *cmdline)
if (i < MAX_LED_STRIP_LENGTH) { if (i < MAX_LED_STRIP_LENGTH) {
ptr = strchr(cmdline, ' '); ptr = strchr(cmdline, ' ');
if (!parseLedStripConfig(i, ++ptr)) { if (!parseLedStripConfig(i, ++ptr)) {
printf("Parse error\r\n"); cliPrint("Parse error\r\n");
} }
} else { } else {
printf("Invalid led index: must be < %u\r\n", MAX_LED_STRIP_LENGTH); printf("Invalid led index: must be < %u\r\n", MAX_LED_STRIP_LENGTH);
@ -718,11 +716,9 @@ static void cliLed(char *cmdline)
static void cliColor(char *cmdline) static void cliColor(char *cmdline)
{ {
int i; int i;
uint8_t len;
char *ptr; char *ptr;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
for (i = 0; i < CONFIGURABLE_COLOR_COUNT; i++) { for (i = 0; i < CONFIGURABLE_COLOR_COUNT; i++) {
printf("color %u %d,%u,%u\r\n", i, masterConfig.colors[i].h, masterConfig.colors[i].s, masterConfig.colors[i].v); printf("color %u %d,%u,%u\r\n", i, masterConfig.colors[i].h, masterConfig.colors[i].s, masterConfig.colors[i].v);
} }
@ -732,7 +728,7 @@ static void cliColor(char *cmdline)
if (i < CONFIGURABLE_COLOR_COUNT) { if (i < CONFIGURABLE_COLOR_COUNT) {
ptr = strchr(cmdline, ' '); ptr = strchr(cmdline, ' ');
if (!parseColor(i, ++ptr)) { if (!parseColor(i, ++ptr)) {
printf("Parse error\r\n", CONFIGURABLE_COLOR_COUNT); cliPrint("Parse error\r\n");
} }
} else { } else {
printf("Invalid color index: must be < %u\r\n", CONFIGURABLE_COLOR_COUNT); printf("Invalid color index: must be < %u\r\n", CONFIGURABLE_COLOR_COUNT);
@ -749,11 +745,9 @@ static void cliServo(char *cmdline)
servoParam_t *servo; servoParam_t *servo;
int i; int i;
uint8_t len;
char *ptr; char *ptr;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
// print out servo settings // print out servo settings
for (i = 0; i < MAX_SUPPORTED_SERVOS; i++) { for (i = 0; i < MAX_SUPPORTED_SERVOS; i++) {
servo = &currentProfile->servoConf[i]; servo = &currentProfile->servoConf[i];
@ -778,7 +772,7 @@ static void cliServo(char *cmdline)
while (*ptr) { while (*ptr) {
if (*ptr == '-' || (*ptr >= '0' && *ptr <= '9')) { if (*ptr == '-' || (*ptr >= '0' && *ptr <= '9')) {
if (validArgumentCount >= SERVO_ARGUMENT_COUNT) { if (validArgumentCount >= SERVO_ARGUMENT_COUNT) {
printf("Wrong argument count\r\n"); cliPrint("Parse error\r\n");
return; return;
} }
@ -790,18 +784,14 @@ static void cliServo(char *cmdline)
} else if (*ptr == ' ') { } else if (*ptr == ' ') {
ptr++; ptr++;
} else { } else {
printf("Bad argument\r\n"); cliPrint("Parse error\r\n");
return; return;
} }
} }
if (validArgumentCount != SERVO_ARGUMENT_COUNT) { // Check we got the right number of args and the servo index is correct (don't validate the other values)
printf("Wrong argument count\r\n"); if (validArgumentCount != SERVO_ARGUMENT_COUNT || arguments[0] < 0 || arguments[0] >= MAX_SUPPORTED_SERVOS) {
return; cliPrint("Parse error\r\n");
}
if (arguments[0] < 0 || arguments[0] >= MAX_SUPPORTED_SERVOS) {
printf("Bad argument\r\n");
return; return;
} }
@ -868,11 +858,11 @@ static void cliDump(char *cmdline)
if (dumpMask & DUMP_MASTER) { if (dumpMask & DUMP_MASTER) {
printf("\r\n# version\r\n"); cliPrint("\r\n# version\r\n");
cliVersion(NULL); cliVersion(NULL);
printf("\r\n# dump master\r\n"); cliPrint("\r\n# dump master\r\n");
printf("\r\n# mixer\r\n"); cliPrint("\r\n# mixer\r\n");
#ifndef USE_QUAD_MIXER_ONLY #ifndef USE_QUAD_MIXER_ONLY
printf("mixer %s\r\n", mixerNames[masterConfig.mixerMode - 1]); printf("mixer %s\r\n", mixerNames[masterConfig.mixerMode - 1]);
@ -887,23 +877,23 @@ static void cliDump(char *cmdline)
yaw = masterConfig.customMixer[i].yaw; yaw = masterConfig.customMixer[i].yaw;
printf("cmix %d", i + 1); printf("cmix %d", i + 1);
if (thr < 0) if (thr < 0)
printf(" "); cliWrite(' ');
printf("%s", ftoa(thr, buf)); printf("%s", ftoa(thr, buf));
if (roll < 0) if (roll < 0)
printf(" "); cliWrite(' ');
printf("%s", ftoa(roll, buf)); printf("%s", ftoa(roll, buf));
if (pitch < 0) if (pitch < 0)
printf(" "); cliWrite(' ');
printf("%s", ftoa(pitch, buf)); printf("%s", ftoa(pitch, buf));
if (yaw < 0) if (yaw < 0)
printf(" "); cliWrite(' ');
printf("%s\r\n", ftoa(yaw, buf)); printf("%s\r\n", ftoa(yaw, buf));
} }
printf("cmix %d 0 0 0 0\r\n", i + 1); printf("cmix %d 0 0 0 0\r\n", i + 1);
} }
#endif #endif
printf("\r\n\r\n# feature\r\n"); cliPrint("\r\n\r\n# feature\r\n");
mask = featureMask(); mask = featureMask();
for (i = 0; ; i++) { // disable all feature first for (i = 0; ; i++) { // disable all feature first
@ -918,7 +908,7 @@ static void cliDump(char *cmdline)
printf("feature %s\r\n", featureNames[i]); printf("feature %s\r\n", featureNames[i]);
} }
printf("\r\n\r\n# map\r\n"); cliPrint("\r\n\r\n# map\r\n");
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
buf[masterConfig.rxConfig.rcmap[i]] = rcChannelLetters[i]; buf[masterConfig.rxConfig.rcmap[i]] = rcChannelLetters[i];
@ -926,10 +916,10 @@ static void cliDump(char *cmdline)
printf("map %s\r\n", buf); printf("map %s\r\n", buf);
#ifdef LED_STRIP #ifdef LED_STRIP
printf("\r\n\r\n# led\r\n"); cliPrint("\r\n\r\n# led\r\n");
cliLed(""); cliLed("");
printf("\r\n\r\n# color\r\n"); cliPrint("\r\n\r\n# color\r\n");
cliColor(""); cliColor("");
#endif #endif
printSectionBreak(); printSectionBreak();
@ -937,20 +927,20 @@ static void cliDump(char *cmdline)
} }
if (dumpMask & DUMP_PROFILE) { if (dumpMask & DUMP_PROFILE) {
printf("\r\n# dump profile\r\n"); cliPrint("\r\n# dump profile\r\n");
printf("\r\n# profile\r\n"); cliPrint("\r\n# profile\r\n");
cliProfile(""); cliProfile("");
printf("\r\n# aux\r\n"); cliPrint("\r\n# aux\r\n");
cliAux(""); cliAux("");
printf("\r\n# adjrange\r\n"); cliPrint("\r\n# adjrange\r\n");
cliAdjustmentRange(""); cliAdjustmentRange("");
printf("\r\n# servo\r\n"); cliPrint("\r\n# servo\r\n");
cliServo(""); cliServo("");
@ -960,9 +950,9 @@ static void cliDump(char *cmdline)
} }
if (dumpMask & DUMP_CONTROL_RATE_PROFILE) { if (dumpMask & DUMP_CONTROL_RATE_PROFILE) {
printf("\r\n# dump rates\r\n"); cliPrint("\r\n# dump rates\r\n");
printf("\r\n# rateprofile\r\n"); cliPrint("\r\n# rateprofile\r\n");
cliRateProfile(""); cliRateProfile("");
printSectionBreak(); printSectionBreak();
@ -1159,12 +1149,11 @@ static void cliMotor(char *cmdline)
{ {
int motor_index = 0; int motor_index = 0;
int motor_value = 0; int motor_value = 0;
int len, index = 0; int index = 0;
char *pch = NULL; char *pch = NULL;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) { cliPrint("Usage:\r\nmotor index [value] - show [or set] motor value\r\n");
printf("Usage:\r\nmotor index [value] - show [or set] motor value\r\n");
return; return;
} }
@ -1193,7 +1182,7 @@ static void cliMotor(char *cmdline)
} }
if (motor_value < PWM_RANGE_MIN || motor_value > PWM_RANGE_MAX) { if (motor_value < PWM_RANGE_MIN || motor_value > PWM_RANGE_MAX) {
printf("Invalid motor value, 1000..2000\r\n"); cliPrint("Invalid motor value, 1000..2000\r\n");
return; return;
} }
@ -1203,11 +1192,9 @@ static void cliMotor(char *cmdline)
static void cliProfile(char *cmdline) static void cliProfile(char *cmdline)
{ {
uint8_t len;
int i; int i;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
printf("profile %d\r\n", getCurrentProfile()); printf("profile %d\r\n", getCurrentProfile());
return; return;
} else { } else {
@ -1223,11 +1210,9 @@ static void cliProfile(char *cmdline)
static void cliRateProfile(char *cmdline) static void cliRateProfile(char *cmdline)
{ {
uint8_t len;
int i; int i;
len = strlen(cmdline); if (isEmpty(cmdline)) {
if (len == 0) {
printf("rateprofile %d\r\n", getCurrentControlRateProfile()); printf("rateprofile %d\r\n", getCurrentControlRateProfile());
return; return;
} else { } else {
@ -1421,7 +1406,7 @@ static void cliGet(char *cmdline)
val = &valueTable[i]; val = &valueTable[i];
printf("%s = ", valueTable[i].name); printf("%s = ", valueTable[i].name);
cliPrintVar(val, 0); cliPrintVar(val, 0);
printf("\r\n"); cliPrint("\r\n");
matchedCommands++; matchedCommands++;
} }