mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-20 14:55:21 +03:00
Merge pull request #3304 from mikeller/add_cli_array
Added support for array variables to CLI.
This commit is contained in:
commit
7b762e640c
2 changed files with 158 additions and 84 deletions
|
@ -125,10 +125,20 @@ extern uint8_t __config_end;
|
|||
|
||||
|
||||
static serialPort_t *cliPort;
|
||||
static bufWriter_t *cliWriter;
|
||||
static uint8_t cliWriteBuffer[sizeof(*cliWriter) + 128];
|
||||
|
||||
static char cliBuffer[64];
|
||||
#ifdef STM32F1
|
||||
#define CLI_IN_BUFFER_SIZE 128
|
||||
#define CLI_OUT_BUFFER_SIZE 64
|
||||
#else
|
||||
// Space required to set array parameters
|
||||
#define CLI_IN_BUFFER_SIZE 256
|
||||
#define CLI_OUT_BUFFER_SIZE 256
|
||||
#endif
|
||||
|
||||
static bufWriter_t *cliWriter;
|
||||
static uint8_t cliWriteBuffer[sizeof(*cliWriter) + CLI_IN_BUFFER_SIZE];
|
||||
|
||||
static char cliBuffer[CLI_OUT_BUFFER_SIZE];
|
||||
static uint32_t bufferIndex = 0;
|
||||
|
||||
static bool configIsInCopy = false;
|
||||
|
@ -294,33 +304,44 @@ static void cliPrintLinef(const char *format, ...)
|
|||
|
||||
static void printValuePointer(const clivalue_t *var, const void *valuePointer, bool full)
|
||||
{
|
||||
int value = 0;
|
||||
if ((var->type & VALUE_MODE_MASK) == MODE_ARRAY) {
|
||||
for (int i = 0; i < var->config.array.length; i++) {
|
||||
uint8_t value = ((uint8_t *)valuePointer)[i];
|
||||
|
||||
switch (var->type & VALUE_TYPE_MASK) {
|
||||
case VAR_UINT8:
|
||||
value = *(uint8_t *)valuePointer;
|
||||
break;
|
||||
|
||||
case VAR_INT8:
|
||||
value = *(int8_t *)valuePointer;
|
||||
break;
|
||||
|
||||
case VAR_UINT16:
|
||||
case VAR_INT16:
|
||||
value = *(int16_t *)valuePointer;
|
||||
break;
|
||||
}
|
||||
|
||||
switch(var->type & VALUE_MODE_MASK) {
|
||||
case MODE_DIRECT:
|
||||
cliPrintf("%d", value);
|
||||
if (full) {
|
||||
cliPrintf(" %d %d", var->config.minmax.min, var->config.minmax.max);
|
||||
cliPrintf("%d", value);
|
||||
if (i < var->config.array.length - 1) {
|
||||
cliPrint(",");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int value = 0;
|
||||
|
||||
switch (var->type & VALUE_TYPE_MASK) {
|
||||
case VAR_UINT8:
|
||||
value = *(uint8_t *)valuePointer;
|
||||
break;
|
||||
|
||||
case VAR_INT8:
|
||||
value = *(int8_t *)valuePointer;
|
||||
break;
|
||||
|
||||
case VAR_UINT16:
|
||||
case VAR_INT16:
|
||||
value = *(int16_t *)valuePointer;
|
||||
break;
|
||||
}
|
||||
|
||||
switch(var->type & VALUE_MODE_MASK) {
|
||||
case MODE_DIRECT:
|
||||
cliPrintf("%d", value);
|
||||
if (full) {
|
||||
cliPrintf(" %d %d", var->config.minmax.min, var->config.minmax.max);
|
||||
}
|
||||
break;
|
||||
case MODE_LOOKUP:
|
||||
cliPrint(lookupTables[var->config.lookup.tableIndex].values[value]);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case MODE_LOOKUP:
|
||||
cliPrint(lookupTables[var->config.lookup.tableIndex].values[value]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -378,14 +399,15 @@ static void dumpPgValue(const clivalue_t *value, uint8_t dumpMask)
|
|||
const char *defaultFormat = "#set %s = ";
|
||||
const int valueOffset = getValueOffset(value);
|
||||
const bool equalsDefault = valuePtrEqualsDefault(value->type, pg->copy + valueOffset, pg->address + valueOffset);
|
||||
|
||||
if (((dumpMask & DO_DIFF) == 0) || !equalsDefault) {
|
||||
if (dumpMask & SHOW_DEFAULTS && !equalsDefault) {
|
||||
cliPrintf(defaultFormat, value->name);
|
||||
printValuePointer(value, (uint8_t*)pg->address + valueOffset, 0);
|
||||
printValuePointer(value, (uint8_t*)pg->address + valueOffset, false);
|
||||
cliPrintLinefeed();
|
||||
}
|
||||
cliPrintf(format, value->name);
|
||||
printValuePointer(value, pg->copy + valueOffset, 0);
|
||||
printValuePointer(value, pg->copy + valueOffset, false);
|
||||
cliPrintLinefeed();
|
||||
}
|
||||
}
|
||||
|
@ -425,26 +447,31 @@ static void cliPrintVarRange(const clivalue_t *var)
|
|||
}
|
||||
cliPrintLinefeed();
|
||||
}
|
||||
break;
|
||||
case (MODE_ARRAY): {
|
||||
cliPrintLinef("Array length: %d", var->config.array.length);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void cliSetVar(const clivalue_t *var, const cliVar_t value)
|
||||
static void cliSetVar(const clivalue_t *var, const int16_t value)
|
||||
{
|
||||
void *ptr = getValuePointer(var);
|
||||
|
||||
switch (var->type & VALUE_TYPE_MASK) {
|
||||
case VAR_UINT8:
|
||||
*(uint8_t *)ptr = value.uint8;
|
||||
*(uint8_t *)ptr = value;
|
||||
break;
|
||||
|
||||
case VAR_INT8:
|
||||
*(int8_t *)ptr = value.int8;
|
||||
*(int8_t *)ptr = value;
|
||||
break;
|
||||
|
||||
case VAR_UINT16:
|
||||
case VAR_INT16:
|
||||
*(int16_t *)ptr = value.int16;
|
||||
*(int16_t *)ptr = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -2508,18 +2535,34 @@ static void cliGet(char *cmdline)
|
|||
cliPrintLine("Invalid name");
|
||||
}
|
||||
|
||||
static char *skipSpace(char *buffer)
|
||||
{
|
||||
while (*(buffer) == ' ') {
|
||||
buffer++;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static uint8_t getWordLength(char *bufBegin, char *bufEnd)
|
||||
{
|
||||
while (*(bufEnd - 1) == ' ') {
|
||||
bufEnd--;
|
||||
}
|
||||
|
||||
return bufEnd - bufBegin;
|
||||
}
|
||||
|
||||
static void cliSet(char *cmdline)
|
||||
{
|
||||
uint32_t len;
|
||||
const clivalue_t *val;
|
||||
char *eqptr = NULL;
|
||||
|
||||
len = strlen(cmdline);
|
||||
const uint32_t len = strlen(cmdline);
|
||||
char *eqptr;
|
||||
|
||||
if (len == 0 || (len == 1 && cmdline[0] == '*')) {
|
||||
cliPrintLine("Current settings: ");
|
||||
|
||||
for (uint32_t i = 0; i < valueTableEntryCount; i++) {
|
||||
val = &valueTable[i];
|
||||
const clivalue_t *val = &valueTable[i];
|
||||
cliPrintf("%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
|
||||
cliPrintLinefeed();
|
||||
|
@ -2527,52 +2570,81 @@ static void cliSet(char *cmdline)
|
|||
} else if ((eqptr = strstr(cmdline, "=")) != NULL) {
|
||||
// has equals
|
||||
|
||||
char *lastNonSpaceCharacter = eqptr;
|
||||
while (*(lastNonSpaceCharacter - 1) == ' ') {
|
||||
lastNonSpaceCharacter--;
|
||||
}
|
||||
uint8_t variableNameLength = lastNonSpaceCharacter - cmdline;
|
||||
uint8_t variableNameLength = getWordLength(cmdline, eqptr);
|
||||
|
||||
// skip the '=' and any ' ' characters
|
||||
eqptr++;
|
||||
while (*(eqptr) == ' ') {
|
||||
eqptr++;
|
||||
}
|
||||
eqptr = skipSpace(eqptr);
|
||||
|
||||
for (uint32_t i = 0; i < valueTableEntryCount; i++) {
|
||||
val = &valueTable[i];
|
||||
const clivalue_t *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)) {
|
||||
|
||||
bool changeValue = false;
|
||||
cliVar_t value = { .int16 = 0 };
|
||||
bool valueChanged = false;
|
||||
int16_t value = 0;
|
||||
switch (valueTable[i].type & VALUE_MODE_MASK) {
|
||||
case MODE_DIRECT: {
|
||||
value.int16 = atoi(eqptr);
|
||||
int16_t value = atoi(eqptr);
|
||||
|
||||
if (value.int16 >= valueTable[i].config.minmax.min && value.int16 <= valueTable[i].config.minmax.max) {
|
||||
changeValue = true;
|
||||
}
|
||||
if (value >= valueTable[i].config.minmax.min && value <= valueTable[i].config.minmax.max) {
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case MODE_LOOKUP: {
|
||||
const lookupTableEntry_t *tableEntry = &lookupTables[valueTable[i].config.lookup.tableIndex];
|
||||
bool matched = false;
|
||||
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry->valueCount && !matched; tableValueIndex++) {
|
||||
matched = strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
|
||||
const lookupTableEntry_t *tableEntry = &lookupTables[valueTable[i].config.lookup.tableIndex];
|
||||
bool matched = false;
|
||||
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry->valueCount && !matched; tableValueIndex++) {
|
||||
matched = strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
|
||||
|
||||
if (matched) {
|
||||
value.int16 = tableValueIndex;
|
||||
changeValue = true;
|
||||
}
|
||||
if (matched) {
|
||||
value = tableValueIndex;
|
||||
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case MODE_ARRAY: {
|
||||
const uint8_t arrayLength = valueTable[i].config.array.length;
|
||||
char *valPtr = eqptr;
|
||||
uint8_t array[256];
|
||||
char curVal[4];
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
valPtr = skipSpace(valPtr);
|
||||
char *valEnd = strstr(valPtr, ",");
|
||||
if ((valEnd != NULL) && (i < arrayLength - 1)) {
|
||||
uint8_t varLength = getWordLength(valPtr, valEnd);
|
||||
if (varLength <= 3) {
|
||||
strncpy(curVal, valPtr, getWordLength(valPtr, valEnd));
|
||||
curVal[varLength] = '\0';
|
||||
array[i] = (uint8_t)atoi((const char *)curVal);
|
||||
valPtr = valEnd + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if ((valEnd == NULL) && (i == arrayLength - 1)) {
|
||||
array[i] = atoi(valPtr);
|
||||
|
||||
uint8_t *ptr = getValuePointer(val);
|
||||
memcpy(ptr, array, arrayLength);
|
||||
valueChanged = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if (changeValue) {
|
||||
cliSetVar(val, value);
|
||||
|
||||
if (valueChanged) {
|
||||
cliPrintf("%s set to ", valueTable[i].name);
|
||||
cliPrintVar(val, 0);
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue