I am a novice in microPython and My English is so-so.
My code has 5 images that can be switched between two buttons for display.
Sometimes the code can run normally, sometimes an error is reported.
I tried img2.delete(),or img2.del_async(),or img2.del_delayed(1000),or lv.img.cache_invalidate_src(None)
In order to describe my problem more clearly, I have created a video on Bilibili at 帮忙看看怎么有时候程序运行不了?lvgl+micropython 实体按键_哔哩哔哩_bilibili
In order to describe my problem more clearl
Here is my code
Blockquote import lvgl as lv
import fs_driver
from ili9XXX import *
from encoder_driver import ButtonsInputEncoder, EncoderInputDriver
import network
import gc,micropython
import sys
#micropython.alloc_emergency_exception_buf(100)
lv.init()
#lv.img.cache_set_size(4)
‘’‘mosi–>sda,clk–>scl’‘’
disp = st7735(
clk=18,mosi=23,rst=22,dc=21, cs=4, backlight=15, backlight_on=1,
width=128, height=160,colormode=COLOR_MODE_RGB, invert=False , rot=PORTRAIT
,start_x=2, start_y=1, double_buffer=False)#dc=12, rst=13,MADCTL_MX|MADCTL_MY False True
button_encoder = ButtonsInputEncoder(left=27, right=25, press=26)
button_driver = EncoderInputDriver(button_encoder)
scr = lv.scr_act()
scr.set_style_bg_color(lv.color_hex(0x000000), lv.PART.MAIN)
Blockquote
a=1
def btn_event_cb(e):
global a,img2
code = e.get_code()
btn = e.get_target_obj()
a+=1
if code == lv.EVENT.CLICKED:
img2.delete()
img2 = lv.img(lv.scr_act())
img2.set_src(fun(a))
img2.align(lv.ALIGN.CENTER,0,0)
lv.img.cache_invalidate_src(None)
gc.collect()
#print(gc.mem_free())
label = btn.get_child(0)
label.set_text(str(a))
def btn_event_cb1(e):
global a,img2
code = e.get_code()
btn = e.get_target_obj()
a-=1
if a<1:
a=1
if code == lv.EVENT.CLICKED:
img2.delete()
img2 = lv.img(lv.scr_act())
img2.set_src(fun(a))
img2.align(lv.ALIGN.CENTER,0,0)
lv.img.cache_invalidate_src(None)
gc.collect()
#print(gc.mem_free())
label = btn.get_child(0)
label.set_text(str(a))
def fun(x):
gc.collect()
try:
with open(‘{}.png’.format(x),‘rb’) as f:
png_data = f.read()
except:
print(“Could not find wink.png”)
sys.exit()
#“header”: {“always_zero”: 0, “w”: 128, “h”: 72, “cf”: lv.COLOR_FORMAT.RAW_ALPHA},
wink_argb = lv.img_dsc_t({
‘data_size’: len(png_data),
‘data’: png_data
})
return wink_argb
Blockquote
‘’‘def fun(x):
fs_drv = lv.fs_drv_t()
fs_driver.fs_register(fs_drv, ‘S’)
#img2 = lv.img(lv.scr_act())
# The File system is attached to letter ‘S’
img2.set_src(“S:{}.png”.format(x) )#“S:bulb.gif” “S:222222.gif”
img2.align(lv.ALIGN.CENTER,0,0)’‘’
Blockquote
img2 = lv.img(lv.scr_act())
img2.set_src(fun(a))
img2.align(lv.ALIGN.CENTER,0,0)
#lv.img.cache_set_size(2)#lv.img.cache_invalidate_src()
print(gc.mem_free()
btn = lv.btn(lv.scr_act()) # Add a button the current screen
btn.set_pos(10, 10) # Set its position
btn.set_size(100, 40) # Set its size
btn.align(lv.ALIGN.CENTER,0,-50)
btn.add_event(btn_event_cb, lv.EVENT.CLICKED, None)
label = lv.label(btn) # Add a label to the button
label.set_text(“Button”) # Set the labels text
label.center()
btn1 = lv.btn(lv.scr_act()) # Add a button the current screen
btn1.set_pos(10, 10) # Set its position
btn1.set_size(100, 40) # Set its size
btn1.align(lv.ALIGN.CENTER,0,50)
btn1.add_event(btn_event_cb1, lv.EVENT.CLICKED, None)
label = lv.label(btn1) # Add a label to the button
label.set_text(“Button”) # Set the labels text
label.center()
group = lv.group_create() # Create a group
group.add_obj(btn)
group.add_obj(btn1)
button_driver.group = group
Here is the error message
Traceback (most recent call last):
File “lv_utils.py”, line 124, in task_handler
MemoryError: memory allocation failed, allocating 20000 bytes
Please give me some help and tell me what to do.
thanks