diff -r 1599e696d947 -r 12506716211c main/nvsio.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/nvsio.c Thu Apr 06 20:49:52 2023 +0200 @@ -0,0 +1,139 @@ +/***************************************************************************** + * Copyright (C) 2023 + * + * Michiel Broek + * + * This file is part of the ESP32 iotbalkon application. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ThermFerm; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "config.h" + +#define NVSIO_Namespace "balkon" + +static const char *TAG = "nvsio"; + + +void nvsio_init(void) +{ + esp_err_t err = ESP_OK; + nvs_handle_t my_handle; + + err = nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Error (%s) opening NVS handle '%s'", esp_err_to_name(err), NVSIO_Namespace); + } else { + nvs_commit(my_handle); + nvs_close(my_handle); + } +} + + +uint8_t nvsio_read_u8(char *key) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint8_t val = 0; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u8(my_handle, key, &val); + if (err == ESP_ERR_NVS_NOT_FOUND) { + err = nvs_set_u8(my_handle, key, 0); + val = 0; + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_read_u8(%s) created", key); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "nvsio_read_u8(%s) Error (%s) opening NVS namespace '%s'", key, esp_err_to_name(err), NVSIO_Namespace); + } + nvs_close(my_handle); + } + + return val; +} + + +void nvsio_write_u8(char *key, uint8_t val) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint8_t tmp; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u8(my_handle, key, &tmp); + if (err == ESP_ERR_NVS_NOT_FOUND) { + nvs_set_u8(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u8(%s) created val %02x", key, val); + } else if (err == ESP_OK && tmp != val) { + nvs_set_u8(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u8(%s) updated val %02x", key, val); + } else { + ESP_LOGI(TAG, "nvsio_write_u8(%s) already val %02x", key, val); + } + nvs_close(my_handle); + } +} + + +uint32_t nvsio_read_u32(char *key) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint32_t val = 0; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u32(my_handle, key, &val); + if (err == ESP_ERR_NVS_NOT_FOUND) { + err = nvs_set_u32(my_handle, key, 0); + val = 0; + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_read_u32(%s) created", key); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "nvsio_read_u32(%s) Error (%s) opening NVS namespace '%s'", key, esp_err_to_name(err), NVSIO_Namespace); + } + nvs_close(my_handle); + } + + return val; +} + + +void nvsio_write_u32(char *key, uint32_t val) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint32_t tmp; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u32(my_handle, key, &tmp); + if (err == ESP_ERR_NVS_NOT_FOUND) { + nvs_set_u32(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u32(%s) created val %08lx", key, val); + } else if (err == ESP_OK && tmp != val) { + nvs_set_u32(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u32(%s) updated val %08lx", key, val); + } else { + ESP_LOGI(TAG, "nvsio_write_u32(%s) already val %08lx", key, val); + } + nvs_close(my_handle); + } +} +