Open FFBoard
Open source force feedback firmware
FFBoardMainCommandThread.cpp
Go to the documentation of this file.
1/*
2 * FFBoardMainCommandThread.cpp
3 *
4 * Created on: Feb 13, 2021
5 * Author: Yannick
6 */
7
9#include <thread.hpp>
10#include "constants.h"
11#include "mainclass_chooser.h"
12#include "eeprom_addresses.h"
13#include "flash_helpers.h"
14#include "eeprom.h"
15#include "voltagesense.h"
16#include "global_callbacks.h"
17#include "PersistentStorage.h"
18#include "ErrorHandler.h"
19#include "ClassChooser.h"
20#include "FFBoardMain.h"
21#include "critical.hpp"
22#include "cpp_target_config.h"
23
24
25
28
29
31
32// Note: allocate enough memory for the command thread to store replies
34 //main = mainclass;
35 commandThread = this;
36 this->Start();
37
38}
39
41
42}
43
44
45
47 while(true){
48 // Ask command interfaces if new commands are available if woken up
50 if(itf->hasNewCommands()){
51 itf->getNewCommands(commands);
52 this->executeCommands(commands, itf);
53 commands.clear();
54 if(commands.capacity() > 20)
55 commands.shrink_to_fit();
56 }
57 }
59 //FFBoardMainCommandThread::threadSem.Take(); // Stop thread again. will be resumed when parser ready
60
61 //Delay(1); // Give the scheduler time
62 }
63}
64
69
70 if(inIsr()){
72// BaseType_t pxHigherPriorityTaskWoken;
73// FFBoardMainCommandThread::threadSem.GiveFromISR(&pxHigherPriorityTaskWoken);
74// portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
75 }else{
77 //FFBoardMainCommandThread::threadSem.Give();
78 }
79
80}
81
82
83
84
85
86
91void FFBoardMainCommandThread::executeCommands(std::vector<ParsedCommand>& commands,CommandInterface* commandInterface){
92 for(ParsedCommand& cmd : commands){
93 this->results.clear();
94 cmd.originalInterface = commandInterface;
95
97 CommandHandler* handler = cmd.target;
98
99 CmdHandlerCommanddef* cmdDef = handler->getCommandFromId(cmd.cmdId);
100 // check flags
101 bool validFlags = cmdDef != nullptr;
102 if(cmdDef->flags & ( CMDFLAG_GET | CMDFLAG_SET | CMDFLAG_INFOSTRING | CMDFLAG_GETADR | CMDFLAG_SETADR) ){
103 // A type flag is preset. check it
104 validFlags = static_cast<uint32_t>(cmd.type) & cmdDef->flags; // type uses the same flag values
105 }
106 if(CommandHandler::isInHandlerList(handler) && validFlags){ // check if pointer is still present in handler list
107 // Call internal commands first
108 this->results.emplace_back(); // Create new result element
109 CommandResult& resultObj = this->results.back();
110 status = handler->internalCommand(cmd,resultObj.reply);
111
112 // internal commands did not return anything call regular custom commands
113 if(status == CommandStatus::NOT_FOUND && handler->hasCommands()){
114 status = handler->command(cmd,resultObj.reply);
115 }
116 // If status is not no reply append a reply object. If command was not found the reply vector should be empty but the not found flag set
117 if(status != CommandStatus::NO_REPLY){
118 resultObj.handlerId = handler->getCommandHandlerID();
119 resultObj.originalCommand = cmd;
120 resultObj.type = status;
121 resultObj.commandHandler = handler;
122 }else{
123 this->results.pop_back(); // Remove result again
124 }
125 }
126 // Results are buffered. send out
127 if(!this->results.empty()){
129 // Block until replies are sent
130 uint32_t remainingTime = 100;
131 while(!itf->readyToSend() && --remainingTime){
132 Delay(1);
133 }
134 if(remainingTime){
135 itf->sendReplies(results, commandInterface);
136 }
137 }
138 }
139 }
141 itf->batchDone(); // Signal that all commands are done
142 }
143
144}
CommandStatus
@ cmdExecutionError
FFBoardMainCommandThread * commandThread
const uint32_t flags
virtual CmdHandlerCommanddef * getCommandFromId(const uint32_t id, uint32_t ignoredFlags=0)
static bool isInHandlerList(CommandHandler *handler)
virtual bool hasCommands()
virtual uint16_t getCommandHandlerID()
virtual CommandStatus internalCommand(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
virtual CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
static std::vector< CommandInterface * > cmdInterfaces
virtual void executeCommands(std::vector< ParsedCommand > &commands, CommandInterface *commandInterface)
std::vector< ParsedCommand > commands
std::vector< CommandResult > results
FFBoardMainCommandThread(FFBoardMain *mainclass)
uint32_t WaitForNotification(TickType_t Timeout=portMAX_DELAY)
Definition: thread.hpp:246
void Delay(const TickType_t Delay)
Definition: thread.hpp:352
static bool inIsr()
Definition: cppmain.h:41
FFBoardMain * mainclass
ParsedCommand originalCommand
CommandStatus type
CommandHandler * commandHandler
std::vector< CommandReply > reply
uint16_t handlerId
ID of the command handler responding to the command.