2020-10-25 22:49:14 -04:00
|
|
|
#include "string.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/timers.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2021-09-24 01:58:57 -04:00
|
|
|
#include "main.h"
|
2020-10-25 22:49:14 -04:00
|
|
|
#include "pumps.h"
|
|
|
|
|
|
|
|
#define TAG "PUMP!"
|
|
|
|
|
|
|
|
uint8_t running = 0;
|
|
|
|
|
|
|
|
TimerHandle_t pump_timers[PUMPS];
|
|
|
|
static uint8_t pumps_duration[PUMPS] = {0};
|
|
|
|
static uint8_t pumps_enabled[PUMPS] = {0};
|
2020-10-26 22:12:25 -04:00
|
|
|
static uint8_t pumps_state[PUMPS] = {0};
|
2020-10-25 22:49:14 -04:00
|
|
|
static uint8_t safety = 1;
|
|
|
|
|
2021-09-24 01:58:57 -04:00
|
|
|
void pumps_update_config() {
|
|
|
|
ESP_LOGI(TAG, "writing config");
|
|
|
|
|
|
|
|
size_t size = sizeof(uint8_t) * PUMPS;
|
|
|
|
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
|
|
|
nvs_commit(config_handle);
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:38:19 -04:00
|
|
|
uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
|
2020-10-25 22:49:14 -04:00
|
|
|
if(idx > PUMPS) return PUMPS_ERR_OUT_IDX;
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "duration update: %d = %d", idx, time);
|
|
|
|
|
|
|
|
pumps_duration[idx] = time;
|
|
|
|
|
2021-09-24 01:58:57 -04:00
|
|
|
pumps_update_config();
|
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pumps_set_enabled(uint8_t idx, uint8_t value) {
|
|
|
|
uint8_t io = pump_gpio_map[idx];
|
|
|
|
|
|
|
|
value = (value ? 1 : 0);
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "gpio: %d value: %d", io, value);
|
|
|
|
// invert IO
|
|
|
|
if(safety)
|
|
|
|
gpio_set_level(io, value);
|
|
|
|
|
|
|
|
return pumps_enabled[idx] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pumps_get_enabled(uint8_t idx) {
|
|
|
|
return pumps_enabled[idx] ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:38:19 -04:00
|
|
|
uint8_t pumps_get_duration(uint8_t idx) {
|
2020-10-25 22:49:14 -04:00
|
|
|
return pumps_duration[idx];
|
|
|
|
}
|
|
|
|
|
2020-10-26 22:12:25 -04:00
|
|
|
uint8_t pumps_get_state(uint8_t idx) {
|
|
|
|
return pumps_state[idx];
|
|
|
|
}
|
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
uint8_t pump_enable(int8_t i) {
|
2021-09-22 21:55:49 -04:00
|
|
|
ESP_LOGI(TAG, "pump: %d enabled", i);
|
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
return gpio_set_level(pump_gpio_map[i], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pump_disable(int8_t i) {
|
2021-09-22 21:55:49 -04:00
|
|
|
ESP_LOGI(TAG, "pump: %d disabled", i);
|
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
return gpio_set_level(pump_gpio_map[i], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pump_timer_done(TimerHandle_t timer) {
|
|
|
|
uint8_t idx = (pcTimerGetTimerName(timer) - 0x48);
|
|
|
|
ESP_LOGD(TAG, "pump done: %d", idx);
|
|
|
|
|
|
|
|
running--;
|
|
|
|
|
|
|
|
pumps_set_enabled(idx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pumps_stop() {
|
|
|
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
|
|
|
if(pump_timers[i] != NULL)
|
2021-09-22 21:55:49 -04:00
|
|
|
pump_disable(i);
|
2020-10-25 22:49:14 -04:00
|
|
|
xTimerStop(pump_timers[i], 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pumps_run() {
|
|
|
|
if(running > 0) return;
|
|
|
|
|
|
|
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
|
|
|
if(pumps_duration[i] == 0) continue;
|
|
|
|
|
|
|
|
running++;
|
|
|
|
|
|
|
|
pump_timers[i] = xTimerCreate((const char *)(0x48 + i), (pumps_duration[i] * 1000 / portTICK_PERIOD_MS), pdFALSE, (void*)0, pump_timer_done);
|
|
|
|
|
|
|
|
xTimerStart(pump_timers[i], 0);
|
|
|
|
|
|
|
|
pumps_set_enabled(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pumps_init() {
|
|
|
|
gpio_config_t io_conf;
|
|
|
|
|
|
|
|
// put the output high before initializing
|
|
|
|
// to prevent relays from clacking
|
|
|
|
for(uint8_t i = 0 ; i < 4; i++) {
|
|
|
|
gpio_set_level(pump_gpio_map[i], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
|
|
|
|
io_conf.mode = GPIO_MODE_OUTPUT;
|
|
|
|
io_conf.pin_bit_mask = GPIO_PUMP_PIN_SEL;
|
|
|
|
io_conf.pull_down_en = 1;
|
|
|
|
io_conf.pull_up_en = 0;
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
|
|
|
|
// zero out pump enabled, duration
|
|
|
|
memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
|
|
|
|
memset(&pumps_duration, 10, sizeof(uint8_t) * PUMPS);
|
|
|
|
|
2021-09-24 01:58:57 -04:00
|
|
|
size_t size = sizeof(uint8_t) * PUMPS;
|
|
|
|
if(!nvs_get_blob(config_handle, "pumps_duration", &pumps_duration, &size)) {
|
|
|
|
ESP_LOGI(TAG, "Initializing pumps config");
|
|
|
|
|
|
|
|
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
|
|
|
};
|
2020-10-25 22:49:14 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "pumps_enabled: %d %d %d %d", pumps_enabled[0], pumps_enabled[1], pumps_enabled[2], pumps_enabled[3]);
|
|
|
|
ESP_LOGI(TAG, "pumps_duration: %d %d %d %d", pumps_duration[0], pumps_duration[1], pumps_duration[2], pumps_duration[3]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|