【问题标题】:Code for joystick on sense hat感应帽上的操纵杆代码
【发布时间】:2017-12-30 09:05:53
【问题描述】:

我刚得到一顶圣诞节的感觉帽,我正在通过以下网站工作: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'

谁能帮我解决这个问题?

【问题讨论】:

    标签: python raspberry-pi joystick


    【解决方案1】:

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

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

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

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

    【讨论】:

    【解决方案2】:

    有一个名为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 然后 enter 保存更改并 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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多