monitor.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #define MONITOR_TIMER (10*1000)
  23. static const char *TAG = "monitor";
  24. static TimerHandle_t monitor_timer;
  25. static struct {
  26. int gpio;
  27. int active;
  28. } jack = { CONFIG_JACK_GPIO, 0 },
  29. spkfault = { CONFIG_SPKFAULT_GPIO, 0 };
  30. void (*jack_handler_svc)(bool inserted);
  31. bool jack_inserted_svc(void);
  32. void (*spkfault_handler_svc)(bool inserted);
  33. bool spkfault_svc(void);
  34. /****************************************************************************************
  35. *
  36. */
  37. static void monitor_callback(TimerHandle_t xTimer) {
  38. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  39. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  40. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  41. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  42. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  43. }
  44. /****************************************************************************************
  45. *
  46. */
  47. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  48. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  49. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  50. }
  51. /****************************************************************************************
  52. *
  53. */
  54. bool jack_inserted_svc (void) {
  55. return button_is_pressed(jack.gpio, NULL);
  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. return button_is_pressed(spkfault.gpio, NULL);
  71. }
  72. /****************************************************************************************
  73. *
  74. */
  75. void set_jack_gpio(int gpio, char *value) {
  76. if (strcasestr(value, "jack")) {
  77. char *p;
  78. jack.gpio = gpio;
  79. if ((p = strchr(value, ':')) != NULL) jack.active = atoi(p + 1);
  80. }
  81. }
  82. /****************************************************************************************
  83. *
  84. */
  85. void set_spkfault_gpio(int gpio, char *value) {
  86. if (strcasestr(value, "spkfault")) {
  87. char *p;
  88. spkfault.gpio = gpio;
  89. if ((p = strchr(value, ':')) != NULL) spkfault.active = atoi(p + 1);
  90. }
  91. }
  92. /****************************************************************************************
  93. *
  94. */
  95. void monitor_svc_init(void) {
  96. ESP_LOGI(TAG, "Initializing monitoring");
  97. #ifdef CONFIG_JACK_GPIO_LEVEL
  98. jack.active = CONFIG_JACK_GPIO_LEVEL;
  99. #endif
  100. #ifndef CONFIG_JACK_LOCKED
  101. parse_set_GPIO(set_jack_gpio);
  102. #endif
  103. // re-use button management for jack handler, it's a GPIO after all
  104. if (jack.gpio != -1) {
  105. ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", jack.active ? "high" : "low", jack.gpio);
  106. button_create(NULL, jack.gpio, jack.active ? BUTTON_HIGH : BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  107. }
  108. #ifdef CONFIG_SPKFAULT_GPIO_LEVEL
  109. spkfault.active = CONFIG_SPKFAULT_GPIO_LEVEL;
  110. #endif
  111. #ifndef CONFIG_SPKFAULT_LOCKED
  112. parse_set_GPIO(set_spkfault_gpio);
  113. #endif
  114. // re-use button management for speaker fault handler, it's a GPIO after all
  115. if (spkfault.gpio != -1) {
  116. ESP_LOGI(TAG,"Adding speaker fault (%s) detection GPIO %d", spkfault.active ? "high" : "low", spkfault.gpio);
  117. button_create(NULL, spkfault.gpio, spkfault.active ? BUTTON_HIGH : BUTTON_LOW, false, 0, spkfault_handler_default, 0, -1);
  118. }
  119. // do we want stats
  120. char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
  121. if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
  122. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  123. xTimerStart(monitor_timer, portMAX_DELAY);
  124. }
  125. free(p);
  126. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  127. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  128. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  129. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  130. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  131. }