Skip to content
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

TSC2007 X,Y in wrong orientation for TFT 2.4" FeatherWing V2 #5

Closed
rimwolf-redux opened this issue Dec 1, 2023 · 1 comment · Fixed by #6
Closed

TSC2007 X,Y in wrong orientation for TFT 2.4" FeatherWing V2 #5

rimwolf-redux opened this issue Dec 1, 2023 · 1 comment · Fixed by #6
Assignees

Comments

@rimwolf-redux
Copy link

When I run the usage example from the adafruit_tsc2007 documentation on a TFT 2.4" FeatherWing V2 (controlled by an M4 Feather in my case), the point['y']value ranges from about 300 to 3600 as I move the touchpoint from left to right on the screen, and the point['x'] value ranges from about 3600 to 300 as I move the touchpoint from top to bottom. This is assuming the screen orientation that is established by using the screen initialization in the TFT 2.4" FeatherWing guide (ILI9341(display_bus, width=320, height= 240)), with the reset switch on the right hand edge and the Feather's USB plug at the top, and the (0,0) point in the upper left corner.

The point values are what one would expect if the screen were in portrait mode, with(0,0) point at the lower-left corner in this landscape orientation. Perhaps one way of addressing this issue is to provide an extra parameter to the TSC2007 initializer which indicates that a coordinate transform is necessary, which maps the (x, y) values in the touch result dict to (y, 4095-x), and documenting it accordingly in the FeatherWing's (as yet unprovided) V2 documentation. (Does the 3.5" FeatherWing have the same issue?)

@mikeysklar
Copy link

@alpierce is using the 3.5" FeatherWing TFT and came up with this solution.

# displayio buttons on Adafruit 3.5" TFT FeatherWing V2
import time
import board
import terminalio
import displayio
from adafruit_hx8357 import HX8357
import adafruit_tsc2007
from adafruit_button import Button
import adafruit_touchscreen
from adafruit_bitmap_font import bitmap_font

i2c = board.STEMMA_I2C()

displayio.release_displays()

spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)

display = HX8357(display_bus, width=480, height=320)


my_display_group = displayio.Group()
display.root_group = my_display_group

font = bitmap_font.load_font("/fonts/GothamBlack-25.bdf")

irq_dio = None
tsc = adafruit_tsc2007.TSC2007(i2c, irq=irq_dio)

# Raw TSC2007 touch range (example values, calibrate these!)
raw_x_min, raw_x_max = 300, 3800
raw_y_min, raw_y_max = 200, 4000

# HX8357 screen resolution
screen_width, screen_height = 480, 320

def scale_touch_coordinates(raw_x, raw_y):
    """Scale raw touch coordinates to screen coordinates."""
    # Scale X
    scaled_x = (raw_x - raw_x_min) * screen_width / (raw_x_max - raw_x_min)
    scaled_x = min(max(scaled_x, 0), screen_width)

    # Scale Y
    scaled_y = (raw_y - raw_y_min) * screen_height / (raw_y_max - raw_y_min)
    scaled_y = min(max(scaled_y, 0), screen_height)

    return int(scaled_x), int(scaled_y)

button1 = Button(
    x=10,
    y=10,
    width=display.width//2,
    height=display.height//3,
    style=Button.ROUNDRECT,
    fill_color=0x330040,
    outline_color=0xFF00FF,
    label="button1",
    label_font=bitmap_font.load_font("/fonts/Lato-Bold-ltd-25.bdf"),
    label_color=0xFF00FF,
    selected_fill=0x220022,
    selected_outline=0xFF0000,
    selected_label=0xFF0000
)

button2 = Button(
    x=10,
    y=display.height//3 + 20,
    width=display.width//2,
    height=display.height//3,
    style=Button.ROUNDRECT,
    fill_color=0x330040,
    outline_color=0xFF00FF,
    label="button2",
    label_font=bitmap_font.load_font("/fonts/Lato-Bold-ltd-25.bdf"),  # terminalio.FONT,
    label_color=0xFF00FF,
    selected_fill=0x220022,
    selected_outline=0xFF0000,
    selected_label=0xFF0000
)

my_display_group.append(button1)
my_display_group.append(button2)

b1_state = False
b2_state = False

while True:
    if tsc.touched:
        raw = tsc.touch
        print(raw)
        y, x = raw["x"], raw["y"]
        scaled_x, scaled_y = scale_touch_coordinates(x, y)
        scaled_y = abs(scaled_y - 320)
        print("Scaled Touch:", scaled_x, scaled_y)
        point = (scaled_x, scaled_y)
        print(point)
        if button1.contains(point):
            button1.selected = True
            if not b1_state:
                print("button1 pressed")
                b1_state=True
        else:
            button1.selected = False  # if touch is dragged outside of button
            if b1_state:
                print("button1 released")
                b1_state=False

        if button2.contains(point):
            button2.selected = True
            if not b2_state:
                print("button2 pressed")
                b2_state=True
        else:
            button2.selected = False
            if b2_state:
                print("button2 released")
                b2_state=False
    else:
        button1.selected = False  # if touch is released
        if b1_state:
            print("button1 released")
            b1_state=False
        button2.selected = False
        if b2_state:
            print("button2 released")
            b2_state=False

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

Successfully merging a pull request may close this issue.

3 participants