Skip to content

Please add rotation to Touchscreen #7

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

Closed
RichardA1 opened this issue Jan 16, 2020 · 8 comments · Fixed by #14
Closed

Please add rotation to Touchscreen #7

RichardA1 opened this issue Jan 16, 2020 · 8 comments · Fixed by #14
Labels
enhancement New feature or request

Comments

@RichardA1
Copy link
Contributor

I am not sure how it could be added to this library, but I created the following code to recalculate the touch coordinates based on the orientation of the screen set by board.DISPLAY.rotation. This fixes an issue with how the hotspots are setup from the Button() function used in Displayio.

### Function for recalculating .touch_point based on Display rotation
def touch_rotate():
    touchData = ts.touch_point
    if touchData:
        if display.rotation == 0:
            return touchData
        elif display.rotation == 90:
            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)
            return (touchData[1],int(touchX) )
        elif display.rotation == 180:
            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)
            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)
            return (int(touchX), int(touchY))
        elif display.rotation == 270:
            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)
            return (int(touchY),touchData[0] )
@RichardA1
Copy link
Contributor Author

So I just got an update pointing me to this option being setup via the adafruit_touchscreen.Touchscreen(). So here are the configurations for each orientation of the screen.

# -------Rotate 0:
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
                                      board.TOUCH_YD, board.TOUCH_YU,
                                      calibration=((5200, 59000), (5800, 57000)),
                                      size=(320, 240))

# -------Rotate 90:
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_YU, board.TOUCH_YD,
                                      board.TOUCH_XL, board.TOUCH_XR, 
                                      calibration=((5200, 59000), (5800, 57000)),
                                      size=(240, 320))

# ------Rotate 180:
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XR, board.TOUCH_XL,
                                      board.TOUCH_YU, board.TOUCH_YD,
                                      calibration=((5200, 59000), (5800, 57000)),
                                      size=(320, 240))

# ------Rotate 270:
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_YD, board.TOUCH_YU,
                                      board.TOUCH_XR, board.TOUCH_XL,
                                      calibration=((5200, 59000), (5800, 57000)),
                                      size=(240, 320))

Basically the board.TOUCH pins need to be rearranged along with switching the size entries for the 90 and 270 degree rotation.

It would be helpful to add something that selects the correct board.TOUCH pin configurations based on the screen orientation. Maybe a new parameter for adafruit_touchscreen.Touchscreen() called rotation just like it is for Display.

I also suggest adding more detailed documentation for what the parameters are responsible for in adafruit_touchscreen.Touchscreen() something like:

class: adafruit_touchscreen.Touchscreen(x1_pin, x2_pin, y1_pin, y2_pin, *, x_resistance=None, samples=4, z_threshhold=10000, calibration=None, size=None)

Parameters:

  • x1_pin (int?) – Data pin for Left side of the screen.
    • Must also be capable of becoming AnalogIn pins.
  • x2_pin (int?) – Data pin for Right side of the screen.
    • Must also be capable of becoming AnalogIn pins.
  • y1_pin (int?) – Data pin for Bottom side of the screen.
  • y2_pin (int?) – Data pin for Top side of the screen.
    • Must also be capable of becoming AnalogIn pins.
  • x_resistance (int?) – If you know the resistance across the x1 and x2 pins when not touched,
    pass that in as 'x_resistance'.
  • samples (int?) – By default we oversample 4 times, change by adjusting 'samples' arg.
  • z_threshhold (int?) – We can also detect the 'z' threshold, how much its pressed. We don't
    register a touch unless its higher than 'z_threshold'
  • calibration (tuple) – A tuple of two tuples, the default is
    ((0, 65535), (0, 65535)). The numbers are the min/max readings for the
    X and Y coordinate planes, respectively.
  • size (list?) – The dimensions of the screen as (x, y)

There is actually some really helpful notes in the adafruit_touchscreen.py file that would have been supper helpful if I knew they were there.

"""Create the Touchscreen object. At a minimum you need the 4 pins

@ladyada
Copy link
Member

ladyada commented Jan 18, 2020

yes! wanna add something to the readme or example?

@RichardA1
Copy link
Contributor Author

I think Adding the more detailed info about the adafruit_touchscreen.Touchscreen() class parameters along with adding that use comments from adafruit_touchscreen.py would be most helpful.

I could also come up with an example for changing the orientation of the display and touchscreen that would let a user uncomment the correct orientation for the touchscreen.

@ladyada
Copy link
Member

ladyada commented Jan 21, 2020

either way is fine by us :)

@kattni
Copy link
Contributor

kattni commented May 4, 2020

@RichardA1 Are you still interested in working on this?

@kattni kattni added the enhancement New feature or request label May 4, 2020
@RichardA1
Copy link
Contributor Author

I am a bit swamped with other stuff ATM, but I could try to figure it out at some point unless someone else more familiar wanted to give it a go. I am sure I could do it but not nearly as fast as someone with more Python experience.

@evaherrada
Copy link
Collaborator

evaherrada commented Jul 1, 2020

Just thought it'd be worth linking #8 since it's related and I don't think was ever linked. I'll take a crack at adding this to the library

Edit: I don't actually have the hardware. I'll see about ordering some to do this.

@jposada202020
Copy link
Contributor

@RichardA1 I could work improving the documentation if you are not planning to. The example with the orientation example was merged. Let me know thanks 💯

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

Successfully merging a pull request may close this issue.

5 participants