Mouse input rotated on PiTFT in pygame

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
henrikmon
 
Posts: 2
Joined: Sat Apr 27, 2013 8:11 am

Mouse input rotated on PiTFT in pygame

Post by henrikmon »

Hi
I'm trying to make a GUI on my PiTFT using pygame. I've loaded the following test code from the CLI without starting X-window:

Code: Select all

import pygame, sys, os
from pygame.locals import *
os.environ["SDL_FBDEV"] = "/dev/fb1"

windowsize = 320, 240
windowtitle = "Drawing"
windowfullscreen = pygame.FULLSCREEN

# set up the colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
GRAY = ( 192, 192, 192)

pygame.init()
DISPLAYSURF = pygame.display.set_mode(windowsize)
DISPLAYSURF.fill(GRAY)
pygame.draw.rect(DISPLAYSURF, WHITE, (20, 20, 280, 200))
pygame.draw.rect(DISPLAYSURF, BLACK, (40, 40, 240, 160))

pygame.display.set_caption(windowtitle)
while True: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            
    pygame.display.update()
It Works great, but the mouse input is not rotated 90 like the output on the screen, resulting in the curser not clicking the same spot as the stylus is touching.
Is there anyone that know a way to rotate the screen input? Or mayby some sort of calibrating that's not interfering with the calibartaion in X-window?

Dave_Hunt
 
Posts: 5
Joined: Fri Aug 24, 2012 12:10 pm

Re: Mouse input rotated on PiTFT in pygame

Post by Dave_Hunt »

I'm having a similar problem myself, I've rotated the screen to 0 in the /etc/modprobe.d/adafruit.conf, but when I test the input, the cursor is moving to the opposite axis to my finger movements - x=y and y=x. I can't for the life of me remember how I solved this when I did the PiPhone...
Any reminders greatly appreciated....
Rgds,
Dave.

Dave_Hunt
 
Posts: 5
Joined: Fri Aug 24, 2012 12:10 pm

Re: Mouse input rotated on PiTFT in pygame

Post by Dave_Hunt »

From the Capacative setup guide:
"With a resistive touchscreen, you have to calibrate it. Since capacitive touchscreens don't require calibration you can just input the numbers directly. Run
sudo nano /etc/pointercal"
That's probably why I didnt try calibrating it until now. However, I found that calibrating my capacitive screen was indeed the solution I needed. Maybe if you leave the screen at default orientation you don't need calibrating, but you do if you change it.
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test
And all is good. The input was now in the correct orientation.

By the way, my /etc/pointercal now contains the following, in case it's handy for others
-66241 -2271 15965738 -723 -67463 21000260 65536
Regards,
Dave.

User avatar
henrikmon
 
Posts: 2
Joined: Sat Apr 27, 2013 8:11 am

Re: Mouse input rotated on PiTFT in pygame

Post by henrikmon »

I tried to calibrate my screen with the commands given by Dave_hunt but the calibration was still rotated. Then I thought: "Is pygame loading my calibration?"
I searched the web again, and found that if I added the following lines to my script, i works:

Code: Select all

os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"
os.environ["SDL_MOUSEDRV"] = "TSLIB"
I assuming that it is just loading my calibration to pygame but I am not sure what it does. I only know it works.

User avatar
eosnut
 
Posts: 4
Joined: Sun Jun 01, 2014 1:55 am

Re: Mouse input rotated on PiTFT in pygame

Post by eosnut »

Henrikmon wrote:

Code: Select all

os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"
os.environ["SDL_MOUSEDRV"] = "TSLIB"
I assuming that it is just loading my calibration to pygame but I am not sure what it does. I only know it works.
I can verify that does indeed fix the issue. Thanks.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”