cleaning
refactore all components now have a base class compnent for loop functions
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
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) {
|
||||
@@ -25,24 +26,11 @@ Speedometer::~Speedometer() {
|
||||
delete this->pulseCounter;
|
||||
}
|
||||
|
||||
uint16_t Speedometer::loop() {
|
||||
void Speedometer::run() {
|
||||
if (this->calibrationRunning)
|
||||
return -1;
|
||||
return;
|
||||
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsedTime = time - this->lastMillisLoop;
|
||||
|
||||
//Cancel if delayLoop is not reached
|
||||
if (elapsedTime < this->delayLoop)
|
||||
return elapsedTime;
|
||||
|
||||
runSpeedometer();
|
||||
this->lastMillisLoop = time;
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
void Speedometer::runSpeedometer() {
|
||||
uint32_t time = millis();
|
||||
|
||||
uint16_t elapsedTime = time - this->lastMillisCalc;
|
||||
this->lastMillisCalc = time;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "counter.h"
|
||||
#include "component.h"
|
||||
|
||||
/**
|
||||
* @brief The default size of numbers to be taken in account for the average.
|
||||
@@ -38,7 +39,7 @@
|
||||
* The calculated speed is the average of an amount of last measurements.
|
||||
*
|
||||
*/
|
||||
class Speedometer {
|
||||
class Speedometer : public Component {
|
||||
public:
|
||||
/**
|
||||
* @brief Enum to control the direction.
|
||||
@@ -66,24 +67,6 @@ class Speedometer {
|
||||
|
||||
~Speedometer();
|
||||
|
||||
/**
|
||||
* @brief Calls runSpeedometer() to update all Values.
|
||||
*
|
||||
* This function should be called every mainloop. If the delayLoop is not reached, than the
|
||||
* functions returns immediately.
|
||||
* @see runSpeedometer()
|
||||
* @see DELAY_SPEEDOMETER
|
||||
* @return time since the last call in Milliseconds
|
||||
*/
|
||||
uint16_t loop();
|
||||
|
||||
/**
|
||||
* @brief Normally called repeatedly by loop() to calculate new values.
|
||||
*
|
||||
* Add a new Value to the average and update the speed.
|
||||
*/
|
||||
void runSpeedometer();
|
||||
|
||||
/**
|
||||
* @brief Set the direction
|
||||
*
|
||||
@@ -107,13 +90,6 @@ class Speedometer {
|
||||
*/
|
||||
void setEncFilter(uint16_t val);
|
||||
|
||||
/**
|
||||
* @brief Set the min delayLoop between each loop
|
||||
*
|
||||
* @param delayLoop time in Milliseconds
|
||||
*/
|
||||
void setDelay(uint8_t delayLoop) { this->delayLoop = delayLoop; }
|
||||
|
||||
/**
|
||||
* @brief Get the Direction
|
||||
*
|
||||
@@ -149,6 +125,7 @@ class Speedometer {
|
||||
|
||||
|
||||
private:
|
||||
void run() override;
|
||||
void init(uint8_t pin, double diameter, uint16_t steps);
|
||||
void initAvgBuf();
|
||||
void clearAvgBuf();
|
||||
@@ -166,13 +143,11 @@ class Speedometer {
|
||||
|
||||
uint8_t printCounter = 0;
|
||||
uint8_t bufSize = BUFSIZE;
|
||||
uint8_t delayLoop = DELAY_SPEEDOMETER;
|
||||
uint8_t bufPos = 0;
|
||||
uint16_t steps;
|
||||
|
||||
int16_t *buf = nullptr;
|
||||
|
||||
uint32_t lastMillisLoop = 0;
|
||||
uint32_t lastMillisCalc = 0;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user