- Replace delay() with light_sleep_chunked_ms() in sender idle path (100ms chunks preserve UART FIFO safety at 9600 baud) - Add ENABLE_LIGHT_SLEEP_IDLE build flag (default: on, fallback: =0) - Meter reader task: exponential backoff on consecutive poll failures (METER_FAIL_BACKOFF_BASE_MS..MAX_MS) to reduce idle Core-0 wakeups - Configurable SENDER_DIAG_LOG_INTERVAL_MS (5s debug / 30s prod) - Configurable METER_FRAME_TIMEOUT_CFG_MS, SENDER_CPU_MHZ - New PlatformIO envs: lowpower, 868-lowpower, lowpower-debug - Add docs/POWER_OPTIMIZATION.md with measurement plan and Go/No-Go
151 lines
3.7 KiB
C++
151 lines
3.7 KiB
C++
#include "power_manager.h"
|
|
#include "config.h"
|
|
#include <WiFi.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_bt.h>
|
|
#include <esp_sleep.h>
|
|
|
|
static constexpr float BATTERY_DIVIDER = 2.0f;
|
|
static constexpr float ADC_REF_V = 3.3f;
|
|
|
|
void power_sender_init() {
|
|
setCpuFrequencyMhz(SENDER_CPU_MHZ);
|
|
WiFi.mode(WIFI_OFF);
|
|
esp_wifi_stop();
|
|
esp_wifi_deinit();
|
|
btStop();
|
|
analogReadResolution(12);
|
|
pinMode(PIN_BAT_ADC, INPUT);
|
|
}
|
|
|
|
void power_receiver_init() {
|
|
btStop();
|
|
analogReadResolution(12);
|
|
pinMode(PIN_BAT_ADC, INPUT);
|
|
}
|
|
|
|
void power_configure_unused_pins_sender() {
|
|
// Board-specific: only touch pins that are known unused and safe on TTGO LoRa32 v1.6.1
|
|
const uint8_t pins[] = {32, 33};
|
|
for (uint8_t pin : pins) {
|
|
pinMode(pin, INPUT_PULLDOWN);
|
|
}
|
|
}
|
|
|
|
void read_battery(MeterData &data) {
|
|
uint32_t sum = 0;
|
|
uint16_t samples[5] = {};
|
|
for (uint8_t i = 0; i < 5; ++i) {
|
|
samples[i] = analogRead(PIN_BAT_ADC);
|
|
sum += samples[i];
|
|
}
|
|
float avg = static_cast<float>(sum) / 5.0f;
|
|
float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL;
|
|
if (SERIAL_DEBUG_MODE) {
|
|
Serial.printf("bat_adc: %u %u %u %u %u avg=%.1f v=%.3f\n",
|
|
samples[0], samples[1], samples[2], samples[3], samples[4],
|
|
static_cast<double>(avg), static_cast<double>(v));
|
|
}
|
|
|
|
data.battery_voltage_v = v;
|
|
data.battery_percent = battery_percent_from_voltage(v);
|
|
}
|
|
|
|
uint8_t battery_percent_from_voltage(float voltage_v) {
|
|
if (isnan(voltage_v)) {
|
|
return 0;
|
|
}
|
|
struct LutPoint {
|
|
float v;
|
|
uint8_t pct;
|
|
};
|
|
static const LutPoint kCurve[] = {
|
|
{4.20f, 100},
|
|
{4.15f, 95},
|
|
{4.11f, 90},
|
|
{4.08f, 85},
|
|
{4.02f, 80},
|
|
{3.98f, 75},
|
|
{3.95f, 70},
|
|
{3.91f, 60},
|
|
{3.87f, 50},
|
|
{3.85f, 45},
|
|
{3.84f, 40},
|
|
{3.82f, 35},
|
|
{3.80f, 30},
|
|
{3.77f, 25},
|
|
{3.75f, 20},
|
|
{3.73f, 15},
|
|
{3.70f, 10},
|
|
{3.65f, 5},
|
|
{3.60f, 2},
|
|
{2.90f, 0},
|
|
};
|
|
if (voltage_v >= kCurve[0].v) {
|
|
return kCurve[0].pct;
|
|
}
|
|
if (voltage_v <= kCurve[sizeof(kCurve) / sizeof(kCurve[0]) - 1].v) {
|
|
return 0;
|
|
}
|
|
for (size_t i = 0; i + 1 < sizeof(kCurve) / sizeof(kCurve[0]); ++i) {
|
|
const LutPoint &hi = kCurve[i];
|
|
const LutPoint &lo = kCurve[i + 1];
|
|
if (voltage_v <= hi.v && voltage_v >= lo.v) {
|
|
float span = hi.v - lo.v;
|
|
if (span <= 0.0f) {
|
|
return lo.pct;
|
|
}
|
|
float t = (voltage_v - lo.v) / span;
|
|
float pct = lo.pct + t * (hi.pct - lo.pct);
|
|
if (pct < 0.0f) {
|
|
pct = 0.0f;
|
|
}
|
|
if (pct > 100.0f) {
|
|
pct = 100.0f;
|
|
}
|
|
return static_cast<uint8_t>(pct + 0.5f);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void light_sleep_ms(uint32_t ms) {
|
|
if (ms == 0) {
|
|
return;
|
|
}
|
|
esp_sleep_enable_timer_wakeup(static_cast<uint64_t>(ms) * 1000ULL);
|
|
esp_light_sleep_start();
|
|
}
|
|
|
|
void light_sleep_chunked_ms(uint32_t total_ms, uint32_t chunk_ms) {
|
|
if (total_ms == 0) {
|
|
return;
|
|
}
|
|
if (chunk_ms == 0) {
|
|
chunk_ms = total_ms;
|
|
}
|
|
uint32_t start = millis();
|
|
for (;;) {
|
|
uint32_t elapsed = millis() - start;
|
|
if (elapsed >= total_ms) {
|
|
break;
|
|
}
|
|
uint32_t remaining = total_ms - elapsed;
|
|
uint32_t this_chunk = remaining > chunk_ms ? chunk_ms : remaining;
|
|
if (this_chunk < 10) {
|
|
// Light-sleep overhead (~1 ms save/restore) not worthwhile for tiny slices.
|
|
delay(this_chunk);
|
|
break;
|
|
}
|
|
light_sleep_ms(this_chunk);
|
|
// After wake the FreeRTOS scheduler runs higher-priority tasks (e.g. the
|
|
// meter_reader_task on Core 0) before returning here, so the UART HW FIFO
|
|
// is drained automatically between chunks.
|
|
}
|
|
}
|
|
|
|
void go_to_deep_sleep(uint32_t seconds) {
|
|
esp_sleep_enable_timer_wakeup(static_cast<uint64_t>(seconds) * 1000000ULL);
|
|
esp_deep_sleep_start();
|
|
}
|