2
0

audio_controls.c 21 KB

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