Skip to content

Commit 49522e1

Browse files
committed
expands the NVS for the esp32
The esp32.NVS class in micropython was limited in the data types it supported. I have expanded this to include unsigned/signed int 8, 16, 32, 64 as well as floats and also strings and buffer objects (memoryview, bytes, bytearray).
1 parent be04058 commit 49522e1

File tree

6 files changed

+378
-11
lines changed

6 files changed

+378
-11
lines changed

api_drivers/py_api_drivers/frozen/indev/touch_calibration/esp32/touch_cal_data.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import esp32 # NOQA
1+
import nvs # NOQA
22

33

44
class TouchCalData(object):
55

66
def __init__(self, name):
7-
self._config = esp32.NVS(name)
7+
self._config = nvs.NVS(name)
88

99
try:
10-
self._left = self._config.get_i32('left')
10+
self._left = self._config.get(nvs.TYPE_I32, 'left')
1111
except OSError:
1212
self._left = -0x80000000
1313
try:
14-
self._right = self._config.get_i32('right')
14+
self._right = self._config.get(nvs.TYPE_I32, 'right')
1515
except OSError:
1616
self._right = -0x80000000
1717

1818
try:
19-
self._top = self._config.get_i32('top')
19+
self._top = self._config.get(nvs.TYPE_I32, 'top')
2020
except OSError:
2121
self._top = -0x80000000
2222
try:
23-
self._bottom = self._config.get_i32('bottom')
23+
self._bottom = self._config.get(nvs.TYPE_I32, 'bottom')
2424
except OSError:
2525
self._bottom = -0x80000000
2626

@@ -43,7 +43,7 @@ def left(self, value):
4343
value = -0x80000000
4444

4545
self._left = value
46-
self._config.set_i32('left', value)
46+
self._config.set(nvs.TYPE_I32, 'left', value)
4747
self._is_dirty = True
4848

4949
@property
@@ -59,7 +59,7 @@ def right(self, value):
5959
value = -0x80000000
6060

6161
self._right = value
62-
self._config.set_i32('right', value)
62+
self._config.set(nvs.TYPE_I32, 'right', value)
6363
self._is_dirty = True
6464

6565
@property
@@ -75,7 +75,7 @@ def top(self, value):
7575
value = -0x80000000
7676

7777
self._top = value
78-
self._config.set_i32('top', value)
78+
self._config.set(nvs.TYPE_I32, 'top', value)
7979
self._is_dirty = True
8080

8181
@property
@@ -88,10 +88,10 @@ def bottom(self):
8888
@bottom.setter
8989
def bottom(self, value):
9090
if value is None:
91-
value = 0x80000000
91+
value = -0x80000000
9292

9393
self._bottom = value
94-
self._config.set_i32('bottom', value)
94+
self._config.set(nvs.TYPE_I32, 'bottom', value)
9595
self._is_dirty = True
9696

9797
def reset(self):

esp32_stubs/nvs.pyi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Union, Final, Literal
2+
from enum import IntEnum
3+
4+
5+
6+
TYPE_I8: Final[int] = 0x11
7+
TYPE_U8: Final[int] = 0x01
8+
TYPE_I16: Final[int] = 0x12
9+
TYPE_U16: Final[int] = 0x02
10+
TYPE_I32: Final[int] = 0x14
11+
TYPE_U32: Final[int] = 0x04
12+
TYPE_I64: Final[int] = 0x18
13+
TYPE_U64: Final[int] = 0x08
14+
TYPE_STR: Final[int] = 0x21
15+
TYPE_BLOB: Final[int] = 0x42
16+
TYPE_FLOAT: Final[int] = 0xFE
17+
18+
19+
class NVS:
20+
21+
def __init__(self, name: str, /):
22+
...
23+
24+
def set(self, type: int, key: str, value: Union[int, float, str, bytes, memoryview, bytearray]) -> None:
25+
...
26+
27+
def get(self, type: int, key: str) -> Union[int, float, str, bytes]:
28+
...
29+
30+
def erase(self, key: str) -> None:
31+
...
32+
33+
def commit(self) -> None:
34+
...
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
#ifndef __NVS_H__
4+
#define __NVS_H__
5+
6+
#include "py/runtime.h"
7+
#include "py/mperrno.h"
8+
#include "mphalport.h"
9+
#include "nvs_flash.h"
10+
#include "nvs.h"
11+
12+
13+
typedef struct _mp_nvs_obj_t {
14+
mp_obj_base_t base;
15+
nvs_handle_t ns;
16+
} mp_nvs_obj_t;
17+
18+
extern const mp_obj_type_t mp_nvs_type;
19+
20+
#endif
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Create an INTERFACE library for our C module.
2+
3+
4+
add_library(usermod_nvs INTERFACE)
5+
6+
set(INCLUDES ${CMAKE_CURRENT_LIST_DIR})
7+
set(SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/nvs.c)
8+
9+
# gets nvs_flash include paths
10+
idf_component_get_property(nvs_flash_includes nvs_flash INCLUDE_DIRS)
11+
idf_component_get_property(nvs_flash_dir nvs_flash COMPONENT_DIR)
12+
13+
# sets the include paths into INCLUDES variable
14+
if(nvs_flash_includes)
15+
list(TRANSFORM nvs_flash_includes PREPEND ${nvs_flash_dir}/)
16+
list(APPEND INCLUDES ${nvs_flash_includes})
17+
endif()
18+
19+
# Add our source files to the lib
20+
target_sources(usermod_nvs INTERFACE ${SOURCES})
21+
22+
# Add include directories.
23+
target_include_directories(usermod_nvs INTERFACE ${INCLUDES})
24+
25+
# Link our INTERFACE library to the usermod target.
26+
target_link_libraries(usermod INTERFACE usermod_nvs)

0 commit comments

Comments
 (0)