Examples¶
Introduction¶
The Sense HAT emulator exactly mirrors the official Sense HAT API. The only
difference (required because both the emulator and actual library can be
installed simultaneously on a Pi) is the name: sense_emu
instead of
sense_hat
. It is recommended to import the library in the following
manner at the top of your code:
from sense_emu import SenseHat
Then, when you want to change your code to run on the actual HAT all you need do is change this line to:
from sense_hat import SenseHat
To run your scripts under the emulator, first start the emulator application, then start your script.
Several example scripts, with varying degrees of difficulty, are available from the
menu within the emulator. Selecting an example from this menu will open it in Python’s IDLE environment.Note
The example will be opened directly from the installation. To edit the
example for your own purposes, use /home/pi
).
A selection of example scripts is given in the following sections.
Temperature¶
Displays the current temperature reading on the Sense HAT’s screen:
from sense_emu import SenseHat
sense = SenseHat()
red = (255, 0, 0)
blue = (0, 0, 255)
while True:
temp = sense.temp
pixels = [red if i < temp else blue for i in range(64)]
sense.set_pixels(pixels)
Humidity¶
Displays the current humidity reading on the Sense HAT’s screen:
from sense_emu import SenseHat
sense = SenseHat()
green = (0, 255, 0)
white = (255, 255, 255)
while True:
humidity = sense.humidity
humidity_value = 64 * humidity / 100
pixels = [green if i < humidity_value else white for i in range(64)]
sense.set_pixels(pixels)
Joystick¶
Scrolls a blip around the Sense HAT’s screen in response to joystick motions:
from sense_emu import SenseHat
x = y = 4
hat = SenseHat()
def update_screen():
hat.clear()
hat.set_pixel(x, y, 255, 255, 255)
def clamp(value, min_value=0, max_value=7):
return min(max_value, max(min_value, value))
def move_dot(event):
global x, y
if event.action in ('pressed', 'held'):
x = clamp(x + {
'left': -1,
'right': 1,
}.get(event.direction, 0))
y = clamp(y + {
'up': -1,
'down': 1,
}.get(event.direction, 0))
update_screen()
while True:
for event in hat.stick.get_events():
move_dot(event)
update_screen()
An alternative way to write this example using the joystick’s event handler attributes is given below:
from sense_emu import SenseHat
from signal import pause
x = y = 4
hat = SenseHat()
def update_screen():
hat.clear()
hat.set_pixel(x, y, 255, 255, 255)
def clamp(value, min_value=0, max_value=7):
return min(max_value, max(min_value, value))
def move_dot(event):
global x, y
if event.action in ('pressed', 'held'):
x = clamp(x + {
'left': -1,
'right': 1,
}.get(event.direction, 0))
y = clamp(y + {
'up': -1,
'down': 1,
}.get(event.direction, 0))
update_screen()
hat.stick.direction_up = move_dot
hat.stick.direction_down = move_dot
hat.stick.direction_left = move_dot
hat.stick.direction_right = move_dot
hat.stick.direction_any = update_screen
pause()
Rainbow¶
Scrolls a rainbow of colours across the Sense HAT’s pixels:
from colorsys import hsv_to_rgb
from time import sleep
from sense_emu import SenseHat
# Hues represent the spectrum of colors as values between 0 and 1. The range
# is circular so 0 represents red, ~0.2 is yellow, ~0.33 is green, 0.5 is cyan,
# ~0.66 is blue, ~0.84 is purple, and 1.0 is back to red. These are the initial
# hues for each pixel in the display.
hues = [
0.00, 0.00, 0.06, 0.13, 0.20, 0.27, 0.34, 0.41,
0.00, 0.06, 0.13, 0.21, 0.28, 0.35, 0.42, 0.49,
0.07, 0.14, 0.21, 0.28, 0.35, 0.42, 0.50, 0.57,
0.15, 0.22, 0.29, 0.36, 0.43, 0.50, 0.57, 0.64,
0.22, 0.29, 0.36, 0.44, 0.51, 0.58, 0.65, 0.72,
0.30, 0.37, 0.44, 0.51, 0.58, 0.66, 0.73, 0.80,
0.38, 0.45, 0.52, 0.59, 0.66, 0.73, 0.80, 0.87,
0.45, 0.52, 0.60, 0.67, 0.74, 0.81, 0.88, 0.95,
]
hat = SenseHat()
def scale(v):
return int(v * 255)
while True:
# Rotate the hues
hues = [(h + 0.01) % 1.0 for h in hues]
# Convert the hues to RGB values
pixels = [hsv_to_rgb(h, 1.0, 1.0) for h in hues]
# hsv_to_rgb returns 0..1 floats; convert to ints in the range 0..255
pixels = [(scale(r), scale(g), scale(b)) for r, g, b in pixels]
# Update the display
hat.set_pixels(pixels)
sleep(0.04)