New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example pygame_joystick.py does not run headless, use python_evdev instead of pygame #9

Closed
tvoverbeek opened this Issue Sep 23, 2015 · 9 comments

Comments

Projects
None yet
6 participants
@tvoverbeek

tvoverbeek commented Sep 23, 2015

The pygame_joystick example needs a display otherwise the event loop does not work.
This is a problem when running headless (e.g. RPi with Sense Hat accessed via ssh).
Also the display is initialized with size 640x480 which is not nice to the new Raspberry Pi Display (800x400).

You can get it to work headless with 'hdmi_force_hotplug=1' in /boot/config.txt.
However the ESC key will not be recognized via ssh. You have to terminate with Ctrl-C

The code only needs keyboard events, so a display should not be needed.
I wrote an evdev_joystick.py using the same logic as pygame_joystick.
If you are interested I can generate a pull request with my added evdev_joystick.py.
Of course, instructions will have to be added how to install python_evdev.

@XECDesign

This comment has been minimized.

Show comment
Hide comment
@XECDesign

XECDesign Sep 23, 2015

Contributor

I don't believe this is an issue, it's just a limitation of using pygame for this. As you say, using evdev gets around this but has its own limitations.

Contributor

XECDesign commented Sep 23, 2015

I don't believe this is an issue, it's just a limitation of using pygame for this. As you say, using evdev gets around this but has its own limitations.

@rhellyer

This comment has been minimized.

Show comment
Hide comment
@rhellyer

rhellyer Sep 29, 2015

I have a similar issue where I want to use my sense hat headless but allow the user to control the led screen from the joystick. I tried with pushed as in the examples but it failed because of no display. Could you post your even example somewhere or could someone recommend am alternative approach? Many thanks

rhellyer commented Sep 29, 2015

I have a similar issue where I want to use my sense hat headless but allow the user to control the led screen from the joystick. I tried with pushed as in the examples but it failed because of no display. Could you post your even example somewhere or could someone recommend am alternative approach? Many thanks

@tvoverbeek

This comment has been minimized.

Show comment
Hide comment
@tvoverbeek

tvoverbeek Sep 29, 2015

rhellyer, easiest is to use hdmi_force_hotplug=1 in /boot/config.txt.
This let the Pi think there is a hdmi display attached.
As in the pygame-joystick example you have to initialize the display, but a size of 1x1 is sufficient:

pygame.init()
pygame.display.set_mode((1, 1))

tvoverbeek commented Sep 29, 2015

rhellyer, easiest is to use hdmi_force_hotplug=1 in /boot/config.txt.
This let the Pi think there is a hdmi display attached.
As in the pygame-joystick example you have to initialize the display, but a size of 1x1 is sufficient:

pygame.init()
pygame.display.set_mode((1, 1))

@tvoverbeek tvoverbeek closed this Sep 29, 2015

@tvoverbeek tvoverbeek reopened this Sep 29, 2015

@rhellyer

This comment has been minimized.

Show comment
Hide comment
@rhellyer

rhellyer Sep 29, 2015

Many thanks - that sounds like the simplest route!

rhellyer commented Sep 29, 2015

Many thanks - that sounds like the simplest route!

@davidhoness

This comment has been minimized.

Show comment
Hide comment
@davidhoness

davidhoness Oct 1, 2015

Contributor

@tvoverbeek is it acceptable to add another version of the program for evdev? That way users can see the advantages and disadvantages of both ways.

Contributor

davidhoness commented Oct 1, 2015

@tvoverbeek is it acceptable to add another version of the program for evdev? That way users can see the advantages and disadvantages of both ways.

@tvoverbeek

This comment has been minimized.

Show comment
Hide comment
@tvoverbeek

tvoverbeek Oct 3, 2015

For those interested here is my evdev version of the pygame-joystick example.
The program logic is the same as in pygame_joystick.py.
Since evdev looks at input sources on the Pi itself, you cannot use ESC to terminate the program over ssh.
Therefore Ctrl-C instead.

#!/usr/bin/python
import sys
import time
from sense_hat import SenseHat
from evdev import InputDevice, list_devices, ecodes

print("Press Ctrl-C to quit")
time.sleep(1)

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

found = False;
devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
  if dev.name == 'Raspberry Pi Sense HAT Joystick':
    found = True;
    break
if not(found):
  print('Raspberry Pi Sense HAT Joystick not found. Aborting ...')
  exit()

# 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_code(code, colour):
    if code == ecodes.KEY_DOWN:
        set_pixels(DOWN_PIXELS, colour)
    elif code == ecodes.KEY_UP:
        set_pixels(UP_PIXELS, colour)
    elif code == ecodes.KEY_LEFT:
        set_pixels(LEFT_PIXELS, colour)
    elif code == ecodes.KEY_RIGHT:
        set_pixels(RIGHT_PIXELS, colour)
    elif code == ecodes.KEY_ENTER:
        set_pixels(CENTRE_PIXELS, colour)


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

try:
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            if event.value == 1:  # key down
                handle_code(event.code, WHITE)
            if event.value == 0:  # key up
                handle_code(event.code, BLACK)
except KeyboardInterrupt:
    sys.exit()

tvoverbeek commented Oct 3, 2015

For those interested here is my evdev version of the pygame-joystick example.
The program logic is the same as in pygame_joystick.py.
Since evdev looks at input sources on the Pi itself, you cannot use ESC to terminate the program over ssh.
Therefore Ctrl-C instead.

#!/usr/bin/python
import sys
import time
from sense_hat import SenseHat
from evdev import InputDevice, list_devices, ecodes

print("Press Ctrl-C to quit")
time.sleep(1)

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

found = False;
devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
  if dev.name == 'Raspberry Pi Sense HAT Joystick':
    found = True;
    break
if not(found):
  print('Raspberry Pi Sense HAT Joystick not found. Aborting ...')
  exit()

# 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_code(code, colour):
    if code == ecodes.KEY_DOWN:
        set_pixels(DOWN_PIXELS, colour)
    elif code == ecodes.KEY_UP:
        set_pixels(UP_PIXELS, colour)
    elif code == ecodes.KEY_LEFT:
        set_pixels(LEFT_PIXELS, colour)
    elif code == ecodes.KEY_RIGHT:
        set_pixels(RIGHT_PIXELS, colour)
    elif code == ecodes.KEY_ENTER:
        set_pixels(CENTRE_PIXELS, colour)


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

try:
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            if event.value == 1:  # key down
                handle_code(event.code, WHITE)
            if event.value == 0:  # key up
                handle_code(event.code, BLACK)
except KeyboardInterrupt:
    sys.exit()
@rhellyer

This comment has been minimized.

Show comment
Hide comment
@rhellyer

rhellyer Oct 5, 2015

This is just what I needed for my project as I want to use the minijoystick but don't need pygame, many thanks for posting!

rhellyer commented Oct 5, 2015

This is just what I needed for my project as I want to use the minijoystick but don't need pygame, many thanks for posting!

@jschrempp

This comment has been minimized.

Show comment
Hide comment
@jschrempp

jschrempp Mar 14, 2016

I used the evdev solution listed above with my SenseHat and it worked great. I never could get pygames to run headless.

jschrempp commented Mar 14, 2016

I used the evdev solution listed above with my SenseHat and it worked great. I never could get pygames to run headless.

@themaire

This comment has been minimized.

Show comment
Hide comment
@themaire

themaire Apr 3, 2016

A big thanks for you tvoverbeek!!!

themaire commented Apr 3, 2016

A big thanks for you tvoverbeek!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment