OMOTE/Platformio/HAL/Targets/ESP32/battery/battery.cpp
2023-09-09 21:44:47 -04:00

38 lines
No EOL
827 B
C++

#include "battery.hpp"
#include <Arduino.h>
void Battery::setup(DisplayInterface& display, int adc_pin, int charging_pin)
{
this->display = display;
this->adc_pin = adc_pin;
this->charging_pin = charging_pin;
// Power Pin Definition
pinMode(this->charging_pin, INPUT_PULLUP);
pinMode(this->adc_pin, INPUT);
}
int Battery::getPercentage()
{
return constrain(map(this->getVoltage(), 3700, 4200, 0, 100), 0, 100);
}
bool Battery::isCharging()
{
return !digitalRead(this->charging_pin);
}
bool Battery::isConnected()
{
return ((!this->isCharging()) && (this->getVoltage() < 4350));
}
int Battery::getVoltage()
{
return analogRead(this->adc_pin)*2*3300/4095 + 350;
}
void Battery::update()
{
display.update_battery(this->getPercentage(), this->isCharging(), this->isConnected());
}