audio_controls.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * audio control callbacks
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/timers.h"
  16. #include "esp_system.h"
  17. #include "esp_log.h"
  18. #include "cJSON.h"
  19. #include "buttons.h"
  20. #include "platform_config.h"
  21. #include "accessors.h"
  22. #include "services.h"
  23. #include "audio_controls.h"
  24. typedef esp_err_t (actrls_config_map_handler) (const cJSON * member, actrls_config_t *cur_config,uint32_t offset);
  25. typedef struct {
  26. char * member;
  27. uint32_t offset;
  28. actrls_config_map_handler * handler;
  29. } actrls_config_map_t;
  30. static esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config);
  31. static esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config);
  32. static esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  33. static esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  34. static esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  35. static esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  36. static esp_err_t actrls_init_json(const char *profile_name, bool create);
  37. static void control_rotary_handler(void *client, rotary_event_e event, bool long_press);
  38. static void volume_rotary_handler(void *client, rotary_event_e event, bool long_press);
  39. static void rotary_timer( TimerHandle_t xTimer );
  40. static const actrls_config_map_t actrls_config_map[] =
  41. {
  42. {"gpio", offsetof(actrls_config_t,gpio), actrls_process_int},
  43. {"debounce", offsetof(actrls_config_t,debounce), actrls_process_int},
  44. {"type", offsetof(actrls_config_t,type), actrls_process_type},
  45. {"pull", offsetof(actrls_config_t,pull), actrls_process_bool},
  46. {"long_press", offsetof(actrls_config_t,long_press),actrls_process_int},
  47. {"shifter_gpio", offsetof(actrls_config_t,shifter_gpio), actrls_process_int},
  48. {"normal", offsetof(actrls_config_t,normal), actrls_process_action},
  49. {"shifted", offsetof(actrls_config_t,shifted), actrls_process_action},
  50. {"longpress", offsetof(actrls_config_t,longpress), actrls_process_action},
  51. {"longshifted", offsetof(actrls_config_t,longshifted), actrls_process_action},
  52. {"", 0, NULL}
  53. };
  54. // BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
  55. // along with the actrls_t controls in LMS_controls, bt_sink and raop_sink
  56. #define EP(x) [x] = #x /* ENUM PRINT */
  57. static const char * actrls_action_s[ ] = { EP(ACTRLS_POWER),EP(ACTRLS_VOLUP),EP(ACTRLS_VOLDOWN),EP(ACTRLS_TOGGLE),EP(ACTRLS_PLAY),
  58. EP(ACTRLS_PAUSE),EP(ACTRLS_STOP),EP(ACTRLS_REW),EP(ACTRLS_FWD),EP(ACTRLS_PREV),EP(ACTRLS_NEXT),
  59. EP(BCTRLS_UP),EP(BCTRLS_DOWN),EP(BCTRLS_LEFT),EP(BCTRLS_RIGHT),
  60. EP(BCTRLS_PS0),EP(BCTRLS_PS1),EP(BCTRLS_PS2),EP(BCTRLS_PS3),EP(BCTRLS_PS4),EP(BCTRLS_PS5),EP(BCTRLS_PS6),EP(BCTRLS_PS7),EP(BCTRLS_PS8),EP(BCTRLS_PS9),
  61. EP(KNOB_LEFT),EP(KNOB_RIGHT),EP(KNOB_PUSH), EP(ACTRLS_SLEEP),
  62. ""} ;
  63. static const char * TAG = "audio controls";
  64. static actrls_config_t *json_config;
  65. cJSON * control_profiles = NULL;
  66. static EXT_RAM_ATTR actrls_t default_controls, current_controls;
  67. static actrls_hook_t *default_hook, *current_hook;
  68. static bool default_raw_controls, current_raw_controls;
  69. static actrls_ir_handler_t *default_ir_handler, *current_ir_handler;
  70. static EXT_RAM_ATTR struct {
  71. bool long_state;
  72. bool volume_lock;
  73. TimerHandle_t timer;
  74. bool click_pending;
  75. int left_count;
  76. } rotary;
  77. static const struct ir_action_map_s{
  78. uint32_t code;
  79. actrls_action_e action;
  80. } ir_action_map[] = {
  81. {0x7689b04f, BCTRLS_DOWN}, {0x7689906f, BCTRLS_LEFT}, {0x7689d02f, BCTRLS_RIGHT}, {0x7689e01f, BCTRLS_UP},
  82. {0x768900ff, ACTRLS_VOLDOWN}, {0x7689807f, ACTRLS_VOLUP},
  83. {0x7689c03f, ACTRLS_PREV}, {0x7689a05f, ACTRLS_NEXT},
  84. {0x768920df, ACTRLS_PAUSE}, {0x768910ef, ACTRLS_PLAY},
  85. {0x00, 0x00},
  86. };
  87. /****************************************************************************************
  88. * This function can be called to map IR codes to default actions
  89. */
  90. bool actrls_ir_action(uint16_t addr, uint16_t cmd) {
  91. uint32_t code = (addr << 16) | cmd;
  92. struct ir_action_map_s const *map = ir_action_map;
  93. while (map->code && map->code != code) map++;
  94. if (map->code && current_controls[map->action]) {
  95. current_controls[map->action](true);
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. /****************************************************************************************
  102. *
  103. */
  104. static void ir_handler(uint16_t addr, uint16_t cmd) {
  105. ESP_LOGD(TAG, "recaived IR %04hx:%04hx", addr, cmd);
  106. if (current_ir_handler) current_ir_handler(addr, cmd);
  107. }
  108. /****************************************************************************************
  109. *
  110. */
  111. static void set_ir_gpio(int gpio, char *value) {
  112. if (strcasestr(value, "ir")) {
  113. if (strcasestr(value, "rc5")) create_infrared(gpio, ir_handler, IR_RC5);
  114. else create_infrared(gpio, ir_handler, IR_NEC);
  115. }
  116. }
  117. /****************************************************************************************
  118. *
  119. */
  120. esp_err_t actrls_init(const char *profile_name) {
  121. esp_err_t err = ESP_OK;
  122. char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0);
  123. if (config && *config) {
  124. char *p;
  125. int A = -1, B = -1, SW = -1, longpress = 0;
  126. // parse config
  127. PARSE_PARAM(config, "A", '=', A);
  128. PARSE_PARAM(config, "B", '=', B);
  129. PARSE_PARAM(config, "SW", '=', SW);
  130. if ((p = strcasestr(config, "knobonly"))) {
  131. p = strchr(p, '=');
  132. int double_press = p ? atoi(p + 1) : 350;
  133. rotary.timer = xTimerCreate("knobTimer", double_press / portTICK_RATE_MS, pdFALSE, NULL, rotary_timer);
  134. longpress = 500;
  135. ESP_LOGI(TAG, "single knob navigation %d", double_press);
  136. } else {
  137. if ((p = strcasestr(config, "volume")) != NULL) rotary.volume_lock = true;
  138. if ((p = strcasestr(config, "longpress")) != NULL) longpress = 1000;
  139. }
  140. // create rotary (no handling of long press)
  141. err = create_rotary(NULL, A, B, SW, longpress, control_rotary_handler) ? ESP_OK : ESP_FAIL;
  142. }
  143. free(config);
  144. config = config_alloc_get_default(NVS_TYPE_STR, "volume_rotary", NULL, 0);
  145. // now see if we have a dedicated volume rotary
  146. if (config && *config) {
  147. int A = -1, B = -1, SW = -1;
  148. // parse config
  149. PARSE_PARAM(config, "A", '=', A);
  150. PARSE_PARAM(config, "B", '=', B);
  151. PARSE_PARAM(config, "SW", '=', SW);
  152. // create rotary (no handling of long press)
  153. err |= create_volume_rotary(NULL, A, B, SW, volume_rotary_handler) ? ESP_OK : ESP_FAIL;
  154. }
  155. free(config);
  156. // set infrared GPIO if any
  157. parse_set_GPIO(set_ir_gpio);
  158. if (!err) return actrls_init_json(profile_name, true);
  159. else return err;
  160. }
  161. /****************************************************************************************
  162. *
  163. */
  164. static void control_handler(void *client, button_event_e event, button_press_e press, bool long_press) {
  165. actrls_config_t *key = (actrls_config_t*) client;
  166. actrls_action_detail_t action_detail;
  167. switch(press) {
  168. case BUTTON_NORMAL:
  169. if (long_press) action_detail = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
  170. else action_detail = key->normal[event == BUTTON_PRESSED ? 0 : 1];
  171. break;
  172. case BUTTON_SHIFTED:
  173. if (long_press) action_detail = key->longshifted[event == BUTTON_PRESSED ? 0 : 1];
  174. else action_detail = key->shifted[event == BUTTON_PRESSED ? 0 : 1];
  175. break;
  176. default:
  177. action_detail.action = ACTRLS_NONE;
  178. break;
  179. }
  180. ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u action:%u", key->gpio, press, long_press, event, action_detail.action);
  181. // stop here if control hook served the request
  182. if (current_hook && (*current_hook)(key->gpio, action_detail.action, event, press, long_press)) return;
  183. // in raw mode, we just do normal action press *and* release, there is no longpress nor shift
  184. if (current_raw_controls && action_detail.action != ACTRLS_SLEEP) {
  185. actrls_action_e action = key->normal[0].action != ACTRLS_NONE ? key->normal[0].action : key->normal[1].action;
  186. ESP_LOGD(TAG, "calling action %u in raw mode", action);
  187. if (action != ACTRLS_NONE && current_controls[action]) current_controls[action](event == BUTTON_PRESSED);
  188. return;
  189. }
  190. // otherwise process using configuration
  191. if (action_detail.action == ACTRLS_REMAP) {
  192. // remap requested
  193. ESP_LOGD(TAG, "remapping buttons to profile %s",action_detail.name);
  194. cJSON * profile_obj = cJSON_GetObjectItem(control_profiles,action_detail.name);
  195. if (profile_obj) {
  196. actrls_config_t *cur_config = (actrls_config_t *) cJSON_GetStringValue(profile_obj);
  197. if (cur_config) {
  198. ESP_LOGD(TAG,"Remapping all the buttons that are found in the new profile");
  199. while (cur_config->gpio != -1) {
  200. ESP_LOGD(TAG,"Remapping button with gpio %u", cur_config->gpio);
  201. button_remap((void*) cur_config, cur_config->gpio, control_handler, cur_config->long_press, cur_config->shifter_gpio);
  202. cur_config++;
  203. }
  204. } else {
  205. ESP_LOGE(TAG,"Profile %s exists, but is empty. Cannot remap buttons",action_detail.name);
  206. }
  207. } else {
  208. ESP_LOGE(TAG,"Invalid profile name %s. Cannot remap buttons",action_detail.name);
  209. }
  210. } else if (action_detail.action == ACTRLS_SLEEP) {
  211. ESP_LOGI(TAG, "special sleep button pressed");
  212. services_sleep_activate(SLEEP_ONKEY);
  213. } else if (action_detail.action != ACTRLS_NONE) {
  214. ESP_LOGD(TAG, "calling action %u", action_detail.action);
  215. if (current_controls[action_detail.action]) (*current_controls[action_detail.action])(event == BUTTON_PRESSED);
  216. }
  217. }
  218. /****************************************************************************************
  219. *
  220. */
  221. static void control_rotary_handler(void *client, rotary_event_e event, bool long_press) {
  222. actrls_action_e action = ACTRLS_NONE;
  223. bool pressed = true;
  224. // in raw mode, we just pass rotary events
  225. if (current_raw_controls) {
  226. if (event == ROTARY_LEFT) (*current_controls[KNOB_LEFT])(true);
  227. else if (event == ROTARY_RIGHT) (*current_controls[KNOB_RIGHT])(true);
  228. else (*current_controls[KNOB_PUSH])(event == ROTARY_PRESSED);
  229. return;
  230. }
  231. switch(event) {
  232. case ROTARY_LEFT:
  233. if (rotary.timer) {
  234. if (rotary.left_count) {
  235. action = KNOB_LEFT;
  236. // need to add a left button the first time
  237. if (rotary.left_count == 1) (*current_controls[KNOB_LEFT])(true);
  238. }
  239. xTimerStart(rotary.timer, 20 / portTICK_RATE_MS);
  240. rotary.left_count++;
  241. }
  242. else if (rotary.long_state) action = ACTRLS_PREV;
  243. else if (rotary.volume_lock) action = ACTRLS_VOLDOWN;
  244. else action = KNOB_LEFT;
  245. break;
  246. case ROTARY_RIGHT:
  247. if (rotary.timer) {
  248. if (rotary.left_count == 1) {
  249. action = ACTRLS_PAUSE;
  250. rotary.left_count = 0;
  251. xTimerStop(rotary.timer, 0);
  252. } else action = KNOB_RIGHT;
  253. }
  254. else if (rotary.long_state) action = ACTRLS_NEXT;
  255. else if (rotary.volume_lock) action = ACTRLS_VOLUP;
  256. else action = KNOB_RIGHT;
  257. break;
  258. case ROTARY_PRESSED:
  259. if (rotary.timer) {
  260. if (long_press) action = ACTRLS_PLAY;
  261. else if (rotary.click_pending) {
  262. action = BCTRLS_LEFT;
  263. xTimerStop(rotary.timer, 0);
  264. }
  265. else xTimerStart(rotary.timer, 20 / portTICK_RATE_MS);
  266. rotary.click_pending = !rotary.click_pending;
  267. }
  268. else if (long_press) rotary.long_state = !rotary.long_state;
  269. else if (rotary.volume_lock) action = ACTRLS_TOGGLE;
  270. else action = KNOB_PUSH;
  271. break;
  272. default:
  273. break;
  274. }
  275. if (action != ACTRLS_NONE) (*current_controls[action])(pressed);
  276. }
  277. /****************************************************************************************
  278. *
  279. */
  280. static void volume_rotary_handler(void *client, rotary_event_e event, bool long_press) {
  281. actrls_action_e action = ACTRLS_NONE;
  282. bool pressed = true;
  283. switch(event) {
  284. case ROTARY_LEFT:
  285. action = ACTRLS_VOLDOWN;
  286. break;
  287. case ROTARY_RIGHT:
  288. action = ACTRLS_VOLUP;
  289. break;
  290. case ROTARY_PRESSED:
  291. action = ACTRLS_TOGGLE;
  292. default:
  293. break;
  294. }
  295. if (action != ACTRLS_NONE) (*current_controls[action])(pressed);
  296. }
  297. /****************************************************************************************
  298. *
  299. */
  300. static void rotary_timer( TimerHandle_t xTimer ) {
  301. if (rotary.click_pending) {
  302. (*current_controls[KNOB_PUSH])(true);
  303. rotary.click_pending = false;
  304. } else if (rotary.left_count) {
  305. if (rotary.left_count == 1) (*current_controls[KNOB_LEFT])(true);
  306. rotary.left_count = 0;
  307. }
  308. }
  309. /****************************************************************************************
  310. *
  311. */
  312. static actrls_action_e actrls_parse_action_json(const char * name){
  313. actrls_action_e action = ACTRLS_NONE;
  314. if(!strcasecmp("ACTRLS_NONE",name)) return ACTRLS_NONE;
  315. for(int i=0;i<ACTRLS_MAX && actrls_action_s[i][0]!='\0' ;i++){
  316. if(!strcmp(actrls_action_s[i], name)){
  317. return (actrls_action_e) i;
  318. }
  319. }
  320. // Action name wasn't recognized.
  321. // Check if this is a profile name that has a match in nvs
  322. ESP_LOGD(TAG,"unknown action %s, trying to find matching profile ", name);
  323. cJSON * existing = cJSON_GetObjectItem(control_profiles, name);
  324. if (!existing) {
  325. ESP_LOGD(TAG,"Loading new audio control profile with name: %s", name);
  326. if (actrls_init_json(name, false) == ESP_OK) {
  327. action = ACTRLS_REMAP;
  328. }
  329. } else {
  330. ESP_LOGD(TAG,"Existing profile %s was referenced", name);
  331. action = ACTRLS_REMAP;
  332. }
  333. return action;
  334. }
  335. /****************************************************************************************
  336. *
  337. */
  338. static esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config,uint32_t offset){
  339. esp_err_t err = ESP_OK;
  340. ESP_LOGD(TAG,"Processing int member");
  341. int *value = (int*)((char*) cur_config + offset);
  342. *value = member->valueint;
  343. return err;
  344. }
  345. /****************************************************************************************
  346. *
  347. */
  348. static esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  349. esp_err_t err = ESP_OK;
  350. ESP_LOGD(TAG,"Processing type member");
  351. int *value = (int *)((char*) cur_config + offset);
  352. if (member->type == cJSON_String) {
  353. *value =
  354. !strcmp(member->valuestring,
  355. "BUTTON_LOW") ?
  356. BUTTON_LOW : BUTTON_HIGH;
  357. } else {
  358. ESP_LOGE(TAG,
  359. "Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
  360. err=ESP_FAIL;
  361. }
  362. return err;
  363. }
  364. /****************************************************************************************
  365. *
  366. */
  367. static esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  368. esp_err_t err = ESP_OK;
  369. if (!member) {
  370. ESP_LOGE(TAG,"Null json member pointer!");
  371. err = ESP_FAIL;
  372. } else {
  373. ESP_LOGD(TAG,"Processing bool member ");
  374. if (cJSON_IsBool(member)) {
  375. bool*value = (bool*)((char*) cur_config + offset);
  376. *value = cJSON_IsTrue(member);
  377. } else {
  378. ESP_LOGE(TAG,"Member %s is not a boolean", member->string?member->string:"unknown");
  379. err = ESP_FAIL;
  380. }
  381. }
  382. return err;
  383. }
  384. /****************************************************************************************
  385. *
  386. */
  387. static esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  388. esp_err_t err = ESP_OK;
  389. cJSON * button_action= cJSON_GetObjectItemCaseSensitive(member, "pressed");
  390. actrls_action_detail_t*value = (actrls_action_detail_t*)((char *)cur_config + offset);
  391. if (button_action != NULL) {
  392. value[0].action = actrls_parse_action_json( button_action->valuestring);
  393. if(value[0].action == ACTRLS_REMAP){
  394. value[0].name = strdup(button_action->valuestring);
  395. }
  396. }
  397. button_action = cJSON_GetObjectItemCaseSensitive(member, "released");
  398. if (button_action != NULL) {
  399. value[1].action = actrls_parse_action_json( button_action->valuestring);
  400. if (value[1].action == ACTRLS_REMAP){
  401. value[1].name = strdup(button_action->valuestring);
  402. }
  403. }
  404. return err;
  405. }
  406. /****************************************************************************************
  407. *
  408. */
  409. static esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config) {
  410. esp_err_t err = ESP_OK;
  411. const actrls_config_map_t * h=actrls_config_map;
  412. char * str = cJSON_Print(member);
  413. while (h->handler && strcmp(member->string, h->member)) { h++; }
  414. if (h->handler) {
  415. ESP_LOGD(TAG,"found handler for member %s, json value %s", h->member,str?str:"");
  416. err = h->handler(member, cur_config, h->offset);
  417. } else {
  418. err = ESP_FAIL;
  419. ESP_LOGE(TAG, "Unknown json structure member : %s", str?str:"");
  420. }
  421. if (str) free(str);
  422. return err;
  423. }
  424. /****************************************************************************************
  425. *
  426. */
  427. static esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config) {
  428. esp_err_t err= ESP_OK;
  429. const cJSON *member;
  430. cJSON_ArrayForEach(member, button)
  431. {
  432. ESP_LOGD(TAG,"Processing member %s. ", member->string);
  433. esp_err_t loc_err = actrls_process_member(member, cur_config);
  434. err = (err == ESP_OK) ? loc_err : err;
  435. }
  436. return err;
  437. }
  438. /****************************************************************************************
  439. *
  440. */
  441. static actrls_config_t * actrls_init_alloc_structure(const cJSON *buttons, const char * name){
  442. int member_count = 0;
  443. const cJSON *button;
  444. actrls_config_t * json_config=NULL;
  445. // Check if the main profiles array was created
  446. if(!control_profiles){
  447. control_profiles = cJSON_CreateObject();
  448. }
  449. ESP_LOGD(TAG,"Counting the number of buttons definition");
  450. cJSON_ArrayForEach(button, buttons) {
  451. member_count++;
  452. }
  453. ESP_LOGD(TAG, "config contains %u button definitions", member_count);
  454. if (member_count != 0) {
  455. json_config = calloc(sizeof(actrls_config_t) * (member_count + 1), 1);
  456. if (json_config){
  457. json_config[member_count].gpio = -1;
  458. } else {
  459. ESP_LOGE(TAG,"Unable to allocate memory to hold configuration for %u buttons ",member_count);
  460. }
  461. } else {
  462. ESP_LOGE(TAG,"No button found in configuration structure");
  463. }
  464. // we're cheating here; we're going to store the control profile
  465. // pointer as a string reference; this will prevent cJSON
  466. // from trying to free the structure if we ever want to free the object
  467. cJSON * new_profile = cJSON_CreateStringReference((const char *)json_config);
  468. cJSON_AddItemToObject(control_profiles, name, new_profile);
  469. return json_config;
  470. }
  471. /****************************************************************************************
  472. *
  473. */
  474. static void actrls_defaults(actrls_config_t *config) {
  475. config->type = BUTTON_LOW;
  476. config->pull = false;
  477. config->debounce = 0;
  478. config->long_press = 0;
  479. config->shifter_gpio = -1;
  480. config->normal[0].action = config->normal[1].action = ACTRLS_NONE;
  481. config->longpress[0].action = config->longpress[1].action = ACTRLS_NONE;
  482. config->shifted[0].action = config->shifted[1].action = ACTRLS_NONE;
  483. config->longshifted[0].action = config->longshifted[1].action = ACTRLS_NONE;
  484. }
  485. /****************************************************************************************
  486. *
  487. */
  488. static esp_err_t actrls_init_json(const char *profile_name, bool create) {
  489. esp_err_t err = ESP_OK;
  490. actrls_config_t *cur_config = NULL;
  491. actrls_config_t *config_root = NULL;
  492. char *config;
  493. const cJSON *button;
  494. if (!profile_name) return ESP_OK;
  495. if ((config = config_alloc_get_str(profile_name, NULL, CONFIG_AUDIO_CONTROLS)) == NULL) return ESP_FAIL;
  496. if (!*config) goto exit;
  497. ESP_LOGD(TAG,"Parsing JSON structure %s", config);
  498. cJSON *buttons = cJSON_Parse(config);
  499. if (buttons == NULL) {
  500. ESP_LOGE(TAG,"JSON Parsing failed for %s", config);
  501. err = ESP_FAIL;
  502. } else {
  503. ESP_LOGD(TAG,"Json parsing completed");
  504. if (cJSON_IsArray(buttons)) {
  505. ESP_LOGD(TAG,"configuration is an array as expected");
  506. cur_config =config_root= actrls_init_alloc_structure(buttons, profile_name);
  507. if(!cur_config) {
  508. ESP_LOGE(TAG,"Config buffer was empty. ");
  509. cJSON_Delete(buttons);
  510. err = ESP_FAIL;
  511. goto exit;
  512. }
  513. ESP_LOGD(TAG,"Processing button definitions. ");
  514. cJSON_ArrayForEach(button, buttons){
  515. char * str = cJSON_Print(button);
  516. ESP_LOGD(TAG,"Processing %s. ", str?str:"");
  517. if(str){
  518. free(str);
  519. }
  520. actrls_defaults(cur_config);
  521. esp_err_t loc_err = actrls_process_button(button, cur_config);
  522. err = (err == ESP_OK) ? loc_err : err;
  523. if (loc_err == ESP_OK) {
  524. if (create) button_create((void*) cur_config, cur_config->gpio,cur_config->type,
  525. cur_config->pull,cur_config->debounce, control_handler,
  526. cur_config->long_press, cur_config->shifter_gpio);
  527. } else {
  528. ESP_LOGE(TAG,"Error parsing button structure. Button will not be registered.");
  529. }
  530. cur_config++;
  531. }
  532. } else {
  533. ESP_LOGE(TAG,"Invalid configuration; array is expected and none received in %s ", config);
  534. }
  535. cJSON_Delete(buttons);
  536. }
  537. // Now update the global json_config object. If we are recursively processing menu structures,
  538. // the last init that completes will assigh the first json config object found, which will match
  539. // the default config from nvs.
  540. json_config = config_root;
  541. exit:
  542. free(config);
  543. return err;
  544. }
  545. /****************************************************************************************
  546. *
  547. */
  548. actrls_handler get_ctrl_handler(actrls_action_e action) {
  549. return current_controls[action];
  550. }
  551. /****************************************************************************************
  552. *
  553. */
  554. void actrls_set_default(const actrls_t controls, bool raw_controls, actrls_hook_t *hook, actrls_ir_handler_t *ir_handler) {
  555. memcpy(default_controls, controls, sizeof(actrls_t));
  556. memcpy(current_controls, default_controls, sizeof(actrls_t));
  557. default_hook = current_hook = hook;
  558. default_raw_controls = current_raw_controls = raw_controls;
  559. default_ir_handler = current_ir_handler = ir_handler;
  560. }
  561. /****************************************************************************************
  562. *
  563. */
  564. void actrls_set(const actrls_t controls, bool raw_controls, actrls_hook_t *hook, actrls_ir_handler_t *ir_handler) {
  565. memcpy(current_controls, controls, sizeof(actrls_t));
  566. current_hook = hook;
  567. current_raw_controls = raw_controls;
  568. current_ir_handler = ir_handler;
  569. }
  570. /****************************************************************************************
  571. *
  572. */
  573. void actrls_unset(void) {
  574. memcpy(current_controls, default_controls, sizeof(actrls_t));
  575. current_hook = default_hook;
  576. current_raw_controls = default_raw_controls;
  577. current_ir_handler = default_ir_handler;
  578. }