audio_controls.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "buttons.h"
  27. #include "audio_controls.h"
  28. static const char * TAG = "audio_controls";
  29. static actrls_t default_controls, current_controls;
  30. static void control_handler(void *id, button_event_e event, button_press_e press, bool long_press) {
  31. actrls_config_t *key = (actrls_config_t*) id;
  32. actrls_action_e action;
  33. ESP_LOGD(TAG, "control gpio:%u press:%u long:%u event:%u", key->gpio, press, long_press, event);
  34. switch(press) {
  35. case BUTTON_NORMAL:
  36. if (long_press) action = key->longpress[event == BUTTON_PRESSED ? 0 : 1];
  37. else action = key->normal[event == BUTTON_PRESSED ? 0 : 1];
  38. break;
  39. case BUTTON_SHIFTED:
  40. if (long_press) action = key->longshifted[event == BUTTON_PRESSED ? 0 : 1];
  41. else action = key->shifted[event == BUTTON_PRESSED ? 0 : 1];
  42. break;
  43. default:
  44. action = ACTRLS_NONE;
  45. break;
  46. }
  47. if (action != ACTRLS_NONE) {
  48. ESP_LOGD(TAG, " calling action %u", action);
  49. if (current_controls[action]) (*current_controls[action])();
  50. }
  51. }
  52. /*
  53. void up(void *id, button_event_e event, button_press_e press, bool longpress) {
  54. if (press == BUTTON_NORMAL) {
  55. if (longpress) ESP_LOGI(TAG, "up long %u", event);
  56. else ESP_LOGI(TAG, "up %u", event);
  57. } else if (press == BUTTON_SHIFTED) {
  58. if (longpress) ESP_LOGI(TAG, "up shifted long %u", event);
  59. else ESP_LOGI(TAG, "up shifted %u", event);
  60. } else {
  61. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  62. }
  63. }
  64. void down(void *id, button_event_e event, button_press_e press, bool longpress) {
  65. if (press == BUTTON_NORMAL) {
  66. if (longpress) ESP_LOGI(TAG, "down long %u", event);
  67. else ESP_LOGI(TAG, "down %u", event);
  68. } else if (press == BUTTON_SHIFTED) {
  69. if (longpress) ESP_LOGI(TAG, "down shifted long %u", event);
  70. else ESP_LOGI(TAG, "down shifted %u", event);
  71. } else {
  72. ESP_LOGI(TAG, "don't know what we are doing here %u", event);
  73. }
  74. }
  75. */
  76. /****************************************************************************************
  77. *
  78. */
  79. void actrls_init(int n, actrls_config_t *config) {
  80. for (int i = 0; i < n; i++) {
  81. button_create(config + i, config[i].gpio, config[i].type, config[i].pull, control_handler, config[i].long_press, config[i].shifter_gpio);
  82. }
  83. }
  84. /****************************************************************************************
  85. *
  86. */
  87. void actrls_set_default(actrls_t controls) {
  88. memcpy(default_controls, controls, sizeof(actrls_t));
  89. memcpy(current_controls, default_controls, sizeof(actrls_t));
  90. }
  91. /****************************************************************************************
  92. *
  93. */
  94. void actrls_set(actrls_t controls) {
  95. memcpy(current_controls, controls, sizeof(actrls_t));
  96. }
  97. /****************************************************************************************
  98. *
  99. */
  100. void actrls_unset(void) {
  101. memcpy(current_controls, default_controls, sizeof(actrls_t));
  102. }