This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Minor mod to build for custom python modules, and NVS string support #226
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b17b5db
Setup a esp32/frozen/Custom dir where you can put any custom python
psychogenic 45acafe
Added support for nvs_setstring/nvs_getstring to store/retrieve string
psychogenic b636bd7
Simplified API for NVS support--now u32 and str work through standard
psychogenic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Custom distro modules | ||
===================== | ||
|
||
Place any python packages/modules you want to have included as frozen .mpy and loaded on boot, here. | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
#include "py/mpconfig.h" | ||
#include "py/obj.h" | ||
#include "py/objstr.h" | ||
#include "py/runtime.h" | ||
#include "mperror.h" | ||
#include "updater.h" | ||
|
@@ -162,8 +163,41 @@ STATIC mp_obj_t mod_pycom_pulses_get (mp_obj_t gpio, mp_obj_t timeout) { | |
} | ||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_pulses_get_obj, mod_pycom_pulses_get); | ||
|
||
|
||
|
||
/* | ||
* nvs_setstring/nvs_getstring: support for NVS string storage and, | ||
* thanks to JSON/other serializers, pretty much any datastructure. | ||
*/ | ||
STATIC mp_obj_t mod_pycom_nvs_setstring (mp_obj_t _key, mp_obj_t _value) { | ||
const char *key = mp_obj_str_get_str(_key); | ||
const char *value = mp_obj_str_get_str(_value); | ||
if (strlen(value) >= 1984) { | ||
// orwell error: maximum length (including null character) is 1984 bytes | ||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "value too long (max: 1984)")); | ||
return mp_const_none; | ||
} | ||
esp_err_t esp_err = nvs_set_str(pycom_nvs_handle, key, value); | ||
if (ESP_OK == esp_err) { | ||
nvs_commit(pycom_nvs_handle); | ||
} 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) { | ||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no space available")); | ||
} else if (ESP_ERR_NVS_INVALID_NAME == esp_err || ESP_ERR_NVS_KEY_TOO_LONG == esp_err) { | ||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid key (or too long)")); | ||
} else { | ||
// not ESP_OK, but not reporting? TODO:check this... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An exception here with a generic message is needed |
||
} | ||
return mp_const_none; | ||
} | ||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_setstring_obj, mod_pycom_nvs_setstring); | ||
|
||
|
||
STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) { | ||
const char *key = mp_obj_str_get_str(_key); | ||
if (MP_OBJ_IS_STR_OR_BYTES(_value)) { | ||
// not certain how to differentiate between string and bytes, here... TODO | ||
return mod_pycom_nvs_setstring(_key, _value); | ||
} | ||
uint32_t value = mp_obj_get_int_truncated(_value); | ||
|
||
esp_err_t esp_err = nvs_set_u32(pycom_nvs_handle, key, value); | ||
|
@@ -178,17 +212,53 @@ STATIC mp_obj_t mod_pycom_nvs_set (mp_obj_t _key, mp_obj_t _value) { | |
} | ||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_pycom_nvs_set_obj, mod_pycom_nvs_set); | ||
|
||
|
||
|
||
STATIC mp_obj_t mod_pycom_nvs_getstring (mp_obj_t _key) { | ||
vstr_t vstr; | ||
const char *key = mp_obj_str_get_str(_key); | ||
size_t required_size = 0; | ||
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_str(pycom_nvs_handle, key, NULL, &required_size)) { | ||
return mp_const_none; | ||
} | ||
vstr_init_len(&vstr, required_size); | ||
if (ESP_OK != nvs_get_str(pycom_nvs_handle, key, vstr.buf, &required_size)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this, after the necessary size is fetched I would simply allocate enough byte with m_malloc(), fetch the value from NVS, then create the return object with mp_obj_new_str(), then free up the allocated memory and return the composed string. In this case it is not needed to compose vstr, remove closing \0 etc. |
||
vstr_clear(&vstr); | ||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't get string")); | ||
return mp_const_none; | ||
} | ||
if (vstr.len && vstr.buf[vstr.len - 1] == '\0') { | ||
/* bit of a hack to get rid of trailing null terminator required by the | ||
* nvs backend to figure out string length... must be a better way, TODO. | ||
*/ | ||
vstr.len = vstr.len - 1; | ||
} | ||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); | ||
|
||
} | ||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_getstring_obj, mod_pycom_nvs_getstring); | ||
|
||
|
||
STATIC mp_obj_t mod_pycom_nvs_get (mp_obj_t _key) { | ||
const char *key = mp_obj_str_get_str(_key); | ||
uint32_t value; | ||
|
||
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u32(pycom_nvs_handle, key, &value)) { | ||
return mp_const_none; | ||
esp_err_t esp_err = nvs_get_u32(pycom_nvs_handle, key, &value); | ||
if (ESP_OK == esp_err) { | ||
return mp_obj_new_int(value); | ||
} | ||
return mp_obj_new_int(value); | ||
if (ESP_ERR_NVS_NOT_FOUND == esp_err || ESP_ERR_NVS_TYPE_MISMATCH == esp_err) { | ||
/* you would expect it to return TYPE_MISMATCH if it's been | ||
stored as a string, but it's actually returning NOT_FOUND... | ||
so: try string */ | ||
return mod_pycom_nvs_getstring(_key); | ||
} | ||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error doing nvs_get: %d", esp_err)); | ||
return mp_const_none; | ||
} | ||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_pycom_nvs_get_obj, mod_pycom_nvs_get); | ||
|
||
|
||
|
||
STATIC mp_obj_t mod_pycom_nvs_erase (mp_obj_t _key) { | ||
const char *key = mp_obj_str_get_str(_key); | ||
|
||
|
@@ -298,7 +368,9 @@ STATIC const mp_map_elem_t pycom_module_globals_table[] = { | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_ota_slot), (mp_obj_t)&mod_pycom_ota_slot_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulses_get), (mp_obj_t)&mod_pycom_pulses_get_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_set), (mp_obj_t)&mod_pycom_nvs_set_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_setstring), (mp_obj_t)&mod_pycom_nvs_setstring_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_get), (mp_obj_t)&mod_pycom_nvs_get_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_getstring), (mp_obj_t)&mod_pycom_nvs_getstring_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_erase), (mp_obj_t)&mod_pycom_nvs_erase_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvs_erase_all), (mp_obj_t)&mod_pycom_nvs_erase_all_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_on_boot), (mp_obj_t)&mod_pycom_wifi_on_boot_obj }, | ||
|
@@ -307,7 +379,7 @@ STATIC const mp_map_elem_t pycom_module_globals_table[] = { | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_ssid), (mp_obj_t)&mod_pycom_wifi_ssid_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_wifi_pwd), (mp_obj_t)&mod_pycom_wifi_pwd_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_heartbeat_on_boot), (mp_obj_t)&mod_pycom_heartbeat_on_boot_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_lte_modem_en_on_boot), (mp_obj_t)&mod_pycom_lte_modem_on_boot_obj }, | ||
{ MP_OBJ_NEW_QSTR(MP_QSTR_lte_modem_en_on_boot), (mp_obj_t)&mod_pycom_lte_modem_on_boot_obj }, | ||
}; | ||
|
||
STATIC MP_DEFINE_CONST_DICT(pycom_module_globals, pycom_module_globals_table); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.