in order to be future proof for other parameters that need to be non-volatilemain
parent
7ffd5b9d7f
commit
74bcb92538
@ -0,0 +1,138 @@ |
|||||||
|
/*
|
||||||
|
* challenge_nvs.c |
||||||
|
* |
||||||
|
* Created on: 08.12.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "challenge_nvs.h" |
||||||
|
#include "esp_log.h" |
||||||
|
#include "esp_system.h" |
||||||
|
#include "nvs_flash.h" |
||||||
|
#include "nvs.h" |
||||||
|
|
||||||
|
/* defines / constants */ |
||||||
|
#define TAG "CHALLENGE_NVS" /* tag for logging with ESP_LOG */ |
||||||
|
const char* NAMESPACE_NVS = "storage"; |
||||||
|
const char* ROBOT_MODE_KEY = "robot_mode"; |
||||||
|
|
||||||
|
/* local vars */ |
||||||
|
bool initializedSuccessfully = false; |
||||||
|
|
||||||
|
/*! \brief Initialize the NVS storage. */ |
||||||
|
void Challenge_Nvs_Init(void){ |
||||||
|
ESP_LOGI(TAG, "Initializing..."); |
||||||
|
|
||||||
|
// Initialize NVS
|
||||||
|
esp_err_t err = nvs_flash_init(); |
||||||
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
||||||
|
// NVS partition was truncated and needs to be erased
|
||||||
|
// Retry nvs_flash_init
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase()); |
||||||
|
err = nvs_flash_init(); |
||||||
|
} |
||||||
|
ESP_ERROR_CHECK( err ); |
||||||
|
initializedSuccessfully = (err == ESP_OK); |
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Initialized. Success=%s", initializedSuccessfully?"true":"false"); |
||||||
|
} |
||||||
|
|
||||||
|
/*! \brief Deinitialize the NVS storage. */ |
||||||
|
void Challenge_Nvs_Deinit(void){ |
||||||
|
ESP_LOGI(TAG, "Deinitializing..."); |
||||||
|
esp_err_t err = nvs_flash_deinit(); |
||||||
|
ESP_LOGI(TAG, "Deinitialized. Success=%s", err==ESP_OK?"true":"false"); |
||||||
|
} |
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Stores the passed robot mode in NVS. |
||||||
|
* \param modeStationary Mode to store. True = stationary mode, False = mobile mode |
||||||
|
* \return true on success |
||||||
|
*/ |
||||||
|
bool Challenge_Nvs_StoreRobotModeToNVS(bool mode){ |
||||||
|
bool success = false; |
||||||
|
|
||||||
|
// Open
|
||||||
|
ESP_LOGI(TAG, "Opening Non-Volatile Storage (NVS) handle... "); |
||||||
|
nvs_handle_t my_handle; |
||||||
|
esp_err_t err = nvs_open(NAMESPACE_NVS, NVS_READWRITE, &my_handle); |
||||||
|
if (err != ESP_OK) { |
||||||
|
ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err)); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
// Write
|
||||||
|
uint8_t value = mode?1:0; |
||||||
|
ESP_LOGI(TAG, "Updating robotMode in NVS ... "); |
||||||
|
err = nvs_set_u8(my_handle, ROBOT_MODE_KEY, value); |
||||||
|
if(err != ESP_OK){ |
||||||
|
ESP_LOGE(TAG, "Failed to update robotMode in NVS. Error=(%s)", esp_err_to_name(err)); |
||||||
|
success = false; |
||||||
|
}else{ // successfully set
|
||||||
|
// Commit written value.
|
||||||
|
// After setting any values, nvs_commit() must be called to ensure changes are written
|
||||||
|
// to flash storage. Implementations may write to storage at other times,
|
||||||
|
// but this is not guaranteed.
|
||||||
|
ESP_LOGI(TAG, "Committing updates in NVS ... "); |
||||||
|
err = nvs_commit(my_handle); |
||||||
|
if(err != ESP_OK){ |
||||||
|
ESP_LOGE(TAG, "Failed to commit to NVS. Error=(%s)", esp_err_to_name(err)); |
||||||
|
success = false; |
||||||
|
}else{ |
||||||
|
ESP_LOGI(TAG, "Successfully committed changes to NVS."); |
||||||
|
success = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle); |
||||||
|
|
||||||
|
return success; |
||||||
|
} |
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Retrieve the robot mode stored in NVS. |
||||||
|
* \param mode Pointer to the location where to retrieve the mode to. True = stationary mode, False = mobile mode |
||||||
|
* \return true on success |
||||||
|
*/ |
||||||
|
bool Challenge_Nvs_GetRobotModeFromNVS(bool *mode){ |
||||||
|
bool success = false; |
||||||
|
bool notInitialized = false; |
||||||
|
|
||||||
|
// Open
|
||||||
|
ESP_LOGI(TAG, "Opening Non-Volatile Storage (NVS) handle... "); |
||||||
|
nvs_handle_t my_handle; |
||||||
|
esp_err_t err = nvs_open(NAMESPACE_NVS, NVS_READWRITE, &my_handle); |
||||||
|
if (err != ESP_OK) { |
||||||
|
ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err)); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
// Read
|
||||||
|
ESP_LOGI(TAG, "Reading robot mode from NVS ... "); |
||||||
|
uint8_t value = 0; |
||||||
|
err = nvs_get_u8(my_handle, ROBOT_MODE_KEY, &value); |
||||||
|
switch (err) { |
||||||
|
case ESP_OK: |
||||||
|
ESP_LOGI(TAG, "Reading robot mode done. Read robotMode = %i", value); |
||||||
|
*mode = (bool)(value==1?true:false); // set value at pointer location
|
||||||
|
success = true; |
||||||
|
break; |
||||||
|
case ESP_ERR_NVS_NOT_FOUND: |
||||||
|
ESP_LOGI(TAG, "Reading robot mode done. The value is not initialized yet!"); |
||||||
|
notInitialized = true; |
||||||
|
break; |
||||||
|
default : |
||||||
|
ESP_LOGI(TAG, "Error reading robot mode. Error=(%s)", esp_err_to_name(err)); |
||||||
|
success = false; |
||||||
|
} |
||||||
|
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle); |
||||||
|
|
||||||
|
if(notInitialized){ |
||||||
|
success = Challenge_Nvs_StoreRobotModeToNVS(false); // initialize to false
|
||||||
|
} |
||||||
|
|
||||||
|
return success; |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
/*
|
||||||
|
* challenge_nvs.h |
||||||
|
* |
||||||
|
* Created on: 08.12.2022 |
||||||
|
* Author: jonas |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef MAIN_CHALLENGE_NVS_H_ |
||||||
|
#define MAIN_CHALLENGE_NVS_H_ |
||||||
|
|
||||||
|
#include <stdbool.h> |
||||||
|
|
||||||
|
/*! \brief Initialize the NVS storage. */ |
||||||
|
void Challenge_Nvs_Init(void); |
||||||
|
|
||||||
|
/*! \brief Deinitialize the NVS storage. */ |
||||||
|
void Challenge_Nvs_Deinit(void); |
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Stores the passed robot mode in NVS. |
||||||
|
* \param modeStationary Mode to store. True = stationary mode, False = mobile mode |
||||||
|
* \return true on success |
||||||
|
*/ |
||||||
|
bool Challenge_Nvs_StoreRobotModeToNVS(bool mode); |
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Retrieve the robot mode stored in NVS. If the robot mode is not yet stored in NVS, it will be initially stored as false. |
||||||
|
* \param mode Pointer to the location where to retrieve the mode to. True = stationary mode, False = mobile mode |
||||||
|
* \return true on success |
||||||
|
*/ |
||||||
|
bool Challenge_Nvs_GetRobotModeFromNVS(bool *mode); |
||||||
|
|
||||||
|
#endif /* MAIN_CHALLENGE_NVS_H_ */ |
||||||
Loading…
Reference in new issue