TopherGopher
Posts: 5
Joined: Sat Jan 30, 2016 4:59 am

One Joystick Key Press, Two Events

Thu Aug 18, 2016 8:57 pm

I'm trying to view of series of images on the Sense Hat using the left and right joystick direction. The code seems to be working, but unfortunately it seems to execute twice when I press in the desired direction. I believe the issue is from the joystick going back to center. Any ideas on how to fix the problem? Here is the code.

Code: Select all

from sense_hat import SenseHat
import glob
from evdev import InputDevice, ecodes, list_devices
from select import select

sense = SenseHat()
sense.clear()

imageNames = glob.glob('/home/pi/8bit/*.jpg')

count = 0

sense.load_image(imageNames[0])

devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
	if dev.name == "Raspberry Pi Sense HAT Joystick":
		js = dev
	
while True:
	r, w, x = select([dev.fd], [], [], 0.01)
	for fd in r:
		for event in dev.read():
			if event.type == ecodes.EV_KEY:# and event.value == 1:
				if event.code == ecodes.KEY_UP:
					print("up")
				elif event.code == ecodes.KEY_LEFT:
					print("left")
					if count > 0:
						count -= 1
						sense.load_image(imageNames[count])
					elif count == 0:
						count = 8
						sense.load_image(imageNames[count])					
				elif event.code == ecodes.KEY_RIGHT:
					print("right")
					if count < 8:
						count += 1
						sense.load_image(imageNames[count])
					elif count == 8:
						count = 0
						sense.load_image(imageNames[count])						
				elif event.code == ecodes.KEY_DOWN:
					print("down")
				else:
					print("enter")

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: One Joystick Key Press, Two Events

Thu Aug 18, 2016 9:29 pm

TopherGopher wrote:if event.type == ecodes.EV_KEY:# and event.value == 1:
With the event.value test commented out, you are responding to press (value==1), release (value==0), and auto-repeat (value==2) events equally.

There is a bug in the device selection too. In the "for dev in devices" loop, you have determined that js is the correct device, so you should be selecting and reading on that, not on dev. They will only be the same if the HAT happens to be the last input device detected.

TopherGopher
Posts: 5
Joined: Sat Jan 30, 2016 4:59 am

Re: One Joystick Key Press, Two Events

Fri Aug 19, 2016 2:16 am

@ jojopi

Brilliant! That is exactly what I needed! Thanks for your help.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: One Joystick Key Press, Two Events

Tue Aug 23, 2016 8:21 am

Folks, did you know this has all been simplified into the Sense HAT API itself? From version 2.2.0 onwards.
Among other things you can now go:

Code: Select all

sense.stick.get_events()
Scroll down to Joystick here: http://pythonhosted.org/sense-hat/api/

You probably have version 2.1.0 installed though so you'll need to pull the latest version of the api.

Code: Select all

sudo apt-get update
sudo apt-get install python-sense-hat python3-sense-hat -y

Return to “Astro Pi”