Files
FacePlant/src/sensors/BatterySensor.h

44 lines
1.2 KiB
C
Raw Normal View History

#pragma once
#include <Arduino.h>
class BatterySensor {
public:
void begin();
void loop();
// Battery status
int percent() const { return _percent; }
float voltage() const { return _voltage; }
bool isLow() const { return _isLow; }
2026-02-10 21:31:36 -05:00
bool isCharging() const { return _isCharging; }
bool shouldBlink() const;
// For display
int iconFillWidth(int maxWidth) const;
private:
static constexpr int PIN_BATTERY_ADC = 2; // Voltage divider input
static constexpr int PIN_LBO = 5; // PowerBoost LBO pin
2026-02-10 21:31:36 -05:00
static constexpr int PIN_CHG = 4; // PowerBoost CHG pin (LOW when charging)
static constexpr unsigned long CHECK_INTERVAL_MS = 2000;
static constexpr int SAMPLES = 10; // Number of ADC samples to average
// LiPo voltage thresholds
static constexpr float VOLTAGE_MIN = 3.0; // 0%
static constexpr float VOLTAGE_MAX = 4.2; // 100%
static constexpr float VOLTAGE_LOW = 3.2; // Low battery threshold
// Voltage divider: R1=100k, R2=100k (2:1 ratio)
static constexpr float DIVIDER_RATIO = 2.0;
int _percent = 100;
float _voltage = 3.7;
bool _isLow = false;
2026-02-10 21:31:36 -05:00
bool _isCharging = false;
unsigned long _lastCheckMs = 0;
float readBatteryVoltage();
int voltageToPercent(float voltage);
};