mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-07 12:07:09 +03:00
audio control JSON parsing works
This commit is contained in:
@@ -27,12 +27,37 @@
|
|||||||
#include "cJSON.h"
|
#include "cJSON.h"
|
||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "audio_controls.h"
|
#include "audio_controls.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ACTRLS_MAP_INT, ACTRLS_MAP_BOOL, ACTRLS_MAP_ACTION,ACTRLS_MAP_TYPE,ACTRLS_MAP_END
|
||||||
|
} actrls_action_map_element_type_e;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char * member;
|
||||||
|
uint32_t offset;
|
||||||
|
actrls_action_map_element_type_e type;
|
||||||
|
} actrls_config_map_t;
|
||||||
|
|
||||||
|
static const actrls_config_map_t actrls_config_map[] =
|
||||||
|
{
|
||||||
|
{"gpio", offsetof(actrls_config_t,gpio), ACTRLS_MAP_INT},
|
||||||
|
{"debounce", offsetof(actrls_config_t,debounce), ACTRLS_MAP_INT},
|
||||||
|
{"type", offsetof(actrls_config_t,type),ACTRLS_MAP_TYPE},
|
||||||
|
{"pull", offsetof(actrls_config_t,pull),ACTRLS_MAP_BOOL},
|
||||||
|
{"long_press", offsetof(actrls_config_t,long_press),ACTRLS_MAP_INT},
|
||||||
|
{"shifter_gpio", offsetof(actrls_config_t,shifter_gpio),ACTRLS_MAP_INT},
|
||||||
|
{"normal", offsetof(actrls_config_t,normal), ACTRLS_MAP_ACTION},
|
||||||
|
{"shifted", offsetof(actrls_config_t,shifted), ACTRLS_MAP_ACTION},
|
||||||
|
{"longpress", offsetof(actrls_config_t,longpress), ACTRLS_MAP_ACTION},
|
||||||
|
{"longshifted", offsetof(actrls_config_t,longshifted), ACTRLS_MAP_ACTION},
|
||||||
|
{"", 0,ACTRLS_MAP_END}
|
||||||
|
};
|
||||||
|
|
||||||
// BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
|
// BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
|
||||||
static const char * actrls_action_s[ ] = { "ACTRLS_VOLUP","ACTRLS_VOLDOWN","ACTRLS_TOGGLE","ACTRLS_PLAY",
|
static const char * actrls_action_s[ ] = { "ACTRLS_VOLUP","ACTRLS_VOLDOWN","ACTRLS_TOGGLE","ACTRLS_PLAY",
|
||||||
"ACTRLS_PAUSE","ACTRLS_STOP","ACTRLS_REW","ACTRLS_FWD","ACTRLS_PREV","ACTRLS_NEXT",
|
"ACTRLS_PAUSE","ACTRLS_STOP","ACTRLS_REW","ACTRLS_FWD","ACTRLS_PREV","ACTRLS_NEXT",
|
||||||
"BCTRLS_PUSH", "BCTRLS_UP","BCTRLS_DOWN","BCTRLS_LEFT","BCTRLS_RIGHT", ""} ;
|
"BCTRLS_PUSH", "BCTRLS_UP","BCTRLS_DOWN","BCTRLS_LEFT","BCTRLS_RIGHT", ""} ;
|
||||||
|
|
||||||
|
|
||||||
static const char * TAG = "audio controls";
|
static const char * TAG = "audio controls";
|
||||||
static actrls_config_t *json_config;
|
static actrls_config_t *json_config;
|
||||||
static actrls_t default_controls, current_controls;
|
static actrls_t default_controls, current_controls;
|
||||||
@@ -41,8 +66,6 @@ static void control_handler(void *id, button_event_e event, button_press_e press
|
|||||||
actrls_config_t *key = (actrls_config_t*) id;
|
actrls_config_t *key = (actrls_config_t*) id;
|
||||||
actrls_action_e action;
|
actrls_action_e action;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u", key->gpio, press, long_press, event);
|
|
||||||
|
|
||||||
switch(press) {
|
switch(press) {
|
||||||
case BUTTON_NORMAL:
|
case BUTTON_NORMAL:
|
||||||
if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
|
if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
|
||||||
@@ -56,6 +79,8 @@ static void control_handler(void *id, button_event_e event, button_press_e press
|
|||||||
action = ACTRLS_NONE;
|
action = ACTRLS_NONE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u action:%u", key->gpio, press, long_press, event, action);
|
||||||
|
|
||||||
if (action != ACTRLS_NONE) {
|
if (action != ACTRLS_NONE) {
|
||||||
ESP_LOGD(TAG, " calling action %u", action);
|
ESP_LOGD(TAG, " calling action %u", action);
|
||||||
@@ -100,7 +125,6 @@ esp_err_t actrls_init(int n, const actrls_config_t *config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actrls_action_e actrls_parse_action_json(const char * name){
|
actrls_action_e actrls_parse_action_json(const char * name){
|
||||||
|
|
||||||
for(int i=0;actrls_action_s[i][0]!='\0' && i<ACTRLS_MAX;i++){
|
for(int i=0;actrls_action_s[i][0]!='\0' && i<ACTRLS_MAX;i++){
|
||||||
if(!strcmp(actrls_action_s[i], name)){
|
if(!strcmp(actrls_action_s[i], name)){
|
||||||
return (actrls_action_e) i;
|
return (actrls_action_e) i;
|
||||||
@@ -135,9 +159,9 @@ esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_con
|
|||||||
void *value = (config_pointer + map->offset);
|
void *value = (config_pointer + map->offset);
|
||||||
switch (map->type) {
|
switch (map->type) {
|
||||||
case ACTRLS_MAP_TYPE:
|
case ACTRLS_MAP_TYPE:
|
||||||
if (member->child != NULL) {
|
if (member->type == cJSON_String) {
|
||||||
*(int*) value =
|
*(int*) value =
|
||||||
!strcmp(member->child->string,
|
!strcmp(member->valuestring,
|
||||||
"BUTTON_LOW") ?
|
"BUTTON_LOW") ?
|
||||||
BUTTON_LOW : BUTTON_HIGH;
|
BUTTON_LOW : BUTTON_HIGH;
|
||||||
} else {
|
} else {
|
||||||
@@ -157,14 +181,14 @@ esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_con
|
|||||||
if (button_action != NULL) {
|
if (button_action != NULL) {
|
||||||
((actrls_action_e*) value)[0] =
|
((actrls_action_e*) value)[0] =
|
||||||
actrls_parse_action_json(
|
actrls_parse_action_json(
|
||||||
button_action->string);
|
button_action->valuestring);
|
||||||
}
|
}
|
||||||
button_action = cJSON_GetObjectItemCaseSensitive(
|
button_action = cJSON_GetObjectItemCaseSensitive(
|
||||||
member, "released");
|
member, "released");
|
||||||
if (button_action != NULL) {
|
if (button_action != NULL) {
|
||||||
((actrls_action_e*) value)[1] =
|
((actrls_action_e*) value)[1] =
|
||||||
actrls_parse_action_json(
|
actrls_parse_action_json(
|
||||||
button_action->string);
|
button_action->valuestring);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -172,14 +196,6 @@ esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_con
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (err == ESP_OK) {
|
|
||||||
button_create((void*) cur_config, cur_config->gpio,
|
|
||||||
cur_config->type, cur_config->pull,cur_config->debounce,
|
|
||||||
control_handler, cur_config->long_press,
|
|
||||||
cur_config->shifter_gpio);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,6 +235,12 @@ esp_err_t actrls_init_json(const char *config) {
|
|||||||
actrls_parse_config_map(member, cur_config);
|
actrls_parse_config_map(member, cur_config);
|
||||||
err = err == ESP_OK ? loc_err : err;
|
err = err == ESP_OK ? loc_err : err;
|
||||||
}
|
}
|
||||||
|
if (loc_err == ESP_OK) {
|
||||||
|
button_create((void*) cur_config, cur_config->gpio,
|
||||||
|
cur_config->type, cur_config->pull,cur_config->debounce,
|
||||||
|
control_handler, cur_config->long_press,
|
||||||
|
cur_config->shifter_gpio);
|
||||||
|
}
|
||||||
cur_config++;
|
cur_config++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,20 +20,17 @@
|
|||||||
|
|
||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
|
|
||||||
// BEWARE: this is the index of the array of action below
|
// BEWARE: this is the index of the array of action below (change actrls_action_s as well!)
|
||||||
typedef enum { ACTRLS_NONE = -1, ACTRLS_VOLUP, ACTRLS_VOLDOWN, ACTRLS_TOGGLE, ACTRLS_PLAY,
|
typedef enum { ACTRLS_NONE = -1, ACTRLS_VOLUP, ACTRLS_VOLDOWN, ACTRLS_TOGGLE, ACTRLS_PLAY,
|
||||||
ACTRLS_PAUSE, ACTRLS_STOP, ACTRLS_REW, ACTRLS_FWD, ACTRLS_PREV, ACTRLS_NEXT,
|
ACTRLS_PAUSE, ACTRLS_STOP, ACTRLS_REW, ACTRLS_FWD, ACTRLS_PREV, ACTRLS_NEXT,
|
||||||
BCTRLS_PUSH, BCTRLS_UP, BCTRLS_DOWN, BCTRLS_LEFT, BCTRLS_RIGHT,
|
BCTRLS_PUSH, BCTRLS_UP, BCTRLS_DOWN, BCTRLS_LEFT, BCTRLS_RIGHT,
|
||||||
ACTRLS_MAX
|
ACTRLS_MAX
|
||||||
} actrls_action_e;
|
} actrls_action_e;
|
||||||
typedef enum {
|
|
||||||
ACTRLS_MAP_INT, ACTRLS_MAP_BOOL, ACTRLS_MAP_ACTION,ACTRLS_MAP_TYPE,ACTRLS_MAP_END
|
|
||||||
} actrls_action_map_element_type_e;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef void (*actrls_handler)(void);
|
typedef void (*actrls_handler)(void);
|
||||||
typedef actrls_handler actrls_t[ACTRLS_MAX - ACTRLS_NONE - 1];
|
typedef actrls_handler actrls_t[ACTRLS_MAX - ACTRLS_NONE - 1];
|
||||||
|
|
||||||
|
// BEWARE any change to struct below must be mapped to actrls_config_map
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int gpio;
|
int gpio;
|
||||||
int type;
|
int type;
|
||||||
@@ -44,29 +41,6 @@ typedef struct {
|
|||||||
actrls_action_e normal[2], longpress[2], shifted[2], longshifted[2]; // [0] keypressed, [1] keyreleased
|
actrls_action_e normal[2], longpress[2], shifted[2], longshifted[2]; // [0] keypressed, [1] keyreleased
|
||||||
} actrls_config_t;
|
} actrls_config_t;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char * member;
|
|
||||||
uint32_t offset;
|
|
||||||
actrls_action_map_element_type_e type;
|
|
||||||
} actrls_config_map_t;
|
|
||||||
|
|
||||||
static const actrls_config_map_t actrls_config_map[] =
|
|
||||||
{
|
|
||||||
{"gpio", offsetof(actrls_config_t,gpio), ACTRLS_MAP_INT},
|
|
||||||
{"debounce", offsetof(actrls_config_t,debounce), ACTRLS_MAP_INT},
|
|
||||||
{"type", offsetof(actrls_config_t,type),ACTRLS_MAP_TYPE},
|
|
||||||
{"pull", offsetof(actrls_config_t,pull),ACTRLS_MAP_BOOL},
|
|
||||||
{"long_press", offsetof(actrls_config_t,long_press),ACTRLS_MAP_INT},
|
|
||||||
{"shifter_gpio", offsetof(actrls_config_t,shifter_gpio),ACTRLS_MAP_INT},
|
|
||||||
{"normal", offsetof(actrls_config_t,normal), ACTRLS_MAP_ACTION},
|
|
||||||
{"longpress", offsetof(actrls_config_t,longpress), ACTRLS_MAP_ACTION},
|
|
||||||
{"longshifted", offsetof(actrls_config_t,longshifted), ACTRLS_MAP_ACTION},
|
|
||||||
{"", 0,ACTRLS_MAP_END}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
esp_err_t actrls_init(int n, const actrls_config_t *config);
|
esp_err_t actrls_init(int n, const actrls_config_t *config);
|
||||||
esp_err_t actrls_init_json(const char * config);
|
esp_err_t actrls_init_json(const char * config);
|
||||||
void actrls_set_default(const actrls_t controls);
|
void actrls_set_default(const actrls_t controls);
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ static void notify(in_addr_t ip, u16_t hport, u16_t cport) {
|
|||||||
* Initialize controls - shall be called once from output_init_embedded
|
* Initialize controls - shall be called once from output_init_embedded
|
||||||
*/
|
*/
|
||||||
void cli_controls_init(void) {
|
void cli_controls_init(void) {
|
||||||
|
LOG_INFO("initializing CLI controls");
|
||||||
get_mac(mac);
|
get_mac(mac);
|
||||||
actrls_set_default(LMS_controls);
|
actrls_set_default(LMS_controls);
|
||||||
chained_notify = server_notify;
|
chained_notify = server_notify;
|
||||||
|
|||||||
Reference in New Issue
Block a user