guglielmocelata
Posts: 1
Joined: Tue Apr 08, 2014 6:08 am

mouse position in pygame with piTFT

Fri Jan 08, 2016 6:59 pm

I am trying to use Pi-Jukebox with a PiTFT 2.8" resistive touchscreen an a Raspberry Pi (2011.12).
I've installed the Jessie image (https://s3.amazonaws.com/adafruit-downl ... tft28r.zip).

I'm having problems with the touchscreen, but only when launching the pygame-based program.

The mouse position detected when the screen is touched is wrong. Positions are erratic and clicking more than once on the same spot on the screen yields different results.

All calibration and tests, as described in https://learn.adafruit.com/adafruit-pit ... -calibrate works properly.

But any program using pygame seems to show this behaviour. This is a test code:

Code: Select all

import pygame
from pygame.locals import *
import os
from time import sleep
#Colours
WHITE = (255,255,255)

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()
lcd = pygame.display.set_mode((320, 240))
lcd.fill((0,0,0))
pygame.display.update()

while True:
    # Scan touchscreen events
    for event in pygame.event.get():
        if(event.type is MOUSEBUTTONDOWN):
            pos = pygame.mouse.get_pos()
            print pos
        elif(event.type is MOUSEBUTTONUP):
            pos = pygame.mouse.get_pos()
            print pos
    sleep(0.1)

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: mouse position in pygame with piTFT

Sat Jan 09, 2016 7:57 am

Hello,

I think Pygame is kind of messed up under Jessie, well for the TFT at least. You can read about it here
https://bugs.debian.org/cgi-bin/bugrepo ... bug=798160

A lot of people will be in the same boat as you (and me) and right now there are a couple of options.
  • 1. go back to Wheezy and use Pygame
    2. find an alternative to Pygame
    3. follow this solution (it work for me), but it is only a temporary measure as I think the future of SDL in Pygame for the TFT is in question.
https://forums.adafruit.com/viewtopic.p ... 58#p435225

the same thread talks about compiling the source, but the above method worked for me so I didn't bother.

as always, I am a newb, so back up your data before you take any of my advice.

good luck

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

Re: mouse position in pygame with piTFT

Thu Mar 10, 2016 8:52 pm

This works for me too - thanks for sharing. Using the script outlined in the link above :

Create an install script :

Code: Select all

sudo nano installsdl.sh
Paste the following code :

Code: Select all

#!/bin/bash

#enable wheezy package sources
echo "deb http://archive.raspbian.org/raspbian wheezy main
" > /etc/apt/sources.list.d/wheezy.list

#set stable as default package source (currently jessie)
echo "APT::Default-release \"stable\";
" > /etc/apt/apt.conf.d/10defaultRelease

#set the priority for libsdl from wheezy higher then the jessie package
echo "Package: libsdl1.2debian
Pin: release n=jessie
Pin-Priority: -10
Package: libsdl1.2debian
Pin: release n=wheezy
Pin-Priority: 900
" > /etc/apt/preferences.d/libsdl

#install
apt-get update
apt-get -y --force-yes install libsdl1.2debian/wheezy
Make it executable, then run it :

Code: Select all

sudo chmod +x installsdl.sh
sudo ./installsdl.sh
My standard test code should now work with Jessie :

Code: Select all

#!/usr/bin/python
# touchv6
# Texy 5/12/13

import pygame, sys, os, time
from pygame.locals import *

from evdev import InputDevice, list_devices
devices = map(InputDevice, list_devices())
eventX=""
for dev in devices:
    if dev.name == "ADS7846 Touchscreen":
        eventX = dev.fn
print eventX

os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ["SDL_MOUSEDRV"] = "TSLIB"
os.environ["SDL_MOUSEDEV"] = eventX

pygame.init()

# set up the window
screen = pygame.display.set_mode((320, 240), 0, 32)
pygame.display.set_caption('Drawing')

# set up the colors
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)
CYAN  = (  0, 255, 255)
MAGENTA=(255,   0, 255)
YELLOW =(255, 255,   0)

# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(WHITE)
box = pygame.draw.rect(background, YELLOW,(40, 0, 40, 240))
box = pygame.draw.rect(background,  CYAN, (80, 0, 40, 240))
box = pygame.draw.rect(background, GREEN, (120, 0, 40, 240))
box = pygame.draw.rect(background,MAGENTA,(160, 0, 40, 240))
box = pygame.draw.rect(background, RED,   (200, 0, 40, 240))
box = pygame.draw.rect(background, BLUE  ,(240, 0, 40, 240))
box = pygame.draw.rect(background, BLACK ,(280, 0, 40, 240))

# Display some text
font = pygame.font.Font(None, 36)
text = font.render("Touch here to quit", 1, (BLACK))
#text = pygame.transform.rotate(text,270)
textpos = text.get_rect(centerx=background.get_width()/2,centery=background.get_height()/2)
background.blit(text, textpos)

screen.blit(background, (0, 0))
pygame.display.flip()

running = True
# run the game loop
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            print("Pos: %sx%s\n" % pygame.mouse.get_pos())
            if textpos.collidepoint(pygame.mouse.get_pos()):
                pygame.quit()
                sys.exit()
                running = False
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            running = False
    pygame.display.update()
Thank to all involved with this fix.

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

keos
Posts: 3
Joined: Sat Nov 26, 2016 1:13 pm

Re: mouse position in pygame with piTFT

Fri Mar 17, 2017 9:36 am

Hi all,

I have a PiTFT 28c and I have almost the same problem.
I don't really know what should I do.For me the mousedevposition is always (or refresing really quickly) in the top left corner on the screen.

Also the calibration button icon only stay in the middle of the screen.

I used this script to check the coordinates.

Code: Select all

#!/usr/bin/python                                                                                                
import os
import locale
import logging as log
import pygame
from time import *

# Tell the RPi to use the TFT screen and that it's a touchscreen device
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV'      , '/dev/fb1')
os.putenv('SDL_MOUSEDRV'   , 'TSLIB')
os.putenv('SDL_MOUSEDEV'   , '/dev/input/touchscreen')

class test():
        def __init__(self):
                pygame.init()
                self.screen = pygame.display.set_mode((320, 240))

        def main_loop(self):
                while True:
                        event = pygame.event.wait()
                        if event.type == pygame.MOUSEBUTTONDOWN:
                                log.debug("MOUSEBUTTONDOWN:{}".format(pygame.mouse.get_pos()))
                                pygame.event.clear()

if __name__ == "__main__":
        log.basicConfig(filename="/tmp/log",level=log.DEBUG)
        main_window = test()
        main_window.main_loop()
And the result is this:
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(288, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 64)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(165, 64)
DEBUG:root:MOUSEBUTTONDOWN:(1, 64)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(0, 0)
DEBUG:root:MOUSEBUTTONDOWN:(4, 0)

Return to “Python”