audio_controls.c 15 KB

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