Notice: Trying to access array offset on value of type bool in /home1/freesand/public_html/wp-content/plugins/wiki-embed/WikiEmbed.php on line 112

Notice: Trying to access array offset on value of type bool in /home1/freesand/public_html/wp-content/plugins/wiki-embed/WikiEmbed.php on line 112

Notice: Trying to access array offset on value of type bool in /home1/freesand/public_html/wp-content/plugins/wiki-embed/WikiEmbed.php on line 116
勇闖新世界︰ W!o《卡夫卡村》變形祭︰感知自然‧外傳‧三 | FreeSandal

勇闖新世界︰ W!o《卡夫卡村》變形祭︰感知自然‧外傳‧三

假使從

Astro Pi: Flight Hardware Tech Specs

Astro_Pi_Logo_WEB-300px

Inputs

  • PCB-mounted 5-button miniature joystick: Alps SKRHABE010 (data sheet)
    Up, down, left, right and middle click. This will emulate the keyboard cursor keys and Enter for the middle click. Accessible via standard Linux event system /dev/input/event*
  • Additional GPIO push buttons will be accessible on the side of the aluminium flight case for functions such as controlled shut-down and reset. These will also be usable in your code via GPIO interrupt detection or polling. You are welcome to re-create them manually on your Astro Pi with any type of push button you have to hand.
  • Standard Pi Camera Module (sensor data sheet)
  • Pi NoIR Camera Module

 

我們可以知道,『 Sense HAT 』裡有一個『小型搖桿』,在 Linux 系統上將被模擬成鍵盤。如果閱讀核心輸出訊息,可以發現︰

 

[    5.400635] rpi-sense 1-0046: Raspberry Pi Sense HAT firmware version 0
[    5.440842] input: Raspberry Pi Sense HAT Joystick as /devices/virtual/input/input2
[    5.466825] fb1: RPi-Sense FB frame buffer device

 

然而要如何確認它被模擬成了鍵盤?以及送出什麼鍵盤碼的呢??假使熟悉

勇闖新世界︰ 《 Kernel 4.X 》之整裝蓄勢‧設備管理及應用‧二上

勇闖新世界︰ 《 Kernel 4.X 》之整裝蓄勢‧設備管理及應用‧三上

系列文本的讀者,可以用

 

pi@raspberrypi ~ $ python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from evdev import InputDevice, list_devices

# 取得系統所有的輸入設備
>>> 輸入裝置列 =  [InputDevice(fn) for fn in list_devices()]
>>> for 裝置 in 輸入裝置列:
...     print(裝置.fn, 裝置.name, 裝置.phys)
... 
/dev/input/event1 Logitech USB Receiver usb-3f980000.usb-1.5.3/input1
/dev/input/event0 Logitech USB Receiver usb-3f980000.usb-1.5.3/input0
/dev/input/event2 Raspberry Pi Sense HAT Joystick rpi-sense-joy/input0

# 選擇搖桿
>>> 裝置 = InputDevice('/dev/input/event2')
>>> print(裝置)
device /dev/input/event2, name "Raspberry Pi Sense HAT Joystick", phys "rpi-sense-joy/input0"

# 裝置詳細功能
>>> 裝置.capabilities(verbose=True)
{('EV_KEY', 1): [('KEY_ENTER', 28), ('KEY_UP', 103), ('KEY_LEFT', 105), ('KEY_RIGHT', 106), ('KEY_DOWN', 108)], ('EV_SYN', 0): [('SYN_REPORT', 0), ('SYN_CONFIG', 1), ('?', 20)]}
>>> 裝置.capabilities()
{0: [0, 1, 20], 1: [28, 103, 105, 106, 108]}

# 搖桿功能測試
>>> 搖桿裝置 = 裝置
>>> from evdev import categorize, ecodes
>>> for 事件 in 搖桿裝置.read_loop():
...     if 事件.type == ecodes.EV_KEY:
...         print(categorize(事件))
...         print(事件)
... 
key event at 1442301324.053264, 103 (KEY_UP), hold
event at 1442301324.053264, code 103, type 01, val 02
key event at 1442301324.093247, 103 (KEY_UP), hold
event at 1442301324.093247, code 103, type 01, val 02
key event at 1442301324.126043, 103 (KEY_UP), up
event at 1442301324.126043, code 103, type 01, val 00
key event at 1442301327.065373, 108 (KEY_DOWN), down
event at 1442301327.065373, code 108, type 01, val 01
key event at 1442301327.313266, 108 (KEY_DOWN), hold
event at 1442301327.313266, code 108, type 01, val 02
key event at 1442301327.336501, 108 (KEY_DOWN), up
event at 1442301327.336501, code 108, type 01, val 00
key event at 1442301331.887788, 105 (KEY_LEFT), down
event at 1442301331.887788, code 105, type 01, val 01
key event at 1442301332.133269, 105 (KEY_LEFT), hold
event at 1442301332.133269, code 105, type 01, val 02
key event at 1442301332.144608, 105 (KEY_LEFT), up
event at 1442301332.144608, code 105, type 01, val 00
key event at 1442301333.899522, 106 (KEY_RIGHT), down
event at 1442301333.899522, code 106, type 01, val 01
key event at 1442301334.142136, 106 (KEY_RIGHT), up
event at 1442301334.142136, code 106, type 01, val 00
key event at 1442301335.854223, 28 (KEY_ENTER), down
event at 1442301335.854223, code 28, type 01, val 01
key event at 1442301336.096757, 28 (KEY_ENTER), up
event at 1442301336.096757, code 28, type 01, val 00
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.2/dist-packages/evdev/device.py", line 280, in read_loop
    r, w, x = select([self.fd], [], [])
KeyboardInterrupt
>>> 

 

來作確認。如是不但能夠解讀

 

【 pygame_joystick.py 】

#!/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)

 

,也曉得即使不用『 pygame 』程式庫,該如何使用這個『搖桿』的了。

 

─── 學習似乎總是前後相隨並且環環相扣的耶!!

 

 

 

 

 

 

 

 

 

 

輕。鬆。學。部落客