Files
squeezelite-esp32/components/services/monitor.c
philippe44 cfae996fd3 Refactoring
- Add SPI display
- Add SSD1326 (not fully tested)
- Remove all but one dependecies to HW (#define)
- Cleanup KProjectBuild
- Update .defaults
2020-02-09 00:25:50 -08:00

148 lines
4.3 KiB
C

/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "esp_system.h"
#include "esp_log.h"
#include "monitor.h"
#include "driver/gpio.h"
#include "buttons.h"
#include "led.h"
#include "globdefs.h"
#include "config.h"
#include "accessors.h"
#define MONITOR_TIMER (10*1000)
static const char *TAG = "monitor";
static TimerHandle_t monitor_timer;
static int jack_gpio = -1;
void (*jack_handler_svc)(bool inserted);
bool jack_inserted_svc(void);
void (*spkfault_handler_svc)(bool inserted);
bool spkfault_svc(void);
/****************************************************************************************
*
*/
static void monitor_callback(TimerHandle_t xTimer) {
ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
}
/****************************************************************************************
*
*/
static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
}
/****************************************************************************************
*
*/
bool jack_inserted_svc (void) {
if (jack_gpio != -1) return button_is_pressed(jack_gpio, NULL);
else return false;
}
/****************************************************************************************
*
*/
#ifdef SPKFAULT_GPIO
static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
if (event == BUTTON_PRESSED) led_on(LED_RED);
else led_off(LED_RED);
if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
}
#endif
/****************************************************************************************
*
*/
bool spkfault_svc (void) {
#ifdef SPKFAULT_GPIO
return !gpio_get_level(SPKFAULT_GPIO);
#else
return false;
#endif
}
/****************************************************************************************
*
*/
void set_jack_gpio(int gpio, char *value) {
bool low = false;
if (!strcasecmp(value, "jack_l")) {
jack_gpio = gpio;
low = true;
} else if (!strcasecmp(value, "jack_h")) {
jack_gpio = gpio;
}
if (jack_gpio != -1) {
gpio_pad_select_gpio(jack_gpio);
gpio_set_direction(jack_gpio, GPIO_MODE_INPUT);
gpio_set_pull_mode(jack_gpio, low ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY);
ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", low ? "low" : "high", gpio);
// re-use button management for jack handler, it's a GPIO after all
button_create(NULL, jack_gpio, low ? BUTTON_LOW : BUTTON_HIGH, false, 250, jack_handler_default, 0, -1);
}
}
/****************************************************************************************
*
*/
void monitor_svc_init(void) {
ESP_LOGI(TAG, "Initializing monitoring");
#ifdef CONFIG_JACK_GPIO
jack_gpio = CONFIG_JACK_GPIO;
#if CONFIG_JACK_GPIO_LEVEL == 1
set_jack_gpio(CONFIG_JACK_GPIO, "jack_h");
#else
set_jack_gpio(CONFIG_JACK_GPIO, "jack_l");
#endif
#endif
#ifndef CONFIG_JACK_LOCKED
parse_set_GPIO(set_jack_gpio);
#endif
#ifdef SPKFAULT_GPIO
gpio_pad_select_gpio(SPKFAULT_GPIO);
gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT);
gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY);
// re-use button management for speaker fault handler, it's a GPIO after all
button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1);
#endif
// do we want stats
char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
xTimerStart(monitor_timer, portMAX_DELAY);
}
free(p);
}