@ -3,23 +3,32 @@
/ / we unset this on ' exit '
extern uint8_t cliMode ;
static void cliDefaults ( char * cmdline ) ;
static void cliExit ( char * cmdline ) ;
static void cliFeature ( char * cmdline ) ;
static void cliHelp ( char * cmdline ) ;
static void cliMixer ( char * cmdline ) ;
static void cliRMod e ( char * cmdline ) ;
static void cliSav e ( char * cmdline ) ;
static void cliSet ( char * cmdline ) ;
static void cliVersion ( char * cmdline ) ;
/ / buffer
static char cliBuffer [ 32 ] ;
static uint8_t bufferIndex = 0 ;
static bool unsaved = false ;
/ / sync this with MultiType enum from mw . h
const char * mixerNames [ ] = {
" TRI " , " QUADP " , " QUADX " , " BI " ,
" GIMBAL " , " Y6 " , " HEX6 " ,
" GIMBAL " , " Y6 " , " HEX6 " ,
" FLYING_WING " , " Y4 " , " HEX6X " , " OCTOX8 " , " OCTOFLATP " , " OCTOFLATX " ,
" AIRPLANE " , " HELI_120_CCPM " , " HELI_90_DEG " , " VTAIL4 "
" AIRPLANE " , " HELI_120_CCPM " , " HELI_90_DEG " , " VTAIL4 " , NULL
} ;
/ / sync this with AvailableFeatures enum from board . h
const char * featureNames [ ] = {
" PPM " , " VBAT " , " INFLIGHT_ACC_CAL " , " DIGITAL_SERVO " , " MOTOR_STOP " ,
" SERVO_TILT " , " CAMTRIG " , " GYRO_SMOOTHING " , " LED_RING " ,
NULL
} ;
typedef struct {
@ -30,17 +39,18 @@ typedef struct {
/ / should be sorted a . . z for bsearch ( )
const clicmd_t cmdTable [ ] = {
{ " defaults " , " reset to defaults and reboot " , cliDefaults } ,
{ " exit " , " " , cliExit } ,
{ " feature " , " list or -val or val " , cliFeature } ,
{ " help " , " " , cliHelp } ,
{ " mixer " , " mixer name " , cliMixer } ,
{ " rmod e" , " pwm / ppm " , cliRMod e } ,
{ " set " , " name=value " , cliSet } ,
{ " mixer " , " mixer name or list " , cliMixer } ,
{ " sav e" , " save and reboot " , cliSav e } ,
{ " set " , " name=value or blank for list " , cliSet } ,
{ " version " , " " , cliVersion } ,
} ;
# define CMD_COUNT (sizeof(cmdTable) / sizeof(cmdTable[0]))
typedef enum {
VAR_BOOL , / / yes / no true / false 1 / 0 type stuff
VAR_UINT8 ,
VAR_INT8 ,
VAR_UINT16 ,
@ -65,12 +75,17 @@ const clivalue_t valueTable[] = {
{ " wing_left_mid " , VAR_UINT16 , & cfg . wing_left_mid , 0 , 2000 } ,
{ " wing_right_mid " , VAR_UINT16 , & cfg . wing_right_mid , 0 , 2000 } ,
{ " tri_yaw_middle " , VAR_UINT16 , & cfg . tri_yaw_middle , 0 , 2000 } ,
{ " tri_yaw_min " , VAR_UINT16 , & cfg . tri_yaw_min , 0 , 2000 } ,
{ " tri_yaw_max " , VAR_UINT16 , & cfg . tri_yaw_max , 0 , 2000 } ,
{ " tilt_pitch_prop " , VAR_INT8 , & cfg . tilt_pitch_prop , - 100 , 100 } ,
{ " tilt_roll_prop " , VAR_INT8 , & cfg . tilt_roll_prop , - 100 , 100 } ,
} ;
# define VALUE_COUNT (sizeof(valueTable) / sizeof(valueTable[0]))
static void cliSetVar ( const clivalue_t * var , const int32_t value ) ;
static void cliPrintVar ( const clivalue_t * var ) ;
static void cliPrompt ( void )
{
uartPrint ( " \r \n # " ) ;
@ -82,12 +97,82 @@ static int cliCompare(const void *a, const void *b)
return strncasecmp ( ca - > name , cb - > name , strlen ( cb - > name ) ) ;
}
static void cliDefaults ( char * cmdline )
{
uartPrint ( " Resetting to defaults... \r \n " ) ;
checkFirstTime ( true ) ;
uartPrint ( " Rebooting... " ) ;
delay ( 10 ) ;
systemReset ( false ) ;
}
static void cliExit ( char * cmdline )
{
uartPrint ( " \r \n Leaving CLI mode... \r \n " ) ;
memset ( cliBuffer , 0 , sizeof ( cliBuffer ) ) ;
bufferIndex = 0 ;
cliMode = 0 ;
/ / save and reboot . . . I think this makes the most sense
cliSave ( cmdline ) ;
}
static void cliFeature ( char * cmdline )
{
uint8_t i ;
uint8_t len ;
uint32_t mask ;
len = strlen ( cmdline ) ;
mask = featureMask ( ) ;
if ( len = = 0 ) {
uartPrint ( " Enabled features: " ) ;
for ( i = 0 ; ; i + + ) {
if ( featureNames [ i ] = = NULL )
break ;
if ( mask & ( 1 < < i ) )
uartPrint ( ( char * ) featureNames [ i ] ) ;
uartWrite ( ' ' ) ;
}
uartPrint ( " \r \n " ) ;
} else if ( strncasecmp ( cmdline , " list " , len ) = = 0 ) {
uartPrint ( " Available features: " ) ;
for ( i = 0 ; ; i + + ) {
if ( featureNames [ i ] = = NULL )
break ;
uartPrint ( ( char * ) featureNames [ i ] ) ;
uartWrite ( ' ' ) ;
}
uartPrint ( " \r \n " ) ;
return ;
} else {
bool remove = false ;
if ( cmdline [ 0 ] = = ' - ' ) {
/ / remove feature
remove = true ;
cmdline + + ; / / skip over -
len - - ;
}
for ( i = 0 ; ; i + + ) {
if ( featureNames [ i ] = = NULL ) {
uartPrint ( " Invalid feature name... \r \n " ) ;
break ;
}
if ( strncasecmp ( cmdline , featureNames [ i ] , len ) = = 0 ) {
if ( remove ) {
featureClear ( 1 < < i ) ;
uartPrint ( " Disabled " ) ;
} else {
featureSet ( 1 < < i ) ;
uartPrint ( " Enabled " ) ;
}
uartPrint ( ( char * ) featureNames [ i ] ) ;
uartPrint ( " \r \n " ) ;
break ;
}
}
}
}
static void cliHelp ( char * cmdline )
@ -106,29 +191,141 @@ static void cliHelp(char *cmdline)
static void cliMixer ( char * cmdline )
{
uint8_t i ;
uint8_t len ;
len = strlen ( cmdline ) ;
if ( len = = 0 ) {
uartPrint ( " Current mixer: " ) ;
uartPrint ( ( char * ) mixerNames [ cfg . mixerConfiguration - 1 ] ) ;
uartPrint ( " \r \n " ) ;
return ;
} else if ( strncasecmp ( cmdline , " list " , len ) = = 0 ) {
uartPrint ( " Available mixers: " ) ;
for ( i = 0 ; ; i + + ) {
if ( mixerNames [ i ] = = NULL )
break ;
uartPrint ( ( char * ) mixerNames [ i ] ) ;
uartWrite ( ' ' ) ;
}
uartPrint ( " \r \n " ) ;
return ;
}
for ( i = 0 ; ; i + + ) {
if ( mixerNames [ i ] = = NULL ) {
uartPrint ( " Invalid mixer type... \r \n " ) ;
break ;
}
if ( strncasecmp ( cmdline , mixerNames [ i ] , len ) = = 0 ) {
cfg . mixerConfiguration = i + 1 ;
uartPrint ( " Mixer set to " ) ;
uartPrint ( ( char * ) mixerNames [ i ] ) ;
uartPrint ( " \r \n " ) ;
break ;
}
}
}
static void cliSave ( char * cmdline )
{
uartPrint ( " Saving... " ) ;
writeParams ( ) ;
uartPrint ( " \r \n Rebooting... " ) ;
delay ( 10 ) ;
systemReset ( false ) ;
}
static void cliRMode ( char * cmdline )
static void cliPrintVar ( const clivalue_t * var )
{
if ( strncasecmp ( cmdline , " pwm " , 3 ) = = 0 ) {
uartPrint ( " PWM Mode " ) ;
featureClear ( FEATURE_PPM ) ;
} else {
uartPrint ( " PPM Mode " ) ;
featureSet ( FEATURE_PPM ) ;
int32_t value ;
char buf [ 16 ] ;
switch ( var - > type ) {
case VAR_UINT8 :
value = * ( uint8_t * ) var - > ptr ;
break ;
case VAR_INT8 :
value = * ( int8_t * ) var - > ptr ;
break ;
case VAR_UINT16 :
value = * ( uint16_t * ) var - > ptr ;
break ;
case VAR_INT16 :
value = * ( int16_t * ) var - > ptr ;
break ;
}
snprintf ( buf , 16 , " %d " , value ) ;
uartPrint ( buf ) ;
}
cliExit ( cmdline ) ;
writeParams ( ) ;
systemReset ( false ) ;
static void cliSetVar ( const clivalue_t * var , const int32_t value )
{
switch ( var - > type ) {
case VAR_UINT8 :
* ( uint8_t * ) var - > ptr = ( uint8_t ) value ;
break ;
case VAR_INT8 :
* ( int8_t * ) var - > ptr = ( int8_t ) value ;
break ;
case VAR_UINT16 :
* ( uint16_t * ) var - > ptr = ( uint16_t ) value ;
break ;
case VAR_INT16 :
* ( int16_t * ) var - > ptr = ( int16_t ) value ;
break ;
}
}
static void cliSet ( char * cmdline )
{
uint8_t i ;
uint8_t len ;
const clivalue_t * val ;
char * eqptr = NULL ;
int32_t value = 0 ;
len = strlen ( cmdline ) ;
if ( len = = 0 ) {
uartPrint ( " Current settings: \r \n " ) ;
for ( i = 0 ; i < VALUE_COUNT ; i + + ) {
val = & valueTable [ i ] ;
uartPrint ( ( char * ) valueTable [ i ] . name ) ;
uartPrint ( " = " ) ;
cliPrintVar ( val ) ;
uartPrint ( " \r \n " ) ;
delay ( 10 ) ;
}
} else if ( eqptr = strstr ( cmdline , " = " ) ) {
/ / has equal , set var
eqptr + + ;
len - - ;
value = atoi ( eqptr ) ;
for ( i = 0 ; i < VALUE_COUNT ; i + + ) {
val = & valueTable [ i ] ;
if ( strncasecmp ( cmdline , valueTable [ i ] . name , strlen ( valueTable [ i ] . name ) ) = = 0 ) {
/ / found
if ( value > = valueTable [ i ] . min & & value < = valueTable [ i ] . max ) {
cliSetVar ( val , value ) ;
uartPrint ( ( char * ) valueTable [ i ] . name ) ;
uartPrint ( " set to " ) ;
cliPrintVar ( val ) ;
} else {
uartPrint ( " ERR: Value assignment out of range \r \n " ) ;
}
return ;
}
}
uartPrint ( " ERR: Unknown variable name \r \n " ) ;
}
}
static void cliVersion ( char * cmdline )
@ -150,7 +347,7 @@ void cliProcess(void)
if ( c = = ' \t ' | | c = = ' ? ' ) {
const clicmd_t * cmd , * pstart = NULL , * pend = NULL ;
int i = bufferIndex ;
for ( cmd = cmdTable ; cmd < cmdTable + CMD_COUNT ; cmd + + ) {
for ( cmd = cmdTable ; cmd < cmdTable + CMD_COUNT ; cmd + + ) {
if ( bufferIndex & & ( strncasecmp ( cliBuffer , cmd - > name , bufferIndex ) ! = 0 ) )
continue ;
if ( ! pstart )
@ -158,7 +355,7 @@ void cliProcess(void)
pend = cmd ;
}
if ( pstart ) { /* Buffer matches one or more commands */
for ( ; ; bufferIndex + + ) {
for ( ; ; bufferIndex + + ) {
if ( pstart - > name [ bufferIndex ] ! = pend - > name [ bufferIndex ] )
break ;
if ( ! pstart - > name [ bufferIndex ] ) {
@ -179,12 +376,13 @@ void cliProcess(void)
cliPrompt ( ) ;
i = 0 ; /* Redraw prompt */
}
for ( ; i < bufferIndex ; i + + )
for ( ; i < bufferIndex ; i + + )
uartWrite ( cliBuffer [ i ] ) ;
} else if ( ! bufferIndex & & c = = 4 ) {
cliExit ( cliBuffer ) ;
return ;
} else if ( c = = 12 ) {
/ / clear screen
uartPrint ( " \033 [2J \033 [1;1H " ) ;
cliPrompt ( ) ;
} else if ( bufferIndex & & ( c = = ' \n ' | | c = = ' \r ' ) ) {
@ -201,7 +399,7 @@ void cliProcess(void)
if ( cmd )
cmd - > func ( cliBuffer + strlen ( cmd - > name ) + 1 ) ;
else
uartPrint ( " ERR: Unknown command, try 'HELP ' " ) ;
uartPrint ( " ERR: Unknown command, try 'help ' " ) ;
memset ( cliBuffer , 0 , sizeof ( cliBuffer ) ) ;
bufferIndex = 0 ;