monitor.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "globdefs.h"
  20. #include "config.h"
  21. #include "accessors.h"
  22. #include "accessors.h"
  23. #define MONITOR_TIMER (10*1000)
  24. static const char *TAG = "monitor";
  25. static TimerHandle_t monitor_timer;
  26. #ifdef JACK_GPIO
  27. static int jack_gpio = JACK_GPIO;
  28. #else
  29. static int jack_gpio = -1;
  30. #endif
  31. void (*jack_handler_svc)(bool inserted);
  32. bool jack_inserted_svc(void);
  33. void (*spkfault_handler_svc)(bool inserted);
  34. bool spkfault_svc(void);
  35. /****************************************************************************************
  36. *
  37. */
  38. static void monitor_callback(TimerHandle_t xTimer) {
  39. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  40. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  41. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  42. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  43. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  44. }
  45. /****************************************************************************************
  46. *
  47. */
  48. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  49. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  50. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  51. }
  52. /****************************************************************************************
  53. *
  54. */
  55. bool jack_inserted_svc (void) {
  56. if (jack_gpio != -1) return button_is_pressed(jack_gpio, NULL);
  57. else return false;
  58. }
  59. /****************************************************************************************
  60. *
  61. */
  62. static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  63. ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
  64. if (event == BUTTON_PRESSED) led_on(LED_RED);
  65. else led_off(LED_RED);
  66. if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
  67. }
  68. /****************************************************************************************
  69. *
  70. */
  71. bool spkfault_svc (void) {
  72. #ifdef SPKFAULT_GPIO
  73. return !gpio_get_level(SPKFAULT_GPIO);
  74. #else
  75. return false;
  76. #endif
  77. }
  78. /****************************************************************************************
  79. *
  80. */
  81. void set_jack_gpio(int gpio, char *value) {
  82. bool low = false;
  83. if (!strcasecmp(value, "jack_l")) {
  84. jack_gpio = gpio;
  85. low = true;
  86. } else if (!strcasecmp(value, "jack_h")) {
  87. jack_gpio = gpio;
  88. }
  89. if (jack_gpio != -1) {
  90. gpio_pad_select_gpio(jack_gpio);
  91. gpio_set_direction(jack_gpio, GPIO_MODE_INPUT);
  92. gpio_set_pull_mode(jack_gpio, low ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY);
  93. ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", low ? "low" : "high", gpio);
  94. // re-use button management for jack handler, it's a GPIO after all
  95. button_create(NULL, jack_gpio, low ? BUTTON_LOW : BUTTON_HIGH, false, 250, jack_handler_default, 0, -1);
  96. }
  97. }
  98. /****************************************************************************************
  99. *
  100. */
  101. void monitor_svc_init(void) {
  102. ESP_LOGI(TAG, "Initializing monitoring");
  103. // if JACK_GPIO is compiled-time defined set it there
  104. if (jack_gpio != -1) {
  105. #if JACK_GPIO_LEVEL == 1
  106. set_jack_gpio(JACK_GPIO, "jack_h");
  107. #else
  108. set_jack_gpio(JACK_GPIO, "jack_l");
  109. #endif
  110. } else {
  111. parse_set_GPIO(set_jack_gpio);
  112. }
  113. #ifdef SPKFAULT_GPIO
  114. gpio_pad_select_gpio(SPKFAULT_GPIO);
  115. gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT);
  116. gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY);
  117. // re-use button management for speaker fault handler, it's a GPIO after all
  118. button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1);
  119. #endif
  120. // do we want stats
  121. char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
  122. if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
  123. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  124. xTimerStart(monitor_timer, portMAX_DELAY);
  125. }
  126. free(p);
  127. }