# HG changeset patch # User Ethan Zonca # Date 2024-10-07 21:56:14 # Node ID aee85174089e0bf936587f0835a2db1bd6cecf3a # Parent 755225bf17312862b2a0406fdd5f4801831dbf80 Add missing files for squeeze and nv storage diff --git a/main/flash.c b/main/flash.c new file mode 100644 --- /dev/null +++ b/main/flash.c @@ -0,0 +1,92 @@ +// +// flash: nonvolatile k/v storage +// + +#include +#include +#include "nvs_flash.h" +#include "flash.h" +#include + + +// Private variables +static nvs_handle_t my_handle; +static const char *TAG = "flash"; + + +// Initialize flash storage +void flash_init(void) +{ + // Initialize NVS + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + +} + + +// Write k/v to flash +void flash_write(char* key, int32_t val) +{ + ESP_LOGI(TAG, "Writing keyval %s => %ld", key, val); + + // Open handle + esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle); + + if (err != ESP_OK) + { + ESP_LOGI(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err)); + } + else + { + // Write + err = nvs_set_i32(my_handle, key, val); + + if(err != ESP_OK) + ESP_LOGI(TAG, "Failed!\n"); + + err = nvs_commit(my_handle); + if(err != ESP_OK) + ESP_LOGI(TAG, "Failed!\n"); + + // Close + nvs_close(my_handle); + } +} + + +// Read k/v from flash +int32_t flash_read(char* key) +{ + int32_t val = -1; // value will default to -1 if not set + + // Open + ESP_LOGI(TAG, "Reading keyval %s", key); + esp_err_t err = nvs_open("storage", NVS_READONLY, &my_handle); + + if (err != ESP_OK) + { + ESP_LOGI(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err)); + } + else + { + // Read + err = nvs_get_i32(my_handle, key, &val); + switch (err) { + case ESP_OK: + ESP_LOGI(TAG, "Read value %ld\n", val); + break; + case ESP_ERR_NVS_NOT_FOUND: + ESP_LOGI(TAG, "The value is not initialized yet!\n"); + break; + default : + ESP_LOGI(TAG, "Error (%s) reading!\n", esp_err_to_name(err)); + } + // Close + nvs_close(my_handle); + } + return val; +} \ No newline at end of file diff --git a/main/flash.h b/main/flash.h new file mode 100644 --- /dev/null +++ b/main/flash.h @@ -0,0 +1,8 @@ +#ifndef _FLASH_H +#define _FLASH_H + +void flash_init(void); +void flash_write(char* key, int32_t val); +int32_t flash_read(char* key); + +#endif \ No newline at end of file diff --git a/main/squeeze.c b/main/squeeze.c new file mode 100644 --- /dev/null +++ b/main/squeeze.c @@ -0,0 +1,78 @@ +#include "driver/adc.h" +#include "esp_adc_cal.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "squeeze.h" + +static const char *TAG = "squeeze"; + +esp_adc_cal_characteristics_t adc1_chars; + +// Private prototypes +static void squeeze_process(void); + + +void squeeze_init(void) +{ + // ADC1 chan0 is PIN1 + adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_12); + esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_12, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars); + xTaskCreatePinnedToCore(squeeze_process, "squeeze", 1024*2, NULL, 10, NULL, 1); +} + + +// Definitions +#define ALPHA_FAST 0.02f +#define ALPHA_SLOW 0.005f + +// Private variables +static float reading_fast = 0.0f; +static float reading_slow = 0.0f; +static float delta_last = 0.0f; +static uint8_t triggered_flag = 0; + + +static void squeeze_process(void) +{ + for(;;) + { + uint32_t millivolts = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_0), &adc1_chars); + + reading_fast = (ALPHA_FAST * millivolts) + ((1.0-ALPHA_FAST) * reading_fast); + reading_slow = (ALPHA_SLOW * millivolts) + ((1.0-ALPHA_SLOW) * reading_slow); + float delta = reading_fast - reading_slow; + + //ESP_LOGI(TAG, "ADC read %lu fast=%g slow=%g delta=%g", millivolts, reading_fast, reading_slow, delta); + + if(delta > 4.0 && delta_last < 4.0) + { + ESP_LOGI(TAG, "Trigger at delta %g", delta); + + // set flag, consumed on read by squeeze_triggered() + triggered_flag = 1; + } + delta_last = delta; + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + +float squeeze_value(void) +{ + return reading_fast; +} + +uint8_t squeeze_triggered(void) +{ + if(triggered_flag == 1) + { + // consume + triggered_flag = 0; + return 1; + } + else + { + return 0; + } +} \ No newline at end of file diff --git a/main/squeeze.h b/main/squeeze.h new file mode 100644 --- /dev/null +++ b/main/squeeze.h @@ -0,0 +1,9 @@ +#ifndef _SQUEEZE_H +#define _SQUEEZE_H + +void squeeze_init(void); +float squeeze_value(void); +uint8_t squeeze_triggered(void); + + +#endif \ No newline at end of file