monitor.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/timers.h"
  13. #include "esp_system.h"
  14. #include "esp_log.h"
  15. #include "monitor.h"
  16. #include "driver/gpio.h"
  17. #include "buttons.h"
  18. #define JACK_GPIO 34
  19. #define MONITOR_TIMER (10*1000)
  20. static const char TAG[] = "monitor";
  21. static TimerHandle_t monitor_timer;
  22. void (*jack_handler_svc)(bool inserted);
  23. bool jack_inserted_svc(void);
  24. /****************************************************************************************
  25. *
  26. */
  27. static void monitor_callback(TimerHandle_t xTimer) {
  28. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  29. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  30. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  31. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  32. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  33. }
  34. /****************************************************************************************
  35. *
  36. */
  37. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  38. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  39. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  40. }
  41. /****************************************************************************************
  42. *
  43. */
  44. bool jack_inserted_svc (void) {
  45. #ifdef JACK_GPIO
  46. return !gpio_get_level(JACK_GPIO);
  47. #else
  48. return false;
  49. #endif
  50. }
  51. /****************************************************************************************
  52. *
  53. */
  54. void monitor_svc_init(void) {
  55. ESP_LOGI(TAG, "Initializing monitoring");
  56. #ifdef JACK_GPIO
  57. gpio_pad_select_gpio(JACK_GPIO);
  58. gpio_set_direction(JACK_GPIO, GPIO_MODE_INPUT);
  59. // re-use button management for jack handler, it's a GPIO after all
  60. button_create(NULL, JACK_GPIO, BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  61. #endif
  62. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  63. xTimerStart(monitor_timer, portMAX_DELAY);
  64. }