|
9 | 9 |
|
10 | 10 | #include "py/mpconfig.h"
|
11 | 11 | #include "py/obj.h"
|
| 12 | +#include "py/objstr.h" |
12 | 13 | #include "py/runtime.h"
|
13 | 14 | #include "mperror.h"
|
14 | 15 | #include "updater.h"
|
@@ -233,30 +234,73 @@ STATIC mp_obj_t mod_pycom_pulses_get (mp_obj_t gpio, mp_obj_t timeout) {
|
233 | 234 | }
|
234 | 235 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_pulses_get_obj, mod_pycom_pulses_get);
|
235 | 236 |
|
| 237 | + |
236 | 238 | STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) {
|
| 239 | + |
237 | 240 | const char *key = mp_obj_str_get_str(_key);
|
238 |
| - uint32_t value = mp_obj_get_int_truncated(_value); |
| 241 | + esp_err_t esp_err = ESP_OK; |
| 242 | + |
| 243 | + if (MP_OBJ_IS_STR_OR_BYTES(_value)) { |
| 244 | + const char *value = mp_obj_str_get_str(_value); |
| 245 | + if (strlen(value) >= 1984) { |
| 246 | + // Maximum length (including null character) can be 1984 bytes |
| 247 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "value too long (max: 1984)")); |
| 248 | + } |
| 249 | + esp_err = nvs_set_str(pycom_nvs_handle, key, value); |
| 250 | + } else if(MP_OBJ_IS_INT(_value)) { |
| 251 | + uint32_t value = mp_obj_get_int_truncated(_value); |
| 252 | + esp_err = nvs_set_u32(pycom_nvs_handle, key, value); |
| 253 | + } else { |
| 254 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Value must be string, bytes or integer")); |
| 255 | + } |
239 | 256 |
|
240 |
| - esp_err_t esp_err = nvs_set_u32(pycom_nvs_handle, key, value); |
241 | 257 | if (ESP_OK == esp_err) {
|
242 | 258 | nvs_commit(pycom_nvs_handle);
|
243 | 259 | } else if (ESP_ERR_NVS_NOT_ENOUGH_SPACE == esp_err || ESP_ERR_NVS_PAGE_FULL == esp_err || ESP_ERR_NVS_NO_FREE_PAGES == esp_err) {
|
244 |
| - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no space available")); |
| 260 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No free space available")); |
245 | 261 | } else if (ESP_ERR_NVS_INVALID_NAME == esp_err || ESP_ERR_NVS_KEY_TOO_LONG == esp_err) {
|
246 |
| - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid key (or too long)")); |
| 262 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Key is invalid")); |
| 263 | + } else { |
| 264 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "Error occurred while storing value, code: %d", esp_err)); |
247 | 265 | }
|
248 | 266 | return mp_const_none;
|
249 | 267 | }
|
250 | 268 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_set_obj, mod_pycom_nvs_set);
|
251 | 269 |
|
252 | 270 | STATIC mp_obj_t mod_pycom_nvs_get (mp_obj_t _key) {
|
| 271 | + |
253 | 272 | const char *key = mp_obj_str_get_str(_key);
|
| 273 | + esp_err_t esp_err = ESP_OK; |
| 274 | + mp_obj_t ret = mp_const_none; |
254 | 275 | uint32_t value;
|
255 | 276 |
|
256 |
| - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u32(pycom_nvs_handle, key, &value)) { |
257 |
| - return mp_const_none; |
| 277 | + esp_err = nvs_get_u32(pycom_nvs_handle, key, &value); |
| 278 | + if (esp_err == ESP_OK) { |
| 279 | + ret = mp_obj_new_int(value); |
258 | 280 | }
|
259 |
| - return mp_obj_new_int(value); |
| 281 | + else { |
| 282 | + esp_err = nvs_get_str(pycom_nvs_handle, key, NULL, &value); |
| 283 | + if(esp_err == ESP_OK) { |
| 284 | + char* value_string = (char*)m_malloc(value); |
| 285 | + |
| 286 | + esp_err = nvs_get_str(pycom_nvs_handle, key, value_string, &value); |
| 287 | + |
| 288 | + if(esp_err == ESP_OK) { |
| 289 | + //do not count the terminating \0 |
| 290 | + ret = mp_obj_new_str(value_string, value-1); |
| 291 | + m_free(value_string); |
| 292 | + } |
| 293 | + m_free(value_string); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + if(esp_err == ESP_ERR_NVS_NOT_FOUND) { |
| 298 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "No matching object for the provided key")); |
| 299 | + } else if(esp_err != ESP_OK) { |
| 300 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "Error occurred while fetching value, code: %d", esp_err)); |
| 301 | + } |
| 302 | + |
| 303 | + return ret; |
260 | 304 | }
|
261 | 305 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_get_obj, mod_pycom_nvs_get);
|
262 | 306 |
|
|
0 commit comments