audio_controls.c 16 KB

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