I am wanting to create a custom input device in micropython using lvgl 9.0. I think I have it setup correctly. Documentation for micropython side is a bit sparse. In concept I have two machine.Pin buttons that I want to map to key up and down. The key presses are seen. The event handler is registered and will see “ALL” events. But, the key press is not resulting in an event, despite setting STATE and KEY. I have scoured the forums and documentation, but I am at a loss.
Thank you in advance.
# Custom input device driver
class InputDriver:
def __init__(self):
from mct_buttons import SMU,SMD
self.smu = SMU
self.smd = SMD
print(f'__init__ {self}')
print(f'smu = {self.smu}')
self.input_driver = lv.indev_create()
self.input_driver.set_type(lv.INDEV_TYPE.KEYPAD)
self.input_driver.set_read_cb(self.read_cb)
print("Initialized")
def read_cb(self, indev_drv, data):
if self.smu.value() == 0:
print("SMU")
data.state = lv.INDEV_STATE.PRESSED
data.key = lv.KEY.UP # Simulate an "up" key press
else:
data.state = lv.INDEV_STATE.RELEASED
if self.smd.value() == 0:
print("SMD")
data.state = lv.INDEV_STATE.PRESSED
data.key = lv.KEY.DOWN # Simulate an "up" key press
else:
data.state = lv.INDEV_STATE.RELEASED
return False # Continue reading the input
# Initialize the custom input driver
smu_input_driver = InputDriver()
# Create a screen and an object
obj1 = lv.screen_active()
obj2 = lv.obj()
obj1.set_style_bg_color(lv.color_make(255, 0, 0),0)
obj2.set_style_bg_color(lv.color_make(0, 255, 0),0)
# Create a dictionary mapping the event codes to their names
event_names = {value: name for name, value in lv.EVENT.__dict__.items() if not name.startswith("__")}
# Function to get the event name from the code
def get_event_name(event_code):
return event_names.get(event_code, "Unknown Event")
# Event handler functions
def obj1_event_cb(event):
event_name = get_event_name(event.code)
print(event.code, ": ", event_name)
if event == lv.EVENT.PRESSED:
print("I am object1 - Button pressed")
def obj2_event_cb(event):
if event == lv.EVENT.CLICKED:
print("I am object2 - Button clicked")
# Assign event handlers
obj1.add_event_cb(obj1_event_cb, lv.EVENT.ALL, None)
obj2.add_event_cb(obj2_event_cb, lv.EVENT.ALL, None)