/** * @file akku.cpp * @author Alexander Klein (alex@kleiax.de) * @brief * @version 0.1 * @date 2022-02-05 * * @copyright Copyright (c) 2022 * */ #include "akku.h" Akku::Akku(uint8_t pin, double maxVolt, double emptyVolt) { this->pin = pin; this->maxVolt = maxVolt; this->emptyVolt = emptyVolt; this->updateOnePercent(); } void Akku::setEmptyVoltage(double volt) { this->emptyVolt = volt; this->updateOnePercent(); } void Akku::setMaxVoltage(double volt) { this->maxVolt = volt; this->updateOnePercent(); } double Akku::getVoltage() { double rawVoltage = this->readVoltage(); if (this->maxVolt) { } return rawVoltage; } uint8_t Akku::getPercent() { uint8_t res = 0; if (this->emptyVolt) res = (uint8_t) ((this->getVoltage() - this->emptyVolt) / this->onePercent); return res; } bool Akku::isEmpty() { if (this->getVoltage() < this->emptyVolt) { return true; } return false; } double Akku::readVoltage() { // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095 double reading = analogRead(this->pin); 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; } double Akku::updateOnePercent() { this->onePercent = (this->maxVolt - this->emptyVolt) / 100.0; }