firmware/src/modules/Telemetry/Sensor/MAX17048Sensor.h
Chloe Bethel d494c23a88
Enable telemetry and I2C sensors on STM32WL (except accelerometers) (#7008)
* Update platformio inis for stm32 platform and wio-e5 variant for enabling i2c

* Don't reference timezone functions if MESHTASTIC_EXCLUDE_TZ is defined

* Use custom pow_of_two in RadioInterface instead of floating-point pow()

* First pass: enable sensors for STM32wL

* Fix AirQualityTelemetryModule being created if the PM25AQI header is missing

* Link in power sensor libraries

* more ini tweaks

* Add =1 to EXCLUDE defines, fix indentation.

* Drop HAS_WIRE in ini, it's defined in architecture.h

* Fix build when power sensor libraries are missing

Make MAX sensor integration into Power.cpp optional based on its library header existing.
Also make NullSensor expose a voltage and current sensor, because Power calls directly into these for INA sensors.
This lets us remove all the deps for the STM32WL platform.

* Change default I2C for RAK3172 to be I2C1, not I2C2

* Respect the laws of mathematics (oops)
2025-07-02 06:01:45 -05:00

111 lines
2.9 KiB
C++

#pragma once
#ifndef MAX17048_SENSOR_H
#define MAX17048_SENSOR_H
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_I2C && __has_include(<Adafruit_MAX1704X.h>)
// Samples to store in a buffer to determine if the battery is charging or discharging
#define MAX17048_CHARGING_SAMPLES 3
// Threshold to determine if the battery is on charge, in percent/hour
#define MAX17048_CHARGING_MINIMUM_RATE 1.0f
// Threshold to determine if the board has bus power
#define MAX17048_BUS_POWER_VOLTS 4.195f
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
#include "VoltageSensor.h"
#include "meshUtils.h"
#include <Adafruit_MAX1704X.h>
#include <queue>
struct MAX17048ChargeSample {
float cellPercent;
float chargeRate;
};
enum MAX17048ChargeState { IDLE, EXPORT, IMPORT };
// Singleton wrapper for the Adafruit_MAX17048 class
class MAX17048Singleton : public Adafruit_MAX17048
{
private:
static MAX17048Singleton *pinstance;
bool initialized = false;
std::queue<MAX17048ChargeSample> chargeSamples;
MAX17048ChargeState chargeState = IDLE;
const String chargeLabels[3] = {F("idle"), F("export"), F("import")};
const char *sensorStr = "MAX17048Sensor";
protected:
MAX17048Singleton();
~MAX17048Singleton();
public:
// Create a singleton instance (not thread safe)
static MAX17048Singleton *GetInstance();
// Singletons should not be cloneable.
MAX17048Singleton(MAX17048Singleton &other) = delete;
// Singletons should not be assignable.
void operator=(const MAX17048Singleton &) = delete;
// Initialise the sensor (not thread safe)
virtual bool runOnce(TwoWire *theWire = &Wire);
// Get the current bus voltage
uint16_t getBusVoltageMv();
// Get the state of charge in percent 0 to 100
uint8_t getBusBatteryPercent();
// Calculate the seconds to charge/discharge
uint16_t getTimeToGoSecs();
// Returns true if the battery sensor has started
inline virtual bool isInitialised() { return initialized; };
// Returns true if the battery is currently on charge (not thread safe)
bool isBatteryCharging();
// Returns true if a battery is actually connected
bool isBatteryConnected();
// Returns true if there is bus or external power connected
bool isExternallyPowered();
};
#if (HAS_TELEMETRY && (!MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR || !MESHTASTIC_EXCLUDE_POWER_TELEMETRY))
class MAX17048Sensor : public TelemetrySensor, VoltageSensor
{
private:
MAX17048Singleton *max17048 = nullptr;
protected:
virtual void setup() override;
public:
MAX17048Sensor();
// Initialise the sensor
virtual int32_t runOnce() override;
// Get the current bus voltage and state of charge
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
// Get the current bus voltage
virtual uint16_t getBusVoltageMv() override;
};
#endif
#endif
#endif