Skip to content

Instantly share code, notes, and snippets.

@amandaroos
Created May 26, 2017 06:55
Show Gist options
  • Select an option

  • Save amandaroos/30e79d5b034ffc4d06bbd61f568e8940 to your computer and use it in GitHub Desktop.

Select an option

Save amandaroos/30e79d5b034ffc4d06bbd61f568e8940 to your computer and use it in GitHub Desktop.
Using the SenseHat joystick to manipulate pixels in the LED matrix
#For use with Rasp Pi SenseHat
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
from signal import pause
sense = SenseHat()
sense.clear()
#Where is the cursor starting point?
x = 2
y = 2
lead_pixel_color = [255,0,0]
sense.set_pixel(x,y,lead_pixel_color)
leave_trail = True
while True:
#Wait for joystick input
event = sense.stick.wait_for_event()
#check for pressed action and whether or not to leave trail
if event.action == "pressed" and leave_trail == True:
sense.set_pixel(x,y,0,0,255)
elif event.action == "pressed" and leave_trail==False:
sense.set_pixel(x,y,0,0,0)
#When joystick pressed up
if event.action == "pressed" and event.direction == "up":
y-=1
y = y%8
sense.set_pixel(x,y,lead_pixel_color)
#When joystick pressed down
if event.action == "pressed" and event.direction == "down":
y+=1
y = y%8
sense.set_pixel(x,y,lead_pixel_color)
#When joystick pressed left
if event.action == "pressed" and event.direction == "left":
x-=1
x=x%8
sense.set_pixel(x,y,lead_pixel_color)
#When joystick pressed right
if event.action == "pressed" and event.direction == "right":
x+=1
x=x%8
sense.set_pixel(x,y,lead_pixel_color)
#When joystick pressed middle
if event.action == "pressed" and event.direction == "middle":
#change color to Green
#sense.set_pixel(x,y,0,255,0)
if leave_trail == True:
leave_trail = False
sense.set_pixel(x,y,0,0,0)
else:
leave_trail = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment