barback32/main/pumps.c

123 lines
2.8 KiB
C
Raw Normal View History

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"
#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;
uint8_t pumps_set_time(uint8_t idx, uint8_t time) {
if(idx > PUMPS) return PUMPS_ERR_OUT_IDX;
ESP_LOGI(TAG, "duration update: %d = %d", idx, time);
pumps_duration[idx] = time;
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;
}
uint8_t pumps_get_time(uint8_t idx) {
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) {
return gpio_set_level(pump_gpio_map[i], 0);
}
uint8_t pump_disable(int8_t i) {
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)
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);
//uint32_t durations;
//esp_err_t err = nvs_get_i32(config_handle, "durations", &durations);
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;
}