new module

This commit is contained in:
2025-12-09 11:48:31 +00:00
parent 4820d9111e
commit e6e2622a95
98 changed files with 5349 additions and 8607 deletions

View File

@@ -2,12 +2,10 @@ set(srcs
"src/adc.c"
"src/adc121s021_dma.c"
"src/peripherals.c"
"src/led.c"
"src/proximity.c"
"src/ac_relay.c"
"src/socket_lock.c"
"src/rcm.c"
"src/aux_io.c"
"src/onewire.c"
"src/ds18x20.c"
"src/temp_sensor.c"

View File

@@ -1,39 +0,0 @@
#ifndef AUX_IO_H_
#define AUX_IO_H_
#include "esp_err.h"
/**
* @brief Initialize aux
*
*/
void aux_init(void);
/**
* @brief Read digital input
*
* @param name
* @param value
* @return esp_err_t
*/
esp_err_t aux_read(const char *name, bool *value);
/**
* @brief Write digial output
*
* @param name
* @param value
* @return esp_err_t
*/
esp_err_t aux_write(const char *name, bool value);
/**
* @brief Read analog input
*
* @param name
* @param value
* @return esp_err_t
*/
esp_err_t aux_analog_read(const char *name, int *value);
#endif /* AUX_IO_H_ */

View File

@@ -1,53 +0,0 @@
#ifndef LED_H_
#define LED_H_
#include <stdint.h>
#include <stdbool.h>
/**
* @brief Identificadores dos LEDs disponíveis no hardware
*/
typedef enum {
LED_ID_STOP,
LED_ID_CHARGING,
LED_ID_ERROR,
LED_ID_MAX
} led_id_t;
/**
* @brief Padrões de comportamento possíveis para os LEDs
*/
typedef enum {
LED_PATTERN_OFF, ///< LED sempre desligado
LED_PATTERN_ON, ///< LED sempre ligado
LED_PATTERN_BLINK, ///< Pisca com ciclo padrão (500ms on / 500ms off)
LED_PATTERN_BLINK_FAST, ///< Pisca rápido (200ms / 200ms)
LED_PATTERN_BLINK_SLOW, ///< Pisca lento (300ms / 1700ms)
LED_PATTERN_CHARGING_EFFECT ///< Efeito visual para carregamento (2s on / 1s off)
} led_pattern_t;
/**
* @brief Inicializa os LEDs com base na configuração da placa
* Deve ser chamada uma única vez na inicialização do sistema.
*/
void led_init(void);
/**
* @brief Define diretamente o tempo ligado/desligado de um LED.
* Pode ser usado para padrões personalizados.
*
* @param led_id Identificador do LED (ver enum led_id_t)
* @param ontime Tempo ligado em milissegundos
* @param offtime Tempo desligado em milissegundos
*/
void led_set_state(led_id_t led_id, uint16_t ontime, uint16_t offtime);
/**
* @brief Aplica um dos padrões de piscar definidos ao LED
*
* @param led_id Identificador do LED (ver enum led_id_t)
* @param pattern Padrão desejado (ver enum led_pattern_t)
*/
void led_apply_pattern(led_id_t led_id, led_pattern_t pattern);
#endif /* LED_H_ */

View File

@@ -1,174 +0,0 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "nvs.h"
#include "aux_io.h"
#include "board_config.h"
#include "adc.h"
#define MAX_AUX_IN 4
#define MAX_AUX_OUT 4
#define MAX_AUX_AIN 4
//static const char* TAG = "aux";
static int aux_in_count = 0;
static int aux_out_count = 0;
static int aux_ain_count = 0;
static struct aux_gpio_s
{
gpio_num_t gpio;
const char* name;
} aux_in[MAX_AUX_IN], aux_out[MAX_AUX_OUT];
static struct aux_adc_s
{
adc_channel_t adc;
const char* name;
} aux_ain[MAX_AUX_AIN];
void aux_init(void)
{
// IN
gpio_config_t io_conf = {
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLDOWN_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
.pin_bit_mask = 0
};
if (board_config.aux_in_1) {
aux_in[aux_in_count].gpio = board_config.aux_in_1_gpio;
aux_in[aux_in_count].name = board_config.aux_in_1_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_in_1_gpio);
aux_in_count++;
}
if (board_config.aux_in_2) {
aux_in[aux_in_count].gpio = board_config.aux_in_2_gpio;
aux_in[aux_in_count].name = board_config.aux_in_2_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_in_2_gpio);
aux_in_count++;
}
if (board_config.aux_in_3) {
aux_in[aux_in_count].gpio = board_config.aux_in_3_gpio;
aux_in[aux_in_count].name = board_config.aux_in_3_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_in_3_gpio);
aux_in_count++;
}
if (board_config.aux_in_4) {
aux_in[aux_in_count].gpio = board_config.aux_in_4_gpio;
aux_in[aux_in_count].name = board_config.aux_in_4_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_in_4_gpio);
aux_in_count++;
}
if (io_conf.pin_bit_mask > 0) {
ESP_ERROR_CHECK(gpio_config(&io_conf));
}
// OUT
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = 0;
if (board_config.aux_out_1) {
aux_out[aux_out_count].gpio = board_config.aux_out_1_gpio;
aux_out[aux_out_count].name = board_config.aux_out_1_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_out_1_gpio);
aux_out_count++;
}
if (board_config.aux_out_2) {
aux_out[aux_out_count].gpio = board_config.aux_out_2_gpio;
aux_out[aux_out_count].name = board_config.aux_out_2_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_out_2_gpio);
aux_out_count++;
}
if (board_config.aux_out_3) {
aux_out[aux_out_count].gpio = board_config.aux_out_3_gpio;
aux_out[aux_out_count].name = board_config.aux_out_3_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_out_3_gpio);
aux_out_count++;
}
if (board_config.aux_out_4) {
aux_out[aux_out_count].gpio = board_config.aux_out_4_gpio;
aux_out[aux_out_count].name = board_config.aux_out_4_name;
io_conf.pin_bit_mask |= BIT64(board_config.aux_out_4_gpio);
aux_out_count++;
}
if (io_conf.pin_bit_mask > 0) {
ESP_ERROR_CHECK(gpio_config(&io_conf));
}
// AIN
adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = ADC_ATTEN_DB_12
};
if (board_config.aux_ain_1) {
aux_ain[aux_ain_count].adc = board_config.aux_ain_1_adc_channel;
aux_ain[aux_ain_count].name = board_config.aux_out_1_name;
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, board_config.aux_ain_1_adc_channel, &config));
aux_ain_count++;
}
if (board_config.aux_ain_2) {
aux_ain[aux_ain_count].adc = board_config.aux_ain_2_adc_channel;
aux_ain[aux_ain_count].name = board_config.aux_out_2_name;
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, board_config.aux_ain_2_adc_channel, &config));
aux_ain_count++;
}
}
esp_err_t aux_read(const char* name, bool* value)
{
for (int i = 0; i < aux_in_count; i++) {
if (strcmp(aux_in[i].name, name) == 0) {
*value = gpio_get_level(aux_in[i].gpio) == 1;
return ESP_OK;
}
}
return ESP_ERR_NOT_FOUND;
}
esp_err_t aux_write(const char* name, bool value)
{
for (int i = 0; i < aux_out_count; i++) {
if (strcmp(aux_out[i].name, name) == 0) {
return gpio_set_level(aux_out[i].gpio, value);
}
}
return ESP_ERR_NOT_FOUND;
}
esp_err_t aux_analog_read(const char* name, int* value)
{
for (int i = 0; i < aux_ain_count; i++) {
if (strcmp(aux_ain[i].name, name) == 0) {
int raw = 0;
esp_err_t ret = adc_oneshot_read(adc_handle, aux_ain[i].adc, &raw);
if (ret == ESP_OK) {
return adc_cali_raw_to_voltage(adc_cali_handle, raw, value);
} else {
return ret;
}
}
}
return ESP_ERR_NOT_FOUND;
}

View File

@@ -1,211 +0,0 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "led.h"
#include "board_config.h"
#include "evse_events.h"
#include "evse_state.h"
#define BLOCK_TIME pdMS_TO_TICKS(10)
static const char *TAG = "led";
typedef struct {
gpio_num_t gpio;
bool on : 1;
uint16_t ontime;
uint16_t offtime;
TimerHandle_t timer;
led_pattern_t pattern;
uint8_t blink_count;
} led_t;
static led_t leds[LED_ID_MAX] = {0};
// ----------------------------
// Funções Internas
// ----------------------------
static void led_timer_callback(TimerHandle_t xTimer)
{
led_t *led = (led_t *)pvTimerGetTimerID(xTimer);
led->on = !led->on;
gpio_set_level(led->gpio, led->on);
uint32_t next_time = led->on ? led->ontime : led->offtime;
xTimerChangePeriod(led->timer, pdMS_TO_TICKS(next_time), BLOCK_TIME);
}
// ----------------------------
// Event Handler: EVSE State
// ----------------------------
static void evse_led_event_handler(void *arg, esp_event_base_t base, int32_t id, void *data) {
if (base != EVSE_EVENTS || id != EVSE_EVENT_STATE_CHANGED || data == NULL) return;
const evse_state_event_data_t *evt = (const evse_state_event_data_t *)data;
// Log do evento recebido
ESP_LOGI(TAG, "EVSE State Changed: state=%d", evt->state);
led_apply_pattern(LED_ID_STOP, LED_PATTERN_OFF);
led_apply_pattern(LED_ID_CHARGING, LED_PATTERN_OFF);
led_apply_pattern(LED_ID_ERROR, LED_PATTERN_OFF);
switch (evt->state) {
case EVSE_STATE_EVENT_IDLE:
ESP_LOGI(TAG, "EVSE_STATE_EVENT_IDLE");
led_apply_pattern(LED_ID_STOP, LED_PATTERN_ON);
break;
case EVSE_STATE_EVENT_WAITING:
ESP_LOGI(TAG, "EVSE_STATE_EVENT_WAITING");
led_apply_pattern(LED_ID_CHARGING, LED_PATTERN_ON);
break;
case EVSE_STATE_EVENT_CHARGING:
ESP_LOGI(TAG, "EVSE_STATE_EVENT_CHARGING");
led_apply_pattern(LED_ID_CHARGING, LED_PATTERN_CHARGING_EFFECT);
break;
case EVSE_STATE_EVENT_FAULT:
ESP_LOGI(TAG, "EVSE_STATE_EVENT_FAULT");
led_apply_pattern(LED_ID_ERROR, LED_PATTERN_BLINK_FAST);
break;
default:
ESP_LOGW(TAG, "Unknown state: %d", evt->state);
break;
}
}
// ----------------------------
// Inicialização
// ----------------------------
void led_init(void)
{
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.intr_type = GPIO_INTR_DISABLE,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_ENABLE,
.pin_bit_mask = 0
};
for (int i = 0; i < LED_ID_MAX; i++) {
leds[i].gpio = GPIO_NUM_NC;
}
if (board_config.led_stop) {
leds[LED_ID_STOP].gpio = board_config.led_stop_gpio;
io_conf.pin_bit_mask |= BIT64(board_config.led_stop_gpio);
}
if (board_config.led_charging) {
leds[LED_ID_CHARGING].gpio = board_config.led_charging_gpio;
io_conf.pin_bit_mask |= BIT64(board_config.led_charging_gpio);
}
if (board_config.led_error) {
leds[LED_ID_ERROR].gpio = board_config.led_error_gpio;
io_conf.pin_bit_mask |= BIT64(board_config.led_error_gpio);
}
if (io_conf.pin_bit_mask != 0) {
ESP_ERROR_CHECK(gpio_config(&io_conf));
}
// Registra handler de evento EVSE
ESP_ERROR_CHECK(esp_event_handler_register(
EVSE_EVENTS,
EVSE_EVENT_STATE_CHANGED,
evse_led_event_handler,
NULL));
ESP_LOGI(TAG, "LED system initialized");
// Aplica o estado atual do EVSE aos LEDs
evse_state_event_data_t evt = {
.state = EVSE_STATE_EVENT_IDLE
};
evse_led_event_handler(NULL, EVSE_EVENTS, EVSE_EVENT_STATE_CHANGED, &evt);
}
// ----------------------------
// API Pública
// ----------------------------
void led_set_state(led_id_t led_id, uint16_t ontime, uint16_t offtime)
{
if (led_id >= LED_ID_MAX) return;
led_t *led = &leds[led_id];
if (led->gpio == GPIO_NUM_NC) return;
if (led->ontime == ontime && led->offtime == offtime)
return;
if (led->timer) {
xTimerStop(led->timer, BLOCK_TIME);
}
led->ontime = ontime;
led->offtime = offtime;
if (ontime == 0) {
led->on = false;
gpio_set_level(led->gpio, 0);
} else if (offtime == 0) {
led->on = true;
gpio_set_level(led->gpio, 1);
} else {
led->on = true;
gpio_set_level(led->gpio, 1);
if (!led->timer) {
led->timer = xTimerCreate("led_timer", pdMS_TO_TICKS(ontime),
pdFALSE, (void *)led, led_timer_callback);
}
if (led->timer) {
xTimerStart(led->timer, BLOCK_TIME);
}
}
}
void led_apply_pattern(led_id_t id, led_pattern_t pattern)
{
if (id >= LED_ID_MAX) return;
led_t *led = &leds[id];
if (led->gpio == GPIO_NUM_NC) return;
if (led->pattern == pattern) return;
if (led->timer) {
xTimerStop(led->timer, BLOCK_TIME);
}
led->pattern = pattern;
led->blink_count = 0;
switch (pattern) {
case LED_PATTERN_OFF:
led_set_state(id, 0, 0);
break;
case LED_PATTERN_ON:
led_set_state(id, 1, 0);
break;
case LED_PATTERN_BLINK:
led_set_state(id, 500, 500);
break;
case LED_PATTERN_BLINK_FAST:
led_set_state(id, 200, 200);
break;
case LED_PATTERN_BLINK_SLOW:
led_set_state(id, 300, 1700);
break;
case LED_PATTERN_CHARGING_EFFECT:
led_set_state(id, 2000, 1000);
break;
}
}

View File

@@ -1,24 +1,23 @@
#include "peripherals.h"
#include "adc.h"
#include "led.h"
//#include "buzzer.h"
//#include "led.h"
// #include "buzzer.h"
#include "proximity.h"
#include "ac_relay.h"
#include "socket_lock.h"
#include "rcm.h"
#include "aux_io.h"
#include "ntc_sensor.h"
void peripherals_init(void)
{
ac_relay_init();
led_init();
//buzzer_init();
// led_init();
// buzzer_init();
adc_init();
proximity_init();
// socket_lock_init();
// rcm_init();
//energy_meter_init();
// energy_meter_init();
// aux_init();
ntc_sensor_init();
}