Open FFBoard
Open source force feedback firmware
CommandHandler Class Referenceabstract

#include <CommandHandler.h>

Inheritance diagram for CommandHandler:
ADS111X_AnalogSource Axis CANPort_2B CanAnalogBase CanButtons EffectsCalculator EncoderBissC EncoderLocal EncoderSSI FFBoardMain I2CPort LocalAnalog LocalButtons MotorPWM MotorSimplemotion MtEncoderSPI ODriveCAN PCF8574Buttons RmdMotorCAN SPI_Buttons SerialFFB ShifterAnalog SystemCommands TMC4671 VescCAN

Public Member Functions

virtual const ClassType getClassType ()
 
 CommandHandler (const char *clsname, uint16_t clsid, uint8_t instance=0)
 
virtual ~CommandHandler ()
 
virtual bool hasCommands ()
 
virtual void setCommandsEnabled (bool enable)
 
virtual CommandStatus command (const ParsedCommand &cmd, std::vector< CommandReply > &replies)
 
void registerCommands ()
 
virtual CommandStatus internalCommand (const ParsedCommand &cmd, std::vector< CommandReply > &replies)
 
virtual const ClassIdentifier getInfo ()=0
 Command handlers always have class infos. Works well with ChoosableClass. More...
 
virtual std::string getHelpstring ()
 
virtual std::string getCommandsHelpstring ()
 
virtual std::string getCsvHelpstring ()
 
virtual uint8_t getCommandHandlerInstance ()
 
void broadcastCommandReply (CommandReply reply, uint32_t cmdId, CMDtype type)
 
void sendCommandReplyAsync (CommandReply reply, uint32_t cmdId, CMDtype type, CommandInterface *interface=nullptr)
 
virtual uint16_t getCommandHandlerID ()
 
virtual CmdHandlerInfogetCommandHandlerInfo ()
 
virtual bool isValidCommandId (uint32_t cmdid, uint32_t ignoredFlags=0, uint32_t requiredFlag=0)
 
virtual CmdHandlerCommanddefgetCommandFromName (const std::string &cmd, uint32_t ignoredFlags=0)
 
virtual CmdHandlerCommanddefgetCommandFromId (const uint32_t id, uint32_t ignoredFlags=0)
 
template<typename ID >
void registerCommand (const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
 

Static Public Member Functions

static void logSerial (std::string string)
 Send a log formatted sequence. More...
 
static void logSerialDebug (std::string string)
 Send a log formatted sequence if debug is on. More...
 
static bool logsEnabled ()
 
static void setLogsEnabled (bool enabled)
 
static uint32_t getClassIdFromName (const char *name)
 
static const char * getClassNameFromId (const uint32_t id)
 
static CommandHandlergetHandlerFromHandlerId (const uint16_t cmdhandlerID)
 
static CommandHandlergetHandlerFromId (const uint16_t id, const uint8_t instance=0xFF)
 
static CommandHandlergetHandlerFromClassName (const char *name, const uint8_t instance=0xFF)
 
static std::vector< CommandHandler * > getHandlersFromClassName (const char *name)
 
static std::vector< CommandHandler * > getHandlersFromId (const uint16_t id)
 
static bool isInHandlerList (CommandHandler *handler)
 
static std::string getAllHelpstrings ()
 
static std::vector< CommandHandler * > & getCommandHandlers ()
 
template<typename TVal >
static CommandStatus handleGetSet (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value)
 
template<typename TVal , class cls , class cls1 >
static CommandStatus handleGetSetFunc (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value, void(cls1::*setfunc)(TVal), cls *obj)
 
template<typename TVal , class cls , class cls1 , class cls2 >
static CommandStatus handleGetFuncSetFunc (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal(cls1::*getfunc)(), void(cls2::*setfunc)(TVal), cls *obj)
 
template<typename TVal , class cls , class cls1 >
static CommandStatus handleGetFuncSet (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value, TVal(cls1::*getfunc)(), cls *obj)
 

Static Public Attributes

static bool logEnabled = true
 

Protected Member Functions

void setInstance (uint8_t instance)
 
virtual void addCommandHandler ()
 
virtual void removeCommandHandler ()
 

Static Protected Member Functions

static std::vector< uint16_t > & getCommandHandlerIds ()
 

Protected Attributes

bool commandsEnabled = true
 
std::vector< CmdHandlerCommanddefregisteredCommands
 
CmdHandlerInfo cmdHandlerInfo
 

Detailed Description

Implements an interface for parsed command handlers.

Adds itself to a global vector of handlers that can be called from the main class when a command gets parsed.

For example motor drivers and button sources can implement this to easily get serial commands

Definition at line 128 of file CommandHandler.h.

Constructor & Destructor Documentation

◆ CommandHandler()

CommandHandler::CommandHandler ( const char *  clsname,
uint16_t  clsid,
uint8_t  instance = 0 
)

clsname and clsid identify this class in commands additionally to the unique instance field which can be assigned at runtime

Definition at line 25 of file CommandHandler.cpp.

◆ ~CommandHandler()

CommandHandler::~CommandHandler ( )
virtual

Definition at line 30 of file CommandHandler.cpp.

Member Function Documentation

◆ addCommandHandler()

void CommandHandler::addCommandHandler ( )
protectedvirtual

Registers a command handler in the global callback list and assigns a unique index number

Definition at line 490 of file CommandHandler.cpp.

◆ broadcastCommandReply()

void CommandHandler::broadcastCommandReply ( CommandReply  reply,
uint32_t  cmdId,
CMDtype  type 
)

Broadcasts an unrequested reply to all command interfaces in the name of this class

Parameters
[in]replya command reply to be sent
[in]cmdIdthe command id in which name to reply
[in]typetype of the command to create (normally get)

Definition at line 422 of file CommandHandler.cpp.

◆ command()

CommandStatus CommandHandler::command ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies 
)
virtual
Parameters
[in]cmdThe parsed command to be executed.
[out]repliesA vector to return one or multiple reply objects into. Replies to the interface will be generated based on the reply objects. A string reply may not contain start, end and separation markers: [,],| Other characters are allowed.

Reimplemented in Axis, CANPort_2B, EffectsCalculator, I2CPort, SerialFFB, ADS111X_AnalogSource, CanAnalogBase, CanBridge, CanButtons, CustomMain, EncoderBissC, EncoderLocal, EncoderSSI, FFBHIDMain, FFBoardMain, LocalAnalog, LocalButtons, MidiMain, MotorPWM, MtEncoderSPI, TMC4671, TMCDebugBridge, VescCAN, MotorSimplemotion, ODriveCAN, PCF8574Buttons, RmdMotorCAN, ShifterAnalog, and SPI_Buttons.

Definition at line 410 of file CommandHandler.cpp.

◆ getAllHelpstrings()

static std::string CommandHandler::getAllHelpstrings ( )
static

◆ getClassIdFromName()

uint32_t CommandHandler::getClassIdFromName ( const char *  name)
static

Searches cmd handlers and returns its class id if the classname matches To be used to convert from string commands to ids

Definition at line 290 of file CommandHandler.cpp.

◆ getClassNameFromId()

const char * CommandHandler::getClassNameFromId ( const uint32_t  id)
static

Returns a pointer to the name of a class corresponding to an id or nullptr if not found

Definition at line 304 of file CommandHandler.cpp.

◆ getClassType()

virtual const ClassType CommandHandler::getClassType ( )
inlinevirtual

Type of this class. Mainclass, motordriver... Should be implemented by the parent class so it is not in the info struct

Reimplemented in SystemCommands, LocalButtons, Axis, CANPort_2B, EffectsCalculator, I2CPort, ADS111X_AnalogSource, FFBoardMain, LocalAnalog, PCF8574Buttons, ShifterAnalog, SPI_Buttons, and TMC4671.

Definition at line 138 of file CommandHandler.h.

◆ getCommandFromId()

CmdHandlerCommanddef * CommandHandler::getCommandFromId ( const uint32_t  id,
uint32_t  ignoredFlags = 0 
)
virtual

Returns the string name of a command from an ID Ignores commands that match ignoredFlags

Definition at line 201 of file CommandHandler.cpp.

◆ getCommandFromName()

CmdHandlerCommanddef * CommandHandler::getCommandFromName ( const std::string &  cmd,
uint32_t  ignoredFlags = 0 
)
virtual

Returns the ID of a command from a string Ignores commands that match ignoredFlags

Definition at line 188 of file CommandHandler.cpp.

◆ getCommandHandlerID()

virtual uint16_t CommandHandler::getCommandHandlerID ( )
inlinevirtual

Definition at line 166 of file CommandHandler.h.

◆ getCommandHandlerIds()

static std::vector< uint16_t > & CommandHandler::getCommandHandlerIds ( )
inlinestaticprotected

Definition at line 280 of file CommandHandler.h.

◆ getCommandHandlerInfo()

CmdHandlerInfo * CommandHandler::getCommandHandlerInfo ( )
virtual

Returns a pointer to this classes command handler info struct containing its name and ids

Definition at line 476 of file CommandHandler.cpp.

◆ getCommandHandlerInstance()

uint8_t CommandHandler::getCommandHandlerInstance ( )
virtual

Returns the instance number of this class

Definition at line 483 of file CommandHandler.cpp.

◆ getCommandHandlers()

static std::vector< CommandHandler * > & CommandHandler::getCommandHandlers ( )
inlinestatic

Definition at line 191 of file CommandHandler.h.

◆ getCommandsHelpstring()

std::string CommandHandler::getCommandsHelpstring ( )
virtual

Generates a readable list of all commands with help information Warning: Large string returned

Definition at line 39 of file CommandHandler.cpp.

◆ getCsvHelpstring()

std::string CommandHandler::getCsvHelpstring ( )
virtual

Generates a csv list of all commands with help information Warning: Large string returned

Definition at line 101 of file CommandHandler.cpp.

◆ getHandlerFromClassName()

CommandHandler * CommandHandler::getHandlerFromClassName ( const char *  name,
const uint8_t  instance = 0xFF 
)
static

Returns a command handler from a classname and instance numer or nullptr if not found

Definition at line 344 of file CommandHandler.cpp.

◆ getHandlerFromHandlerId()

CommandHandler * CommandHandler::getHandlerFromHandlerId ( const uint16_t  cmdhandlerID)
static

Returns a command handler with a specific unique handler id or nullptr if not found

Definition at line 318 of file CommandHandler.cpp.

◆ getHandlerFromId()

CommandHandler * CommandHandler::getHandlerFromId ( const uint16_t  id,
const uint8_t  instance = 0xFF 
)
static

Returns a command handler which matches the class id and instance number or nullptr if not found

Definition at line 330 of file CommandHandler.cpp.

◆ getHandlersFromClassName()

std::vector< CommandHandler * > CommandHandler::getHandlersFromClassName ( const char *  name)
static

Returns all command handlers with a supplied classname

Definition at line 358 of file CommandHandler.cpp.

◆ getHandlersFromId()

std::vector< CommandHandler * > CommandHandler::getHandlersFromId ( const uint16_t  id)
static

Returns all command handlers with a supplied class id

Definition at line 371 of file CommandHandler.cpp.

◆ getHelpstring()

std::string CommandHandler::getHelpstring ( )
virtual

◆ getInfo()

◆ handleGetFuncSet()

template<typename TVal , class cls , class cls1 >
static CommandStatus CommandHandler::handleGetFuncSet ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies,
TVal &  value,
TVal(cls1::*)()  getfunc,
cls *  obj 
)
inlinestatic

Reads from a member function and writes to a variable

Definition at line 243 of file CommandHandler.h.

◆ handleGetFuncSetFunc()

template<typename TVal , class cls , class cls1 , class cls2 >
static CommandStatus CommandHandler::handleGetFuncSetFunc ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies,
TVal(cls1::*)()  getfunc,
void(cls2::*)(TVal)  setfunc,
cls *  obj 
)
inlinestatic

Reads from a member function and sets to a member function

Definition at line 229 of file CommandHandler.h.

◆ handleGetSet()

template<typename TVal >
static CommandStatus CommandHandler::handleGetSet ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies,
TVal &  value 
)
inlinestatic

Reads or writes a variable

Definition at line 201 of file CommandHandler.h.

◆ handleGetSetFunc()

template<typename TVal , class cls , class cls1 >
static CommandStatus CommandHandler::handleGetSetFunc ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies,
TVal &  value,
void(cls1::*)(TVal)  setfunc,
cls *  obj 
)
inlinestatic

Reads from a variable and passes set commands to a member callback

Definition at line 215 of file CommandHandler.h.

◆ hasCommands()

bool CommandHandler::hasCommands ( )
virtual

Definition at line 268 of file CommandHandler.cpp.

◆ internalCommand()

CommandStatus CommandHandler::internalCommand ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies 
)
virtual

Some standard commands most classes will need

Reimplemented in SystemCommands.

Definition at line 221 of file CommandHandler.cpp.

◆ isInHandlerList()

bool CommandHandler::isInHandlerList ( CommandHandler handler)
static

Returns true if a command handler pointer is in the list of active command handlers

Definition at line 385 of file CommandHandler.cpp.

◆ isValidCommandId()

bool CommandHandler::isValidCommandId ( uint32_t  cmdid,
uint32_t  ignoredFlags = 0,
uint32_t  requiredFlags = 0 
)
virtual

Returns true if the command id is valid for this command handler and does NOT contain ignoredFlags but contains all requiredFlags

Definition at line 392 of file CommandHandler.cpp.

◆ logsEnabled()

bool CommandHandler::logsEnabled ( )
static

Definition at line 275 of file CommandHandler.cpp.

◆ logSerial()

void CommandHandler::logSerial ( std::string  string)
static

Send a log formatted sequence.

Sends log info back via cdc. UNUSED

Definition at line 448 of file CommandHandler.cpp.

◆ logSerialDebug()

void CommandHandler::logSerialDebug ( std::string  string)
static

Send a log formatted sequence if debug is on.

Sends log info back via cdc if debug mode is on

Definition at line 459 of file CommandHandler.cpp.

◆ registerCommand()

template<typename ID >
void CommandHandler::registerCommand ( const char *  cmd,
const ID  cmdid,
const char *  help = nullptr,
uint32_t  flags = 0 
)
inline

Registers a command for this command handler

Parameters
[in]cmdstring name of command for serial interface
[in]cmdidid enum for command for HID
[in]helphelp message displayed on serial interface
[in]flagswhat access methods are available for a command (see CMDFLAG definitions)

Definition at line 262 of file CommandHandler.h.

◆ registerCommands()

void CommandHandler::registerCommands ( )

Registers commonly used internal commands to identify a class

Definition at line 174 of file CommandHandler.cpp.

◆ removeCommandHandler()

void CommandHandler::removeCommandHandler ( )
protectedvirtual

Removes a class from the global callback list and frees its id

Definition at line 517 of file CommandHandler.cpp.

◆ sendCommandReplyAsync()

void CommandHandler::sendCommandReplyAsync ( CommandReply  reply,
uint32_t  cmdId,
CMDtype  type,
CommandInterface interface = nullptr 
)

Broadcasts an unrequested reply to a specific command interface in the name of this class

Parameters
[in]interfaceMust be a valid interface pointer from a previously received command or nullptr (treated like a broadcast)
[in]replya command reply to be sent
[in]cmdIdthe command id in which name to reply
[in]typetype of the command to create (normally get)

Definition at line 434 of file CommandHandler.cpp.

◆ setCommandsEnabled()

void CommandHandler::setCommandsEnabled ( bool  enable)
virtual

Definition at line 271 of file CommandHandler.cpp.

◆ setInstance()

void CommandHandler::setInstance ( uint8_t  instance)
protected

Changes the command handler instance number for addressing. Should only be called in the constructor

Definition at line 214 of file CommandHandler.cpp.

◆ setLogsEnabled()

void CommandHandler::setLogsEnabled ( bool  enable)
static

Enables or disables logs sent by "logSerial"

Definition at line 282 of file CommandHandler.cpp.

Member Data Documentation

◆ cmdHandlerInfo

CmdHandlerInfo CommandHandler::cmdHandlerInfo
protected

Definition at line 288 of file CommandHandler.h.

◆ commandsEnabled

bool CommandHandler::commandsEnabled = true
protected

Definition at line 276 of file CommandHandler.h.

◆ logEnabled

bool CommandHandler::logEnabled = true
static

Definition at line 163 of file CommandHandler.h.

◆ registeredCommands

std::vector<CmdHandlerCommanddef> CommandHandler::registeredCommands
protected

Definition at line 286 of file CommandHandler.h.


The documentation for this class was generated from the following files: