Skip to content

XPT2046, x-axis inverted after calibration #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Kalrkloss opened this issue Apr 10, 2025 · 6 comments
Open

XPT2046, x-axis inverted after calibration #325

Kalrkloss opened this issue Apr 10, 2025 · 6 comments

Comments

@Kalrkloss
Copy link

Kalrkloss commented Apr 10, 2025

Describe the bug
Calibration runs with screen orientation 0°, but afterwards the x-axis of the touch screen is inverted, meaning when touching an object on the right side of the screen, the left side is instead selected, and vice versa. The y-axis is fine. Can I configure this somehow?

When trying to calibrate with other orientations, the calibration points appear off the screen.
When changing the orientation after calibration, the touch is all over the place.

The display driver is an ILI9341, screen is TFT 320x240.

Expected behavior
Both axis being configured right.

import lcd_bus
from micropython import const
import machine


# display settings
_WIDTH = const(240)
_HEIGHT = const(320)
_BL = const(46)
_RST = const(11)
_DC = const(8)

_MOSI = const(13)
_MISO = const(12)
_SCK = const(14)
_HOST = const(1)  # SPI2

_LCD_CS = const(10)
_LCD_FREQ = const(80000000)

_TOUCH_CS = const(9)
_TOUCH_FREQ = const(1000000)

spi_bus = machine.SPI.Bus(
    host=_HOST,
    mosi=_MOSI,
    miso=_MISO,
    sck=_SCK
)

display_bus = lcd_bus.SPIBus(
    spi_bus=spi_bus,
    freq=_LCD_FREQ,
    dc=_DC,
    cs=_LCD_CS,
)


import ili9341  # NOQA
import lvgl as lv  # NOQA


display = ili9341.ILI9341(
    data_bus=display_bus,
    display_width=_WIDTH,
    display_height=_HEIGHT,
     reset_pin=_RST,
    reset_state=ili9341.STATE_LOW,
    backlight_pin=_BL,
    backlight_on_state=ili9341.STATE_HIGH,
    color_space=lv.COLOR_FORMAT.RGB565,
    color_byte_order=ili9341.BYTE_ORDER_RGB,
    rgb565_byte_swap=True,
)

import task_handler  # NOQA
import xpt2046  # NOQA

display.set_power(True)
display.init(1)
display.set_color_inversion(True)
#display.set_rotation(lv.DISPLAY_ROTATION._270)

display.set_backlight(100)

touch_dev = machine.SPI.Device(
    spi_bus=spi_bus,
    freq=_TOUCH_FREQ,
    cs=_TOUCH_CS
)

indev = xpt2046.XPT2046(touch_dev,debug=False,startup_rotation=lv.DISPLAY_ROTATION._0)

#indev.calibrate()


th = task_handler.TaskHandler()

scrn = lv.screen_active()
#scrn.set_style_bg_color(lv.color_hex(0xFFFFFF), 0)

#btnm = lv.buttonmatrix(scrn)
#btnm.add_event_cb(lambda e: btnm_event_handler(e,scrn),lv.EVENT.VALUE_CHANGED, None)
#btnm.set_size(230,120)
#btnm.align(1,5,5)

tabview = lv.tabview(scrn)
tabview.set_tab_bar_size(30)

tab1 = tabview.add_tab("Tab 1")
tab2 = tabview.add_tab("Tab 2")
tab3 = tabview.add_tab("Tab 3")

# Add content to the tabs
label1 = lv.label(tab1)
label1.set_text("This is the content of Tab 1")

#label2 = lv.label(tab2)
#label2.set_text("This is the content of Tab 2")

label3 = lv.label(tab3)
label3.set_text("This is the content of Tab 3")




btn = lv.button(tab1)
btn.center()
btn.set_size(100,50)
lbl = lv.label(btn)
lbl.set_text('Start')
lbl.center()

tab2.set_flex_flow(lv.FLEX_FLOW.COLUMN)
lab21 = lv.label(tab2)
lab21.set_text('Group 1')
chk21 = lv.checkbox(tab2)
chk21.set_text('Option 1')
chk22 = lv.checkbox(tab2)
chk22.set_text('Option 2')
chk23 = lv.checkbox(tab2)
chk23.set_text('Option 3')
chk24 = lv.checkbox(tab2)
chk24.set_text('Option 4')
lab22 = lv.label(tab2)
lab22.set_text('Group 2')
chk25 = lv.checkbox(tab2)
chk25.set_text('Option 5')
chk26 = lv.checkbox(tab2)
chk26.set_text('Option 6')
chk27 = lv.checkbox(tab2)
chk27.set_text('Option 7')
chk28 = lv.checkbox(tab2)
chk28.set_text('Option 8')

o = 1
def btnm_event_handler(e,ta):
    global o
    obj = e.get_target()
    o=obj
    print("Toggled")

**Exact make and model number of the MCU that you are compiling for or the firmware is running on. **

  • Make: ESP32
  • Model: S3 WROOM N16R8

For ESP32 MCU's The PSRAM and FLASH SPI type, quad SPI or octal SPI.

  • PSRAM: octal
  • FLASH: octal

I need to know the OS and OS version of the machine that compiled the binary. If using a VM then I need to know the OS and OS version the VM is running. (WSL == VM)

  • OS: Windows 10 WSL v2 (doesn't matter, the same happens when compiling with Linux (Debian))

Build Command

python3 make.py esp32 BOARD=ESP32_GENERIC_S3 BOARD_VARIANT=SPIRAM_OCT DISPLAY=ili9341 INDEV=xpt2046
@kdschlosser
Copy link
Collaborator

yes as a matter of fact you can...

import touch_cal_data

touch_cal = TouchCalData('My Display')

indev = xpt2046.XPT2046(touch_dev, touch_cal=touch_cal)

if not indev.is_calibrated:
    indev.calibrate()
    touch_cal.mirrorX = not touch_cal.mirrorX
    touch_cal.save()

only perform the calibration with the rotation set to zero. If you set it to any other rotation the numbers are going to get all mixed up resulting in the coordinates mapping to off display areas.

@Kalrkloss
Copy link
Author

Many thanks!

Can I also do this in the board config?

@kdschlosser
Copy link
Collaborator

???

@Kalrkloss
Copy link
Author

I mean, are there some settings in the board config, that would define this mirroring automatically at build time?

@Kalrkloss
Copy link
Author

Kalrkloss commented Apr 11, 2025

Oh, and I forgot: How do I rotate the screen properly after the calibration?
Whenever I rotate with display.set_rotation(lv.DISPLAY_ROTATION._270) or another angle than 0, the touch is all over the place.
Do I also have to rotate the touch data?

@Kalrkloss
Copy link
Author

Ok, found it myself. I actually have to adapt the mirroring to the display angle, for lv.DISPLAY_ROTATION._90 this would be
touch_cal.mirrorY = not touch_cal.mirrorY, while X stays untouched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants