I am trying to get the Raspberry Pi 7'' touchscreen to run with Python. After googling through the web I started using PyGame as a library to display and get touch events. The displaying part works great, but I struggle with the touch events. The coordinates I get back are totaly off, often the bottom right of the screen no matter where I touch. Here is a sample code to demonstrate the problem:
Code: Select all
import pygame
from pygame.locals import *
import os
from time import *
# Init the screen and everything else
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV' , '/dev/fb1')
os.putenv('SDL_MOUSEDRV' , 'TSLIB')
os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen')
pygame.init()
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((800,480),pygame.FULLSCREEN)
screen.fill((0,0,0))
pygame.display.update()
pygame.event.clear()
# Let's just draw a filled rectangle to proof init and pygame worked
# Parameters are the color (255,100,0) and the position (300,160) and size (200,160)
# Result is a orange rectangle in the middle of the screen
pygame.draw.rect(screen, (255,100,0), (300,160,200,160), 0)
# If the display gets touched write of the position to the console for the next 10 seconds
i=0
while i<100:
for event in pygame.event.get():
if(event.type is MOUSEBUTTONUP):
pos = pygame.mouse.get_pos()
print "UP: " + str(pos[0]) + ":" + str(pos[1])
elif(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
print "DOWN: " + str(pos[0]) + ":" + str(pos[1])
pygame.event.clear()
pygame.display.update()
sleep(0.1)
i=i+1
Code: Select all
DOWN: 670:435
DOWN: 799:479
UP: 799:479
DOWN: 799:479
UP: 799:479
DOWN: 799:479
UP: 799:479
DOWN: 799:479
UP: 799:479
DOWN: 799:479
UP: 799:479
DOWN: 799:479
UP: 799:479
...
Code: Select all
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV' , '/dev/fb1')
os.putenv('SDL_MOUSEDRV' , 'TSLIB')
os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen')
Tuxius
