static void cliSet(char *cmdline) { uint32_t i; uint32_t len; const clivalue_t *val; char *eqptr = NULL; int32_t value = 0; float valuef = 0; len = strlen(cmdline); if (len == 0 || (len == 1 && cmdline[0] == '*')) { cliPrint("Current settings: \r\n"); for (i = 0; i < VALUE_COUNT; i++) { val = &valueTable[i]; printf("%s = ", valueTable[i].name); cliPrintVar(val, len); // when len is 1 (when * is passed as argument), it will print min/max values as well, for gui cliPrint("\r\n"); } } else if ((eqptr = strstr(cmdline, "=")) != NULL) { // has equal, set var char *lastNonSpaceCharacter = eqptr; while (*(lastNonSpaceCharacter - 1) == ' ') { lastNonSpaceCharacter--; } uint8_t variableNameLength = lastNonSpaceCharacter - cmdline; eqptr++; len--; value = atoi(eqptr); valuef = fastA2F(eqptr); for (i = 0; i < VALUE_COUNT; i++) { val = &valueTable[i]; // ensure exact match when setting to prevent setting variables with shorter names if (strncasecmp(cmdline, valueTable[i].name, strlen(valueTable[i].name)) == 0 && variableNameLength == strlen(valueTable[i].name)) { if (valuef >= valueTable[i].min && valuef <= valueTable[i].max) { // here we compare the float value since... it should work, RIGHT? int_float_value_t tmp; if (valueTable[i].type & VAR_FLOAT) tmp.float_value = valuef; else tmp.int_value = value; cliSetVar(val, tmp); printf("%s set to ", valueTable[i].name); cliPrintVar(val, 0); } else { cliPrint("Value assignment out of range\r\n"); } return; } } cliPrint("Unknown variable name\r\n"); } else { // no equals, check for matching variables. cliGet(cmdline); } }
static void cliCMix(char *cmdline) { int i, check = 0; int num_motors = 0; uint8_t len; char buf[16]; float mixsum[3]; char *ptr; len = strlen(cmdline); if (len == 0) { cliPrint("Custom mixer: \r\nMotor\tThr\tRoll\tPitch\tYaw\r\n"); for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) { if (masterConfig.customMixer[i].throttle == 0.0f) break; num_motors++; printf("#%d:\t", i + 1); printf("%s\t", ftoa(masterConfig.customMixer[i].throttle, buf)); printf("%s\t", ftoa(masterConfig.customMixer[i].roll, buf)); printf("%s\t", ftoa(masterConfig.customMixer[i].pitch, buf)); printf("%s\r\n", ftoa(masterConfig.customMixer[i].yaw, buf)); } mixsum[0] = mixsum[1] = mixsum[2] = 0.0f; for (i = 0; i < num_motors; i++) { mixsum[0] += masterConfig.customMixer[i].roll; mixsum[1] += masterConfig.customMixer[i].pitch; mixsum[2] += masterConfig.customMixer[i].yaw; } cliPrint("Sanity check:\t"); for (i = 0; i < 3; i++) cliPrint(fabsf(mixsum[i]) > 0.01f ? "NG\t" : "OK\t"); cliPrint("\r\n"); return; } else if (strncasecmp(cmdline, "reset", 5) == 0) { // erase custom mixer for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) masterConfig.customMixer[i].throttle = 0.0f; } else if (strncasecmp(cmdline, "load", 4) == 0) { ptr = strchr(cmdline, ' '); if (ptr) { len = strlen(++ptr); for (i = 0; ; i++) { if (mixerNames[i] == NULL) { cliPrint("Invalid mixer type...\r\n"); break; } if (strncasecmp(ptr, mixerNames[i], len) == 0) { mixerLoadMix(i, masterConfig.customMixer); printf("Loaded %s mix...\r\n", mixerNames[i]); cliCMix(""); break; } } } } else { ptr = cmdline; i = atoi(ptr); // get motor number if (--i < MAX_SUPPORTED_MOTORS) { ptr = strchr(ptr, ' '); if (ptr) { masterConfig.customMixer[i].throttle = fastA2F(++ptr); check++; } ptr = strchr(ptr, ' '); if (ptr) { masterConfig.customMixer[i].roll = fastA2F(++ptr); check++; } ptr = strchr(ptr, ' '); if (ptr) { masterConfig.customMixer[i].pitch = fastA2F(++ptr); check++; } ptr = strchr(ptr, ' '); if (ptr) { masterConfig.customMixer[i].yaw = fastA2F(++ptr); check++; } if (check != 4) { cliPrint("Wrong number of arguments, needs idx thr roll pitch yaw\r\n"); } else { cliCMix(""); } } else { printf("Motor number must be between 1 and %d\r\n", MAX_SUPPORTED_MOTORS); } } }