Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit f2a6eb2

Browse files
author
Islam Wahdan
authored
Merge pull request #32 from pycom/psychogenic-master
PYFW-334: Merge and test PR #226
2 parents c0d6703 + cf9be59 commit f2a6eb2

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

esp32/mods/modpycom.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "py/mpconfig.h"
1111
#include "py/obj.h"
12+
#include "py/objstr.h"
1213
#include "py/runtime.h"
1314
#include "mperror.h"
1415
#include "updater.h"
@@ -233,30 +234,73 @@ STATIC mp_obj_t mod_pycom_pulses_get (mp_obj_t gpio, mp_obj_t timeout) {
233234
}
234235
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_pulses_get_obj, mod_pycom_pulses_get);
235236

237+
236238
STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) {
239+
237240
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+
}
239256

240-
esp_err_t esp_err = nvs_set_u32(pycom_nvs_handle, key, value);
241257
if (ESP_OK == esp_err) {
242258
nvs_commit(pycom_nvs_handle);
243259
} 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"));
245261
} 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));
247265
}
248266
return mp_const_none;
249267
}
250268
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_set_obj, mod_pycom_nvs_set);
251269

252270
STATIC mp_obj_t mod_pycom_nvs_get (mp_obj_t _key) {
271+
253272
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;
254275
uint32_t value;
255276

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);
258280
}
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;
260304
}
261305
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_get_obj, mod_pycom_nvs_get);
262306

0 commit comments

Comments
 (0)