/* * LedCommand.cpp * * Created on: Jul 16, 2021 * Author: Andreas Berthoud */ #include "LedCommand.hpp" #include "commands.h" #include "stm32wbxx_hal.h" #include "main.h" LedResponse::LedResponse(uint16_t response_identifier, bool was_successful) : Response(response_identifier) { (this->payload_ptr + this->get_payload_size())[0] = (uint8_t)was_successful; this->add_to_payload_size(1); } LedRequest::LedRequest(uint8_t * payload_ptr, uint16_t size) : Request(payload_ptr, size) { uint16_t expected_length = this->buffer_offset + 2; if (expected_length != size) { //log_error("LedCommand: received request with length %d, expected length %d", 2, size, expected_length); this->has_error = true; return; } uint8_t * data_ptr = payload_ptr + this->buffer_offset; this->led_id = static_cast(data_ptr[0]); this->led_command = static_cast(data_ptr[1]); } LedResponse * LedRequest::execute_request(uint16_t response_identifier) { bool was_successful = true; int led_pin_mapping[3] = {LED_GREEN_Pin, LED_RED_Pin, LED_BLUE_Pin}; GPIO_TypeDef* led_prio_port_mapping[3] = {LED_GREEN_GPIO_Port, LED_RED_GPIO_Port, LED_BLUE_GPIO_Port}; if (led_id >= led_id_max) { //log_error("LedCommand: invalid LED ID %d", 1, led_id); was_successful = false; } else { switch (this->led_command) { case led_off: HAL_GPIO_WritePin(led_prio_port_mapping[led_id], led_pin_mapping[led_id], GPIO_PIN_RESET); break; case led_on: HAL_GPIO_WritePin(led_prio_port_mapping[led_id], led_pin_mapping[led_id], GPIO_PIN_SET); break; case led_toggle: HAL_GPIO_TogglePin(led_prio_port_mapping[led_id], led_pin_mapping[led_id]); break; default: //log_error("LedCommand: invalid LED command %d", 1, this->led_id); was_successful = false; break; } } return new LedResponse(response_identifier, was_successful); }