audio_controls.c 7.2 KB

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