rhellyer
Posts: 11
Joined: Sat Sep 26, 2015 3:21 am

Headless sense hat with joystick

Tue Sep 29, 2015 5:06 pm

I am trying to create a simple headless game which uses the onboard joystick and led matrix. But pygame requires a screen and so I get an error when trying to use the pygame to access the joystick ... Any suggestions for a workaround?

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6228
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Headless sense hat with joystick

Tue Sep 29, 2015 5:30 pm

There's this:
https://python-evdev.readthedocs.org/en/latest/


Also, works is being done on the python-sense-hat library to make using the joystick easier.

subjektivdk
Posts: 9
Joined: Tue Sep 01, 2015 6:13 pm
Location: Denmark
Contact: Website

Re: Headless sense hat with joystick

Wed Oct 07, 2015 11:40 am

ShiftPlusOne wrote:There's this:
https://python-evdev.readthedocs.org/en/latest/


Also, works is being done on the python-sense-hat library to make using the joystick easier.
Sounds nice - I am looking forward to that.
subjektiv.dk

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6228
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Headless sense hat with joystick

Wed Oct 07, 2015 11:43 am


richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Headless sense hat with joystick

Sat Dec 19, 2015 8:53 pm

I puzzled over how to use the stick without blocking the program (too much).
This crude example (using the stick.py in the previous post) might help others (Note the stick.wait with 0.01 as timeout) :

Code: Select all

from sense_hat import SenseHat
import time
import random
from stick import SenseStick

sense = SenseHat()
sense.set_rotation(270)

KEY_UP = 103
KEY_LEFT = 105
KEY_RIGHT = 106
KEY_DOWN = 108
KEY_ENTER = 28

stick = SenseStick()

try:
	while True:
		red = random.randint(0, 255)
		green = random.randint(0, 255)
		blue = random.randint(0, 255)
		x = random.randint(0, 7)
		y = random.randint(0, 7)
		sense.set_pixel(x, y, [red, green, blue])
		x = random.randint(0, 7)
		y = random.randint(0, 7)
		sense.set_pixel(x, y, [0, 0, 0])
		x = random.randint(0, 7)
		y = random.randint(0, 7)
		sense.set_pixel(x, y, [0, 0, 0])
		time.sleep(0.02)
		
		if stick.wait(timeout=0.01)==True:
			user_input = stick.read()[1]
			if user_input==KEY_LEFT:
				print "Left"
			elif user_input==KEY_RIGHT:
				print "Right"
			elif user_input==KEY_UP:
				print "Up"
			elif user_input==KEY_DOWN:
				print "Down"				
			elif user_input==KEY_ENTER:
				print "exiting"
				sense.clear()
				break

except KeyboardInterrupt:
	sense.clear()
	print
	print ("program stopped")

RichR
p.s. I am using Jessie, so no need for sudo to access GPIO
P.p.s. this program could be used as a (home security) TV simulator !!!

Return to “Astro Pi”