astro-pi / python-sense-emu

An emulator for the Raspberry Pi Sense HAT
https://sense-emu.readthedocs.io/
Other
40 stars 26 forks source link

Joystick buttons non-functional - probable window focus issue #20

Open spl237 opened 7 years ago

spl237 commented 7 years ago

The joystick buttons on the GUI window do not seem to correctly emulate the joystick. They do not generate the relevant key codes - this may just be an issue with window focus, but the effect is that the buttons are not useful in Python code.

This sample code shows the problem:

import pygame
from pygame.locals import *
from sense_emu import SenseHat

pygame.init()
pygame.display.set_mode((640,480))
sense = SenseHat()
sense.clear()

running = True

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == 273:
                sense.set_pixel (0,0,255,0,0)
            if event.key == 274:
                sense.set_pixel (0,0,0,255,0)
            if event.key == 275:
                sense.set_pixel (0,0,0,0,255)
            if event.key == 276:
                sense.set_pixel (0,0,255,255,255)
        if event.type == QUIT:
            running = False

If this code is run with a SenseHAT connected (and the real SenseHAT library loaded instead of the emulator library), pressing the joystick in the 4 directions changes the colour of the LED, as expected.

If this code is run under the emulator, pressing the buttons on the window which are supposed to emulate the joystick has no effect. If the emulator window has focus, pressing the keyboard cursor buttons causes the buttons on the window to be activated (as they can be seen to highlight), but the state of the LED doesn't change.

The only way to get the LED to respond to button pushes is to change focus to the pygame window, and to press the keyboard cursor keys - at this point, the simulated LED does respond as intended.

I am not sure if it is actually possible to have the buttons on the window cause the required events to be generated given the limitations of focus and the targeting of events. It might be preferable to remove these buttons from the window completely and to rely on keyboard events; this seems to more accurately reflect the operation of a real SenseHAT.

waveform80 commented 7 years ago

Pushing stuff into the global keyboard queue from Python is possible, but quite hard (because platforms are rather different in the way they handle keyboard buffers). However, the point of developing the "stick" API in both the sense_hat library, and the sense_emu emulation was to avoid the need for people to go querying the keyboard itself. The "stick" API allows you to do the same stuff, but only queries the joystick (not any other keyboard), and works in both the "real" HAT and the emulator.

In other words I'd suggest migrating the code to that API for a quick fix; anything else will take quite a bit longer (and I'm not entirely convinced the emulator should be encouraging people to query the keyboard for the joystick).

waveform80 commented 6 years ago

Might combine this with #23 and consider permitting various different layouts including options with / without the joystick buttons (though I still think people should just use the stick API).