123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
-
- #include <stdlib.h>
- #include <string.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";
- static actrls_t default_controls, current_controls;
- static void control_handler(void *id, button_event_e event, button_press_e press, bool long_press) {
- actrls_config_t *key = (actrls_config_t*) id;
- actrls_action_e action;
- ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u", key->gpio, press, long_press, event);
-
- switch(press) {
- case BUTTON_NORMAL:
- if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
- else action = key->normal[event == BUTTON_PRESSED ? 0 : 1];
- break;
- case BUTTON_SHIFTED:
- if (long_press) action = key->longshifted[event == BUTTON_PRESSED ? 0 : 1];
- else action = key->shifted[event == BUTTON_PRESSED ? 0 : 1];
- break;
- default:
- action = ACTRLS_NONE;
- break;
- }
- if (action != ACTRLS_NONE) {
- ESP_LOGD(TAG, " calling action %u", action);
- if (current_controls[action]) (*current_controls[action])();
- }
- }
- void actrls_init(int n, actrls_config_t *config) {
- for (int i = 0; i < n; i++) {
- button_create(config + i, config[i].gpio, config[i].type, config[i].pull, control_handler, config[i].long_press, config[i].shifter_gpio);
- }
- }
- void actrls_set_default(actrls_t controls) {
- memcpy(default_controls, controls, sizeof(actrls_t));
- memcpy(current_controls, default_controls, sizeof(actrls_t));
- }
- void actrls_set(actrls_t controls) {
- memcpy(current_controls, controls, sizeof(actrls_t));
- }
- void actrls_unset(void) {
- memcpy(current_controls, default_controls, sizeof(actrls_t));
- }
|