basic led control with local btn
This commit is contained in:
85
main/localbtn.c
Normal file
85
main/localbtn.c
Normal file
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @file localbtn.c
|
||||
* @brief Local GPIO0 BTN reading implementation using edge capture
|
||||
*/
|
||||
|
||||
#include "localbtn.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "soc/gpio_num.h"
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "LOCALBTN";
|
||||
uint8_t current_mode;
|
||||
localbtn_mode_change_callback_t callback;
|
||||
|
||||
#define BOOT_BTN GPIO_NUM_0
|
||||
#define MAX_MODES 14 // TODO: get from control
|
||||
|
||||
bool boot_button_pressed(void)
|
||||
{
|
||||
return gpio_get_level(BOOT_BTN) == 0; // active LOW
|
||||
}
|
||||
|
||||
static void monitor_task(void *arg)
|
||||
{
|
||||
bool lastState = false;
|
||||
while (1)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
bool currentState = boot_button_pressed();
|
||||
if ((currentState) && (lastState != currentState))
|
||||
{
|
||||
printf("BOOT button pressed\n");
|
||||
|
||||
current_mode = (current_mode + 1) % MAX_MODES;
|
||||
|
||||
ESP_LOGI(TAG, "Mode changed to: %d ", current_mode);
|
||||
|
||||
if (callback)
|
||||
{
|
||||
callback(current_mode);
|
||||
}
|
||||
}
|
||||
|
||||
lastState = currentState;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t localbtn_init()
|
||||
{
|
||||
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = 1ULL << BOOT_BTN,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_ENABLE, // safe even if external pull-up exists
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE};
|
||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||
|
||||
// Create monitor task
|
||||
BaseType_t ret = xTaskCreate(monitor_task, "localbtn_monitor", 2048, NULL, 5, NULL);
|
||||
if (ret != pdPASS)
|
||||
{
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// TODO: rcsignal.initialized = true;
|
||||
ESP_LOGI(TAG, "local btn initialized on GPIO%d", BOOT_BTN);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void localbtn_deinit(void)
|
||||
{
|
||||
// TODO:
|
||||
}
|
||||
|
||||
void localbtn_register_callback(localbtn_mode_change_callback_t cb)
|
||||
{
|
||||
callback = cb;
|
||||
}
|
||||
Reference in New Issue
Block a user