audio_controls.c 11 KB

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