audio_controls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "audio_controls.h"
  29. typedef esp_err_t (actrls_config_map_handler) (const cJSON * member, actrls_config_t *cur_config,uint32_t offset);
  30. typedef struct {
  31. char * member;
  32. uint32_t offset;
  33. actrls_config_map_handler * handler;
  34. } actrls_config_map_t;
  35. static esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config);
  36. static esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config);
  37. static esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  38. static esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  39. static esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  40. static esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  41. static const actrls_config_map_t actrls_config_map[] =
  42. {
  43. {"gpio", offsetof(actrls_config_t,gpio), actrls_process_int},
  44. {"debounce", offsetof(actrls_config_t,debounce), actrls_process_int},
  45. {"type", offsetof(actrls_config_t,type), actrls_process_type},
  46. {"pull", offsetof(actrls_config_t,pull), actrls_process_bool},
  47. {"long_press", offsetof(actrls_config_t,long_press),actrls_process_int},
  48. {"shifter_gpio", offsetof(actrls_config_t,shifter_gpio), actrls_process_int},
  49. {"normal", offsetof(actrls_config_t,normal), actrls_process_action},
  50. {"shifted", offsetof(actrls_config_t,shifted), actrls_process_action},
  51. {"longpress", offsetof(actrls_config_t,longpress), actrls_process_action},
  52. {"longshifted", offsetof(actrls_config_t,longshifted), actrls_process_action},
  53. {"", 0, NULL}
  54. };
  55. // BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
  56. #define EP(x) [x] = #x /* ENUM PRINT */
  57. static const char * actrls_action_s[ ] = { EP(ACTRLS_VOLUP),EP(ACTRLS_VOLDOWN),EP(ACTRLS_TOGGLE),EP(ACTRLS_PLAY),
  58. EP(ACTRLS_PAUSE),EP(ACTRLS_STOP),EP(ACTRLS_REW),EP(ACTRLS_FWD),EP(ACTRLS_PREV),EP(ACTRLS_NEXT),
  59. EP(BCTRLS_PUSH), EP(BCTRLS_UP),EP(BCTRLS_DOWN),EP(BCTRLS_LEFT),EP(BCTRLS_RIGHT), ""} ;
  60. static const char * TAG = "audio controls";
  61. static actrls_config_t *json_config;
  62. static actrls_t default_controls, current_controls;
  63. static void control_handler(void *client, button_event_e event, button_press_e press, bool long_press) {
  64. actrls_config_t *key = (actrls_config_t*) client;
  65. actrls_action_e action;
  66. switch(press) {
  67. case BUTTON_NORMAL:
  68. if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1].action;
  69. else action = key->normal[event == BUTTON_PRESSED ? 0 : 1].action;
  70. break;
  71. case BUTTON_SHIFTED:
  72. if (long_press) action = key->longshifted[event == BUTTON_PRESSED ? 0 : 1].action;
  73. else action = key->shifted[event == BUTTON_PRESSED ? 0 : 1].action;
  74. break;
  75. default:
  76. action = ACTRLS_NONE;
  77. break;
  78. }
  79. ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u action:%u", key->gpio, press, long_press, event, action);
  80. if (action > ACTRLS_MAX) {
  81. // need to do the remap here using button_remap
  82. ESP_LOGD(TAG, "remapping buttons");
  83. } else if (action != ACTRLS_NONE) {
  84. ESP_LOGD(TAG, "calling action %u", action);
  85. if (current_controls[action]) (*current_controls[action])();
  86. }
  87. }
  88. /*
  89. void up(void *id, button_event_e event, button_press_e press, bool longpress) {
  90. if (press == BUTTON_NORMAL) {
  91. if (longpress) ESP_LOGI(TAG, "up long %u", event);
  92. else ESP_LOGI(TAG, "up %u", event);
  93. } else if (press == BUTTON_SHIFTED) {
  94. if (longpress) ESP_LOGI(TAG, "up shifted long %u", event);
  95. else ESP_LOGI(TAG, "up shifted %u", event);
  96. } else {
  97. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  98. }
  99. }
  100. void down(void *id, button_event_e event, button_press_e press, bool longpress) {
  101. if (press == BUTTON_NORMAL) {
  102. if (longpress) ESP_LOGI(TAG, "down long %u", event);
  103. else ESP_LOGI(TAG, "down %u", event);
  104. } else if (press == BUTTON_SHIFTED) {
  105. if (longpress) ESP_LOGI(TAG, "down shifted long %u", event);
  106. else ESP_LOGI(TAG, "down shifted %u", event);
  107. } else {
  108. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  109. }
  110. }
  111. */
  112. /****************************************************************************************
  113. *
  114. */
  115. esp_err_t actrls_init(int n, const actrls_config_t *config) {
  116. for (int i = 0; i < n; i++) {
  117. 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);
  118. }
  119. return ESP_OK;
  120. }
  121. /****************************************************************************************
  122. *
  123. */
  124. static actrls_action_e actrls_parse_action_json(const char * name){
  125. for(int i=0;i<ACTRLS_MAX && actrls_action_s[i][0]!='\0' ;i++){
  126. if(!strcmp(actrls_action_s[i], name)){
  127. return (actrls_action_e) i;
  128. }
  129. }
  130. return ACTRLS_NONE;
  131. }
  132. /****************************************************************************************
  133. *
  134. */
  135. static esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config,uint32_t offset){
  136. esp_err_t err = ESP_OK;
  137. ESP_LOGD(TAG,"Processing int member");
  138. int *value = (int*)((char*) cur_config + offset);
  139. *value = member->valueint;
  140. return err;
  141. }
  142. /****************************************************************************************
  143. *
  144. */
  145. static esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  146. esp_err_t err = ESP_OK;
  147. ESP_LOGD(TAG,"Processing type member");
  148. int *value = (int *)((char*) cur_config + offset);
  149. if (member->type == cJSON_String) {
  150. *value =
  151. !strcmp(member->valuestring,
  152. "BUTTON_LOW") ?
  153. BUTTON_LOW : BUTTON_HIGH;
  154. } else {
  155. ESP_LOGE(TAG,
  156. "Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
  157. err=ESP_FAIL;
  158. }
  159. return err;
  160. }
  161. /****************************************************************************************
  162. *
  163. */
  164. static esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  165. esp_err_t err = ESP_OK;
  166. if(!member) {
  167. ESP_LOGE(TAG,"Null json member pointer!");
  168. err = ESP_FAIL;
  169. }
  170. else {
  171. ESP_LOGD(TAG,"Processing bool member ");
  172. if(cJSON_IsBool(member)){
  173. bool*value = (bool*)((char*) cur_config + offset);
  174. *value = cJSON_IsTrue(member);
  175. }
  176. else {
  177. ESP_LOGE(TAG,"Member %s is not a boolean", member->string?member->string:"unknown");
  178. err = ESP_FAIL;
  179. }
  180. }
  181. return err;
  182. }
  183. /****************************************************************************************
  184. *
  185. */
  186. static esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  187. esp_err_t err = ESP_OK;
  188. cJSON * button_action= cJSON_GetObjectItemCaseSensitive(member, "pressed");
  189. actrls_action_e*value = (actrls_action_e*)((char *)cur_config + offset);
  190. if (button_action != NULL) {
  191. value[0] = actrls_parse_action_json( button_action->valuestring);
  192. }
  193. else{
  194. ESP_LOGW(TAG,"Action pressed not found in json structure");
  195. err = ESP_FAIL;
  196. }
  197. button_action = cJSON_GetObjectItemCaseSensitive(member, "released");
  198. if (button_action != NULL) {
  199. value[1] = actrls_parse_action_json( button_action->valuestring);
  200. }
  201. else{
  202. ESP_LOGW(TAG,"Action released not found in json structure");
  203. err = ESP_FAIL;
  204. }
  205. return err;
  206. }
  207. /****************************************************************************************
  208. *
  209. */
  210. static esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config) {
  211. esp_err_t err = ESP_OK;
  212. const actrls_config_map_t * h=actrls_config_map;
  213. char * str = cJSON_Print(member);
  214. while (h->handler && strcmp(member->string, h->member)) { h++; }
  215. if (h->handler) {
  216. ESP_LOGD(TAG,"found handler for member %s, json value %s", h->member,str?str:"");
  217. err = h->handler(member, cur_config, h->offset);
  218. } else {
  219. err = ESP_FAIL;
  220. ESP_LOGE(TAG, "Unknown json structure member : %s", str?str:"");
  221. }
  222. if(str) free(str);
  223. return err;
  224. }
  225. /****************************************************************************************
  226. *
  227. */
  228. static esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config) {
  229. esp_err_t err= ESP_OK;
  230. const cJSON *member;
  231. cJSON_ArrayForEach(member, button)
  232. {
  233. ESP_LOGD(TAG,"Processing member %s. ", member->string);
  234. esp_err_t loc_err = actrls_process_member(member, cur_config);
  235. err = (err == ESP_OK) ? loc_err : err;
  236. }
  237. return err;
  238. }
  239. /****************************************************************************************
  240. *
  241. */
  242. static actrls_config_t * actrls_init_alloc_structure(const cJSON *buttons){
  243. int member_count = 0;
  244. const cJSON *button;
  245. actrls_config_t * json_config=NULL;
  246. ESP_LOGD(TAG,"Counting the number of buttons definition");
  247. cJSON_ArrayForEach(button, buttons) {
  248. member_count++;
  249. }
  250. ESP_LOGD(TAG, "config contains %u button definitions",
  251. member_count);
  252. if (member_count != 0) {
  253. json_config = malloc(sizeof(actrls_config_t) * member_count);
  254. if(json_config){
  255. memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
  256. }
  257. else {
  258. ESP_LOGE(TAG,"Unable to allocate memory to hold configuration for %u buttons ",member_count);
  259. }
  260. }
  261. else {
  262. ESP_LOGE(TAG,"No button found in configuration structure");
  263. }
  264. return json_config;
  265. }
  266. /****************************************************************************************
  267. *
  268. */
  269. esp_err_t actrls_init_json(const char *config) {
  270. esp_err_t err = ESP_OK;
  271. actrls_config_t *cur_config;
  272. const cJSON *button;
  273. ESP_LOGD(TAG,"Parsing JSON structure ");
  274. cJSON *buttons = cJSON_Parse(config);
  275. if (buttons == NULL) {
  276. ESP_LOGE(TAG,"JSON Parsing failed for %s", config);
  277. err = ESP_FAIL;
  278. }
  279. else {
  280. ESP_LOGD(TAG,"Json parsing completed");
  281. if (cJSON_IsArray(buttons)) {
  282. ESP_LOGD(TAG,"configuration is an array as expected");
  283. cur_config = json_config = actrls_init_alloc_structure(buttons);
  284. if(!cur_config) {
  285. ESP_LOGE(TAG,"Config buffer was empty. ");
  286. cJSON_Delete(buttons);
  287. return ESP_FAIL;
  288. }
  289. ESP_LOGD(TAG,"Processing button definitions. ");
  290. cJSON_ArrayForEach(button, buttons){
  291. char * str = cJSON_Print(button);
  292. ESP_LOGD(TAG,"Processing %s. ", str?str:"");
  293. if(str){
  294. free(str);
  295. }
  296. esp_err_t loc_err = actrls_process_button(button, cur_config);
  297. err = (err == ESP_OK) ? loc_err : err;
  298. if (loc_err == ESP_OK) {
  299. button_create((void*) cur_config, cur_config->gpio,cur_config->type, cur_config->pull,cur_config->debounce,
  300. control_handler, cur_config->long_press, cur_config->shifter_gpio);
  301. }
  302. else{
  303. ESP_LOGE(TAG,"Error parsing button structure. Button will not be registered.");
  304. }
  305. cur_config++;
  306. }
  307. }
  308. else {
  309. ESP_LOGE(TAG,"Invalid configuration; array is expected and none received in %s ", config);
  310. }
  311. cJSON_Delete(buttons);
  312. }
  313. return err;
  314. }
  315. /****************************************************************************************
  316. *
  317. */
  318. void actrls_set_default(const actrls_t controls) {
  319. memcpy(default_controls, controls, sizeof(actrls_t));
  320. memcpy(current_controls, default_controls, sizeof(actrls_t));
  321. }
  322. /****************************************************************************************
  323. *
  324. */
  325. void actrls_set(const actrls_t controls) {
  326. memcpy(current_controls, controls, sizeof(actrls_t));
  327. }
  328. /****************************************************************************************
  329. *
  330. */
  331. void actrls_unset(void) {
  332. memcpy(current_controls, default_controls, sizeof(actrls_t));
  333. }