1

I just got a sense hat for Christmas, and I am working through the following website: https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat. When doing the joystick part of the website, i typed in this code:

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

and got the following error:

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'

Can anyone help me on this?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Andrew Bell
  • 23
  • 2
  • 7

2 Answers2

0

According to the source code, the latest version (as of June 2016) has the stick property on the SenseHat class. Make sure you have the latest sense-hat installed:

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

If that does not work, you can clone the repo and install it manually:

git clone https://github.com/RPi-Distro/python-sense-hat
cd python-sense-hat
sudo python setup.py install
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I found a website which took you through some steps on how to do it. the website was:https://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/blog/2017/01/23/raspberry-pi-sense-hat-enabling-the-joystick – Andrew Bell Dec 30 '17 at 10:30
0

There is a website called https://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/blog/2017/01/23/raspberry-pi-sense-hat-enabling-the-joystick which takes you through how to make the joystick work and how to make a programme which responds to the joystick being moved, to move a dot around the screen.

To make the joy stick work, type the following into the terminal:

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

then:

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

then:

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  

then:

sudo nano sense_hat.py

then look on line 17, and the code should look like this:

from .stick import SenseStick

but you need to delete the dot, and line 17 should look like this:

from .stick import SenseStick

then you need to press ctrl+o then enter to save changes and ctrl+x to exit nano

An example of code using the joystick:

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")
Andrew Bell
  • 23
  • 2
  • 7