Hello @amirgon, thanks for answering.
Today looking at more examples I managed to understand a little more how the input devices work. But I still can’t get to change the focus between elements in the screen. I’ll share my updated code:
import lvgl as lv
from utime import sleep_ms
from machine import Pin, ADC
import lodepng as png
from imagetools import get_png_info, open_png
lv.init()
vrx = ADC(Pin(32, Pin.IN))
vry = ADC(Pin(33, Pin.IN))
sw = Pin(25, Pin.IN)
vrx.width(ADC.WIDTH_10BIT)
vry.width(ADC.WIDTH_10BIT)
vrx.atten(ADC.ATTN_11DB)
vry.atten(ADC.ATTN_11DB)
from ili9XXX import ili9341
disp = ili9341(mosi=23, miso=19, clk=18, dc=21, cs=5, rst=22, power=-1, backlight=-1)
scr = lv.obj()
def test(drv, data):
read_x = vrx.read()
read_y = vry.read()
press = sw.value()
if read_x > 900:
print("right")
data.key = lv.KEY.PREV
elif read_x < 100:
print("left")
data.key = lv.KEY.NEXT
else:
data.key = 0
if press == 1:
print("pressed")
data.key = lv.KEY.ENTER
data.state = lv.INDEV_STATE.PRESSED
else:
data.key = 0
data.state = lv.INDEV_STATE.RELEASED
return False
indev = lv.indev_drv_t()
indev.init()
indev.type = lv.INDEV_TYPE.ENCODER
indev.read_cb = test
keypad = indev.register()
g = lv.group_create()
keypad.set_group(g)
After that, I have this code to draw a certain screen:
def draw_edit_screen(username, usericon):
h1 = lv.label(scr)
h1.set_pos(96, 16)
h1.set_text("Perfil")
h1 = lv.label(scr)
h1.set_pos(145, 80)
h1.set_text(username)
with open("008-man.png", 'rb') as i:
png_data = i.read()
png_img_dsc = lv.img_dsc_t({
'data_size': len(png_data),
'data': png_data
})
img2 = lv.img(scr)
img2.set_pos(40, 72)
img2.set_zoom(256+256)
raw_dsc = lv.img_dsc_t()
get_png_info(None, png_img_dsc, raw_dsc.header)
dsc = lv.img_decoder_dsc_t({'src': png_img_dsc})
if open_png(None, dsc) == lv.RES.OK:
raw_dsc.data = dsc.img_data
raw_dsc.data_size = raw_dsc.header.w * raw_dsc.header.h * lv.color_t.__SIZE__
img2.set_src(raw_dsc)
btn1 = lv.btn(scr)
btn1.set_pos(16, 150)
btn1.set_width(200)
label1 = lv.label(btn1)
label1.set_text(lv.SYMBOL.OK + " Seleccionar perfil")
g.add_obj(btn1)
btn1 = lv.btn(scr)
btn1.set_pos(16, 190)
btn1.set_width(200)
label1 = lv.label(btn1)
label1.set_text(lv.SYMBOL.EDIT + " Editar nombre")
g.add_obj(btn1)
btn1 = lv.btn(scr)
btn1.set_pos(16, 230)
btn1.set_width(200)
label1 = lv.label(btn1)
label1.set_text(lv.SYMBOL.EDIT + " Editar icono")
g.add_obj(btn1)
btn1 = lv.btn(scr)
btn1.set_pos(16, 270)
btn1.set_width(200)
label1 = lv.label(btn1)
label1.set_text(lv.SYMBOL.TRASH + " Borrar perfil")
g.add_obj(btn1)
draw_edit_screen("Gian", "008-man.png")
lv.scr_load(scr)
This screen ends up looking like this (sorry for the bad lighting):
So, I can see the items are getting focused, and when I declare data.key
as lv.KEY.ENTER
it triggers the buttons callback function. But when it is set as either lv.KEY.RIGHT
, lv.KEY.LEFT
, also NEXT / PREV
, it does not ‘travel’ between buttons (it does print ‘right’ or ‘left’ though, so maybe I am adding them wrong to the groups?).
EDIT: Sorry, forgot to mention, in this code I have removed the button event callbacks, as now I am just trying to get it to move between items.