sense hat 上的操纵杆代码

Code for joystick on sense hat

我刚买了一顶圣诞帽,我正在通过以下网站工作:https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat。在做网站的操纵杆部分时,我输入了这段代码:

from sense_hat import SenseHat
sense = SenseHat()
while True:
    for event in sense.stick.get_events():
        print(event.direction, event.action)

并出现以下错误:

Traceback (most recent call last):
  File "/home/pi/python_programmes/hat_short.py", line 4, in <module>
    for event in sense.stick.get_events():
AttributeError: 'SenseHat' object has no attribute 'stick'

谁能帮我解决这个问题?

根据 source code,最新版本(截至 2016 年 6 月)在 SenseHat class 上有 stick 属性。确保您安装了最新的 sense-hat

sudo apt-get install --only-upgrade sense-hat

如果这不起作用,您可以克隆存储库并手动安装:

git clone https://github.com/RPi-Distro/python-sense-hat
cd python-sense-hat
sudo python setup.py install

有一个名为 https://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/blog/2017/01/23/raspberry-pi-sense-hat-enabling-the-joystick 的网站,它会带您了解如何使操纵杆工作以及如何制作一个程序来响应操纵杆的移动,在屏幕上移动一个点。

要使操纵杆工作,请在终端中输入以下内容:

cd /usr/lib/python2.7/dist-packages/sense_hat

然后:

sudo rm __init__.py sense_hat.py __init__.pyc sense_hat.pyc

然后:

sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/__init__.py   
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/sense_hat.py   
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/stick.py  

然后:

sudo nano sense_hat.py

然后看第17行,代码应该是这样的:

from .stick import SenseStick

但是你需要删除点,第 17 行应该如下所示:

from .stick import SenseStick

然后你需要按 ctrl+o 然后输入保存更改和 ctrl+x 退出 nano

使用操纵杆的代码示例:

from sense_hat import SenseHat

sense = SenseHat()
while True:
for x in sense.stick.get_events():
    if x.direction == 'up':
        sense.show_letter("U")
    elif x.direction == 'down':
        sense.show_letter("D")
    elif x.direction == 'left':
        sense.show_letter("L")
    elif x.direction == 'right':
        sense.show_letter("R")
    elif x.direction == 'middle':
        sense.show_letter("M")