Open FFBoard
Open source force feedback firmware
CommandInterface.h
Go to the documentation of this file.
1/*
2 * CommandInterface.h
3 *
4 * Created on: 17.11.2021
5 * Author: Yannick
6 */
7
8#ifndef SRC_COMMANDINTERFACE_H_
9#define SRC_COMMANDINTERFACE_H_
10
11#include "CmdParser.h"
13#include "UART.h"
14#include "thread.hpp"
15#include "CommandHandler.h"
16
17
19
25public:
26 static std::vector<CommandInterface*> cmdInterfaces;
27
29 virtual ~CommandInterface();
30 const virtual std::string getHelpstring(){return "";};
31 virtual bool getNewCommands(std::vector<ParsedCommand>& commands) = 0;
32 virtual bool hasNewCommands();
33 static CommandResult makeCommandReply(const std::vector<CommandReply>& reply,CommandHandler* handler, uint32_t cmdId,CMDtype type = CMDtype::get,CommandInterface* originalInterface = nullptr);
34 static void broadcastCommandReplyAsync(const std::vector<CommandReply>& reply,CommandHandler* handler, uint32_t cmdId,CMDtype type = CMDtype::get);
35 virtual void sendReplies(const std::vector<CommandResult>& results,CommandInterface* originalInterface) = 0; // All commands from batch done
36 virtual void sendReplyAsync(std::vector<CommandReply>& reply,CommandHandler* handler, uint32_t cmdId,CMDtype type);
37 virtual bool readyToSend();
38 virtual void batchDone();
39protected:
40 bool parserReady = false;
41};
42
47public:
48 StringCommandInterface(uint32_t bufferMaxCapacity = 512) : parser(CmdParser(bufferMaxCapacity)){}
49 bool addBuf(char* Buf, uint32_t *Len);
50 uint32_t bufferCapacity();
51 bool getNewCommands(std::vector<ParsedCommand>& commands) override;
52 static void formatReply(std::string& reply,const std::vector<CommandResult>& results,const bool formatWriteAsRead=false);
53 static std::string formatOriginalCommandFromResult(const ParsedCommand& originalCommand,CommandHandler* commandHandler,const bool formatWriteAsRead=false);
54 static void generateReplyValueString(std::string& replyPart,const CommandReply& reply);
55 static void generateReplyFromCmd(std::string& replyPart,const ParsedCommand& originalCommand);
56 const std::string getHelpstring(){return "Syntax:\nGet: cls.(instance.)cmd? or cls.(instance.)cmd?adr\nSet: cls.(instance.)cmd=val or cls.(instance.)cmd=val?adr";};
57
58
59protected:
60 CmdParser parser; // String parser
61};
62
63
64//receives bytes from mainclass. calls its own parser instance, calls global parser thread, passes replies back to cdc port.
66public:
68 virtual ~CDC_CommandInterface();
69
70 void Run();
71
72 void sendReplies(const std::vector<CommandResult>& results,CommandInterface* originalInterface) override;
73 const std::string getHelpstring(){return "CDC interface. " + StringCommandInterface::getHelpstring();};
74 bool readyToSend() override;
75 void batchDone() override;
76
77private:
79 uint32_t lastSendTime = 0;
80 const uint32_t parserTimeout = 500;
81 //cpp_freertos::BinarySemaphore sendSem;
82 std::vector<CommandResult> resultsBuffer;
83 bool nextFormat = false;
84 std::string sendBuffer;
85 uint32_t bufferLength = 0;
86 const uint32_t maxSendBuffer = 1024; // Max buffered command size before sending immediately
87};
88
89
90
92public:
93 UART_CommandInterface(uint32_t baud = 115200);
94 virtual ~UART_CommandInterface();
95
96 void Run();
97
98 void sendReplies(const std::vector<CommandResult>& results,CommandInterface* originalInterface) override;
99 void uartRcv(char& buf);
100 bool readyToSend();
101 const std::string getHelpstring(){return "UART interface. " + StringCommandInterface::getHelpstring();};
102 //void endUartTransfer(UARTPort* port) override;
103 void batchDone() override;
104
105private:
106 const uint32_t parserTimeout = 2000;
107
108 UART_InitTypeDef uartconfig;
109 uint32_t baud = 115200;
110 std::string sendBuffer;
111 bool enableBroadcastFromOtherInterfaces = false; // uart is slow. do not broadcast other messages by default
112 //cpp_freertos::BinarySemaphore cmdUartSem;
113 //cpp_freertos::BinarySemaphore bufferSem;
114 std::vector<CommandResult> resultsBuffer;
115 bool nextFormat = false;
116 uint32_t bufferLength = 0;
117 const uint32_t maxSendBuffer = 512; // Max buffered command size before sending immediately
118};
119
120
121
122
123#endif /* SRC_COMMANDINTERFACE_H_ */
CMDtype
std::vector< CommandResult > resultsBuffer
const uint32_t maxSendBuffer
void batchDone() override
bool readyToSend() override
const uint32_t parserTimeout
void sendReplies(const std::vector< CommandResult > &results, CommandInterface *originalInterface) override
const std::string getHelpstring()
virtual bool getNewCommands(std::vector< ParsedCommand > &commands)=0
virtual bool readyToSend()
virtual void sendReplyAsync(std::vector< CommandReply > &reply, CommandHandler *handler, uint32_t cmdId, CMDtype type)
static std::vector< CommandInterface * > cmdInterfaces
virtual bool hasNewCommands()
virtual void batchDone()
static void broadcastCommandReplyAsync(const std::vector< CommandReply > &reply, CommandHandler *handler, uint32_t cmdId, CMDtype type=CMDtype::get)
virtual ~CommandInterface()
static CommandResult makeCommandReply(const std::vector< CommandReply > &reply, CommandHandler *handler, uint32_t cmdId, CMDtype type=CMDtype::get, CommandInterface *originalInterface=nullptr)
virtual void sendReplies(const std::vector< CommandResult > &results, CommandInterface *originalInterface)=0
virtual const std::string getHelpstring()
bool addBuf(char *Buf, uint32_t *Len)
static std::string formatOriginalCommandFromResult(const ParsedCommand &originalCommand, CommandHandler *commandHandler, const bool formatWriteAsRead=false)
bool getNewCommands(std::vector< ParsedCommand > &commands) override
const std::string getHelpstring()
static void generateReplyValueString(std::string &replyPart, const CommandReply &reply)
StringCommandInterface(uint32_t bufferMaxCapacity=512)
static void generateReplyFromCmd(std::string &replyPart, const ParsedCommand &originalCommand)
static void formatReply(std::string &reply, const std::vector< CommandResult > &results, const bool formatWriteAsRead=false)
const uint32_t parserTimeout
void sendReplies(const std::vector< CommandResult > &results, CommandInterface *originalInterface) override
UART_CommandInterface(uint32_t baud=115200)
std::vector< CommandResult > resultsBuffer
const uint32_t maxSendBuffer
const std::string getHelpstring()
UART_InitTypeDef uartconfig