Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Created March 29, 2017 08:59
Show Gist options
  • Save sourceperl/bf612e5fa9fa202f2d8185a63c7f4552 to your computer and use it in GitHub Desktop.
Save sourceperl/bf612e5fa9fa202f2d8185a63c7f4552 to your computer and use it in GitHub Desktop.
Spirit bubble with Rpi Sense Hat: use 8x8 LEDs and accelerometer
#!/usr/bin/env python3
# Spirit bubble with Rpi Sense Hat: use 8x8 LEDs and accelerometer
from sense_hat import SenseHat
import schedule
import time
# some const
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
def cut(value, max_value, min_value):
return min(max(value, min_value), max_value)
class Bubble:
def __init__(self, sense_hat):
"""
Bubble constructor
:param sense_hat: SenseHat instance
:type sense_hat: SenseHat
"""
# init SenseHat
self.sense = sense_hat
self.sense = sense_hat
self.sense.rotation = 180
self.sense.low_light = True
self.sense.clear()
# private vars
self._x = None
self._y = None
def set_pos(self, x, y):
"""
Set Bubble at x, y position
:param x: x offset between -3 and +3
:type x: int
:param y: y offset between -3 and +3
:type y: int
"""
# scale args
x = int(cut(x, max_value=+3, min_value=-3))
y = int(cut(y, max_value=+3, min_value=-3))
# draw target
if not((self._x == x) and (self._y == y)):
self.sense.clear()
self.sense.set_pixel(3+x, 3+y, GREEN)
self.sense.set_pixel(3+x, 4+y, GREEN)
self.sense.set_pixel(4+x, 3+y, GREEN)
self.sense.set_pixel(4+x, 4+y, GREEN)
self._x = x
self._y = y
def job_display():
acc = sense.get_accelerometer_raw()
# print('x=%.2f, y=%.2f, z=%.2f' % (acc['x'], acc['y'], acc['z']))
# scale G to +/- 3 offset
x_off = cut(round(acc['x'] * 10), max_value=+3, min_value=-3)
y_off = cut(round(acc['y'] * 10), max_value=+3, min_value=-3)
# update bubble position
bubble.set_pos(x_off, y_off)
if __name__ == '__main__':
# init target
sense = SenseHat()
bubble = Bubble(sense)
# init scheduler
schedule.every(.2).seconds.do(job_display)
# main loop
while True:
schedule.run_pending()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment