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

Merge pull request #3304 from mikeller/add_cli_array

Added support for array variables to CLI.
This commit is contained in:
Michael Keller 2017-06-19 21:39:49 +12:00 committed by GitHub
commit 7b762e640c
2 changed files with 158 additions and 84 deletions

View file

@ -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,6 +304,16 @@ static void cliPrintLinef(const char *format, ...)
static void printValuePointer(const clivalue_t *var, const void *valuePointer, bool full)
{
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];
cliPrintf("%d", value);
if (i < var->config.array.length - 1) {
cliPrint(",");
}
}
} else {
int value = 0;
switch (var->type & VALUE_TYPE_MASK) {
@ -323,6 +343,7 @@ static void printValuePointer(const clivalue_t *var, const void *valuePointer, b
break;
}
}
}
static bool valuePtrEqualsDefault(uint8_t type, const void *ptr, const void *ptrDefault)
{
@ -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,33 +2570,29 @@ 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;
case MODE_LOOKUP: {
const lookupTableEntry_t *tableEntry = &lookupTables[valueTable[i].config.lookup.tableIndex];
@ -2562,17 +2601,50 @@ static void cliSet(char *cmdline)
matched = strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
if (matched) {
value.int16 = tableValueIndex;
changeValue = true;
value = tableValueIndex;
cliSetVar(val, value);
valueChanged = true;
}
}
}
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);
if (changeValue) {
cliSetVar(val, value);
uint8_t *ptr = getValuePointer(val);
memcpy(ptr, array, arrayLength);
valueChanged = true;
} else {
break;
}
}
}
break;
}
if (valueChanged) {
cliPrintf("%s set to ", valueTable[i].name);
cliPrintVar(val, 0);
} else {

View file

@ -69,34 +69,31 @@ typedef struct lookupTableEntry_s {
#define VALUE_TYPE_OFFSET 0
#define VALUE_SECTION_OFFSET 4
#define VALUE_MODE_OFFSET 6
#define VALUE_SECTION_OFFSET 2
#define VALUE_MODE_OFFSET 4
typedef enum {
// value type, bits 0-3
// value type, bits 0-1
VAR_UINT8 = (0 << VALUE_TYPE_OFFSET),
VAR_INT8 = (1 << VALUE_TYPE_OFFSET),
VAR_UINT16 = (2 << VALUE_TYPE_OFFSET),
VAR_INT16 = (3 << VALUE_TYPE_OFFSET),
// value section, bits 4-5
// value section, bits 2-3
MASTER_VALUE = (0 << VALUE_SECTION_OFFSET),
PROFILE_VALUE = (1 << VALUE_SECTION_OFFSET),
PROFILE_RATE_VALUE = (2 << VALUE_SECTION_OFFSET), // 0x20
// value mode
MODE_DIRECT = (0 << VALUE_MODE_OFFSET), // 0x40
MODE_LOOKUP = (1 << VALUE_MODE_OFFSET) // 0x80
PROFILE_RATE_VALUE = (2 << VALUE_SECTION_OFFSET),
// value mode, bits 4-5
MODE_DIRECT = (0 << VALUE_MODE_OFFSET),
MODE_LOOKUP = (1 << VALUE_MODE_OFFSET),
MODE_ARRAY = (2 << VALUE_MODE_OFFSET)
} cliValueFlag_e;
#define VALUE_TYPE_MASK (0x0F)
#define VALUE_SECTION_MASK (0x30)
#define VALUE_MODE_MASK (0xC0)
typedef union {
int8_t int8;
uint8_t uint8;
int16_t int16;
} cliVar_t;
#define VALUE_TYPE_MASK (0x03)
#define VALUE_SECTION_MASK (0x0c)
#define VALUE_MODE_MASK (0x30)
typedef struct cliMinMaxConfig_s {
const int16_t min;
@ -107,9 +104,14 @@ typedef struct cliLookupTableConfig_s {
const lookupTableIndex_e tableIndex;
} cliLookupTableConfig_t;
typedef struct cliArrayLengthConfig_s {
const uint8_t length;
} cliArrayLengthConfig_t;
typedef union {
cliLookupTableConfig_t lookup;
cliMinMaxConfig_t minmax;
cliArrayLengthConfig_t array;
} cliValueConfig_t;
typedef struct {