Buttons management (WIP)

This commit is contained in:
philippe44
2020-01-01 19:53:23 -08:00
parent b268d2e8b0
commit 9230b10de6
6 changed files with 412 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
/*
* audio control callbacks
*
* (c) Sebastien 2019
* Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <unistd.h>
#include "esp_system.h"
#include "esp_log.h"
#include "buttons.h"
#include "audio_controls.h"
static const char * TAG = "audio_controls";
typedef struct {
void (*volume_up)(void);
void (*volume_down)(void);
void (*play_toggle)(void);
void (*play)(void);
void (*pause)(void);
void (*stop)(void);
} audio_control_t;
static audio_control_t default_control, current_control;
/****************************************************************************************
* Volume DOWN command
*/
static void volume_down(button_event_e event, button_press_e press) {
ESP_LOGI(TAG, "volume down %u", event);
//if (event == BUTTON_PRESSED) (*current_control.volume_down)();
}
static void volume_down_special(button_event_e event, button_press_e press) {
if (press == BUTTON_LONG) ESP_LOGI(TAG, "volume down long %u", event);
else ESP_LOGI(TAG, "volume down shifted %u", event);
//if (event == BUTTON_PRESSED) (*current_control.volume_down)();
}
/****************************************************************************************
* Volume UP commands
*/
static void volume_up(button_event_e event, button_press_e press) {
ESP_LOGI(TAG, "volume up %u", event);
//if (event == BUTTON_PRESSED) (*current_control.volume_up)();
}
static void volume_up_long(button_event_e event, button_press_e press) {
if (press == BUTTON_LONG) ESP_LOGI(TAG, "volume up long %u", event);
else ESP_LOGI(TAG, "volume up shifted %u", event);
//if (event == BUTTON_PRESSED) (*current_control.volume_down)();
}
/****************************************************************************************
*
*/
void audio_controls_init(void) {
/*
button_create(18, BUTTON_LOW, true, volume_up, NULL);
button_create(19, BUTTON_LOW, true, volume_down, "long", volume_down_long, 3000, NULL);
button_create(21, BUTTON_LOW, true, volume_up, NULL);
*/
button_create(4, BUTTON_LOW, true, volume_up, NULL);
button_create(5, BUTTON_LOW, true, volume_down, "long", volume_down_special, 3000, "shift", volume_down_special, 4, NULL);
}
/****************************************************************************************
*
*/
void default_audio_control(audio_control_t *control) {
default_control = current_control = *control;
}
/****************************************************************************************
*
*/
void register_audio_control(audio_control_t *control) {
current_control = *control;
}
/****************************************************************************************
*
*/
void deregister_audio_control(void) {
current_control = default_control;
}

View File

@@ -0,0 +1,21 @@
/*
* (c) Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
void audio_controls_init(void);

View File

@@ -0,0 +1,233 @@
/*
* a crude button press/long-press/shift management based on GPIO
*
* (c) Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_task.h"
#include "driver/gpio.h"
#include "buttons.h"
static const char * TAG = "audio_controls";
static int n_buttons = 0;
#define MAX_BUTTONS 16
#define DEBOUNCE 50
static struct button_s {
int gpio, index;
button_handler handler, long_handler, shift_handler;
struct button_s *shifter;
int long_press;
bool long_timer, shifted, shifting;
int type, level;
TimerHandle_t timer;
} buttons[MAX_BUTTONS];
static xQueueHandle button_evt_queue = NULL;
static void buttons_task(void* arg);
/****************************************************************************************
* GPIO low-level handler
*/
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
struct button_s *button = (struct button_s*) arg;
BaseType_t woken = pdFALSE;
if (xTimerGetPeriod(button->timer) > DEBOUNCE / portTICK_RATE_MS) xTimerChangePeriodFromISR(button->timer, DEBOUNCE / portTICK_RATE_MS, &woken); // does that restart the timer?
else xTimerResetFromISR(button->timer, &woken);
// ESP_EARLY_LOGD(TAG, "INT gpio %u level %u", button->gpio, button->level);
}
/****************************************************************************************
* Buttons debounce/longpress timer
*/
static void buttons_timer( TimerHandle_t xTimer ) {
struct button_s *button = (struct button_s*) pvTimerGetTimerID (xTimer);
button->level = gpio_get_level(button->gpio);
if (button->long_press && !button->long_timer && button->level == button->type) {
// detect a long press, so hold event generation
ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
xTimerChangePeriod(xTimer, button->long_press / portTICK_RATE_MS, 0);
button->long_timer = true;
} else {
// send a button pressed/released event (content is copied in queue)
ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
// queue will have a copy of button's context
xQueueSend(button_evt_queue, button, 0);
button->long_timer = false;
}
}
/****************************************************************************************
* Tasks that calls the appropriate functions when buttons are pressed
*/
static void buttons_task(void* arg) {
ESP_LOGI(TAG, "starting button tasks");
while (1) {
struct button_s button;
button_event_e event;
bool shifted = false;
if (!xQueueReceive(button_evt_queue, &button, portMAX_DELAY)) continue;
event = (button.level == button.type) ? BUTTON_PRESSED : BUTTON_RELEASED;
ESP_LOGD(TAG, "received event:%u from gpio:%u level:%u (timer %u shifting %u)", event, button.gpio, button.level, button.long_timer, button.shifting);
// find if shifting is activated
if (button.shifter && button.shifter->type == button.shifter->level) {
button.shifter->shifting = true;
shifted = true;
}
/*
long_timer will be set either because we truly have a long press
or we have a release before the long press timer elapsed, so two
events shall be sent
*/
if (button.long_timer) {
if (event == BUTTON_RELEASED) {
// early release of a long-press button, send press/release
if (shifted) {
(*button.shift_handler)(BUTTON_PRESSED, BUTTON_SHIFTED);
(*button.shift_handler)(BUTTON_RELEASED, BUTTON_SHIFTED);
} else if (!button.shifting) {
(*button.handler)(BUTTON_PRESSED, BUTTON_NORMAL);
(*button.handler)(BUTTON_RELEASED, BUTTON_NORMAL);
}
// button is a copy, so need to go to real context
buttons[button.index].shifting = false;
} else if (!button.shifting) {
/*
normal long press and not shifting so don't discard but there
is no long-press when shifted
*/
if (shifted) (*button.shift_handler)(BUTTON_PRESSED, BUTTON_SHIFTED);
else (*button.long_handler)(BUTTON_PRESSED, BUTTON_LONG);
}
} else if (shifted) {
// we are shifted
(*button.shift_handler)(event, BUTTON_SHIFTED);
} else {
// normal press/release of a button or release of a long-press button
if (!button.shifting) {
if (button.long_press) (*button.long_handler)(event, BUTTON_LONG);
else (*button.handler)(event, BUTTON_NORMAL);
}
// button is a copy, so need to go to real context
buttons[button.index].shifting = false;
}
}
}
/****************************************************************************************
* dummy button handler
*/
void dummy_handler(button_event_e event, button_press_e press) {
ESP_LOGW(TAG, "should not be here");
}
/****************************************************************************************
* Create buttons
*/
void button_create(int gpio, int type, bool pull, button_handler handler, ...) {
char *param;
va_list args;
if (n_buttons >= MAX_BUTTONS) return;
ESP_LOGI(TAG, "creating button using GPIO %u, type %u, pull-up/down %u", gpio, type, pull);
if (!n_buttons) {
button_evt_queue = xQueueCreate(10, sizeof(struct button_s));
gpio_install_isr_service(0);
xTaskCreate(buttons_task, "buttons_task", 2048, NULL, ESP_TASK_PRIO_MIN + 1, NULL);
}
// just in case this structure is allocated later
memset(buttons + n_buttons, 0, sizeof(struct button_s));
// set mandatory parameters
buttons[n_buttons].gpio = gpio;
buttons[n_buttons].handler = handler;
buttons[n_buttons].level = -1;
buttons[n_buttons].type = type;
buttons[n_buttons].timer = xTimerCreate("buttonTimer", DEBOUNCE / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
// little trick to find ourselves from queued copy
buttons[n_buttons].index = n_buttons;
va_start(args, handler);
// set optional parameters
while ((param = va_arg(args, char*)) != NULL) {
if (!strcasecmp(param, "long")) {
buttons[n_buttons].long_handler = (button_handler) va_arg(args, void*);
buttons[n_buttons].long_press = va_arg(args, int);
ESP_LOGI(TAG, "adding long press %u (ms)", buttons[n_buttons].long_press);
} else if (!strcasecmp(param, "shift")) {
buttons[n_buttons].shift_handler = (button_handler) va_arg(args, void*);
int shifter_gpio = va_arg(args, int);
for (int i = 0; i < n_buttons; i++) {
if (buttons[i].gpio == shifter_gpio) {
buttons[n_buttons].shifter = buttons + i;
// a shifter must have a long-press handler
if (!buttons[i].long_press) {
buttons[i].long_press = 60*60*1000;
buttons[i].long_handler = dummy_handler;
}
break;
}
}
ESP_LOGI(TAG, "adding shifter %u", buttons[n_buttons].shifter->gpio);
}
}
va_end(args);
gpio_set_direction(gpio, GPIO_MODE_INPUT);
// we need any edge detection
gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE);
// do we need pullup or pulldown
if (pull) {
if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
}
gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
gpio_intr_enable(gpio);
n_buttons++;
}

View File

@@ -0,0 +1,42 @@
/*
* (c) Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
// button type (pressed = LOW or HIGH)
#define BUTTON_LOW 0
#define BUTTON_HIGH 1
typedef enum { BUTTON_PRESSED, BUTTON_RELEASED } button_event_e;
typedef enum { BUTTON_NORMAL, BUTTON_LONG, BUTTON_SHIFTED } button_press_e;
typedef void (*button_handler)(button_event_e event, button_press_e mode);
/*
a button might have variable functions
- "long", <long_handler>, <duration_in_ms>
- "shift", <shift_handler>, <shifter_gpio>
button_create(2, BUTTON_LOW, true, handler, NULL);
button_create(5, BUTTON_HIGH, true, handler, "long", long_handler, 2000, NULL);
button_create(6, BUTTON_LOW, true, handler, "shift", shift_handler, 5, NULL);
button_create(6, BUTTON_HIGH, true, handler, "long", long_handler, 2000, "shift", shift_handler, 5);
NOTE: shifter buttons *must* be created before shiftee
*/
void button_create(int gpio, int type, bool pull, button_handler handler, ...);

View File

@@ -0,0 +1,10 @@
#
# Component Makefile
#
# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the SDK documents if you need to do this.
#
COMPONENT_ADD_INCLUDEDIRS := .

View File

@@ -46,6 +46,7 @@
#include "squeezelite-ota.h"
#include <math.h>
#include "config.h"
#include "audio_controls.h"
extern bool enable_bt_sink;
extern bool enable_airplay;
@@ -375,11 +376,13 @@ void app_main()
}
ESP_LOGD(TAG,"Configuring Green led");
led_config(LED_GREEN, LED_GREEN_GPIO, 0);
ESP_LOGD(TAG,"Configuring Red led");
led_config(LED_RED, LED_RED_GPIO, 0);
ESP_LOGD(TAG,"Initializing audio control buttons");
audio_controls_init();
/* start the wifi manager */
ESP_LOGD(TAG,"Blinking led");
led_blink(LED_GREEN, 250, 250);