5 234 octets ajoutés
, 7 octobre 2015 à 20:31
{{RASP-SENSE-HAT-ASTRO-PI-NAV}}
{{traduction}}
== Le joystick ==
{{bloc-etroit|text=The Sense HAT joystick is mapped to the four keyboard cursor keys, with the joystick middle-click being mapped to the Return key. This means that moving the joystick has exactly the same effect as pressing those keys on the keyboard. Remember that the down direction is with the HDMI port facing downwards.
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Joystick-01.jpg]]
== Keyboard Mapping ==
"Keyboard Mapping" que nous traduirons par "mappage clavier" qui est pris en charge par un pilote et transforme l'utilisation du joystick en simulation d'activation sur le clavier du PC.
L'utilisation d'un pilote et du "Keyboard Mapping" est plus que pratique, votre programme n'a pas besoin de savoir comment lire l'état du joystick... il a juste besoin de détecter les touches pressées sur le clavier (et cela, tous les programmes savent le faire).
'''1.''' Open '''Python 3''' from a terminal window as {{fname|sudo}} by typing:
<nowiki>sudo idle3 &</nowiki>
'''2.''' A Python Shell window will now appear.
'''3.''' Select {{fname|File > New Window}}.
'''4.''' Et tapez le code suivant:
<nowiki>import pygame
from pygame.locals import *
from sense_hat import SenseHat
pygame.init()
pygame.display.set_mode((640, 480))
sense = SenseHat()
sense.clear()
running = True
while running:
for event in pygame.event.get():
print(event)
if event.type == QUIT:
running = False
print("AU REVOIR")</nowiki>
'''5.''' Select {{fname|File > Save}} and choose a file name for your program.
'''6.''' Then select {{fname|Run > Run module}}.<br /><br />Note that we are using the {{underline|pygame}} Python module to detect the key presses.
'''7.''' A blank window will appear. Use the mouse to move it to one side of your screen so that the Python Shell window is also visible.
'''8.''' Keep the blank window selected but move the mouse over it, press and release some keys on the keyboard and waggle the Sense HAT joystick. Try pressing and holding a key for a moment and then releasing it a few seconds later. You should notice that two events occur when you do this: one for the key going down, and another for the key being released. For this program you will only use the '''KEY DOWN''' event.<small><br />KEY DOWN signifie FLECHE BAS</small>
'''9.''' Click the {{fname|x}} in the corner of the blank pygame window. You should see the {{fname|AU REVOIR}} message appear in the Python Shell window but the blank window does not close.
We're consuming the pygame event queue using the {{fname|for event in pygame.event.get():}} syntax. This will loop through all keyboard and mouse events that occur. Inside the loop, we display what the event was by using {{fname|print(event)}} and then test to see if the event type is {{fname|QUIT}}. If it is, we set {{fname|running}} to {{fname|False}} which causes the {{fname|while}} loop to end and the program to finish. The program should print a line of text in the Python Shell window whenever we move the mouse, click the mouse, and press or release a keyboard key.
== Détecter les mouvements du joystick ==
Consider how a joystick might work. You can use the LED matrix to help you think about it. Let's make a pixel white and use the joystick to move the pixel around the 8x8 matrix. To do this you can use an event to detect a key press. For example, if a key is pressed DOWN what steps need to happen to move the pixel?
<nowiki>- Turn OFF the LED using current `x` and `y`
- If DOWN then add 1 to `y`
- If UP then subtract 1 from `y`
- If RIGHT then add 1 to `x`
- If LEFT then subtract 1 from `x`
- Turn ON the LED using updated `x` and `y`</nowiki>
'''1.''' Start by just adding the code for the DOWN key. Delete the {{fname|print(event)}} command that you used in the previous section and insert the code below at the same indentation level:
<nowiki>if event.type == KEYDOWN:
sense.set_pixel(x, y, 0, 0, 0) # Black 0,0,0 means OFF
if event.key == K_DOWN:
y = y + 1
sense.set_pixel(x, y, 255, 255, 255)</nowiki>
'''2.''' Save and run the code. You should be able to move the pixel point down using the {{fname|DOWN}} key or the joystick. If you keep going, you'll eventually see this error:
<nowiki>ValueError: Y position must be between 0 and 7</nowiki>
'''3.''' Our {{fname|y}} value can only be between 0-7, otherwise it's off the edge of the matrix and into empty space! So that's why the error happens; the Sense HAT doesn't understand values outside this range. Our code just keeps adding 1 to {{fname|y}} every time the DOWN key is pressed, so we need to stop {{fname|y}} going above 7.
We can achieve this by adding the syntax {{fname|and y < 7}} (and {{fname|y}} is less than 7) to the key direction test:
<nowiki>if event.key == K_DOWN and y < 7:
y = y + 1</nowiki>
'''4.''' Save and run the code again; this time, the code should not allow the point to go beyond the edge of the screen.
'''5.''' Now that this works, you will need to add the other directions for the joystick. Where you have {{fname|1=if event.key == K_DOWN:}} in your code, you can also use:
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}