mrb78
Posts: 3
Joined: Sat Oct 31, 2015 1:13 am

Joystick Not Working

Sat Oct 31, 2015 1:19 am

I have copied the code from the pygame tutorial on to a Pi2 and a B+, using two different sense hat boards and under no configuration can I get the joystick to move the pixel around, though the arrow keys on my keyboard will. I am running Jessie. Hopefully this is a known problem and there is some help out there!

User avatar
Kratos
Posts: 395
Joined: Sun Apr 12, 2015 12:41 pm

Re: Joystick Not Working

Sat Oct 31, 2015 6:07 pm

Try this code:

Code: Select all

#!/usr/bin/python
from sense_hat import SenseHat
import os
import time
import pygame  # See http://www.pygame.org/docs
from pygame.locals import *


print("Press Escape to quit")
time.sleep(1)

pygame.init()
pygame.display.set_mode((640, 480))

sense = SenseHat()
sense.clear()  # Blank the LED matrix

# 0, 0 = Top left
# 7, 7 = Bottom right
UP_PIXELS = [[3, 0], [4, 0]]
DOWN_PIXELS = [[3, 7], [4, 7]]
LEFT_PIXELS = [[0, 3], [0, 4]]
RIGHT_PIXELS = [[7, 3], [7, 4]]
CENTRE_PIXELS = [[3, 3], [4, 3], [3, 4], [4, 4]]


def set_pixels(pixels, col):
    for p in pixels:
        sense.set_pixel(p[0], p[1], col[0], col[1], col[2])


def handle_event(event, colour):
    if event.key == pygame.K_DOWN:
        set_pixels(DOWN_PIXELS, colour)
    elif event.key == pygame.K_UP:
        set_pixels(UP_PIXELS, colour)
    elif event.key == pygame.K_LEFT:
        set_pixels(LEFT_PIXELS, colour)
    elif event.key == pygame.K_RIGHT:
        set_pixels(RIGHT_PIXELS, colour)
    elif event.key == pygame.K_RETURN:
        set_pixels(CENTRE_PIXELS, colour)


running = True

BLACK = [0, 0, 0]
WHITE = [255, 255, 255]

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            handle_event(event, WHITE)
        if event.type == KEYUP:
            handle_event(event, BLACK)

The tutorial program had an error in the pygame events, so I fixed it. I hope this helps!

Kratos
I have posted mostly with a Pi 2 running either Ubuntu MATE, or Raspbian.

mrb78
Posts: 3
Joined: Sat Oct 31, 2015 1:13 am

Re: Joystick Not Working

Sat Oct 31, 2015 8:10 pm

Hi Kratos,

Massive thanks, works perfectly.

mrb78

tejonbiker
Posts: 30
Joined: Tue Aug 28, 2012 1:36 am

Re: Joystick Not Working

Sun Nov 01, 2015 7:19 pm

Woks for me, but only the arrows of my keyboard of my computer (via VNC), the joystick still not working, theres any other option that I can test?.

Cheers.

mrb78
Posts: 3
Joined: Sat Oct 31, 2015 1:13 am

Re: Joystick Not Working

Sun Nov 01, 2015 8:50 pm

I had similar issues via VNC, where only the keyboard keys worked. When I plugged the Pi into the TV (having used Kratos's code), the joystick worked. I'm sure someone could give a proper technical explanation (with correct terminology) but I guess if you're using VNC (or ssh -X), the Pi is just acting as a server for that instance and any hardware inputs do not get put through to the VNC instance?

tejonbiker
Posts: 30
Joined: Tue Aug 28, 2012 1:36 am

Re: Joystick Not Working

Mon Nov 02, 2015 7:13 am

If I connect monitor, keyboard, mouse and access to the X system, all works as expected, I have interest in use the joystick and the sense hat in headless mode, I don't know if this is posible.

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

Re: Joystick Not Working

Sat Dec 19, 2015 9:02 pm

see under this
viewtopic.php?f=104&t=121920

I use X-Windows exclusively to access my pi's....from Linux Mint.
This does it all!

ssh -l pi pi-01.local -X
pcmanfm&

alphanumeric
Posts: 2528
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Joystick Not Working

Tue Jan 19, 2016 3:25 pm

I used evdev, from here, https://www.raspberrypi.org/learning/as ... tsheet.pdf ecodes.KEY_ENTER: is the joystick press, I use that to run os.system("sudo shutdown now -P"). That shuts my Pi down. I have the ecodes.KEY_LEFT: set to run raise SystemExit. That lets me just stop my running Python script. Mine is totally headless, no keyboard, mouse, or monitor. I use remote desktop on my Windows PC to do remote login.

Return to “Astro Pi”