refactore
all components now have a base class compnent for loop functions
This commit is contained in:
2023-08-18 10:47:35 +02:00
parent 25d37e3970
commit 11d21e2e89
35 changed files with 303 additions and 511 deletions
+14 -7
View File
@@ -16,28 +16,27 @@ Battery::Battery(uint8_t pin, uint32_t r1, uint32_t r2) {
this->r1 = r1;
this->r2 = r2;
this->initBuffer();
this->loopDelay = 100;
}
Battery::Battery(uint8_t pin) {
this->pin = pin;
this->initBuffer();
this->loopDelay = 100;
}
bool Battery::loop() {
if (millis() - this->lastMillis < this->delay)
return false;
void Battery::run() {
this->readAdcToBuf();
this->loopCounter++;
this->lastMillis = millis();
if (this->loopCounter == this->calulationDelayMultiplier) {
this->calculateBatteryVoltage();
this->calculateBatteryPercent();
return true;
this->loopCounter = 0;
this->calculatetNewValues = true;
}
return false;
return;
}
double Battery::getBatteryVoltage() {
@@ -51,6 +50,14 @@ bool Battery::isBatteryLow(double voltage) {
return false;
}
bool Battery::isNewValue() {
if (!this->calculatetNewValues)
return false;
this->calculatetNewValues = false;
return true;
}
double Battery::calculateInputVoltage() {
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = this->getBufAvg();
+8 -25
View File
@@ -17,6 +17,8 @@
#include <math.h>
#include <Arduino.h>
#include "component.h"
/**
* @brief A class for battery monitoring
*
@@ -25,7 +27,7 @@
* to be after a voltage diveder, so that maximum voltage for the
* microcontroller is 3.3 Volt.
*/
class Battery {
class Battery : public Component {
public:
/**
* @brief Construct a new Battery object
@@ -41,27 +43,6 @@ class Battery {
Battery(uint8_t pin, uint32_t r1, uint32_t r2);
Battery(uint8_t pin);
/**
* @brief Read new values if the delay is reached.
*
* @return bool true if new values are calculated
*/
bool loop();
/**
* @brief Set the delay to wait befor new values are read
*
* @param delay in milliseconds
*/
void setDelay(uint16_t delay) {this->delay = delay; }
/**
* @brief Get the delay to wait befor new values are read
*
* @return uint16_t delay in milliseconds
*/
uint16_t getDelay() {return this->delay; }
/**
* @brief Get the battery voltage
*
@@ -74,7 +55,7 @@ class Battery {
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() {return this->batteryPercent;}
uint8_t getBatteryPercent() { return this->batteryPercent; }
/**
* @brief Checks if the battery is low.
@@ -89,7 +70,10 @@ class Battery {
*/
bool isBatteryLow(double voltage);
bool isNewValue();
private:
void run() override;
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
@@ -104,13 +88,12 @@ class Battery {
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t delay = 100;
uint8_t calulationDelayMultiplier = 10;
uint8_t loopCounter = 0;
uint16_t adcBuffer[bufferSize];
uint32_t r1 = 0;
uint32_t r2 = 0;
uint64_t lastMillis = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12, // 0 5 10 15