Moved the lambdas to actual functions to avoid them dissapearing after setup.

This commit is contained in:
2024-09-25 18:47:12 -04:00
parent 59bafb2549
commit a5652102a0
3 changed files with 39 additions and 34 deletions

View File

@@ -3,7 +3,7 @@
CommandHandler::CommandHandler(){ CommandHandler::CommandHandler(){
for(uint32_t i = 0; i < MAX_COMMAND_SIZE; i++){ for(uint32_t i = 0; i < MAX_COMMAND_SIZE; i++){
commandCallbacks[i] = nullptr; commandCallbacks[i] = nullptr;
commandCallbackIDs[i] = 0; commandCallbackIDs[i] = -1;
} }
} }
@@ -30,6 +30,10 @@ bool CommandHandler::RemoveCommand(uint32_t commandID){
} }
CommandHandler::CommandStatus CommandHandler::ProcessCommand(uint32_t * command, uint32_t commandSize){ CommandHandler::CommandStatus CommandHandler::ProcessCommand(uint32_t * command, uint32_t commandSize){
if(commandSize == 0){
return CommandStatus::INVALID;
}
uint32_t commandID{command[0]}; uint32_t commandID{command[0]};
// get a pointer to the second element in the array because the first element is the command ID // get a pointer to the second element in the array because the first element is the command ID
uint32_t * args{&(command[1])}; uint32_t * args{&(command[1])};

View File

@@ -39,8 +39,6 @@ public:
private: private:
static constexpr uint32_t MAX_COMMAND_SIZE{10}; static constexpr uint32_t MAX_COMMAND_SIZE{10};
// pointer to the head of the command array
uint32_t * command{nullptr};
// an array of command callbacks // an array of command callbacks
CommandCallback commandCallbacks[MAX_COMMAND_SIZE]; CommandCallback commandCallbacks[MAX_COMMAND_SIZE];

View File

@@ -104,6 +104,34 @@ void SetStackColor(uint32_t * args, uint32_t argsLength){
boardManager.SetColumnColors(V3D<uint32_t>{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors, numColors); boardManager.SetColumnColors(V3D<uint32_t>{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors, numColors);
} }
// command handling functions
CommandHandler::CommandStatus BoardStateCommandHandler(uint32_t * /*args*/, uint32_t /*argsLength*/){
printBoardState();
return CommandHandler::CommandStatus::SUCCESS;
}
CommandHandler::CommandStatus PingCommandHandler(uint32_t * /*args*/, uint32_t /*argsLength*/){
GlobalPrint::Println("!" + String(Commands::PING) + ";");
return CommandHandler::CommandStatus::SUCCESS;
}
CommandHandler::CommandStatus SetColorCommandHandler(uint32_t * args, uint32_t argsLength){
GlobalPrint::Println("!2;");
animator.isEnabled = false;
V3D<uint32_t> black{};
boardManager.FillColor(black);
SetStackColor(reinterpret_cast<uint32_t *>(args), argsLength);
return CommandHandler::CommandStatus::SUCCESS;
}
CommandHandler::CommandStatus GoToIdleCommandHandler(uint32_t * /*args*/, uint32_t /*argsLength*/){
GlobalPrint::Println("!3;");
animator.isEnabled = true;
return CommandHandler::CommandStatus::SUCCESS;
}
// -------------------------------------------------- // --------------------------------------------------
// ----------------- FREERTOS TASKS ----------------- // ----------------- FREERTOS TASKS -----------------
// -------------------------------------------------- // --------------------------------------------------
@@ -113,7 +141,7 @@ void UpdateCommunication(void * params){
// DO serial processing // DO serial processing
serialMessage.Update(); serialMessage.Update();
if(serialMessage.IsNewData()){ if(serialMessage.IsNewData()){
// TODO: Is it really a good idea to cast to unsigned here? do we ever use signed values? Find out. // We reinterpret cast the args to a uint32_t pointer because we know that the args will always be positive
commandHandler.ProcessCommand(reinterpret_cast<uint32_t *>(serialMessage.GetArgs()), serialMessage.GetPopulatedArgs()); commandHandler.ProcessCommand(reinterpret_cast<uint32_t *>(serialMessage.GetArgs()), serialMessage.GetPopulatedArgs());
serialMessage.ClearNewData(); serialMessage.ClearNewData();
} }
@@ -181,36 +209,11 @@ void setup() {
SetupBluetoothModule(); SetupBluetoothModule();
Serial.begin(9600); Serial.begin(9600);
// TODO: We should define these as functions and not lambdas because I think these get // Register all of our commands with the command handler
// destroyed once the setup function exits and that will break everything commandHandler.RegisterCommand(Commands::BoardState, BoardStateCommandHandler);
// register the commands with the command handler commandHandler.RegisterCommand(Commands::PING, PingCommandHandler);
commandHandler.RegisterCommand(Commands::BoardState, [](uint32_t * /*args*/, uint32_t /*argsLength*/){ commandHandler.RegisterCommand(Commands::SetStackColors, SetColorCommandHandler);
printBoardState(); commandHandler.RegisterCommand(Commands::GoToIdle, GoToIdleCommandHandler);
return CommandHandler::CommandStatus::SUCCESS;
});
// create a lambda function to handle the ping command which calls GlobalPrint::Println("!" + String(Commands::PING) + ";");
commandHandler.RegisterCommand(Commands::PING, [](uint32_t * /*args*/, uint32_t /*argsLength*/){
GlobalPrint::Println("!" + String(Commands::PING) + ";");
return CommandHandler::CommandStatus::SUCCESS;
});
commandHandler.RegisterCommand(Commands::SetStackColors, [](uint32_t * args, uint32_t argsLength){
GlobalPrint::Println("!2;");
animator.isEnabled = false;
V3D<uint32_t> black{};
boardManager.FillColor(black);
SetStackColor(reinterpret_cast<uint32_t *>(args), argsLength);
return CommandHandler::CommandStatus::SUCCESS;
});
commandHandler.RegisterCommand(Commands::GoToIdle, [](uint32_t * /*args*/, uint32_t /*argsLength*/){
GlobalPrint::Println("!3;");
animator.isEnabled = true;
return CommandHandler::CommandStatus::SUCCESS;
});
Serial.println("Configuring communication methods"); Serial.println("Configuring communication methods");
serialMessage.Init(9600); serialMessage.Init(9600);