95 lines
3.2 KiB
C++
95 lines
3.2 KiB
C++
/**
|
|
* @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(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin) {
|
|
this->radio = new RF24(cePin, csnPin);
|
|
|
|
std::cout << "ControlPad::ControlPad: sizeof ControlPadInput in byte: " << sizeof(ControlPadInput) << std::endl;
|
|
|
|
|
|
if (!this->radio->begin(spiPort)) {
|
|
std::cout << "ControlPad::ControlPad: Error radio->begin!" << std::endl;
|
|
while(true);
|
|
}
|
|
|
|
// this->radio->setAddressWidth(5);
|
|
// this->radio->setChannel(7);
|
|
// this->radio->setDataRate(RF24_1MBPS);
|
|
// this->radio->setAutoAck(true);
|
|
// this->radio->enableDynamicPayloads();
|
|
// this->radio->enableAckPayload();
|
|
// this->radio->setCRCLength(RF24_CRC_16);
|
|
|
|
this->radio->setPALevel(RF24_PA_LOW);
|
|
|
|
// this->radio->openWritingPipe(this->address[1]);
|
|
this->radio->openReadingPipe(0, this->address[0]);
|
|
|
|
this->radio->startListening();
|
|
|
|
// this->radio->printPrettyDetails();
|
|
this->radio->printPrettyDetails();
|
|
}
|
|
|
|
bool ControlPad::loop() {
|
|
this->receiveData();
|
|
|
|
if (millis() - this->lastLoopMillis < this->loopDelay)
|
|
return false;
|
|
this->lastLoopMillis = millis();
|
|
|
|
// if(!this->connected)
|
|
// return false;
|
|
|
|
// if (millis() - this->lastMessageReceive > this->disconnectTime) {
|
|
// this->connected = false;
|
|
// return false;
|
|
// }
|
|
|
|
// if (this->menuControl && this->controlInput.buttons > 0 && this->updated)
|
|
// if (!this->firstButtonPress)
|
|
// this->menuControl->printMenu();
|
|
|
|
// 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->menuControl->update();
|
|
|
|
return true;
|
|
}
|
|
|
|
const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime) const {
|
|
if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime)
|
|
return nullptr;
|
|
return &this->controlInput;
|
|
}
|
|
|
|
void ControlPad::receiveData() {
|
|
this->radio->startListening();
|
|
if (this->radio->available()) {
|
|
ControlPadInput lastInput = this->controlInput;
|
|
this->radio->read(&this->controlInput, sizeof(ControlPadInput));
|
|
std::cout << "Data RF24: " << this->controlInput.counter << std::endl;
|
|
}
|
|
}
|