Files
watch-watch/main/main.c

83 lines
2.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stddef.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "dcdc_controller.h"
#include "ina226_monitor.h"
#include "uart_mux.h"
#include "usb_cdc_cli.h"
#include "ws2812_status.h"
static const char *TAG = "watch-watch";
void app_main(void)
{
if (dcdc_init() != ESP_OK) {
ESP_LOGE(TAG, "Помилка ініціалізації DCDC контролера");
ws2812_status_init();
ws2812_status_set_error(true);
return;
}
if (ws2812_status_init() == ESP_OK) {
ws2812_status_refresh_from_dcdc();
} else {
ESP_LOGW(TAG, "WS2812 статусний індикатор недоступний");
}
if (ina226_monitor_init() == ESP_OK) {
ESP_LOGI(TAG, "INA226 моніторинг активовано");
ina226_monitor_sample(NULL);
} else {
ESP_LOGW(TAG, "Моніторинг навантаження недоступний");
}
if (uart_mux_init() == ESP_OK) {
ESP_LOGI(TAG, "UART мультиплексор активовано");
} else {
ESP_LOGW(TAG, "UART мультиплексор недоступний");
}
if (usb_cdc_cli_init() != ESP_OK) {
ESP_LOGW(TAG, "USB CDC CLI недоступний");
} else {
ESP_LOGI(TAG, "USB CDC CLI запущено");
}
ESP_LOGI(TAG, "Початок послідовного ввімкнення каналів з інтервалом 4 с");
const TickType_t on_time = pdMS_TO_TICKS(4000);
size_t prev_channel = DCDC_CHANNEL_COUNT - 1;
while (true) {
for (size_t ch = 0; ch < dcdc_channel_count(); ++ch) {
if (prev_channel != ch) {
dcdc_disable(prev_channel);
ws2812_status_set_channel_state(prev_channel, false);
}
ESP_LOGI(TAG, "-> Ввімкнення каналу %d", (int)ch);
dcdc_enable(ch);
ws2812_status_set_channel_state(ch, true);
ws2812_status_mark_active(ch);
vTaskDelay(on_time);
ina226_reading_t reading = {0};
if (ina226_monitor_sample(&reading) == ESP_OK) {
ESP_LOGI(TAG, "Живлення: %.2f В, %.1f мА, %.1f мВт",
reading.voltage_v, reading.current_ma, reading.power_mw);
}
if (uart_mux_ready()) {
char msg[64];
int len = snprintf(msg, sizeof(msg), "PWR %.2fV %.0fmA\r\n",
reading.voltage_v, reading.current_ma);
uart_mux_write(ch, (const uint8_t *)msg, len, pdMS_TO_TICKS(100));
}
prev_channel = ch;
}
}
}