tuxius
Posts: 2
Joined: Sat Jan 02, 2016 10:01 am

Python

Sat Jan 02, 2016 12:32 pm

Hi,

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
Running this gives me the following output while touching the screen randomly all over the place:

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
...
This is not what I expected as I touched the Display all over the place. Anyone got an idea how to fix this? My wild guess is that it has something to do with the init part / using the right "Driver"?

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')
Best
Tuxius

kdbdallas
Posts: 5
Joined: Tue Dec 15, 2015 10:29 pm

Re: Python

Sat Feb 13, 2016 11:56 pm

I am running into this same issue, did you ever figure it out?

tuxius
Posts: 2
Joined: Sat Jan 02, 2016 10:01 am

Re: Python

Fri Apr 01, 2016 7:53 pm

Hi kdbdallas,

so far I have not found a solution, nor did anybody respond besides you :cry:

I would like to appeal to the programmer's kudos - can anyone help?

Best
Tuxius

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python

Sat Apr 02, 2016 8:05 am

Try using Kivy. There's some good sample programs for Kivy with the touchscreen.

viewtopic.php?t=121013
viewtopic.php?t=121392
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

texy
Forum Moderator
Forum Moderator
Posts: 5162
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python

Mon Apr 04, 2016 10:14 am

DougieLawson wrote:Try using Kivy. There's some good sample programs for Kivy with the touchscreen.

viewtopic.php?t=121013
viewtopic.php?t=121392
That's not what the OP is asking for.....

However a couple of possibilities - ts_calibrate may work (I haven't tried it) :

Code: Select all

sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate
the event0 may need to be event1 or another number depending on the number of input devise you have connected.
Also overscan may be a cause, see this thread :
viewtopic.php?f=108&t=121781

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Official Foundation Display”