|
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"
|
@@ -162,21 +163,7 @@ STATIC mp_obj_t mod_pycom_pulses_get (mp_obj_t gpio, mp_obj_t timeout) {
|
162 | 163 | }
|
163 | 164 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_pulses_get_obj, mod_pycom_pulses_get);
|
164 | 165 |
|
165 |
| -STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) { |
166 |
| - const char *key = mp_obj_str_get_str(_key); |
167 |
| - uint32_t value = mp_obj_get_int_truncated(_value); |
168 | 166 |
|
169 |
| - esp_err_t esp_err = nvs_set_u32(pycom_nvs_handle, key, value); |
170 |
| - if (ESP_OK == esp_err) { |
171 |
| - nvs_commit(pycom_nvs_handle); |
172 |
| - } 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) { |
173 |
| - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no space available")); |
174 |
| - } else if (ESP_ERR_NVS_INVALID_NAME == esp_err || ESP_ERR_NVS_KEY_TOO_LONG == esp_err) { |
175 |
| - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid key (or too long)")); |
176 |
| - } |
177 |
| - return mp_const_none; |
178 |
| -} |
179 |
| -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_set_obj, mod_pycom_nvs_set); |
180 | 167 |
|
181 | 168 | /*
|
182 | 169 | * nvs_setstring/nvs_getstring: support for NVS string storage and,
|
@@ -204,16 +191,27 @@ STATIC mp_obj_t mod_pycom_nvs_setstring (mp_obj_t _key, mp_obj_t _value) {
|
204 | 191 | }
|
205 | 192 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_setstring_obj, mod_pycom_nvs_setstring);
|
206 | 193 |
|
207 |
| -STATIC mp_obj_t mod_pycom_nvs_get (mp_obj_t _key) { |
| 194 | + |
| 195 | +STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) { |
208 | 196 | const char *key = mp_obj_str_get_str(_key);
|
209 |
| - uint32_t value; |
| 197 | + if (MP_OBJ_IS_STR_OR_BYTES(_value)) { |
| 198 | + // not certain how to differentiate between string and bytes, here... TODO |
| 199 | + return mod_pycom_nvs_setstring(_key, _value); |
| 200 | + } |
| 201 | + uint32_t value = mp_obj_get_int_truncated(_value); |
210 | 202 |
|
211 |
| - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u32(pycom_nvs_handle, key, &value)) { |
212 |
| - return mp_const_none; |
| 203 | + esp_err_t esp_err = nvs_set_u32(pycom_nvs_handle, key, value); |
| 204 | + if (ESP_OK == esp_err) { |
| 205 | + nvs_commit(pycom_nvs_handle); |
| 206 | + } 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) { |
| 207 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no space available")); |
| 208 | + } else if (ESP_ERR_NVS_INVALID_NAME == esp_err || ESP_ERR_NVS_KEY_TOO_LONG == esp_err) { |
| 209 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid key (or too long)")); |
213 | 210 | }
|
214 |
| - return mp_obj_new_int(value); |
| 211 | + return mp_const_none; |
215 | 212 | }
|
216 |
| -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_get_obj, mod_pycom_nvs_get); |
| 213 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_set_obj, mod_pycom_nvs_set); |
| 214 | + |
217 | 215 |
|
218 | 216 |
|
219 | 217 | STATIC mp_obj_t mod_pycom_nvs_getstring (mp_obj_t _key) {
|
@@ -241,6 +239,26 @@ STATIC mp_obj_t mod_pycom_nvs_getstring (mp_obj_t _key) {
|
241 | 239 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_getstring_obj, mod_pycom_nvs_getstring);
|
242 | 240 |
|
243 | 241 |
|
| 242 | +STATIC mp_obj_t mod_pycom_nvs_get (mp_obj_t _key) { |
| 243 | + const char *key = mp_obj_str_get_str(_key); |
| 244 | + uint32_t value; |
| 245 | + esp_err_t esp_err = nvs_get_u32(pycom_nvs_handle, key, &value); |
| 246 | + if (ESP_OK == esp_err) { |
| 247 | + return mp_obj_new_int(value); |
| 248 | + } |
| 249 | + if (ESP_ERR_NVS_NOT_FOUND == esp_err || ESP_ERR_NVS_TYPE_MISMATCH == esp_err) { |
| 250 | + /* you would expect it to return TYPE_MISMATCH if it's been |
| 251 | + stored as a string, but it's actually returning NOT_FOUND... |
| 252 | + so: try string */ |
| 253 | + return mod_pycom_nvs_getstring(_key); |
| 254 | + } |
| 255 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error doing nvs_get: %d", esp_err)); |
| 256 | + return mp_const_none; |
| 257 | +} |
| 258 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_get_obj, mod_pycom_nvs_get); |
| 259 | + |
| 260 | + |
| 261 | + |
244 | 262 | STATIC mp_obj_t mod_pycom_nvs_erase (mp_obj_t _key) {
|
245 | 263 | const char *key = mp_obj_str_get_str(_key);
|
246 | 264 |
|
|
0 commit comments