working remoteControl via espNow

This commit is contained in:
2023-04-11 12:47:32 +02:00
parent e3c452436d
commit 3fab7c7f83
8 changed files with 125 additions and 78 deletions
+42 -35
View File
@@ -16,58 +16,65 @@ ControlPad::ControlPad() {
}
bool ControlPad::loop() {
this->receiveData();
if (millis() - this->lastLoopMillis < this->loopDelay)
return false;
this->lastLoopMillis = millis();
// if(!this->connected)
// return false;
if(!this->connected)
return false;
// if (millis() - this->lastMessageReceive > this->disconnectTime) {
// this->connected = false;
// return false;
// }
if (millis() - this->lastMessageReceive > this->disconnectTime) {
this->connected = false;
this->controlInput.buttons = 0;
this->controlInput.x = 127;
this->controlInput.y = 127;
return false;
}
// if (this->menuControl && this->controlInput.buttons > 0 && this->updated)
// if (!this->firstButtonPress)
// this->menuControl->printMenu();
if (this->lastButtons != controlInput.buttons)
this->updated = true;
// 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();
if (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 true;
}
// this->menuControl->update();
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;
}
return true;
}
void ControlPad::insertData(const uint8_t *data) {
this->connected = true;
this->lastMessageReceive = millis();
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;
this->lastMessageReceive = millis();
std::cout << "data: " << (int) this->controlInput.counter << std::endl;
}
const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime) const {
if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime)
return nullptr;
return &this->controlInput;
}
void ControlPad::receiveData() {
if (this->controlInput.x > 127 - this->deadZoneX
&& this->controlInput.x < 127 + this->deadZoneX)
this->controlInput.x = 127;
if (this->controlInput.y > 127 - this->deadZoneY
&& this->controlInput.y < 127 + this->deadZoneY)
this->controlInput.y = 127;
}