Having had some fun playing with the display on the Sense Hat I moved on to trying to utilise the Joystick. However copying the examples set out in the Sense Hat API results in the following error
AttributeError: 'SenseHat' object has no attribute 'stick'
Upon further investigation it turns out that the sense_hat.py library doesn't include any functionality for the joystick, trying to run sudo apt remove --purge sense-hat then sudo apt-get update and sudo apt-get install sense-hat only installs the same version without the joystick functions.
Navigating to the github repositories shows that there is an updated version that does include the joystick functions, to get the updates were going to have to manually install them.
Time to implement the update!!
Step 1.
Open up a terminal on the Raspberry Pi (or if your going over ssh just use the terminal your already in).
Navigate to the sense_hat python directory
cd /usr/lib/python2.7/dist-packages/sense_hat
delete the existing files
sudo rm __init__.py sense_hat.py __init__.pyc sense_hat.pyc
Step 2.
Download the new files straight from github
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
Step 3.
We just need to make a slight alteration to the sense_hat.py file that was downloaded, open the file using nano:
sudo nano sense_hat.py
Line 17 (highlighted in the picture below) reads
from .stick import SenseStick
the . needs deleting from in front of the work stick so it reads:
from stick import SenseStick
Press ctrl+o then enter to save changes and ctrl+x to exit nano.
Thats it! happy playing!!
Here's an example code and video showing how to move a pixel around using the joystick:
from sense_hat import SenseHat sense = SenseHat() x = 3 y = 3 def draw_dot(): sense.clear() sense.set_pixel(x,y,255,90,90) def pushed_up(): global y y = y - 1 if y < 0: y = 0 draw_dot() def pushed_down(): global y y = y + 1 if y > 7: y = 7 draw_dot() def pushed_left(): global x x = x - 1 if x < 0: x = 0 draw_dot() def pushed_right(): global x x = x + 1 if x > 7: x = 7 draw_dot() sense.stick.direction_up = pushed_up sense.stick.direction_down = pushed_down sense.stick.direction_left = pushed_left sense.stick.direction_right = pushed_right draw_dot()
Top Comments