buttons extension + config memory leak & external allocation

This commit is contained in:
philippe44
2020-01-14 23:29:19 -08:00
parent ccf214fbe2
commit f8b5d856a3
6 changed files with 68 additions and 14 deletions

View File

@@ -69,7 +69,7 @@ void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
void * pval = config_alloc_get(nt, key);\
if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
return ESP_FAIL;}
#ifdef RECOVERY_APPLICATION
#if RECOVERY_APPLICATION==0
static void * malloc_fn(size_t sz){
void * ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
@@ -80,7 +80,7 @@ static void * malloc_fn(size_t sz){
}
static void * free_fn(void * ptr){
if(ptr!=NULL){
free(ptr);
heap_caps_free(ptr);
}
else {
ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
@@ -92,7 +92,7 @@ void init_cJSON(){
static cJSON_Hooks hooks;
// initialize cJSON hooks it uses SPIRAM memory
// as opposed to IRAM
#ifndef RECOVERY_APPLICATION
#if RECOVERY_APPLICATION==0
// In squeezelite mode, allocate memory from PSRAM. Otherwise allocate from internal RAM
// as recovery will lock flash access when erasing FLASH or writing to OTA partition.
hooks.malloc_fn=&malloc_fn;
@@ -228,6 +228,8 @@ cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, void * value
}
else {
ESP_LOGD(TAG, "Config not changed. ");
cJSON_Delete(entry);
entry = existing;
}
}
else {
@@ -697,7 +699,6 @@ esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
else {
ESP_LOGV(TAG,"config_set_value completed");
}
}
config_unlock();
return result;

View File

@@ -41,7 +41,7 @@ static int n_buttons = 0;
#define DEBOUNCE 50
static EXT_RAM_ATTR struct button_s {
void *id;
void *client;
int gpio;
int debounce;
button_handler handler;
@@ -123,18 +123,18 @@ static void buttons_task(void* arg) {
if (event == BUTTON_RELEASED) {
// early release of a long-press button, send press/release
if (!button.shifting) {
(*button.handler)(button.id, BUTTON_PRESSED, press, false);
(*button.handler)(button.id, BUTTON_RELEASED, press, false);
(*button.handler)(button.client, BUTTON_PRESSED, press, false);
(*button.handler)(button.client, BUTTON_RELEASED, press, false);
}
// button is a copy, so need to go to real context
button.self->shifting = false;
} else if (!button.shifting) {
// normal long press and not shifting so don't discard
(*button.handler)(button.id, BUTTON_PRESSED, press, true);
(*button.handler)(button.client, BUTTON_PRESSED, press, true);
}
} else {
// normal press/release of a button or release of a long-press button
if (!button.shifting) (*button.handler)(button.id, event, press, button.long_press);
if (!button.shifting) (*button.handler)(button.client, event, press, button.long_press);
// button is a copy, so need to go to real context
button.self->shifting = false;
}
@@ -151,7 +151,7 @@ void dummy_handler(void *id, button_event_e event, button_press_e press) {
/****************************************************************************************
* Create buttons
*/
void button_create(void *id, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) {
void button_create(void *client, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) {
static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
static EXT_RAM_ATTR StackType_t xStack[BUTTON_STACK_SIZE] __attribute__ ((aligned (4)));
@@ -168,7 +168,7 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
memset(buttons + n_buttons, 0, sizeof(struct button_s));
// set mandatory parameters
buttons[n_buttons].id = id;
buttons[n_buttons].client = client;
buttons[n_buttons].gpio = gpio;
buttons[n_buttons].debounce = debounce ? debounce: DEBOUNCE;
buttons[n_buttons].handler = handler;
@@ -213,4 +213,54 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
gpio_intr_enable(gpio);
n_buttons++;
}
}
/****************************************************************************************
* Get stored id
*/
void button_get_client(int gpio) {
for (int i = 0; i < n_buttons; i++) {
if (buttons[i].gpio == gpio) return buttons[i].client;
}
return NULL;
}
/****************************************************************************************
* Update buttons
*/
void *button_remap(void *client, int gpio, button_handler handler, int long_press, int shifter_gpio) {
int i;
struct button_s *button = NULL;
void *prev_client;
ESP_LOGI(TAG, "remapping GPIO %u, long press %u shifter %u", gpio, long_press, shifter_gpio);
// find button
for (i = 0; i < n_buttons; i++) {
if (buttons[i].gpio == gpio) {
button = buttons + i;
break;
}
}
// huh
if (!button) return NULL;
prev_client = button->client;
button->client = client;
button->handler = handler;
button->long_press = long_press;
button->shifter_gpio = shifter_gpio;
// find our shifter (if any)
for (i = 0; shifter_gpio != -1 && i < n_buttons; i++) {
if (buttons[i].gpio == shifter_gpio) {
button->shifter = buttons + i;
// a shifter must have a long-press handler
if (!buttons[i].long_press) buttons[i].long_press = -1;
break;
}
}
return prev_client;
}

View File

@@ -33,4 +33,6 @@ set shifter_gpio to -1 for no shift
NOTE: shifter buttons *must* be created before shiftee
*/
void button_create(void *id, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio);
void button_create(void *client, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio);
void *button_remap(void *client, int gpio, button_handler handler, int long_press, int shifter_gpio);
void button_get_client(int gpio);

View File

@@ -40,6 +40,7 @@ void services_init(void) {
if ((p = strcasestr(nvs_item, "sda")) != NULL) sda = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c_speed = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
free(nvs_item);
}
#ifdef CONFIG_SQUEEZEAMP

View File

@@ -7,7 +7,7 @@
<sha>799ae4860f9c009ac25c2ec35eb4070c5f474659</sha>
<email>philippe_44@outlook.com</email>
<desc lang="EN">SqueezeESP32 additional player id (100)</desc>
<url>http://github.com/sle118/squeezelite-esp32/raw/master/plugin/SqueezeESP32/SqueezeESP32.zip</url>
<url>http://github.com/sle118/squeezelite-esp32/raw/master/plugin/SqueezeESP32.zip</url>
<title lang="EN">SqueezeESP32</title>
</plugin>
</plugins>