/** * @file controlPad.cpp * @author Alexander Klein (alex@kleiax.de) * @brief * @version 0.1 * @date 2023-03-30 * * @copyright Copyright (c) 2023 * */ #include "controlPad.h" ControlPad::ControlPad() : Component(10), controlInput{0, 0, 0, 0} {} void ControlPad::run() { if (!this->connected) { return; } if (millis() - this->lastMessageReceive > this->disconnectTime) { this->connected = false; this->controlInput.buttons = 0; this->controlInput.x = UINT8_MAX / 2; this->controlInput.y = UINT8_MAX / 2; return; } if (this->lastButtons != controlInput.buttons) { this->updated = true; } if (static_cast(this->menuControl) && this->controlInput.buttons > 0 && this->updated) { if (this->firstButtonPress) { this->menuControl->printMenu(); this->firstButtonPress = false; std::cout << "ControlPad::loop - First menu print" << std::endl; return; } if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Left)) { this->menuControl->left(); } else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Right)) { this->menuControl->right(); } else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Up)) { this->menuControl->up(); } else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Down)) { this->menuControl->down(); } else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Yes)) { this->menuControl->yes(); } else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::No)) { this->menuControl->no(); } this->updated = false; this->lastButtons = controlInput.buttons; } } void ControlPad::insertData(const uint8_t *data) { this->connected = true; this->lastMessageReceive = millis(); const uint8_t lastCount = this->controlInput.counter + 1; memcpy(&(this->controlInput), data, sizeof(this->controlInput)); if (lastCount != this->controlInput.counter) std::cout << "ControlPad::insertData counter wrong value" << std::endl; if (this->controlInput.x > UINT8_MAX / 2 - this->deadZoneX && this->controlInput.x < UINT8_MAX / 2 + this->deadZoneX) this->controlInput.x = UINT8_MAX / 2; if (this->controlInput.y > UINT8_MAX / 2 - this->deadZoneY && this->controlInput.y < UINT8_MAX / 2 + this->deadZoneY) this->controlInput.y = UINT8_MAX / 2; }