2020-03-26 16:24:53 +00:00
|
|
|
#pragma once
|
2020-06-28 04:19:49 +00:00
|
|
|
#include "PowerStatus.h"
|
2020-10-09 06:16:51 +00:00
|
|
|
#include "concurrency/OSThread.h"
|
2023-05-16 05:40:42 +00:00
|
|
|
#ifdef ARCH_ESP32
|
2023-05-14 00:38:37 +00:00
|
|
|
#include <esp_adc_cal.h>
|
|
|
|
#include <soc/adc_channel.h>
|
2023-05-16 05:40:42 +00:00
|
|
|
#endif
|
2024-02-16 12:09:57 +00:00
|
|
|
|
|
|
|
#ifndef NUM_OCV_POINTS
|
|
|
|
#define NUM_OCV_POINTS 11
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 3400,3350,3320,3290,3270,3260,3250,3230,3200,3120,3000 //3.4 to 3.0 LiFePO4
|
|
|
|
// 2120,2090,2070,2050,2030,2010,1990,1980,1970,1960,1950 //2.12 to 1.95 Lead Acid
|
|
|
|
// 4200,4050,3990,3890,3790,3700,3650,3550,3450,3300,3200 //4.2 to 3.2 LiIon/LiPo
|
|
|
|
// 4200,4050,3990,3890,3790,3700,3650,3550,3400,3300,3000 //4.2 to 3.0 LiIon/LiPo
|
|
|
|
// 4150,4050,3990,3890,3790,3690,3620,3520,3420,3300,3100 //4.15 to 3.1 LiIon/LiPo
|
|
|
|
// 2770,2650,2540,2420,2300,2180,2060,1940,1800,1680,1550 //2.8 to 1.5 Lithium Titanate
|
|
|
|
|
|
|
|
#ifndef OCV_ARRAY
|
|
|
|
#ifdef CELL_TYPE_LIFEPO4
|
|
|
|
#define OCV_ARRAY 3400, 3350, 3320, 3290, 3270, 3260, 3250, 3230, 3200, 3120, 3000
|
|
|
|
#elif defined(CELL_TYPE_LEADACID)
|
|
|
|
#define OCV_ARRAY 2120, 2090, 2070, 2050, 2030, 2010, 1990, 1980, 1970, 1960, 1950
|
|
|
|
#elif defined(CELL_TYPE_ALKALINE)
|
|
|
|
#define OCV_ARRAY 1580, 1400, 1350, 1300, 1280, 1250, 1230, 1190, 1150, 1100, 1000
|
|
|
|
#elif defined(CELL_TYPE_NIMH)
|
|
|
|
#define OCV_ARRAY 1400, 1300, 1280, 1270, 1260, 1250, 1240, 1230, 1210, 1150, 1000
|
|
|
|
#elif defined(CELL_TYPE_LTO)
|
|
|
|
#define OCV_ARRAY 2770, 2650, 2540, 2420, 2300, 2180, 2060, 1940, 1800, 1680, 1550
|
|
|
|
#else // LiIon
|
|
|
|
#define OCV_ARRAY 4190, 4050, 3990, 3890, 3800, 3720, 3630, 3530, 3420, 3300, 3100
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*Note: 12V lead acid is 6 cells, most board accept only 1 cell LiIon/LiPo*/
|
|
|
|
#ifndef NUM_CELLS
|
|
|
|
#define NUM_CELLS 1
|
|
|
|
#endif
|
|
|
|
|
2023-05-14 00:38:37 +00:00
|
|
|
#ifdef BAT_MEASURE_ADC_UNIT
|
|
|
|
extern RTC_NOINIT_ATTR uint64_t RTC_reg_b;
|
|
|
|
#include "soc/sens_reg.h" // needed for adc pin reset
|
|
|
|
#endif
|
2020-06-21 23:21:34 +00:00
|
|
|
|
2023-06-02 11:32:34 +00:00
|
|
|
#if HAS_TELEMETRY && !defined(ARCH_PORTDUINO)
|
|
|
|
#include "modules/Telemetry/Sensor/INA219Sensor.h"
|
|
|
|
#include "modules/Telemetry/Sensor/INA260Sensor.h"
|
2023-11-17 12:46:59 +00:00
|
|
|
#include "modules/Telemetry/Sensor/INA3221Sensor.h"
|
2023-06-02 11:32:34 +00:00
|
|
|
extern INA260Sensor ina260Sensor;
|
|
|
|
extern INA219Sensor ina219Sensor;
|
2023-11-17 12:46:59 +00:00
|
|
|
extern INA3221Sensor ina3221Sensor;
|
2023-06-02 11:32:34 +00:00
|
|
|
#endif
|
|
|
|
|
2020-10-09 06:16:51 +00:00
|
|
|
class Power : private concurrency::OSThread
|
2020-03-26 16:24:53 +00:00
|
|
|
{
|
|
|
|
|
2020-08-13 00:03:36 +00:00
|
|
|
public:
|
2020-06-29 01:17:52 +00:00
|
|
|
Observable<const meshtastic::PowerStatus *> newStatus;
|
2020-03-26 16:24:53 +00:00
|
|
|
|
2020-10-09 06:16:51 +00:00
|
|
|
Power();
|
|
|
|
|
2020-11-23 02:50:14 +00:00
|
|
|
void shutdown();
|
2020-06-28 04:19:49 +00:00
|
|
|
void readPowerStatus();
|
|
|
|
virtual bool setup();
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual int32_t runOnce() override;
|
2020-08-13 00:03:36 +00:00
|
|
|
void setStatusHandler(meshtastic::PowerStatus *handler) { statusHandler = handler; }
|
2024-02-16 12:09:57 +00:00
|
|
|
const uint16_t OCV[11] = {OCV_ARRAY};
|
2020-08-13 00:03:36 +00:00
|
|
|
|
|
|
|
protected:
|
2020-06-29 01:17:52 +00:00
|
|
|
meshtastic::PowerStatus *statusHandler;
|
2020-06-28 04:19:49 +00:00
|
|
|
|
2022-09-06 07:58:33 +00:00
|
|
|
/// Setup a xpowers chip axp192/axp2101, return true if found
|
|
|
|
bool axpChipInit();
|
2020-08-13 00:03:36 +00:00
|
|
|
/// Setup a simple ADC input based battery sensor
|
|
|
|
bool analogInit();
|
2021-09-19 13:03:37 +00:00
|
|
|
|
|
|
|
private:
|
2024-02-16 12:09:57 +00:00
|
|
|
// open circuit voltage lookup table
|
2021-09-19 13:03:37 +00:00
|
|
|
uint8_t low_voltage_counter;
|
2023-01-21 13:34:29 +00:00
|
|
|
#ifdef DEBUG_HEAP
|
2022-12-21 12:36:38 +00:00
|
|
|
uint32_t lastheap;
|
|
|
|
#endif
|
2020-06-28 04:19:49 +00:00
|
|
|
};
|
2020-04-24 21:55:51 +00:00
|
|
|
|
2023-05-14 00:38:37 +00:00
|
|
|
extern Power *power;
|