new meter
This commit is contained in:
@@ -1,62 +1,108 @@
|
||||
#include "evse_state.h"
|
||||
#include "evse_api.h"
|
||||
#include "evse_limits.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
|
||||
// ========================
|
||||
// Estado interno
|
||||
// External state references
|
||||
// ========================
|
||||
|
||||
static bool limit_reached = false;
|
||||
static uint32_t consumption_limit = 0;
|
||||
static uint32_t charging_time_limit = 0;
|
||||
static uint16_t under_power_limit = 0;
|
||||
//extern evse_state_t current_state; // Current EVSE FSM state
|
||||
//extern TickType_t session_start_tick; // Timestamp of charging session start
|
||||
|
||||
static uint32_t default_consumption_limit = 0;
|
||||
// ========================
|
||||
// Concurrency protection
|
||||
// ========================
|
||||
|
||||
static portMUX_TYPE evse_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
// ========================
|
||||
// Runtime state (volatile)
|
||||
// ========================
|
||||
|
||||
static bool limit_reached = false;
|
||||
static uint32_t consumption_limit = 0; // Energy limit in Wh
|
||||
static uint32_t charging_time_limit = 0; // Time limit in seconds
|
||||
static uint16_t under_power_limit = 0; // Minimum acceptable power in W
|
||||
|
||||
// ========================
|
||||
// Default (persistent) limits
|
||||
// ========================
|
||||
|
||||
static uint32_t default_consumption_limit = 0;
|
||||
static uint32_t default_charging_time_limit = 0;
|
||||
static uint16_t default_under_power_limit = 0;
|
||||
static uint16_t default_under_power_limit = 0;
|
||||
|
||||
// ========================
|
||||
// Estado de controle
|
||||
// Limit status flag
|
||||
// ========================
|
||||
|
||||
void evse_set_limit_reached(uint8_t value) {
|
||||
limit_reached = (value != 0);
|
||||
bool evse_get_limit_reached(void) {
|
||||
bool val;
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
val = limit_reached;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
return val;
|
||||
}
|
||||
|
||||
bool evse_is_limit_reached(void) {
|
||||
return limit_reached;
|
||||
void evse_set_limit_reached(bool v) {
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
limit_reached = v;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
}
|
||||
|
||||
// ========================
|
||||
// Limites em tempo de execução
|
||||
// Runtime limit accessors
|
||||
// ========================
|
||||
|
||||
uint32_t evse_get_consumption_limit(void) {
|
||||
return consumption_limit;
|
||||
uint32_t val;
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
val = consumption_limit;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
return val;
|
||||
}
|
||||
|
||||
void evse_set_consumption_limit(uint32_t value) {
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
consumption_limit = value;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
}
|
||||
|
||||
uint32_t evse_get_charging_time_limit(void) {
|
||||
return charging_time_limit;
|
||||
uint32_t val;
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
val = charging_time_limit;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
return val;
|
||||
}
|
||||
|
||||
void evse_set_charging_time_limit(uint32_t value) {
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
charging_time_limit = value;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
}
|
||||
|
||||
uint16_t evse_get_under_power_limit(void) {
|
||||
return under_power_limit;
|
||||
uint16_t val;
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
val = under_power_limit;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
return val;
|
||||
}
|
||||
|
||||
void evse_set_under_power_limit(uint16_t value) {
|
||||
portENTER_CRITICAL(&evse_mux);
|
||||
under_power_limit = value;
|
||||
portEXIT_CRITICAL(&evse_mux);
|
||||
}
|
||||
|
||||
// ========================
|
||||
// Limites padrão (persistentes)
|
||||
// Default (persistent) limit accessors
|
||||
// These values can be stored/restored via NVS
|
||||
// ========================
|
||||
|
||||
uint32_t evse_get_default_consumption_limit(void) {
|
||||
@@ -83,15 +129,45 @@ void evse_set_default_under_power_limit(uint16_t value) {
|
||||
default_under_power_limit = value;
|
||||
}
|
||||
|
||||
bool evse_is_limit_reached(void) {
|
||||
return evse_get_limit_reached();
|
||||
}
|
||||
|
||||
|
||||
// ========================
|
||||
// Lógica de verificação de limites
|
||||
// Limit checking logic
|
||||
// This function must be called periodically while charging.
|
||||
// It will flag the session as "limit reached" when thresholds are violated.
|
||||
// ========================
|
||||
|
||||
void evse_limits_check(evse_state_t state) {
|
||||
// Se algum limite estiver ativo, verifique o estado
|
||||
if ((consumption_limit > 0 || charging_time_limit > 0 || under_power_limit > 0)
|
||||
&& evse_state_is_charging(state)) {
|
||||
// (Lógica real a ser aplicada aqui, ex: medição de consumo, tempo ou potência)
|
||||
evse_set_limit_reached(1);
|
||||
void evse_limits_check(void) {
|
||||
evse_state_t state = evse_get_state();
|
||||
if (!evse_state_is_charging(state)) return;
|
||||
|
||||
bool reached = false;
|
||||
|
||||
uint32_t energy = evse_get_total_energy();
|
||||
uint32_t power = evse_get_instant_power();
|
||||
TickType_t now = xTaskGetTickCount();
|
||||
TickType_t start = evse_get_session_start();
|
||||
|
||||
if (consumption_limit > 0 && energy >= consumption_limit) {
|
||||
ESP_LOGW("EVSE", "Energy limit reached");
|
||||
reached = true;
|
||||
}
|
||||
|
||||
if (charging_time_limit > 0 &&
|
||||
(now - start) >= pdMS_TO_TICKS(charging_time_limit * 1000)) {
|
||||
ESP_LOGW("EVSE", "Charging time limit reached");
|
||||
reached = true;
|
||||
}
|
||||
|
||||
if (under_power_limit > 0 && power < under_power_limit) {
|
||||
ESP_LOGW("EVSE", "Under power limit reached");
|
||||
reached = true;
|
||||
}
|
||||
|
||||
if (reached) {
|
||||
evse_set_limit_reached(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user