You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.2 KiB

/*
* Command.cpp
*
* Created on: Jul 8, 2021
* Author: Andreas Berthoud
*/
#include <queue>
#include <cstring>
#include "Command.hpp"
#include "string.h"
#include "stm32wbxx_hal.h"
#include "commands.hpp"
#include "HeartbeatCommand.hpp"
#include "LedCommand.hpp"
std::queue<Command*> command_queue;
// TODO: Add limit and return false if queue is full. Otherwise, we will get a full heap and crash :-(
void push_command(Command * command) {
command_queue.push(command);
}
void pop_and_execute_commands() {
while (!command_queue.empty()) {
Command * command = command_queue.front();
bool was_successful = command->execute();
HAL_Delay(5); // this delay is required. Otherwise, the command were not sent somehow...
if (!was_successful) {
log_error("Execution of command with ID %n was not successful!", 1, command->id);
}
delete command;
command_queue.pop();
}
}
void handle_received_command(uint8_t command_id, uint8_t * payload_ptr, uint16_t size) {
switch (command_id)
{
case COMMAND_HEARTBEAT_REQUEST:
push_command(new HeartbeatRequest(payload_ptr, size));
break;
case COMMAND_LED_REQUEST:
push_command(new LedRequest(payload_ptr, size));
break;
default:
break;
}
}