2024-09-28 18:02:34 +00:00
|
|
|
#include <driver/gpio.h>
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
#include "relays.h"
|
|
|
|
#include "constants.h"
|
|
|
|
|
|
|
|
static const char *TAG = "relays";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Device relays GPIO ids
|
|
|
|
*/
|
2024-09-28 18:27:12 +00:00
|
|
|
static int DEVICE_GPIO_IDS[3] = {4, 14, 15};
|
|
|
|
|
|
|
|
int relays_count()
|
|
|
|
{
|
|
|
|
return sizeof(DEVICE_GPIO_IDS) / sizeof(int);
|
|
|
|
}
|
|
|
|
|
|
|
|
void relays_setup()
|
|
|
|
{
|
|
|
|
int pin_bit_mask = 0;
|
|
|
|
for (size_t i = 0; i < relays_count(); i++)
|
|
|
|
{
|
|
|
|
pin_bit_mask |= 1ULL << DEVICE_GPIO_IDS[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// zero-initialize the config structure.
|
|
|
|
gpio_config_t io_conf = {};
|
|
|
|
// disable interrupt
|
|
|
|
io_conf.intr_type = GPIO_INTR_DISABLE;
|
|
|
|
// set as output mode
|
|
|
|
io_conf.mode = GPIO_MODE_OUTPUT;
|
|
|
|
// bit mask of the pins that you want to set,e.g.GPIO18/19
|
|
|
|
io_conf.pin_bit_mask = pin_bit_mask;
|
|
|
|
// disable pull-down mode
|
|
|
|
io_conf.pull_down_en = 0;
|
|
|
|
// disable pull-up mode
|
|
|
|
io_conf.pull_up_en = 0;
|
|
|
|
// configure GPIO with the given settings
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
}
|
2024-09-28 18:02:34 +00:00
|
|
|
|
|
|
|
void relays_turn_off_all()
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Turning off all relays...");
|
|
|
|
|
2024-09-28 18:27:12 +00:00
|
|
|
for (size_t i = 0; i < relays_count(); i++)
|
2024-09-28 18:02:34 +00:00
|
|
|
{
|
|
|
|
relays_set(i, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void relays_set(int number, bool is_on)
|
|
|
|
{
|
|
|
|
size_t gpio_id = DEVICE_GPIO_IDS[number];
|
|
|
|
ESP_LOGI(TAG, "Set relay %d (gpio %d) to %s", number, gpio_id, is_on ? "on" : "off");
|
2024-09-28 18:27:12 +00:00
|
|
|
|
|
|
|
int res = gpio_set_level(gpio_id, is_on ? 0 : 1);
|
|
|
|
if (res != ESP_OK)
|
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Failed to toggle GPIO %d : %d", gpio_id, res);
|
|
|
|
}
|
2024-09-28 18:02:34 +00:00
|
|
|
}
|