barback32/main/main.c
Morgan 'ARR\!' Allen c57f25af03
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
rampering! fixed ramp times but works
2025-07-31 19:12:20 -07:00

124 lines
3.8 KiB
C

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_debug_helpers.h"
#include "nvs_flash.h"
#include "esp_vfs_dev.h"
#include "led_strip.h"
#include "configulator.h"
#include "main.h"
#include "console.h"
#include "ble.h"
#include "user_button.h"
#include "pumps.h"
#define LED_GPIO 14
#define LED_COUNT 5
static const char *TAG = "BARBACK";
uint8_t mac[6];
nvs_handle_t config_handle = {};
led_strip_handle_t led_strip;
void led_init() {
led_strip_config_t strip_config = {
.strip_gpio_num = LED_GPIO, // The GPIO that connected to the LED strip's data line
.max_leds = LED_COUNT, // The number of LEDs in the strip,
.led_model = LED_MODEL_WS2812, // LED strip model, it determines the bit timing
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, // The color component format is G-R-B
.flags = {
.invert_out = false, // don't invert the output signal
}
};
/// RMT backend specific configuration
led_strip_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
.resolution_hz = 10 * 1000 * 1000, // RMT counter clock frequency: 10MHz
.mem_block_symbols = 64, // the memory size of each RMT channel, in words (4 bytes)
.flags = {
.with_dma = false, // DMA feature is available on chips like ESP32-S3/P4
}
};
/// Create the LED strip object
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
}
void app_main(void) {
// init pump IO immediately to limit run away pumps in case power is on
pumps_io_init();
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
err = nvs_open("config", NVS_READWRITE, &config_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
ESP_LOGI(TAG, "config_handle: %d", (int)config_handle);
}
esp_efuse_mac_get_default((uint8_t*)&mac);
ESP_LOGI(TAG, "MAC: [%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
char *name = malloc(strlen(TAG) + 4);
sprintf(name, "%s-%02X", TAG, mac[5]);
uint8_t tank_levels[] = { 100, 100, 100, 100 };
uint16_t pump_ramp_ms[] = { 500, 500, 500, 500 };
cfglr_handle_t cfglr_handle = {
.namespace = "configulator",
.backend = CFGLR_BACKEND_NVS(),
.elements = {
CFGLR_ELEMENT_U8("armed", 1, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_U8("monitor", 0, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_U8("pour_button", 19, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_STR("device_name", 32, name, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_BIN("tank_levels", 4, tank_levels, 4, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_BIN("pump_pins", 4, pump_gpio_map, 4, CFGLR_SIGNALER_IDF_EVENT()),
CFGLR_ELEMENT_BIN("pump_ramp_ms", sizeof(uint16_t) * 4, pump_ramp_ms, sizeof(uint16_t) * 4, CFGLR_SIGNALER_IDF_EVENT()),
{ NULL },
},
};
uint8_t ret = cfglr_init(&cfglr_handle);
vTaskDelay(1000 / portTICK_PERIOD_MS);
user_button_init();
pumps_init();
ble_init(name);
xTaskCreate(console_task, "console", 4048, NULL, tskIDLE_PRIORITY + 3, NULL);
led_init();
uint16_t tick = 0;
while(1) {
uint16_t delay = running > 0 ? 500 / running : 500;
vTaskDelay(delay / portTICK_PERIOD_MS);
uint8_t color = tick % (LED_COUNT * 2) > LED_COUNT ? 0 : 100;
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, tick % LED_COUNT, 5, 5, color));
ESP_ERROR_CHECK(led_strip_refresh(led_strip));
tick++;
}
}