audio_controls.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 enum {
  30. ACTRLS_MAP_INT, ACTRLS_MAP_BOOL, ACTRLS_MAP_ACTION,ACTRLS_MAP_TYPE,ACTRLS_MAP_END
  31. } actrls_action_map_element_type_e;
  32. typedef struct {
  33. char * member;
  34. uint32_t offset;
  35. actrls_action_map_element_type_e type;
  36. } actrls_config_map_t;
  37. static const actrls_config_map_t actrls_config_map[] =
  38. {
  39. {"gpio", offsetof(actrls_config_t,gpio), ACTRLS_MAP_INT},
  40. {"debounce", offsetof(actrls_config_t,debounce), ACTRLS_MAP_INT},
  41. {"type", offsetof(actrls_config_t,type),ACTRLS_MAP_TYPE},
  42. {"pull", offsetof(actrls_config_t,pull),ACTRLS_MAP_BOOL},
  43. {"long_press", offsetof(actrls_config_t,long_press),ACTRLS_MAP_INT},
  44. {"shifter_gpio", offsetof(actrls_config_t,shifter_gpio),ACTRLS_MAP_INT},
  45. {"normal", offsetof(actrls_config_t,normal), ACTRLS_MAP_ACTION},
  46. {"shifted", offsetof(actrls_config_t,shifted), ACTRLS_MAP_ACTION},
  47. {"longpress", offsetof(actrls_config_t,longpress), ACTRLS_MAP_ACTION},
  48. {"longshifted", offsetof(actrls_config_t,longshifted), ACTRLS_MAP_ACTION},
  49. {"", 0,ACTRLS_MAP_END}
  50. };
  51. // BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
  52. static const char * actrls_action_s[ ] = { "ACTRLS_VOLUP","ACTRLS_VOLDOWN","ACTRLS_TOGGLE","ACTRLS_PLAY",
  53. "ACTRLS_PAUSE","ACTRLS_STOP","ACTRLS_REW","ACTRLS_FWD","ACTRLS_PREV","ACTRLS_NEXT",
  54. "BCTRLS_PUSH", "BCTRLS_UP","BCTRLS_DOWN","BCTRLS_LEFT","BCTRLS_RIGHT", ""} ;
  55. static const char * TAG = "audio controls";
  56. static actrls_config_t *json_config;
  57. static actrls_t default_controls, current_controls;
  58. static void control_handler(void *id, button_event_e event, button_press_e press, bool long_press) {
  59. actrls_config_t *key = (actrls_config_t*) id;
  60. actrls_action_e action;
  61. switch(press) {
  62. case BUTTON_NORMAL:
  63. if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
  64. else action = key->normal[event == BUTTON_PRESSED ? 0 : 1];
  65. break;
  66. case BUTTON_SHIFTED:
  67. if (long_press) action = key->longshifted[event == BUTTON_PRESSED ? 0 : 1];
  68. else action = key->shifted[event == BUTTON_PRESSED ? 0 : 1];
  69. break;
  70. default:
  71. action = ACTRLS_NONE;
  72. break;
  73. }
  74. ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u action:%u", key->gpio, press, long_press, event, action);
  75. if (action != ACTRLS_NONE) {
  76. ESP_LOGD(TAG, " calling action %u", action);
  77. if (current_controls[action]) (*current_controls[action])();
  78. }
  79. }
  80. /*
  81. void up(void *id, button_event_e event, button_press_e press, bool longpress) {
  82. if (press == BUTTON_NORMAL) {
  83. if (longpress) ESP_LOGI(TAG, "up long %u", event);
  84. else ESP_LOGI(TAG, "up %u", event);
  85. } else if (press == BUTTON_SHIFTED) {
  86. if (longpress) ESP_LOGI(TAG, "up shifted long %u", event);
  87. else ESP_LOGI(TAG, "up shifted %u", event);
  88. } else {
  89. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  90. }
  91. }
  92. void down(void *id, button_event_e event, button_press_e press, bool longpress) {
  93. if (press == BUTTON_NORMAL) {
  94. if (longpress) ESP_LOGI(TAG, "down long %u", event);
  95. else ESP_LOGI(TAG, "down %u", event);
  96. } else if (press == BUTTON_SHIFTED) {
  97. if (longpress) ESP_LOGI(TAG, "down shifted long %u", event);
  98. else ESP_LOGI(TAG, "down shifted %u", event);
  99. } else {
  100. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  101. }
  102. }
  103. */
  104. /****************************************************************************************
  105. *
  106. */
  107. esp_err_t actrls_init(int n, const actrls_config_t *config) {
  108. for (int i = 0; i < n; i++) {
  109. 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);
  110. }
  111. return ESP_OK;
  112. }
  113. actrls_action_e actrls_parse_action_json(const char * name){
  114. for(int i=0;actrls_action_s[i][0]!='\0' && i<ACTRLS_MAX;i++){
  115. if(!strcmp(actrls_action_s[i], name)){
  116. return (actrls_action_e) i;
  117. }
  118. }
  119. return ACTRLS_NONE;
  120. }
  121. esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_config){
  122. const actrls_config_map_t * map=NULL;
  123. esp_err_t err=ESP_OK;
  124. char *config_pointer = (char*) cur_config;
  125. cJSON *button_action;
  126. char * string = cJSON_Print(member);
  127. ESP_LOGD(TAG, "Processing structure member json : %s", string);
  128. free(string);
  129. for(int i = 0;!map && actrls_config_map[i].type!=ACTRLS_MAP_END ;i++){
  130. if(!strcmp(member->string, actrls_config_map[i].member)){
  131. map = &actrls_config_map[i];
  132. }
  133. }
  134. if(!map){
  135. ESP_LOGE(TAG,"Unknown structure member [%s]", member->string?member->string:"");
  136. return ESP_FAIL;
  137. }
  138. ESP_LOGD(TAG,
  139. "Found map type %d at structure offset %u",
  140. map->type, map->offset);
  141. void *value = (config_pointer + map->offset);
  142. switch (map->type) {
  143. case ACTRLS_MAP_TYPE:
  144. if (member->type == cJSON_String) {
  145. *(int*) value =
  146. !strcmp(member->valuestring,
  147. "BUTTON_LOW") ?
  148. BUTTON_LOW : BUTTON_HIGH;
  149. } else {
  150. ESP_LOGE(TAG,
  151. "Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
  152. }
  153. break;
  154. case ACTRLS_MAP_INT:
  155. *(int*) value = member->valueint;
  156. break;
  157. case ACTRLS_MAP_BOOL:
  158. *(bool*) value = cJSON_IsTrue(member);
  159. break;
  160. case ACTRLS_MAP_ACTION:
  161. button_action= cJSON_GetObjectItemCaseSensitive(
  162. member, "pressed");
  163. if (button_action != NULL) {
  164. ((actrls_action_e*) value)[0] =
  165. actrls_parse_action_json(
  166. button_action->valuestring);
  167. }
  168. button_action = cJSON_GetObjectItemCaseSensitive(
  169. member, "released");
  170. if (button_action != NULL) {
  171. ((actrls_action_e*) value)[1] =
  172. actrls_parse_action_json(
  173. button_action->valuestring);
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. return err;
  180. }
  181. /****************************************************************************************
  182. *
  183. */
  184. esp_err_t actrls_init_json(const char *config) {
  185. esp_err_t err = ESP_OK;
  186. actrls_config_t *cur_config;
  187. cJSON *buttons = cJSON_Parse(config);
  188. if (buttons != NULL) {
  189. if (cJSON_IsArray(buttons)) {
  190. int member_count = 0;
  191. const cJSON *button;
  192. const cJSON *member;
  193. cJSON_ArrayForEach(button, buttons)
  194. {
  195. member_count++;
  196. }
  197. ESP_LOGD(TAG, "config contains %u button definitions",
  198. member_count);
  199. if (member_count == 0) {
  200. return ESP_FAIL;
  201. }
  202. json_config = malloc(sizeof(actrls_config_t) * member_count);
  203. memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
  204. cur_config = json_config;
  205. cJSON_ArrayForEach(button, buttons)
  206. {
  207. esp_err_t loc_err = ESP_OK;
  208. cJSON_ArrayForEach(member, button)
  209. {
  210. actrls_parse_config_map(member, cur_config);
  211. err = err == ESP_OK ? loc_err : err;
  212. }
  213. if (loc_err == ESP_OK) {
  214. button_create((void*) cur_config, cur_config->gpio,
  215. cur_config->type, cur_config->pull,cur_config->debounce,
  216. control_handler, cur_config->long_press,
  217. cur_config->shifter_gpio);
  218. }
  219. cur_config++;
  220. }
  221. }
  222. cJSON_Delete(buttons);
  223. } else {
  224. err = ESP_FAIL;
  225. }
  226. return err;
  227. }
  228. /****************************************************************************************
  229. *
  230. */
  231. void actrls_set_default(const actrls_t controls) {
  232. memcpy(default_controls, controls, sizeof(actrls_t));
  233. memcpy(current_controls, default_controls, sizeof(actrls_t));
  234. }
  235. /****************************************************************************************
  236. *
  237. */
  238. void actrls_set(const actrls_t controls) {
  239. memcpy(current_controls, controls, sizeof(actrls_t));
  240. }
  241. /****************************************************************************************
  242. *
  243. */
  244. void actrls_unset(void) {
  245. memcpy(current_controls, default_controls, sizeof(actrls_t));
  246. }