audio_controls.c 16 KB

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