monitor.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "led.h"
  19. #ifdef CONFIG_SQUEEZEAMP
  20. #define JACK_GPIO 34
  21. #define SPKFAULT_GPIO 2
  22. #endif
  23. #define MONITOR_TIMER (10*1000)
  24. static const char TAG[] = "monitor";
  25. static TimerHandle_t monitor_timer;
  26. void (*jack_handler_svc)(bool inserted);
  27. bool jack_inserted_svc(void);
  28. void (*spkfault_handler_svc)(bool inserted);
  29. bool spkfault_svc(void);
  30. /****************************************************************************************
  31. *
  32. */
  33. static void monitor_callback(TimerHandle_t xTimer) {
  34. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  35. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  36. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  37. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  38. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  39. }
  40. /****************************************************************************************
  41. *
  42. */
  43. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  44. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  45. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  46. }
  47. /****************************************************************************************
  48. *
  49. */
  50. bool jack_inserted_svc (void) {
  51. #ifdef JACK_GPIO
  52. return !gpio_get_level(JACK_GPIO);
  53. #else
  54. return false;
  55. #endif
  56. }
  57. /****************************************************************************************
  58. *
  59. */
  60. static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  61. ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
  62. if (event == BUTTON_PRESSED) led_on(LED_RED);
  63. else led_off(LED_RED);
  64. if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
  65. }
  66. /****************************************************************************************
  67. *
  68. */
  69. bool spkfault_svc (void) {
  70. #ifdef SPKFAULT_GPIO
  71. return !gpio_get_level(SPKFAULT_GPIO);
  72. #else
  73. return false;
  74. #endif
  75. }
  76. #include "driver/rtc_io.h"
  77. /****************************************************************************************
  78. *
  79. */
  80. void monitor_svc_init(void) {
  81. ESP_LOGI(TAG, "Initializing monitoring");
  82. #ifdef JACK_GPIO
  83. gpio_pad_select_gpio(JACK_GPIO);
  84. gpio_set_direction(JACK_GPIO, GPIO_MODE_INPUT);
  85. // re-use button management for jack handler, it's a GPIO after all
  86. button_create(NULL, JACK_GPIO, BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  87. #endif
  88. #ifdef SPKFAULT_GPIO
  89. gpio_pad_select_gpio(SPKFAULT_GPIO);
  90. ESP_LOGI(TAG, "DIR %d", gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT));
  91. ESP_LOGI(TAG, "PULLUP %d", gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY));
  92. // re-use button management for speaker fault handler, it's a GPIO after all
  93. button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1);
  94. #endif
  95. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  96. xTimerStart(monitor_timer, portMAX_DELAY);
  97. }