-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlumen_energy_tracker.cpp
More file actions
150 lines (117 loc) · 3.7 KB
/
Copy pathlumen_energy_tracker.cpp
File metadata and controls
150 lines (117 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "lumen_energy_tracker.h"
#include <esp_log.h>
#include <math.h>
#include "lumen_board_config.h"
#include "lumen_config_manager.h"
#include "lumen_system_utils.h"
#include "lumen_type_validation.h"
namespace {
static const char* TAG = "energy_tracker";
static float total_wh = 0.0f;
static float session_wh = 0.0f;
static uint64_t light_on_ms = 0;
static uint64_t max_brightness_ms = 0;
static uint64_t last_update_ms = 0;
static float last_persisted_wh = 0.0f;
static bool energy_dirty = false;
static bool is_initialized = false;
static constexpr uint32_t MAX_DELTA_MS = 5000U;
static constexpr float PERSIST_EPSILON_WH = 0.01f;
uint32_t toSeconds(uint64_t duration_ms) { return static_cast<uint32_t>(duration_ms / 1000ULL); }
uint32_t getActiveDistributionPct(const LedState& led) {
uint32_t total_pct = 0U;
if (led.red_enabled) {
total_pct += led.red_dist_pct;
}
if (led.blue_enabled) {
total_pct += led.blue_dist_pct;
}
if (led.far_red_enabled) {
total_pct += led.far_red_dist_pct;
}
return total_pct;
}
float computePowerWatts(const LedState& led) {
if (!led.power || led.brightness_pct == 0U) {
return 0.0f;
}
const uint32_t active_distribution_pct = getActiveDistributionPct(led);
const float brightness_ratio = static_cast<float>(led.brightness_pct) / 100.0f;
const float channel_ratio = static_cast<float>(active_distribution_pct) / 100.0f;
return LED_ESTIMATED_FULL_SCALE_POWER_W * brightness_ratio * channel_ratio;
}
uint32_t clampDeltaMs(uint64_t delta_ms) {
if (delta_ms > MAX_DELTA_MS) {
return MAX_DELTA_MS;
}
return static_cast<uint32_t>(delta_ms);
}
} // namespace
namespace EnergyTracker {
bool init() {
const RuntimeConfig config = ConfigManager::getConfigSnapshot();
if (!isEnergyTotalValid(config.energy_total_wh)) {
ESP_LOGE(TAG, "persisted energy invalid");
return false;
}
total_wh = config.energy_total_wh;
session_wh = 0.0f;
light_on_ms = 0;
max_brightness_ms = 0;
last_update_ms = getNowMs();
last_persisted_wh = total_wh;
energy_dirty = false;
is_initialized = true;
ESP_LOGI(TAG, "energy tracker initialized");
return true;
}
void updateFromLedState(const LedState& led) {
if (!is_initialized) {
return;
}
const uint64_t now_ms = getNowMs();
if (now_ms <= last_update_ms) {
return;
}
const uint32_t delta_ms = clampDeltaMs(now_ms - last_update_ms);
last_update_ms = now_ms;
const float power_w = computePowerWatts(led);
const float delta_wh = power_w * (static_cast<float>(delta_ms) / 3600000.0f);
total_wh += delta_wh;
session_wh += delta_wh;
if (delta_wh > 0.0f) {
energy_dirty = true;
}
if (led.power && led.brightness_pct > 0U) {
light_on_ms += delta_ms;
}
if (led.power && led.brightness_pct == 100U) {
max_brightness_ms += delta_ms;
}
}
EnergyMessage getSnapshot() {
EnergyMessage snapshot = {};
snapshot.timestamp_ms = getNowMs();
snapshot.total_wh = total_wh;
snapshot.session_wh = session_wh;
snapshot.light_on_sec = toSeconds(light_on_ms);
snapshot.max_brightness_sec = toSeconds(max_brightness_ms);
return snapshot;
}
bool requestPersist() {
if (!is_initialized) {
ESP_LOGW(TAG, "persist before init");
return false;
}
if (!energy_dirty || fabsf(total_wh - last_persisted_wh) < PERSIST_EPSILON_WH) {
return true;
}
if (!ConfigManager::saveEnergyTotal(total_wh)) {
ESP_LOGW(TAG, "energy persist failed");
return false;
}
last_persisted_wh = total_wh;
energy_dirty = false;
return true;
}
} // namespace EnergyTracker