audio_controls.c 20 KB

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