Start to work on GPIOs

This commit is contained in:
2024-09-28 20:27:12 +02:00
parent 274b7089d1
commit 4d07d83904
5 changed files with 51 additions and 9 deletions

View File

@ -9,13 +9,42 @@ static const char *TAG = "relays";
/**
* Device relays GPIO ids
*/
static int DEVICE_GPIO_IDS[5] = {2, 4, 12, 14, 15};
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);
}
void relays_turn_off_all()
{
ESP_LOGI(TAG, "Turning off all relays...");
for (size_t i = 0; i < DEV_MAX_RELAYS; i++)
for (size_t i = 0; i < relays_count(); i++)
{
relays_set(i, false);
}
@ -25,4 +54,10 @@ 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");
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);
}
}