audio_controls.c 19 KB

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