Hey there, I’ve recently received my esp32-s3 and setup the LVGL library. All works besides touch. I went off this tutorial here. https://www.youtube.com/watch?v=dbjVZEuHTyQ&list=LL&index=1
That tutorial sets up LVGL With xpt2046 touch controls, But i need FT6336 touch controls.
I get errors when trying to use LVGL built in touch detection.
This is the code from that video using the xpt2046 driver.
# test_keyboard2_display.py
#
# Updated: 19 May 2025 for MP on ESP32
#
# Copyright (C) 2025 KW Services.
# MIT License
# MicroPython v1.20.0-2504.g9fe842956 on 2025-05-17; Generic ESP32S3 module with Octal-SPIRAM with ESP32S3
# ESP32-S3 with SPIRAM_OCT (N16R8)
# LVGL 9.3 <-------------------------------Note
#
#import display_driver
import lvgl as lv
from display_driver import disp, disp_drv, touch
import time
##############################################
# UI
###############################################
# current screen
scr = lv.obj()
try:
from display_driver import HardReset
h = HardReset(scr)
except ImportError:
pass
#### Frame the screen #####################
scr.set_style_bg_color(lv.color_hex(0),0)
scr.set_style_border_width(2, 0)
scr.set_style_border_color(lv.palette_main(lv.PALETTE.BLUE),0)
#### Text Area ############
style = lv.style_t()
style.init()
style.set_bg_color(lv.color_black())
style.set_text_color(lv.palette_main(lv.PALETTE.YELLOW))
style.set_border_color(lv.palette_main(lv.PALETTE.BLUE))
ta = lv.textarea(scr)
ta.add_style(style, lv.PART.MAIN)
ta.set_size(120, 50)
ta.align(lv.ALIGN.BOTTOM_LEFT,10,-10)
ta.set_placeholder_text("enter text")
#### Keyboard ###########
kb = lv.keyboard(scr)
kb.align(lv.ALIGN.TOP_LEFT, 0, 0)
kb.set_textarea(ta)
kb.set_style_bg_color(lv.color_black(), lv.PART.MAIN)
kb.set_style_text_color(lv.color_black(),lv.PART.ITEMS)
ta.align_to(kb, lv.ALIGN.BOTTOM_LEFT,10,70)
btn = lv.button(scr)
btn.set_pos(245,140)
btn.align_to(kb,lv.ALIGN.BOTTOM_RIGHT,-45,45)
lbl = lv.label(btn)
lbl.set_text("Clear")
lbl.set_style_text_color(lv.color_black(), lv.PART.MAIN)
def btn_clear_cb(e):
global cnt
ta.set_text("")
print("Clicked clear button.")
btn.add_event_cb(btn_clear_cb, lv.EVENT.CLICKED, None)
###################################################
lv.screen_load(scr)
# Run the event loop
# while True:
# lv.timer_handler()
# time.sleep_ms(10)
Can somebody supply my with a basic LVGL code with a button and have that button print something when clicked?
This code uses manual touch polling which means it places a button on the screen and checks if you’ve touched in that area. This is a terrible approach to doing this but thats only been whats worked so far.
import lvgl as lv
import time
# Assume 'touch' is your uFT6336 touch driver instance
# and 'scr' is the loaded LVGL screen object
# --- Setup your screen
scr = lv.obj()
lv.screen_load(scr)
# --- Create some buttons
btn1 = lv.button(scr)
btn1.set_size(120, 50)
btn1.align(lv.ALIGN.CENTER, 0, -40)
label1 = lv.label(btn1)
label1.set_text("Button 1")
btn2 = lv.button(scr)
btn2.set_size(120, 50)
btn2.align(lv.ALIGN.CENTER, 0, 40)
label2 = lv.label(btn2)
label2.set_text("Button 2")
# --- Define callbacks
def btn1_click():
print("Button 1 clicked!")
def btn2_click():
print("Button 2 clicked!")
# --- Helper to check if point is inside obj
def point_in_obj(obj, x, y):
coords = obj.get_coords()
return (x >= coords.x1 and x <= coords.x2 and
y >= coords.y1 and y <= coords.y2)
# --- Variables to track touch state
was_pressed = False
pressed_btn = None
while True:
lv.task_handler()
pos = touch.get_positions()
if pos:
x, y = pos[0]
if not was_pressed:
# Detect which button is pressed
if point_in_obj(btn1, x, y):
pressed_btn = btn1
btn1_click()
elif point_in_obj(btn2, x, y):
pressed_btn = btn2
btn2_click()
else:
pressed_btn = None
was_pressed = True
else:
was_pressed = False
pressed_btn = None
time.sleep_ms(10)
I got my firmware for lvgl + micropython on esp32-s3 from here for any of you wondering / trying to help me.
https://github.com/kwinter745321/ESP32LVGL/tree/main/Firmware/May2025
If someone could provide me basic code thats uses LVGL’s touch detection please do! Im stuck… There is no tutorials online. If i worded this wrong please reply to this thread and i’ll get back to you with a clearer answer to your quesiton right away. Any help is appreciated. Thank you!