Skip to content

Commit e65b31e

Browse files
committed
test/esp32: Add nvs test example
Signed-off-by: lbuque <[email protected]>
1 parent 744bd55 commit e65b31e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tests/esp32/test_nvs.m5f2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","type":"core2","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__core2_screen","createTime":1697017152463,"x":0,"y":0,"width":320,"height":240,"backgroundColor":"#222222","size":0,"isSelected":true},{"name":"label0","type":"label","layer":1,"screenId":"builtin","screenName":"","id":"qr9jBpywDK+`BSw-","createTime":1697017155835,"x":4,"y":25,"color":"#ffffff","backgroundColor":"#222222","text":"Key","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false,"width":32,"height":20},{"name":"label1","type":"label","layer":2,"screenId":"builtin","screenName":"","id":"l+zT#hMgc5y^EW0s","createTime":1697017159167,"x":73,"y":25,"color":"#ffffff","backgroundColor":"#222222","text":"Value","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false,"width":49,"height":20},{"name":"title0","type":"title","layer":1,"screenId":"builtin","screenName":"","id":"kd`u#P=YmawItp0j","createTime":1697017987380,"x":0,"y":0,"color":"#FFFFFF","backgroundColor":"#0000FF","text":"NVS Example","textOffset":3,"font":"Widgets.FONTS.DejaVu18","isSelected":false,"width":321,"height":19},{"name":"label2","type":"label","layer":2,"screenId":"builtin","screenName":"","id":"lDV3yR%q6l3&wQzk","createTime":1697017155835,"x":4,"y":50,"color":"#ffffff","backgroundColor":"#222222","text":"count","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false,"width":32,"height":20},{"name":"label3","type":"label","layer":3,"screenId":"builtin","screenName":"","id":"nV4uNQVE5qblQYBR","createTime":1697017159167,"x":75,"y":50,"color":"#ffffff","backgroundColor":"#222222","text":"Value","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"isSelected":false,"width":49,"height":20}],"resources":[{"software":["software_time","nvs"]},{"hardware":["speaker"]}],"units":[],"hats":[],"i2cs":[],"blockly":"<variables><variable id=\"P8m:a4|na{N$g(rw?GrZ\">count</variable></variables><block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"50\" y=\"51\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"nvs_open\" id=\"@!S+}nr3msx+@AS#H)Lt\"><field name=\"NAME\">nvs_0</field><value name=\"VALUE\"><shadow type=\"text\" id=\"`kLoinh3cvZ7wn)Iual6\"><field name=\"TEXT\">db</field></shadow></value><next><block type=\"variables_set\" id=\"jY|6:$$D;N3j)0yh1nDl\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field><value name=\"VALUE\"><block type=\"math_number\" id=\"/n)0b];Z6KZ[@6ia5Of*\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></block></value><next><block type=\"nvs_set_i32\" id=\"nQ-R|sn9|j0`LFA`-If$\"><field name=\"NAME\">nvs_0</field><value name=\"KEY\"><shadow type=\"text\" id=\"ECzIBbg_+Ap*OFU`SNa;\"><field name=\"TEXT\">count</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"O)(|I)ZPqTn*TBQrh5g,\"><mutation max=\"2147483647\" min=\"-2147483648\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"variables_get\" id=\"]vK6}=uJ`],;.K;?C$H:\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field></block></value></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"50\" y=\"290\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"><next><block type=\"variables_set\" id=\"e;${mJ9+^]BSYReXHV{i\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field><value name=\"VALUE\"><block type=\"nvs_get_i32\" id=\"g;5Rb5O_v;~JL#:B-4FJ\"><field name=\"NAME\">nvs_0</field><value name=\"KEY\"><shadow type=\"text\" id=\"`mjO[f*+`otqBKr*!.gw\"><field name=\"TEXT\">count</field></shadow></value></block></value><next><block type=\"speaker_tone\" id=\"WDQ55VhSO;mjq;o=VFO.\"><value name=\"FREQ\"><shadow type=\"math_number\" id=\"r#LYr=l^MYb4}f#Xg)D{\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><value name=\"MS\"><shadow type=\"math_number\" id=\"w~cdnJ.@|TD;dnc`tzr]\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">50</field></shadow></value><next><block type=\"label_set_text\" id=\"U-mb?Xl+q-Rmi$MQLKuy\"><field name=\"NAME\">label3</field><value name=\"TEXT\"><shadow type=\"text\" id=\"E.a@e[fUA9tH.K(;2scl\"><field name=\"TEXT\">Label</field></shadow><block type=\"variables_get\" id=\"@(Yw^^*z.a++WjV5+/*!\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field></block></value><next><block type=\"label_set_color_multiple\" id=\"ezM4+VE:lk8=f5RXbQeu\"><field name=\"NAME\">label3</field><value name=\"COLOR\"><block type=\"color_rgb_palette\" id=\"jFq(sx({8kIlSg9@JzNm\"><mutation mode=\"palette\"></mutation><field name=\"MODE\">palette</field><field name=\"COLOR\">#33ccff</field></block></value><value name=\"BG_COLOR\"><block type=\"color_rgb_palette\" id=\"{uT4X!q[(.IzG:}^,zf%\"><mutation mode=\"palette\"></mutation><field name=\"MODE\">palette</field><field name=\"COLOR\">#000000</field></block></value><next><block type=\"math_change\" id=\"o!t|@6#kx.1^`^(pk4^#\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field><value name=\"DELTA\"><shadow type=\"math_number\" id=\"}+(!4R9e9naxK[d1Z=0V\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value><next><block type=\"time_sleep_second\" id=\"`gInTKpGEBf*C^1!we*v\"><value name=\"SECOND\"><shadow type=\"math_number\" id=\"^VZOZGeYU#X@{{NMHUy`\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value><next><block type=\"nvs_set_i32\" id=\"qfT`DN-!L{daI$0fw=p6\"><field name=\"NAME\">nvs_0</field><value name=\"KEY\"><shadow type=\"text\" id=\"BmJF#-h#J/_g[VBm7HZ!\"><field name=\"TEXT\">count</field></shadow></value><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"O)(|I)ZPqTn*TBQrh5g,\"><mutation max=\"2147483647\" min=\"-2147483648\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"variables_get\" id=\"hgAQs=Un^tql:UXDxyIP\"><field name=\"VAR\" id=\"P8m:a4|na{N$g(rw?GrZ\">count</field></block></value><next><block type=\"speaker_tone\" id=\"vTuN.8rE{Ke[MrGER:_[\"><value name=\"FREQ\"><shadow type=\"math_number\" id=\"SF=,poA}hI~UI3u_a]z@\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">3222</field></shadow></value><value name=\"MS\"><shadow type=\"math_number\" id=\"+H/K~!`ju-ZZK4~Le)M1\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">50</field></shadow></value><next><block type=\"label_set_color_multiple\" id=\"9AD$Tf#VP5[0?0dH,khI\"><field name=\"NAME\">label3</field><value name=\"COLOR\"><block type=\"color_rgb_palette\" id=\"%$F|RJj2f:76KuXJTGuj\"><mutation mode=\"palette\"></mutation><field name=\"MODE\">palette</field><field name=\"COLOR\">#ff6600</field></block></value><value name=\"BG_COLOR\"><block type=\"color_rgb_palette\" id=\"XK4Y_d0E1Nv.0~[JK|P|\"><mutation mode=\"palette\"></mutation><field name=\"MODE\">palette</field><field name=\"COLOR\">#000000</field></block></value><next><block type=\"time_sleep_second\" id=\"lt}?qpMdi$IlO_6Veire\"><value name=\"SECOND\"><shadow type=\"math_number\" id=\"C2jIKW/9z-0zo*5rP?7N\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":320,"height":240,"scale":0.78,"screenName":"","blockId":"","id":"builtin","createTime":1697017152461}]}

tests/esp32/test_nvs.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os, sys, io
2+
import M5
3+
from M5 import *
4+
from esp32 import NVS
5+
import time
6+
7+
8+
label0 = None
9+
title0 = None
10+
label1 = None
11+
label2 = None
12+
label3 = None
13+
nvs_0 = None
14+
15+
16+
count = None
17+
18+
19+
def setup():
20+
global label0, title0, label1, label2, label3, nvs_0, count
21+
22+
M5.begin()
23+
Widgets.fillScreen(0x222222)
24+
label0 = Widgets.Label("Key", 4, 25, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
25+
title0 = Widgets.Title("NVS Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
26+
label1 = Widgets.Label("Value", 73, 25, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27+
label2 = Widgets.Label("count", 4, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28+
label3 = Widgets.Label("Value", 75, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29+
30+
nvs_0 = NVS("db")
31+
count = 0
32+
nvs_0.set_i32("count", count)
33+
34+
35+
def loop():
36+
global label0, title0, label1, label2, label3, nvs_0, count
37+
M5.update()
38+
count = nvs_0.get_i32("count")
39+
Speaker.tone(2000, 50)
40+
label3.setText(str(count))
41+
label3.setColor(0x33CCFF, 0x000000)
42+
count = (count if isinstance(count, (int, float)) else 0) + 1
43+
time.sleep(1)
44+
nvs_0.set_i32("count", count)
45+
Speaker.tone(3222, 50)
46+
label3.setColor(0xFF6600, 0x000000)
47+
time.sleep(1)
48+
49+
50+
if __name__ == "__main__":
51+
try:
52+
setup()
53+
while True:
54+
loop()
55+
except (Exception, KeyboardInterrupt) as e:
56+
try:
57+
from utility import print_error_msg
58+
59+
print_error_msg(e)
60+
except ImportError:
61+
print("please update to latest firmware")

0 commit comments

Comments
 (0)