remove most of the defines

This commit is contained in:
2023-08-18 15:34:56 +02:00
parent 4cd2b9368d
commit a82b32087c
21 changed files with 155 additions and 285 deletions
+14 -39
View File
@@ -11,18 +11,22 @@
*/
#include "speedometer.h"
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg) {
this->init(pin, diameter, steps);
this->bufSize = numOfValForAvg;
this->loopDelay = DELAY_SPEEDOMETER;
}
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps) {
this->init(pin, diameter, steps);
this->diameter = diameter;
this->steps = steps;
this->pulseCounter = new Counter(pin);
this->pulseCounter->setFilterValue(1000); // ignore pulses less than 1000 x 2.5ns
this->pulseCounter->clear();
this->pulseCounter->resume();
Component::loopDelay = Speedometer::loopDelay;
clearAvgBuf();
}
Speedometer::~Speedometer() {
delete[] this->buf;
delete this->pulseCounter;
}
@@ -58,7 +62,7 @@ void Speedometer::run() {
break;
}
this->addValToBuf(static_cast<int16_t>(this->speed * CONVERSION_FACTOR));
this->addValToBuf(static_cast<int16_t>(this->speed * Speedometer::conversionFactor));
}
void Speedometer::setDirection(Direction dir) {
@@ -68,11 +72,6 @@ void Speedometer::setDirection(Direction dir) {
this->clearAvgBuf();
}
void Speedometer::setNumOfValForAvg(uint8_t val) {
this->bufSize = val;
updateAvgBufSize();
}
void Speedometer::setEncFilter(uint16_t val) {
if (val > 1023)
val = 1023;
@@ -81,7 +80,7 @@ void Speedometer::setEncFilter(uint16_t val) {
double Speedometer::getAvgSpeed() const {
int16_t avg = this->calcAverage();
return (float)avg / CONVERSION_FACTOR;
return (float)avg / Speedometer::conversionFactor;
}
void Speedometer::calibrationMeasurementStart() {
@@ -101,25 +100,6 @@ uint16_t Speedometer::calibrationMeasurementStop() {
return res;
}
void Speedometer::init(uint8_t pin, double diameter, uint16_t steps) {
this->diameter = diameter;
this->steps = steps;
this->pulseCounter = new Counter(pin);
this->pulseCounter->setFilterValue(1000); // ignore pulses less than 1000 x 2.5ns
this->pulseCounter->clear();
this->pulseCounter->resume();
initAvgBuf();
}
void Speedometer::initAvgBuf() {
this->buf = new int16_t[bufSize];
for (uint8_t i = 0; i < bufSize; i++)
this->buf[i] = 0;
}
void Speedometer::clearAvgBuf() {
for (uint8_t i = 0; i < bufSize; i++)
this->buf[i] = 0;
@@ -133,11 +113,6 @@ void Speedometer::addValToBuf(int16_t val) {
bufPos = 0;
}
void Speedometer::updateAvgBufSize() {
delete[] this->buf;
initAvgBuf();
}
int16_t Speedometer::calcAverage() const {
int16_t sum = 0;
for (int i = 0; i < this->bufSize; i++)
+5 -19
View File
@@ -19,19 +19,6 @@
#include "counter.h"
#include "component.h"
/**
* @brief The default size of numbers to be taken in account for the average.
*
*/
#define BUFSIZE 5
#define CONVERSION_FACTOR 100
/**
* @brief Default value for min Millisseconds between each loop
* @see setDelay(uint8_t val)
*/
#define DELAY_SPEEDOMETER 30
/**
* @brief A class which use a encoder to calc the speed
*
@@ -60,9 +47,7 @@ class Speedometer : public Component {
* @param pin Pin on the Esp from the encoder.
* @param diameter Diameter of the wheel in meters.
* @param steps Encodersteps for a complete wheel rotation.
* @param numOfValForAvg Number of last values to be taken into account for the average.
*/
Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg);
Speedometer(uint8_t pin, double diameter, uint16_t steps);
~Speedometer();
@@ -127,12 +112,14 @@ class Speedometer : public Component {
private:
void run() override;
void init(uint8_t pin, double diameter, uint16_t steps);
void initAvgBuf();
void clearAvgBuf();
void addValToBuf(int16_t val);
void updateAvgBufSize();
int16_t calcAverage() const;
static constexpr uint8_t loopDelay = 30;
static constexpr uint8_t bufSize = 5;
static constexpr uint8_t conversionFactor = 100;
Counter* pulseCounter;
Direction currentDirection = Direction::None;
@@ -142,11 +129,10 @@ class Speedometer : public Component {
double diameter;
uint8_t printCounter = 0;
uint8_t bufSize = BUFSIZE;
uint8_t bufPos = 0;
uint16_t steps;
int16_t *buf = nullptr;
int16_t buf[Speedometer::bufSize];
uint32_t lastMillisCalc = 0;
};