audio_controls.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config);
  36. esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config);
  37. esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  38. esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  39. esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
  40. 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
  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. actrls_action_e actrls_parse_action_json(const char * name){
  122. for(int i=0;i<ACTRLS_MAX && actrls_action_s[i][0]!='\0' ;i++){
  123. if(!strcmp(actrls_action_s[i], name)){
  124. return (actrls_action_e) i;
  125. }
  126. }
  127. return ACTRLS_NONE;
  128. }
  129. esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config,uint32_t offset){
  130. esp_err_t err = ESP_OK;
  131. ESP_LOGD(TAG,"Processing int member");
  132. int *value = (int*)((char*) cur_config + offset);
  133. *value = member->valueint;
  134. return err;
  135. }
  136. esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  137. esp_err_t err = ESP_OK;
  138. ESP_LOGD(TAG,"Processing type member");
  139. int *value = (int *)((char*) cur_config + offset);
  140. if (member->type == cJSON_String) {
  141. *value =
  142. !strcmp(member->valuestring,
  143. "BUTTON_LOW") ?
  144. BUTTON_LOW : BUTTON_HIGH;
  145. } else {
  146. ESP_LOGE(TAG,
  147. "Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
  148. err=ESP_FAIL;
  149. }
  150. return err;
  151. }
  152. esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  153. esp_err_t err = ESP_OK;
  154. if(!member) {
  155. ESP_LOGE(TAG,"Null json member pointer!");
  156. err = ESP_FAIL;
  157. }
  158. else {
  159. ESP_LOGD(TAG,"Processing bool member ");
  160. if(cJSON_IsBool(member)){
  161. bool*value = (bool*)((char*) cur_config + offset);
  162. *value = cJSON_IsTrue(member);
  163. }
  164. else {
  165. ESP_LOGE(TAG,"Member %s is not a boolean", member->string?member->string:"unknown");
  166. err = ESP_FAIL;
  167. }
  168. }
  169. return err;
  170. }
  171. esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
  172. esp_err_t err = ESP_OK;
  173. cJSON * button_action= cJSON_GetObjectItemCaseSensitive(member, "pressed");
  174. actrls_action_e*value = (actrls_action_e*)((char *)cur_config + offset);
  175. if (button_action != NULL) {
  176. value[0] = actrls_parse_action_json( button_action->valuestring);
  177. }
  178. else{
  179. ESP_LOGW(TAG,"Action pressed not found in json structure");
  180. err = ESP_FAIL;
  181. }
  182. button_action = cJSON_GetObjectItemCaseSensitive(member, "released");
  183. if (button_action != NULL) {
  184. value[1] = actrls_parse_action_json( button_action->valuestring);
  185. }
  186. else{
  187. ESP_LOGW(TAG,"Action released not found in json structure");
  188. err = ESP_FAIL;
  189. }
  190. return err;
  191. }
  192. esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config) {
  193. esp_err_t err = ESP_OK;
  194. const actrls_config_map_t * h=actrls_config_map;
  195. char * str = cJSON_Print(member);
  196. while (h->handler && strcmp(member->string, h->member)) { h++; }
  197. if (h->handler) {
  198. ESP_LOGD(TAG,"found handler for member %s, json value %s", h->member,str?str:"");
  199. err = h->handler(member, cur_config, h->offset);
  200. } else {
  201. err = ESP_FAIL;
  202. ESP_LOGE(TAG, "Unknown json structure member : %s", str?str:"");
  203. }
  204. if(str) free(str);
  205. return err;
  206. }
  207. esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config) {
  208. esp_err_t err= ESP_OK;
  209. const cJSON *member;
  210. cJSON_ArrayForEach(member, button)
  211. {
  212. ESP_LOGD(TAG,"Processing member %s. ", member->string);
  213. esp_err_t loc_err = actrls_process_member(member, cur_config);
  214. err = (err == ESP_OK) ? loc_err : err;
  215. }
  216. return err;
  217. }
  218. actrls_config_t * actrls_init_alloc_structure(const cJSON *buttons){
  219. int member_count = 0;
  220. const cJSON *button;
  221. actrls_config_t * json_config=NULL;
  222. ESP_LOGD(TAG,"Counting the number of buttons definition");
  223. cJSON_ArrayForEach(button, buttons) {
  224. member_count++;
  225. }
  226. ESP_LOGD(TAG, "config contains %u button definitions",
  227. member_count);
  228. if (member_count != 0) {
  229. json_config = malloc(sizeof(actrls_config_t) * member_count);
  230. if(json_config){
  231. memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
  232. }
  233. else {
  234. ESP_LOGE(TAG,"Unable to allocate memory to hold configuration for %u buttons ",member_count);
  235. }
  236. }
  237. else {
  238. ESP_LOGE(TAG,"No button found in configuration structure");
  239. }
  240. return json_config;
  241. }
  242. /****************************************************************************************
  243. *
  244. */
  245. esp_err_t actrls_init_json(const char *config) {
  246. esp_err_t err = ESP_OK;
  247. actrls_config_t *cur_config;
  248. const cJSON *button;
  249. ESP_LOGD(TAG,"Parsing JSON structure ");
  250. cJSON *buttons = cJSON_Parse(config);
  251. if (buttons == NULL) {
  252. ESP_LOGE(TAG,"JSON Parsing failed for %s", config);
  253. err = ESP_FAIL;
  254. }
  255. else {
  256. ESP_LOGD(TAG,"Json parsing completed");
  257. if (cJSON_IsArray(buttons)) {
  258. ESP_LOGD(TAG,"configuration is an array as expected");
  259. cur_config = json_config = actrls_init_alloc_structure(buttons);
  260. if(!cur_config) {
  261. ESP_LOGE(TAG,"Config buffer was empty. ");
  262. cJSON_Delete(buttons);
  263. return ESP_FAIL;
  264. }
  265. ESP_LOGD(TAG,"Processing button definitions. ");
  266. cJSON_ArrayForEach(button, buttons){
  267. char * str = cJSON_Print(button);
  268. ESP_LOGD(TAG,"Processing %s. ", str?str:"");
  269. if(str){
  270. free(str);
  271. }
  272. esp_err_t loc_err = actrls_process_button(button, cur_config);
  273. err = (err == ESP_OK) ? loc_err : err;
  274. if (loc_err == ESP_OK) {
  275. button_create((void*) cur_config, cur_config->gpio,cur_config->type, cur_config->pull,cur_config->debounce,
  276. control_handler, cur_config->long_press, cur_config->shifter_gpio);
  277. }
  278. else{
  279. ESP_LOGE(TAG,"Error parsing button structure. Button will not be registered.");
  280. }
  281. cur_config++;
  282. }
  283. }
  284. else {
  285. ESP_LOGE(TAG,"Invalid configuration; array is expected and none received in %s ", config);
  286. }
  287. cJSON_Delete(buttons);
  288. }
  289. return err;
  290. }
  291. /****************************************************************************************
  292. *
  293. */
  294. void actrls_set_default(const actrls_t controls) {
  295. memcpy(default_controls, controls, sizeof(actrls_t));
  296. memcpy(current_controls, default_controls, sizeof(actrls_t));
  297. }
  298. /****************************************************************************************
  299. *
  300. */
  301. void actrls_set(const actrls_t controls) {
  302. memcpy(current_controls, controls, sizeof(actrls_t));
  303. }
  304. /****************************************************************************************
  305. *
  306. */
  307. void actrls_unset(void) {
  308. memcpy(current_controls, default_controls, sizeof(actrls_t));
  309. }