Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for floats to NVS (IDFGH-12814) #13789

Open
kdschlosser opened this issue May 13, 2024 · 0 comments
Open

Add support for floats to NVS (IDFGH-12814) #13789

kdschlosser opened this issue May 13, 2024 · 0 comments
Labels
Status: Opened Issue is new Type: Feature Request Feature request for IDF

Comments

@kdschlosser
Copy link

Is your feature request related to a problem?

It would be nice to have the ability to store floats into NVRAM. It's not that hard to do using type punning. This is able to be achieved partly by the user with the get and set but the missing component is the NVS_TYPE when pulling the info for that key. You would not be able to identify it as a float via the info. It would end up being a U32 type.

Describe the solution you'd like.

From what I am seeing this is what would need to be done.

nvs.h

typedef enum {
    NVS_TYPE_U8    = 0x01,  /*!< Type uint8_t */
    NVS_TYPE_I8    = 0x11,  /*!< Type int8_t */
    NVS_TYPE_U16   = 0x02,  /*!< Type uint16_t */
    NVS_TYPE_I16   = 0x12,  /*!< Type int16_t */
    NVS_TYPE_U32   = 0x04,  /*!< Type uint32_t */
    NVS_TYPE_I32   = 0x14,  /*!< Type int32_t */
    NVS_TYPE_U64   = 0x08,  /*!< Type uint64_t */
    NVS_TYPE_I64   = 0x18,  /*!< Type int64_t */
    NVS_TYPE_STR   = 0x21,  /*!< Type string */
    NVS_TYPE_BLOB  = 0x42,  /*!< Type blob */
    NVS_TYPE_FLOAT = 0x24,  /*!< Type float */
    NVS_TYPE_ANY   = 0xff   /*!< Must be last */
} nvs_type_t;


esp_err_t nvs_get_float (nvs_handle_t handle, const char *key, float *value);

esp_err_t nvs_set_float(nvs_handle_t c_handle, const char* key, float value);

nvs_api.cpp

extern "C" esp_err_t nvs_get_float (nvs_handle_t c_handle, const char* key, float* out_value)
{
    Lock lock;
    ESP_LOGD(TAG, "%s %s", __func__, key);
    NVSHandleSimple *handle;
    auto err = nvs_find_ns_handle(c_handle, &handle);
    if (err != ESP_OK) {
        return err;
    }

    return handle->get_float(key, out_value);
}


extern "C" esp_err_t nvs_set_float(nvs_handle_t c_handle, const char* key, float in_float)
{
    Lock lock;
    ESP_LOGD(TAG, "%s %s %f", __func__, key, in_float);
    NVSHandleSimple *handle;
    auto err = nvs_find_ns_handle(c_handle, &handle);
    if (err != ESP_OK) {
        return err;
    }

    return handle->set_float(key, in_float);
}

nvs_handle.hpp

enum class ItemType : uint8_t {
    U8   = NVS_TYPE_U8,
    I8   = NVS_TYPE_I8,
    U16  = NVS_TYPE_U16,
    I16  = NVS_TYPE_I16,
    U32  = NVS_TYPE_U32,
    I32  = NVS_TYPE_I32,
    U64  = NVS_TYPE_U64,
    I64  = NVS_TYPE_I64,
    SZ   = NVS_TYPE_STR,
    BLOB = 0x41,
    BLOB_DATA = NVS_TYPE_BLOB,
    BLOB_IDX  = 0x48,
    FLOAT = NVS_TYPE_FLOAT,
    ANY  = NVS_TYPE_ANY
};

virtual esp_err_t get_float(const char *key, float* out_float) = 0;
virtual esp_err_t set_float(const char *key, float in_float) = 0;

nvs_handle_locked.cpp

esp_err_t NVSHandleLocked::get_float(const char *key, float* out_float) {
    Lock lock;
    return handle->get_float(key, out_float);
}

esp_err_t NVSHandleLocked::set_float(const char *key, float in_float) {
    Lock lock;
    return handle->set_float(key, in_float);
}

nvs_handle_simple.cpp

esp_err_t NVSHandleSimple::get_float(const char *key, float* out_float)
{
    if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;

    out_float
    union {float f; uint32_t i;} u;

    auto err = mStoragePtr->readItem(mNsIndex, nvs::ItemType::FLOAT, key, &u.i, 4);
    if (err != ESP_OK) {
        return err;
    }

    *out_float = u.f;
    return err;
}


esp_err_t NVSHandleSimple::set_float(const char *key, float in_float)
{
    if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
    if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;

    union {float f; uint32_t i;} u;
    u.f = in_float;

    return mStoragePtr->writeItem(mNsIndex, nvs::ItemType::FLOAT, key, u.i, 4);
}

I could be missing some thing. C and CPP is not my strong language. It would however be nice to see this ability added to the esp-idf

Describe alternatives you've considered.

No response

Additional context.

No response

@kdschlosser kdschlosser added the Type: Feature Request Feature request for IDF label May 13, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label May 13, 2024
@github-actions github-actions bot changed the title Add support for floats to NVS Add support for floats to NVS (IDFGH-12814) May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new Type: Feature Request Feature request for IDF
Projects
None yet
Development

No branches or pull requests

2 participants