clangtidy corrections part 1

This commit is contained in:
2023-10-11 17:35:40 +02:00
parent 77a52b82c3
commit 23d854a826
36 changed files with 2383 additions and 1165 deletions
+118 -78
View File
@@ -4,30 +4,32 @@
* @brief Contains the implementation of the class Battery
* @version 0.1
* @date 2022-02-05
*
*
* @copyright Copyright (c) 2022
*
*
*/
#include "battery.h"
Battery::Battery(uint8_t pin, uint32_t r1, uint32_t r2) {
this->pin = pin;
this->r1 = r1;
this->r2 = r2;
this->batteryVoltageFactor = (double) (this->r1 + this->r2) / (double) this->r2;
Battery::Battery(uint8_t pin, uint32_t firstResistor, uint32_t secondResistor)
: pin{pin}, firstResistor{firstResistor}, secondResistor{secondResistor},
batteryVoltageFactor{firstResistor + secondResistor / static_cast<double>(secondResistor)}
{
this->initBuffer();
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
Battery::Battery(uint8_t pin) {
this->pin = pin;
Battery::Battery(uint8_t pin)
: pin{pin}
{
this->initBuffer();
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
void Battery::run() {
if (this->calibrationState != CalibrationState::None) {
void Battery::run()
{
if (this->calibrationState != CalibrationState::None)
{
this->runCalibration();
return;
}
@@ -35,164 +37,202 @@ void Battery::run() {
this->readAdcToBuf();
this->loopCounter++;
if (this->loopCounter >= this->calulationDelayMultiplier) {
if (this->loopCounter >= this->calulationDelayMultiplier)
{
this->calculateBatteryVoltage();
this->calculateBatteryPercent();
this->loopCounter = 0;
this->calculatetNewValues = true;
}
return;
}
void Battery::runCalibration() {
void Battery::runCalibration()
{
if (this->calibrationState != CalibrationState::Reading)
{
return;
}
this->readAdcToBuf();
if (this->bufferPos == 0) {
uint16_t res = this->getBufAvg();
this->readAdcToBuf();
if (this->bufferPos == 0)
{
const uint16_t res = this->getBufAvg();
this->newRawAdcVoltages[this->currentCalibrationVoltage] = res;
std::cout << "Index: "
<< (int) this->currentCalibrationVoltage
std::cout << "Index: "
<< (int)this->currentCalibrationVoltage
<< " Value: "
<< (int) res
<< (int)res
<< std::endl;
this->currentCalibrationVoltage++;
this->calibrationState = CalibrationState::Waiting;
if (this->currentCalibrationVoltage == 60) {
if (this->currentCalibrationVoltage == Battery::rawAdcVoltagesCount)
{
this->calibrationState = CalibrationState::Finished;
}
}
}
double Battery::getBatteryVoltage() const {
double res = this->batteryVoltage;
return (int)(res*100+0.5)/100.0;
double Battery::getBatteryVoltage() const
{
return static_cast<int>((this->batteryVoltage * 100 + 0.5)) / 100.0;
}
bool Battery::isBatteryLow(double voltage) const {
bool Battery::isBatteryLow(double voltage) const
{
if (this->getBatteryVoltage() <= voltage && this->batteryVoltage > this->absurdLowVoltage)
{
return true;
}
return false;
}
bool Battery::isNewValue() {
bool Battery::isNewValue()
{
if (!this->calculatetNewValues)
{
return false;
}
this->calculatetNewValues = false;
return true;
}
void Battery::nextVoltageIsReady() {
if (this->calibrationState == CalibrationState::Waiting) {
void Battery::nextVoltageIsReady()
{
if (this->calibrationState == CalibrationState::Waiting)
{
this->calibrationState = CalibrationState::Reading;
}
}
void Battery::startCalibration() {
void Battery::startCalibration()
{
this->calibrationState = CalibrationState::Waiting;
this->currentCalibrationVoltage = 0;
this->bufferPos = 0;
this->loopDelay = 50;
this->newRawAdcVoltages = new uint16_t[60];
Component::loopDelay = Battery::loopDelay / 2;
this->newRawAdcVoltages = new uint16_t[Battery::rawAdcVoltagesCount];
}
void Battery::finishCalibration() {
void Battery::finishCalibration()
{
if (this->calibrationState != CalibrationState::None)
{
return;
}
delete[] this->newRawAdcVoltages;
this->calibrationState = CalibrationState::None;
this->loopDelay = 100;
Component::loopDelay = Battery::loopDelay;
}
double Battery::calculateInputVoltage() {
double Battery::calculateInputVoltage()
{
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = this->getBufAvg();
if(reading < 1 || reading > 4095) return 0;
return - 0.000000000000016 * pow(reading,4)
+ 0.000000000118171 * pow(reading,3)
- 0.000000301211691 * pow(reading,2)
+ 0.001109019271794 * reading
+ 0.034143524634089;
if (reading < 1 || reading > Battery::adcMaxValue)
{
return 0;
}
return -this->adcCurveCoeficient[0] * pow(reading, 4) + this->adcCurveCoeficient[1] * pow(reading, 3) - this->adcCurveCoeficient[2] * pow(reading, 2) + this->adcCurveCoeficient[3] * reading + this->adcCurveCoeficient[4];
}
void Battery::calculateBatteryVoltage() {
if (this->r1 && this->r2) {
void Battery::calculateBatteryVoltage()
{
if (this->firstResistor && this->secondResistor)
{
this->batteryVoltage = this->calculateInputVoltage() * this->batteryVoltageFactor;
return;
}
uint16_t adcValue = this->getBufAvg();
if (adcValue < this->rawAdcVoltages[0]) {
const uint16_t adcValue = this->getBufAvg();
if (adcValue < this->rawAdcVoltages[0])
{
this->batteryVoltage = -1;
return;
}
if (adcValue > this->rawAdcVoltages[this->rawAdcVoltagesCount] + 50) {
this->batteryVoltage = -2;
return;
}
uint8_t index;
for (index = 1; index < this->rawAdcVoltagesCount; index++) {
uint8_t index = 1;
for (; index < this->rawAdcVoltagesCount; index++)
{
if (adcValue < this->rawAdcVoltages[index])
{
break;
}
}
double indexDelta = this->rawAdcVoltages[index] - this->rawAdcVoltages[index - 1];
double valueDelta = this->rawAdcVoltages[index] - adcValue;
const double indexDelta = this->rawAdcVoltages[index] - this->rawAdcVoltages[index - 1];
const double valueDelta = this->rawAdcVoltages[index] - adcValue;
double voltage = this->startVoltage + (index - 1) * this->stepVoltage;
voltage += valueDelta / indexDelta * this->stepVoltage;
this->batteryVoltage = voltage;
// std::cout << "Battery::calculateBatteryVoltage() - Voltage: " << voltage << " Index: " <<(int) index << " adcValue: " << (int) adcValue <<" iD: " << indexDelta << " vD: " << valueDelta << std::endl;
}
void Battery::calculateBatteryPercent() {
void Battery::calculateBatteryPercent()
{
int8_t size = sizeof(this->capacityVoltages) / sizeof(*this->capacityVoltages);
uint8_t i;
for (i = 0; i < size; i++) {
if (this->batteryVoltage <= this->capacityVoltages[i])
uint8_t index = 0;
for (; index < size; index++)
{
if (this->batteryVoltage <= this->capacityVoltages[index])
{
break;
}
}
if (i == 0) {
if (this->batteryVoltage > 6)
std::cout << "Critical low battery!" << std::endl;
} else if (i == size - 1) {
} else {
double diffToLowerVal = this->batteryVoltage - this->capacityVoltages[i - 1];
double diffToHigherVal = this->capacityVoltages[i] - this->batteryVoltage;
if (diffToLowerVal > diffToHigherVal)
i--;
if (index == 0)
{
if (this->batteryVoltage > this->absurdLowVoltage)
{
std::cout << "Critical low battery!" << std::endl;
}
}
this->batteryPercent = i * (100 / (size - 1));
else if (index == size - 1)
{
}
else
{
const double diffToLowerVal = this->batteryVoltage - this->capacityVoltages[index - 1];
const double diffToHigherVal = this->capacityVoltages[index] - this->batteryVoltage;
if (diffToLowerVal > diffToHigherVal)
{
index--;
}
}
this->batteryPercent = index * (100 / (size - 1));
}
void Battery::readAdcToBuf() {
void Battery::readAdcToBuf()
{
this->adcBuffer[this->bufferPos] = analogRead(this->pin);
this->bufferPos++;
if (this->bufferPos == Battery::bufferSize)
{
this->bufferPos = 0;
}
// std::cout << "Battery::readAdcToBuf added Value: " << this->adcBuffer[this->bufferPos] << std::endl;
}
void Battery::initBuffer() {
void Battery::initBuffer()
{
for (uint8_t i = 0; i < Battery::bufferSize; i++)
{
this->adcBuffer[i] = 0;
}
}
uint16_t Battery::getBufAvg() const {
uint16_t Battery::getBufAvg() const
{
uint32_t res = 0;
uint8_t emptyPos = 0;
for (uint8_t i = 0; i < Battery::bufferSize; i++) {
for (uint8_t i = 0; i < Battery::bufferSize; i++)
{
if (this->adcBuffer[i] == 0)
{
emptyPos++;
}
res += this->adcBuffer[i];
}
return res / (Battery::bufferSize - emptyPos);
+106 -97
View File
@@ -4,9 +4,9 @@
* @brief Contains a class for battery monitoring
* @version 0.1
* @date 2022-02-05
*
*
* @copyright Copyright (c) 2022
*
*
*/
#ifndef BATTERY_H
@@ -21,118 +21,127 @@
/**
* @brief A class for battery monitoring
*
*
* This class reads the voltage from an analog pin to calculate the
* charge level of a 3 Cell Li-Poly battery pack. The battery pack have
* to be after a voltage diveder, so that maximum voltage for the
* microcontroller is 3.3 Volt.
*/
class Battery : public Component {
public:
enum CalibrationState {
None,
Reading,
Waiting,
Finished
};
class Battery : public Component
{
public:
enum CalibrationState
{
None,
Reading,
Waiting,
Finished
};
/**
* @brief Construct a new Battery object
*
* The voltage devider have to be calculated, so that the input
* voltage from 3.3 Volt is never exceeded. It is assumed that
* the microcontroller is connected to the second resistor.
*
* @param pin The analog to read from.
* @param r1 First resistor of the voltage devider.
* @param r2 Second resistor of the voltage devider.
*/
Battery(uint8_t pin, uint32_t r1, uint32_t r2);
Battery(uint8_t pin);
/**
* @brief Construct a new Battery object
*
* The voltage devider have to be calculated, so that the input
* voltage from 3.3 Volt is never exceeded. It is assumed that
* the microcontroller is connected to the second resistor.
*
* @param pin The analog to read from.
* @param firstResistor First resistor of the voltage devider.
* @param secondResistor Second resistor of the voltage devider.
*/
Battery(uint8_t pin, uint32_t firstResistor, uint32_t secondResistor);
Battery(uint8_t pin);
/**
* @brief Get the battery voltage
*
* @return double in Volt
*/
double getBatteryVoltage() const;
/**
* @brief Get the battery voltage
*
* @return double in Volt
*/
double getBatteryVoltage() const;
/**
* @brief Get the charge level of the battery
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() const { return this->batteryPercent; }
/**
* @brief Get the charge level of the battery
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() const { return this->batteryPercent; }
/**
* @brief Checks if the battery is low.
*
* The function will also return false if the battery voltage is
* absurd low. This is for the case that the uController is powered
* by usb and no battery is connected.
*
* @param voltage the limit the battery have to
* @return true if the battery is low
* @return false if the battery is high
*/
bool isBatteryLow(double voltage) const;
/**
* @brief Checks if the battery is low.
*
* The function will also return false if the battery voltage is
* absurd low. This is for the case that the uController is powered
* by usb and no battery is connected.
*
* @param voltage the limit the battery have to
* @return true if the battery is low
* @return false if the battery is high
*/
bool isBatteryLow(double voltage) const;
bool isNewValue();
bool isNewValue();
//Calibration
CalibrationState getCalibrationState() const { return this->calibrationState; }
uint8_t getCurrentCalibrationVoltage() const { return this->currentCalibrationVoltage; }
void nextVoltageIsReady();
void startCalibration();
void finishCalibration();
// Calibration
CalibrationState getCalibrationState() const { return this->calibrationState; }
uint8_t getCurrentCalibrationVoltage() const { return this->currentCalibrationVoltage; }
void nextVoltageIsReady();
void startCalibration();
void finishCalibration();
private:
void run() override;
void runCalibration();
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
void readAdcToBuf();
void initBuffer();
uint16_t getBufAvg() const;
private:
void run() override;
void runCalibration();
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
void readAdcToBuf();
void initBuffer();
uint16_t getBufAvg() const;
static const uint8_t bufferSize = 30;
static constexpr uint8_t bufferSize = 30;
static constexpr uint8_t loopDelay = 100;
static constexpr uint16_t adcMaxValue = 4095;
CalibrationState calibrationState = CalibrationState::None;
CalibrationState calibrationState = CalibrationState::None;
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t calulationDelayMultiplier = 5;
uint8_t loopCounter = 0;
uint8_t currentCalibrationVoltage = 0; // *0.1 + 7
uint16_t adcBuffer[bufferSize];
uint16_t* newRawAdcVoltages;
uint32_t r1 = 0;
uint32_t r2 = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
double batteryVoltageFactor;
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t calulationDelayMultiplier = 5;
uint8_t loopCounter = 0;
uint8_t currentCalibrationVoltage = 0; // *0.1 + 7
uint16_t adcBuffer[bufferSize];
uint16_t *newRawAdcVoltages = nullptr;
uint32_t firstResistor = 0;
uint32_t secondResistor = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
double batteryVoltageFactor;
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12, // 0 5 10 15
11.18, 11.24, 11.3, 11.36, // 20 25 30 35
11.39, 11.45, 11.51, 11.56, // 40 45 50 55
11.62, 11.74, 11.86, 11.95, // 60 65 70 75
12.07, 12.25, 12.33, 12.45, // 80 85 90 95
12.6 };
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12, // 0 5 10 15
11.18, 11.24, 11.3, 11.36, // 20 25 30 35
11.39, 11.45, 11.51, 11.56, // 40 45 50 55
11.62, 11.74, 11.86, 11.95, // 60 65 70 75
12.07, 12.25, 12.33, 12.45, // 80 85 90 95
12.6};
const uint8_t rawAdcVoltagesCount = 60;
const double startVoltage = 7;
const double stepVoltage = 0.1;
const uint16_t rawAdcVoltages[60] = // from 7.0V to 12.9V in 0.1V steps
{1992, 2021, 2056, 2090, 2118, 2145, 2177, 2208, 2241, 2272,
2298, 2331, 2362, 2387, 2420, 2453, 2482, 2514, 2543, 2577,
2607, 2640, 2670, 2703, 2736, 2763, 2794, 2826, 2858, 2890,
2920, 2956, 2983, 3019, 3054, 3088, 3121, 3158, 3189, 3226,
3264, 3300, 3339, 3379, 3414, 3453, 3500, 3544, 3598, 3636,
3682, 3730, 3781, 3837, 3887, 3943, 3997, 4054, 4093, 4095};
static constexpr uint8_t rawAdcVoltagesCount = 60;
const double startVoltage = 7;
const double stepVoltage = 0.1;
const uint16_t rawAdcVoltages[rawAdcVoltagesCount] = // from 7.0V to 12.9V in 0.1V steps
{1992, 2021, 2056, 2090, 2118, 2145, 2177, 2208, 2241, 2272,
2298, 2331, 2362, 2387, 2420, 2453, 2482, 2514, 2543, 2577,
2607, 2640, 2670, 2703, 2736, 2763, 2794, 2826, 2858, 2890,
2920, 2956, 2983, 3019, 3054, 3088, 3121, 3158, 3189, 3226,
3264, 3300, 3339, 3379, 3414, 3453, 3500, 3544, 3598, 3636,
3682, 3730, 3781, 3837, 3887, 3943, 3997, 4054, 4093, 4095};
const double adcCurveCoeficient[5] = {0.000000000000016,
0.000000000118171,
0.000000301211691,
0.001109019271794,
0.034143524634089};
};
#endif // BATTERY_H
+61 -54
View File
@@ -1,114 +1,121 @@
/**
* @file calcAzimuth.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-09-03
*
*
* @copyright Copyright (c) 2023
*
*
*/
#include "calcAzimuth.h"
CalcAzimuth::CalcAzimuth(Point point) {
this->lastChangePoint = point;
this->currentPosition = point;
this->loopDelay = 50;
CalcAzimuth::CalcAzimuth(Point point)
: lastChangePoint{point}, currentPosition{point}
{
Component::loopDelay = CalcAzimuth::loopDelay;
}
void CalcAzimuth::drivingDirectionChange(Point point) {
if (point.isInit() && point.isValid()) {
void CalcAzimuth::drivingDirectionChange(Point point)
{
if (point.isInit() && point.isValid())
{
this->directionChangeMode = true;
this->lastChangePoint = point;
this->state = State::Invalid;
}
}
void CalcAzimuth::updateCurrentPosition(Point point) {
void CalcAzimuth::updateCurrentPosition(Point point)
{
this->currentPosition = point;
this->positionChanged = true;
}
String CalcAzimuth::stateToString(State state) {
switch (state) {
case State::Invalid:
return "Invalid";
String CalcAzimuth::stateToString(State state)
{
switch (state)
{
case State::Invalid:
return "Invalid";
case State::Bad:
return "Bad";
case State::Bad:
return "Bad";
case State::Ok:
return "Ok";
case State::Ok:
return "Ok";
case State::Good:
return "Good";
case State::Good:
return "Good";
case State::Super:
return "Super";
default:
return "UNKOWN";
case State::Super:
return "Super";
default:
return "UNKOWN";
}
}
void CalcAzimuth::run() {
void CalcAzimuth::run()
{
if (!this->positionChanged)
{
return;
}
this->positionChanged = false;
this->updateAzimuth();
}
void CalcAzimuth::updateAzimuth() {
if (!this->directionChangeMode
|| this->lastChangePoint.distanceTo(this->currentPosition) < 1.0)
void CalcAzimuth::updateAzimuth()
{
if (!this->directionChangeMode || this->lastChangePoint.distanceTo(this->currentPosition) < 1.0)
{
this->state = State::Invalid;
this->calcAzimuth = 999;
this->calcAzimuth = INT16_MIN;
return;
}
this->calcAzimuth = this->lastChangePoint.courseTo(this->currentPosition);
// Map point accuracy to State
if (this->lastChangePoint.getAccuracy() == Point::Accuracy::oneDigOfCM
|| this->currentPosition.getAccuracy() == Point::Accuracy::oneDigOfCM)
if (this->lastChangePoint.getAccuracy() == Point::Accuracy::oneDigOfCM || this->currentPosition.getAccuracy() == Point::Accuracy::oneDigOfCM)
{
this->state = State::Good;
}
else if (this->lastChangePoint.getAccuracy() == Point::Accuracy::twoDigOfCM
|| this->currentPosition.getAccuracy() == Point::Accuracy::twoDigOfCM)
}
else if (this->lastChangePoint.getAccuracy() == Point::Accuracy::twoDigOfCM || this->currentPosition.getAccuracy() == Point::Accuracy::twoDigOfCM)
{
this->state = State::Ok;
}
else if (this->lastChangePoint.getAccuracy() == Point::Accuracy::threeDigOfCM
|| this->currentPosition.getAccuracy() == Point::Accuracy::threeDigOfCM)
}
else if (this->lastChangePoint.getAccuracy() == Point::Accuracy::threeDigOfCM || this->currentPosition.getAccuracy() == Point::Accuracy::threeDigOfCM)
{
this->state = State::Bad;
}
else
}
else
{
this->state = State::Invalid;
}
// Upgrade quality if the range grows up
if (this->lastChangePoint.distanceTo(this->currentPosition) > 2.0) {
switch (this->state) {
case State::Bad :
this->state = State::Ok;
break;
case State::Ok :
this->state = State::Good;
break;
if (this->lastChangePoint.distanceTo(this->currentPosition) > this->minDistanceForBetterQuality)
{
switch (this->state)
{
case State::Bad:
this->state = State::Ok;
break;
case State::Good :
this->state = State::Super;
break;
case State::Ok:
this->state = State::Good;
break;
default:
break;
case State::Good:
this->state = State::Super;
break;
default:
break;
}
}
}
+34 -29
View File
@@ -1,12 +1,12 @@
/**
* @file calcAzimuth.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-09-03
*
*
* @copyright Copyright (c) 2023
*
*
*/
#ifndef CALC_AZIMUTH_H
@@ -15,38 +15,43 @@
#include "component.h"
#include "point.h"
class CalcAzimuth : public Component {
public:
enum State {
Invalid,
Bad,
Ok,
Good,
Super
};
class CalcAzimuth : public Component
{
public:
enum State
{
Invalid,
Bad,
Ok,
Good,
Super
};
CalcAzimuth(Point point);
CalcAzimuth(Point point);
void drivingDirectionChange(Point point);
void updateCurrentPosition(Point point);
void disableCalcAzimuth() { this->directionChangeMode = false; }
void drivingDirectionChange(Point point);
void updateCurrentPosition(Point point);
void disableCalcAzimuth() { this->directionChangeMode = false; }
int16_t getAzimuth() const { return this->calcAzimuth; }
State getState() const { return this->state; }
int16_t getAzimuth() const { return this->calcAzimuth; }
State getState() const { return this->state; }
static String stateToString(State state);
static String stateToString(State state);
private:
void run() override;
void updateAzimuth();
private:
void run() override;
void updateAzimuth();
State state = State::Invalid;
Point lastChangePoint;
Point currentPosition;
State state = State::Invalid;
Point lastChangePoint;
Point currentPosition;
bool positionChanged = false;
bool directionChangeMode = false;
int16_t calcAzimuth = INT16_MAX;
bool positionChanged = false;
bool directionChangeMode = false;
int16_t calcAzimuth = INT16_MAX;
double minDistanceForBetterQuality = 2;
static constexpr uint8_t loopDelay = 50;
};
#endif //CALC_AZIMUTH_H
#endif // CALC_AZIMUTH_H
+22 -8
View File
@@ -1,25 +1,35 @@
#include "component.h"
Component::Component(uint16_t loopDelay) {
this->loopDelay = loopDelay;
Component::Component(uint16_t loopDelay) : loopDelay{loopDelay}
{
}
void Component::loop() {
void Component::loop()
{
if (!this->active)
{
return;
}
if (this->childComponents.size()){
std::list<Component*>::iterator it;
if (this->childComponents.size())
{
std::list<Component *>::iterator it;
for (it = this->childComponents.begin(); it != this->childComponents.end(); it++)
{
(*it)->loop();
}
}
this->runAsChild();
if (this->onlyChilds)
{
return;
}
if (this->loopDelay && millis() - this->lastMillis < this->loopDelay)
if (static_cast<bool>(this->loopDelay) && millis() - this->lastMillis < this->loopDelay)
{
return;
}
this->lastMillis = millis();
@@ -28,13 +38,17 @@ void Component::loop() {
this->afterRun();
if (this->timeUpdateAfter)
{
this->lastMillis = millis();
}
}
void Component::addChildComponent(Component* child) {
void Component::addChildComponent(Component *child)
{
this->childComponents.push_back(child);
}
void Component::removeChildComponent(Component* child) {
void Component::removeChildComponent(Component *child)
{
this->childComponents.remove(child);
}
+30 -29
View File
@@ -1,12 +1,12 @@
/**
* @file component.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-08-16
*
*
* @copyright Copyright (c) 2023
*
*
*/
#pragma once
@@ -15,37 +15,38 @@
#include <list>
class Component {
public:
Component() {}
Component(uint16_t loopDelay);
class Component
{
public:
Component() {}
Component(uint16_t loopDelay);
void loop();
void deactivate() { this->active = false; }
void activate() { this->active = false; }
protected:
virtual void runAsChild() {}
virtual void beforeRun() {}
virtual void run() = 0;
virtual void afterRun() {}
void loop();
void addChildComponent(Component* child);
void removeChildComponent(Component* child);
void deactivate() { this->active = false; }
void activate() { this->active = false; }
void activateOnlyChilds() { this->onlyChilds = true; }
void deactivateOnlyChilds() { this->onlyChilds = false; }
protected:
virtual void runAsChild() {}
virtual void beforeRun() {}
virtual void run() = 0;
virtual void afterRun() {}
void setTimerAfterTask() { this->timeUpdateAfter = true; }
void addChildComponent(Component *child);
void removeChildComponent(Component *child);
uint16_t loopDelay = 0;
void activateOnlyChilds() { this->onlyChilds = true; }
void deactivateOnlyChilds() { this->onlyChilds = false; }
private:
std::list<Component*> childComponents;
void setTimerAfterTask() { this->timeUpdateAfter = true; }
bool active = true;
bool onlyChilds = false;
bool timeUpdateAfter = false;
uint32_t lastMillis = 0;
uint16_t loopDelay = 0;
private:
std::list<Component *> childComponents;
bool active = true;
bool onlyChilds = false;
bool timeUpdateAfter = false;
uint32_t lastMillis = 0;
};
+38 -22
View File
@@ -1,37 +1,43 @@
/**
* @file controlPad.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-03-30
*
*
* @copyright Copyright (c) 2023
*
*
*/
#include "controlPad.h"
ControlPad::ControlPad() {
this->loopDelay = 5;
}
ControlPad::ControlPad() : Component(5), controlInput{0, 0, 0, 0} {}
void ControlPad::run() {
if(!this->connected)
void ControlPad::run()
{
if (!this->connected)
{
return;
}
if (millis() - this->lastMessageReceive > this->disconnectTime) {
if (millis() - this->lastMessageReceive > this->disconnectTime)
{
this->connected = false;
this->controlInput.buttons = 0;
this->controlInput.x = 127;
this->controlInput.y = 127;
this->controlInput.x = UINT8_MAX / 2;
this->controlInput.y = UINT8_MAX / 2;
return;
}
if (this->lastButtons != controlInput.buttons)
{
this->updated = true;
}
if (this->menuControl && this->controlInput.buttons > 0 && this->updated) {
if (this->firstButtonPress) {
if (static_cast<bool>(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;
@@ -39,38 +45,48 @@ void ControlPad::run() {
}
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;
}
void ControlPad::insertData(const uint8_t *data) {
void ControlPad::insertData(const uint8_t *data)
{
this->connected = true;
this->lastMessageReceive = millis();
uint8_t lastCount = this->controlInput.counter + 1;
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 > 127 - this->deadZoneX
&& this->controlInput.x < 127 + this->deadZoneX)
this->controlInput.x = 127;
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 > 127 - this->deadZoneY
&& this->controlInput.y < 127 + this->deadZoneY)
this->controlInput.y = 127;
if (this->controlInput.y > UINT8_MAX / 2 - this->deadZoneY && this->controlInput.y < UINT8_MAX / 2 + this->deadZoneY)
this->controlInput.y = UINT8_MAX / 2;
}
+23 -22
View File
@@ -1,12 +1,12 @@
/**
* @file controlPad.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-03-30
*
*
* @copyright Copyright (c) 2023
*
*
*/
#pragma once
@@ -16,33 +16,34 @@
#include <controlPadInput.h>
#include <component.h>
class ControlPad : public Component {
public:
ControlPad();
class ControlPad : public Component
{
public:
ControlPad();
void insertData(const uint8_t *data);
void insertData(const uint8_t *data);
void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; }
void setMenuControl(MenuControl *menuControl) { this->menuControl = menuControl; }
const ControlPadInput* getControlPadDataPtr() const { return &this->controlInput; }
const ControlPadInput *getControlPadDataPtr() const { return &this->controlInput; }
bool isControlPadConnected() const { return this->connected; }
bool isControlPadConnected() const { return this->connected; }
private:
void run() override;
private:
void run() override;
MenuControl* menuControl = nullptr;
ControlPadInput controlInput;
MenuControl *menuControl = nullptr;
ControlPadInput controlInput;
bool connected = false;
bool updated = false;
bool firstButtonPress = true;
bool connected = false;
bool updated = false;
bool firstButtonPress = true;
uint8_t deadZoneX = 20;
uint8_t deadZoneY = 20;
uint8_t lastButtons = 0;
uint8_t deadZoneX = 20;
uint8_t deadZoneY = 20;
uint8_t lastButtons = 0;
uint16_t disconnectTime = 100;
uint16_t disconnectTime = 100;
uint32_t lastMessageReceive = 0;
uint32_t lastMessageReceive = 0;
};
+48 -43
View File
@@ -1,12 +1,12 @@
/**
* @file controlPadInput.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-03-30
*
*
* @copyright Copyright (c) 2023
*
*
*/
#ifndef CONTROL_PAD_INPUT_H
@@ -14,61 +14,66 @@
#include <stdint.h>
struct ControlPadInput {
struct ControlPadInput
{
uint8_t buttons;
uint8_t x;
uint8_t y;
uint8_t counter;
};
class ControlPadButton
{
public:
enum PadButton
{
Left = 4,
Right = 8,
Up = 2,
Down = 1,
Yes = 32,
No = 16,
Action = 64
};
class ControlPadButton {
public:
enum PadButton {
Left = 4,
Right = 8,
Up = 2,
Down = 1,
Yes = 32,
No = 16,
Action = 64
};
static bool isControlPadButtonPressed(const ControlPadInput *input, PadButton button)
{
uint8_t buttonNum = input->buttons;
switch (button)
{
case PadButton::Left:
return buttonNum & (uint8_t)PadButton::Left;
break;
static bool isControlPadButtonPressed(const ControlPadInput *input, PadButton button) {
uint8_t buttonNum = input->buttons;
switch (button) {
case PadButton::Left :
return buttonNum & (uint8_t) PadButton::Left;
break;
case PadButton::Right:
return buttonNum & (uint8_t)PadButton::Right;
break;
case PadButton::Right :
return buttonNum & (uint8_t) PadButton::Right;
break;
case PadButton::Up:
return buttonNum & (uint8_t)PadButton::Up;
break;
case PadButton::Up :
return buttonNum & (uint8_t) PadButton::Up;
break;
case PadButton::Down:
return buttonNum & (uint8_t)PadButton::Down;
break;
case PadButton::Down :
return buttonNum & (uint8_t) PadButton::Down;
break;
case PadButton::Yes:
return buttonNum & (uint8_t)PadButton::Yes;
break;
case PadButton::Yes :
return buttonNum & (uint8_t) PadButton::Yes;
break;
case PadButton::No:
return buttonNum & (uint8_t)PadButton::No;
break;
case PadButton::No :
return buttonNum & (uint8_t) PadButton::No;
break;
case PadButton::Action:
return buttonNum & (uint8_t)PadButton::Action;
break;
case PadButton::Action :
return buttonNum & (uint8_t) PadButton::Action;
break;
default:
return false;;
}
default:
return false;
;
}
}
};
#endif // CONTROL_PAD_INPUT_H
+21 -14
View File
@@ -1,14 +1,14 @@
#include "counter.h"
#include <inttypes.h>
uint8_t Counter::amountOfCounter = 0;
Counter::Counter(uint8_t pin) {
this->pulsePin = pin;
this->unit = static_cast<pcnt_unit_t>(Counter::amountOfCounter);
if (Counter::amountOfCounter < 7)
Counter::Counter(uint8_t pin)
: pulsePin{pin}, unit{static_cast<pcnt_unit_t>(Counter::amountOfCounter)}
{
if (Counter::amountOfCounter <= Counter::maxCounter)
{
Counter::amountOfCounter++;
}
pcnt_config_t config;
config.unit = this->unit;
@@ -24,33 +24,40 @@ Counter::Counter(uint8_t pin) {
pcnt_unit_config(&config);
}
void Counter::pause() {
void Counter::pause()
{
pcnt_counter_pause(this->unit);
}
void Counter::resume() {
void Counter::resume()
{
pcnt_counter_resume(this->unit);
}
void Counter::clear() {
void Counter::clear()
{
pcnt_counter_clear(this->unit);
}
int16_t Counter::getValue() const {
int16_t res;
int16_t Counter::getValue() const
{
int16_t res = 0;
pcnt_get_counter_value(this->unit, &res);
return res;
}
void Counter::setFilterValue(uint16_t value) {
void Counter::setFilterValue(uint16_t value)
{
pcnt_set_filter_value(this->unit, value);
this->filterEnable();
}
void Counter::filterEnable() {
void Counter::filterEnable()
{
pcnt_filter_enable(this->unit);
}
void Counter::filterDisable() {
void Counter::filterDisable()
{
pcnt_filter_disable(this->unit);
}
+20 -17
View File
@@ -2,29 +2,32 @@
#include <Arduino.h>
#include <driver/pcnt.h>
#include <cinttypes>
class Counter {
public:
Counter(uint8_t pin);
class Counter
{
public:
Counter(uint8_t pin);
void pause();
void resume();
void clear();
void pause();
void resume();
void clear();
int16_t getValue() const;
int16_t getValue() const;
void setFilterValue(uint16_t value);
void filterEnable();
void filterDisable();
void setFilterValue(uint16_t value);
void filterEnable();
void filterDisable();
private:
static constexpr int16_t highLimit = INT16_MAX;
static constexpr uint8_t lowLimit = 0;
private:
static constexpr int16_t highLimit = INT16_MAX;
static constexpr uint8_t lowLimit = 0;
static constexpr uint8_t maxCounter = 6;
static uint8_t amountOfCounter;
static uint8_t amountOfCounter;
bool initalised = false;
bool initalised = false;
uint8_t pulsePin;
pcnt_unit_t unit;
uint8_t pulsePin;
pcnt_unit_t unit;
};
+36 -18
View File
@@ -4,62 +4,80 @@
* @brief Contains the implementation of the class LcdWrapper
* @version 0.1
* @date 2023-01-08
*
*
* @copyright Copyright (c) 2023
*
*
*/
#include "LcdWrapper.h"
LcdWrapper::LcdWrapper(LiquidCrystal_I2C* lcd) {
this->lcd = lcd;
LcdWrapper::LcdWrapper(LiquidCrystal_I2C *lcd)
: lcd{lcd}, changed{false}
{
this->clear();
this->changed = false;
}
void LcdWrapper::run() {
void LcdWrapper::run()
{
if (!this->changed)
{
return;
}
this->lcd->clear();
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++) {
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++)
{
this->lcd->setCursor(0, i);
this->lcd->print(this->data[i]);
this->lcd->print(static_cast<const char *>(this->data[i]));
}
if (this->callback)
if (static_cast<bool>(this->callback))
{
this->callback(this->data, LcdWrapper::totalLines, LcdWrapper::totalRows);
}
this->changed = false;
}
void LcdWrapper::clear() {
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++) {
for (uint8_t j = 0; j < LcdWrapper::totalRows; j++) {
void LcdWrapper::clear()
{
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++)
{
for (uint8_t j = 0; j < LcdWrapper::totalRows; j++)
{
data[i][j] = ' ';
}
}
this->changed = true;
}
void LcdWrapper::setCursor(uint8_t row, uint8_t line) {
void LcdWrapper::setCursor(uint8_t row, uint8_t line)
{
if (row > LcdWrapper::totalRows - 1)
{
row = LcdWrapper::totalRows - 1;
}
if (line > LcdWrapper::totalLines - 1)
{
line = LcdWrapper::totalLines - 1;
}
this->cursorRow = row;
this->cursorLine = line;
}
void LcdWrapper::print(const char *str) {
void LcdWrapper::print(const char *str)
{
uint8_t inputStringPosition = 0;
for (uint8_t i = this->cursorRow; i < LcdWrapper::totalRows; i++) {
for (uint8_t i = this->cursorRow; i < LcdWrapper::totalRows; i++)
{
if (str[inputStringPosition] == '\0')
{
break;
else
this->data[this->cursorLine][i] = str[inputStringPosition];
}
this->data[this->cursorLine][i] = str[inputStringPosition];
inputStringPosition++;
}
this->changed = true;
+45 -44
View File
@@ -4,9 +4,9 @@
* @brief Contains the LcdWrapper class
* @version 0.1
* @date 2023-01-08
*
*
* @copyright Copyright (c) 2023
*
*
*/
#ifndef LCD_WRAPPER_H
@@ -17,59 +17,60 @@
#include <displayWrapper.h>
#include <component.h>
typedef void (*LcdWrapperCallback) (const char data[][16], uint8_t lines, uint8_t rows);
typedef void (*LcdWrapperCallback)(const char data[][16], uint8_t lines, uint8_t rows);
/**
* @brief A class for the Menu class to print information
*
* @brief A class for the Menu class to print information
*
* This class inherits the DisplayWrapper class as a interface.
* The class takes the information to print from any thread. The
* The class takes the information to print from any thread. The
* loop function has to be called to print the data.
*/
class LcdWrapper : public DisplayWrapper, public Component {
public:
/**
* @brief Construct a new Lcd Wrapper object
*
* @param lcd
*/
LcdWrapper(LiquidCrystal_I2C* lcd);
class LcdWrapper : public DisplayWrapper, public Component
{
public:
/**
* @brief Construct a new Lcd Wrapper object
*
* @param lcd
*/
LcdWrapper(LiquidCrystal_I2C *lcd);
/**
* @brief Empty the buffer
*
*/
void clear() override;
/**
* @brief Empty the buffer
*
*/
void clear() override;
/**
* @brief Set point where data to be saved
*
* @param row
* @param line
*/
void setCursor(uint8_t row, uint8_t line) override;
void setCallback(LcdWrapperCallback callback) { this->callback = callback; }
/**
* @brief Set point where data to be saved
*
* @param row
* @param line
*/
void setCursor(uint8_t row, uint8_t line) override;
void setCallback(LcdWrapperCallback callback) { this->callback = callback; }
/**
* @brief Save the data to be printed
*
* @param str
*/
void print(const char *str) override;
/**
* @brief Save the data to be printed
*
* @param str
*/
void print(const char *str) override;
static constexpr uint8_t totalRows = 16;
static constexpr uint8_t totalLines = 2;
private:
void run() override;
static constexpr uint8_t totalRows = 16;
static constexpr uint8_t totalLines = 2;
LiquidCrystal_I2C* lcd;
LcdWrapperCallback callback = nullptr;
char data[LcdWrapper::totalLines][LcdWrapper::totalRows];
private:
void run() override;
uint8_t cursorRow = 0;
uint8_t cursorLine = 0;
LiquidCrystal_I2C *lcd;
LcdWrapperCallback callback = nullptr;
char data[LcdWrapper::totalLines][LcdWrapper::totalRows];
bool changed = false;
uint8_t cursorRow = 0;
uint8_t cursorLine = 0;
bool changed = false;
};
#endif // DISPLAY_WRAPPER_H
+70 -45
View File
@@ -1,68 +1,79 @@
/**
* @file calibrateCompass.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @brief
* @version 0.1
* @date 2023-05-23
*
*
* @copyright Copyright (c) 2023
*
*
*/
#include "calibrateCompass.h"
CalibrateCompass::CalibrateCompass(QMC5883LCompass* compass) {
CalibrateCompass::CalibrateCompass(QMC5883LCompass *compass)
{
this->compass = compass;
this->state = State::Ready;
this->clearData();
this->activateOnlyChilds();
}
void CalibrateCompass::runAsChild() {
void CalibrateCompass::runAsChild()
{
if (this->state != State::Calibrating)
{
return;
}
bool changed = false;
this->compass->read();
int x = this->compass->getX();
int y = this->compass->getY();
int z = this->compass->getZ();
int xAxis = this->compass->getX();
int yAxis = this->compass->getY();
int zAxis = this->compass->getZ();
if(x < this->data.data[0][0]) {
this->data.data[0][0] = x;
if (xAxis < this->data.data[0][0])
{
this->data.data[0][0] = xAxis;
changed = true;
}
if(x > this->data.data[0][1]) {
this->data.data[0][1] = x;
if (xAxis > this->data.data[0][1])
{
this->data.data[0][1] = xAxis;
changed = true;
}
if(y < this->data.data[1][0]) {
this->data.data[1][0] = y;
if (yAxis < this->data.data[1][0])
{
this->data.data[1][0] = yAxis;
changed = true;
}
if(y > this->data.data[1][1]) {
this->data.data[1][1] = y;
if (yAxis > this->data.data[1][1])
{
this->data.data[1][1] = yAxis;
changed = true;
}
if(z < this->data.data[2][0]) {
this->data.data[2][0] = z;
if (zAxis < this->data.data[2][0])
{
this->data.data[2][0] = zAxis;
changed = true;
}
if(z > this->data.data[2][1]) {
this->data.data[2][1] = z;
if (zAxis > this->data.data[2][1])
{
this->data.data[2][1] = zAxis;
changed = true;
}
if (changed)
this->lastChange = millis();
if (millis() - this->lastChange > this->maxTimeWithoutChange) {
if (millis() - this->lastChange > this->maxTimeWithoutChange)
{
this->state = State::Finished;
this->checkDataValidity();
}
@@ -70,9 +81,12 @@ void CalibrateCompass::runAsChild() {
void CalibrateCompass::run() {}
void CalibrateCompass::start() {
void CalibrateCompass::start()
{
if (this->state != State::Ready)
{
return;
}
this->clearData();
this->state = State::Calibrating;
@@ -80,39 +94,45 @@ void CalibrateCompass::start() {
this->lastChange = millis();
}
void CalibrateCompass::useData() {
if (!this->dataValid) {
void CalibrateCompass::useData()
{
if (!this->dataValid)
{
std::cout << "CalibrateCompass::useData - Data not valid" << std::endl;
return;
}
this->compass->setCalibration( this->data.data[0][0],
this->data.data[0][1],
this->data.data[1][0],
this->data.data[1][1],
this->data.data[2][0],
this->data.data[2][1]
);
this->compass->setCalibration(this->data.data[0][0],
this->data.data[0][1],
this->data.data[1][0],
this->data.data[1][1],
this->data.data[2][0],
this->data.data[2][1]);
std::cout << "CalibrateCompass::useData " << *this << std::endl;
std::cout << "CalibrateCompass::useData " << *this << std::endl;
}
void CalibrateCompass::removeCalibration() {
void CalibrateCompass::removeCalibration()
{
this->compass->clearCalibration();
}
void CalibrateCompass::reset() {
void CalibrateCompass::reset()
{
this->clearData();
this->state = State::Ready;
}
void CalibrateCompass::saveData() {
void CalibrateCompass::saveData()
{
if (!this->dataValid)
{
return;
}
Preferences preferences;
preferences.begin("compass", false);
preferences.putInt("xLow", this->data.data[0][0]);
preferences.putInt("xHigh", this->data.data[0][1]);
preferences.putInt("yLow", this->data.data[1][0]);
@@ -123,7 +143,8 @@ void CalibrateCompass::saveData() {
preferences.end();
}
void CalibrateCompass::loadData() {
void CalibrateCompass::loadData()
{
Preferences preferences;
preferences.begin("compass", true);
@@ -138,20 +159,23 @@ void CalibrateCompass::loadData() {
this->checkDataValidity();
}
void CalibrateCompass::clearData() {
for (uint8_t i = 0; i < 3; i++) {
void CalibrateCompass::clearData()
{
for (uint8_t i = 0; i < 3; i++)
{
this->data.data[i][0] = 0;
this->data.data[i][1] = 0;
}
this->dataValid = false;
}
void CalibrateCompass::checkDataValidity() {
void CalibrateCompass::checkDataValidity()
{
int sum = 0;
for (uint8_t i = 0; i < 3; i++) {
if (this->data.data[i][0] > INT16_MAX || this->data.data[i][0] < INT16_MIN
|| this->data.data[i][1] > INT16_MAX || this->data.data[i][1] < INT16_MIN)
{
for (uint8_t i = 0; i < 3; i++)
{
if (this->data.data[i][0] > INT16_MAX || this->data.data[i][0] < INT16_MIN || this->data.data[i][1] > INT16_MAX || this->data.data[i][1] < INT16_MIN)
{
this->dataValid = false;
return;
}
@@ -161,7 +185,8 @@ void CalibrateCompass::checkDataValidity() {
this->dataValid = sum;
}
std::ostream& operator<<(std::ostream& os, const CalibrateCompass& caliComp) {
std::ostream &operator<<(std::ostream &os, const CalibrateCompass &caliComp)
{
os << "(";
os << caliComp.data.data[0][0];
os << ", ";