Device: M5stack
Chip: Esp32 , 520kb ram, no psram
Micropython
I am having problems when connecting to network.
At first I was having a DMA-able memory allocation error when I tried to connect to network and initialize the display which was solved in Not enough DMA-able memory to allocate display buffer by changing the factor in the ili9341.py driver from 4 to 8 or 16.
Now when I connect to network it messes with my program, mainly when I have some kind of list and I try to interact with it using buttons (check video : https://imgur.com/YJBO0JU ). It looks like network is conflicting with some internal loop (maybe event loop?).
Also, when I turn off the wifi my program works as expected⦠I can use the buttons and select an item from the list. When I reconnect it conflicts again.
I am using the test code bellow (you need to have the main.py and the m5_lvgl.py )
main.py
import machine
import gc
import network
import lvgl as lv
from m5_lvgl import ButtonsInputEncoder, EncoderInputDriver
from ili9341 import ili9341
import gc
import utime
import micropython
micropython.alloc_emergency_exception_buf(100)
AUTHENTICATED = False
OPTION1 = False
OPTION2 = False
OPTION3 = False
OPTION4 = False
def connect():
ssid = "xxx"
password = "xxx"
station = network.WLAN(network.STA_IF)
if station.isconnected() == True:
print("Already connected")
return
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print("Connection successful")
print(station.ifconfig())
disp = ili9341() # Create a display driver
connect()
def event_handler(obj, event):
"""
Called when a button is released.
Parameters
----------
btn :
The Button that triggered the event.
event :
The triggering event.
"""
global OPTION1, OPTION2, OPTION3, OPTION4
if event == lv.EVENT.RELEASED:
print("Clicked: %s" % lv.list.get_btn_text(obj))
if lv.list.get_btn_text(obj) == "Option1":
OPTION1 = True
elif lv.list.get_btn_text(obj) == "Option2":
OPTION2 = True
elif lv.list.get_btn_text(obj) == "Option3":
OPTION3 = True
elif lv.list.get_btn_text(obj) == "Option4":
OPTION4 = True
screen = lv.obj()
button_encoder = ButtonsInputEncoder()
button_driver = EncoderInputDriver(button_encoder)
list1 = lv.list(screen)
AUTHENTICATED = True
if AUTHENTICATED:
list1.set_size(300, 154)
list1.align(None, lv.ALIGN.CENTER, 0, -5)
# Add buttons to the list
list_btn = list1.add_btn(lv.SYMBOL.FILE, "Option1")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.DIRECTORY, "Option2")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.DIRECTORY, "Option3")
list_btn.set_event_cb(event_handler)
else:
list1.set_size(300, 100)
list1.align(None, lv.ALIGN.CENTER, 0, -5)
# Add buttons to the list
list_btn = list1.add_btn(lv.SYMBOL.FILE, "Option2")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.DIRECTORY, "Option3")
list_btn.set_event_cb(event_handler)
group = lv.group_create() # Create a group
lv.group_add_obj(group, list1)
button_driver.group =group
lv.group_set_style_mod_cb(group, None)
lv.group_set_style_mod_edit_cb(group,None)
lv.group_set_editing(group, True)
lv.scr_load(screen)
m5_lvgl.py
import gc
import struct
import lvgl as lv
import lvesp32
import machine
import utime
DEFAULT_ENCODER_ADDR = 0x5E # (94)
__all__ = ['ButtonsInputEncoder', 'FacesEncoderInputEncoder',
'EncoderInputDriver', 'general_event_handler', 'init_ili9341']
class ButtonsInputEncoder:
def __init__(self, left=39, right=37, press=38):
self._left = 0
self._right = 0
self._pressed = False
def on_press_left(*args):
self._left_time = utime.ticks_ms()
self._left += 1
def on_press_right(*args):
self._right_time = utime.ticks_ms()
self._right += 1
def on_toggle_press(pin):
self._press_time = utime.ticks_ms()
self._pressed = not pin.value()
btn_left = machine.Pin(left, machine.Pin.IN, machine.Pin.PULL_UP)
btn_left.irq(trigger=machine.Pin.IRQ_RISING, handler=on_press_left)
btn_right = machine.Pin(right, machine.Pin.IN, machine.Pin.PULL_UP)
btn_right.irq(trigger=machine.Pin.IRQ_RISING, handler=on_press_right)
btn_press = machine.Pin(press, machine.Pin.IN, machine.Pin.PULL_UP)
btn_press.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING,
handler=on_toggle_press)
@property
def diff_peek(self):
return self._right - self._left
@property
def diff(self):
diff = self._right - self._left
self._left = 0
self._right = 0
return diff
@property
def pressed(self):
return self._pressed
class EncoderInputDriver:
def __init__(self, encoder, group=None):
def input_callback(drv, data):
data.enc_diff = encoder.diff
if encoder.pressed:
data.state = lv.INDEV_STATE.PR
else:
data.state = lv.INDEV_STATE.REL
gc.collect()
return False
self.drv = lv.indev_drv_t()
self.encoder = encoder
lv.indev_drv_init(self.drv)
self.drv.type = lv.INDEV_TYPE.ENCODER
self.drv.read_cb = input_callback
self.win_drv = lv.indev_drv_register(self.drv)
self.group = group
@property
def group(self):
return self._group
@group.setter
def group(self, value):
self._group = value
if self._group is not None:
lv.indev_set_group(self.win_drv, self._group)