Sense HAT API¶
The main class which is used to interact with the Sense HAT emulator is
SenseHat
. This provides accesss to all sensors, the LED pixel display,
and the joystick. It is recommended that you import the library using the
following idiom:
from sense_emu import SenseHat
This way, when you wish to deploy your code on an actual Sense HAT the only change you need to make is to this line, changing it to:
from sense_hat import SenseHat
SenseHat¶
-
class
sense_emu.
SenseHat
(imu_settings_file='RTIMULib', text_assets='sense_hat_text')[source]¶ The main interface the Raspberry Pi Sense HAT.
This class provides properties to query the various sensors on the Sense HAT (
temp
,pressure
,humidity
,gyro
, etc.) and methods to control the LED “screen” on the HAT (set_pixel()
,set_pixels()
).The imu_settings_file parameter specifies the base name of the configuration file used to calibrate the sensors on the HAT. An “.ini” suffix will be implicitly added to this filename. If a file with the resulting name is present in
~/.config/sense_hat
, it will be used in the configuration. Otherwise, the file will be located within/etc
, and will be copied to~/.config/sense_hat
before use.The text_assets parameter provides the base name of the PNG image and text file which will be used to define the font used by the
show_message()
method.-
clear
(*args)[source]¶ Clears the LED matrix with a single colour, default is black / off
e.g. ap.clear() or ap.clear(r, g, b) or colour = (r, g, b) ap.clear(colour)
-
get_orientation_degrees
()[source]¶ Returns a dictionary object to represent the current orientation in degrees, 0 to 360, using the aircraft principal axes of pitch, roll and yaw
-
get_orientation_radians
()[source]¶ Returns a dictionary object to represent the current orientation in radians using the aircraft principal axes of pitch, roll and yaw
-
get_pixel
(x, y)[source]¶ Returns a list of [R,G,B] representing the pixel specified by x and y on the LED matrix. Top left = 0,0 Bottom right = 7,7
-
get_pixels
()[source]¶ Returns a list containing 64 smaller lists of
[R,G,B]
pixels representing what is currently displayed on the LED matrix
-
get_temperature_from_humidity
()[source]¶ Returns the temperature in Celsius from the humidity sensor
-
get_temperature_from_pressure
()[source]¶ Returns the temperature in Celsius from the pressure sensor
-
load_image
(file_path, redraw=True)[source]¶ Accepts a path to an 8 x 8 image file and updates the LED matrix with the image
-
set_imu_config
(compass_enabled, gyro_enabled, accel_enabled)[source]¶ Enables and disables the gyroscope, accelerometer and/or magnetometer input to the orientation functions
-
set_pixel
(x, y, *args)[source]¶ Updates the single
[R,G,B]
pixel specified by x and y on the LED matrix Top left = 0,0 Bottom right = 7,7e.g. ap.set_pixel(x, y, r, g, b) or pixel = (r, g, b) ap.set_pixel(x, y, pixel)
-
set_pixels
(pixel_list)[source]¶ Accepts a list containing 64 smaller lists of
[R,G,B]
pixels and updates the LED matrix. R,G,B elements must intergers between 0 and 255
-
set_rotation
(r=0, redraw=True)[source]¶ Sets the LED matrix rotation for viewing, adjust if the Pi is upside down or sideways. 0 is with the Pi HDMI port facing downwards
-
show_letter
(s, text_colour=[255, 255, 255], back_colour=[0, 0, 0])[source]¶ Displays a single text character on the LED matrix using the specified colours
-
show_message
(text_string, scroll_speed=0.1, text_colour=[255, 255, 255], back_colour=[0, 0, 0])[source]¶ Scrolls a string of text across the LED matrix using the specified speed and colours
-
stick
¶ A
SenseStick
object representing the Sense HAT’s joystick.
-
SenseStick¶
-
class
sense_emu.
SenseStick
[source]¶ Represents the joystick on the Sense HAT.
-
get_events
()[source]¶ Returns a list of all joystick events that have occurred since the last call to
get_events()
. The list contains events in the order that they occurred. If no events have occurred in the intervening time, the result is an empty list.
-
wait_for_event
(emptybuffer=False)[source]¶ Waits until a joystick event becomes available. Returns the event, as an
InputEvent
tuple.If emptybuffer is
True
(it defaults toFalse
), any pending events will be thrown away first. This is most useful if you are only interested in “pressed” events.
-
direction_any
¶ The function to be called when the joystick is used. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.This event will always be called after events associated with a specific action. Assign
None
to prevent this event from being fired.
-
direction_down
¶ The function to be called when the joystick is pushed down. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.Assign
None
to prevent this event from being fired.
-
direction_left
¶ The function to be called when the joystick is pushed left. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.Assign
None
to prevent this event from being fired.
-
direction_middle
¶ The function to be called when the joystick middle click is pressed. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.Assign
None
to prevent this event from being fired.
-
direction_right
¶ The function to be called when the joystick is pushed right. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.Assign
None
to prevent this event from being fired.
-
direction_up
¶ The function to be called when the joystick is pushed up. The function can either take a parameter which will be the
InputEvent
tuple that has occurred, or the function can take no parameters at all.Assign
None
to prevent this event from being fired.
-
InputEvent¶
-
class
sense_emu.
InputEvent
[source]¶ A
namedtuple()
derivative representing a joystick event. The following attributes are present:-
timestamp
¶ The time at which the event occurred, represented as the number of seconds since the UNIX epoch (same output as
time()
).
-
direction
¶ The direction in which the joystick was pushed (or released from), as one of the constants
DIRECTION_UP
,DIRECTION_DOWN
,DIRECTION_LEFT
,DIRECTION_RIGHT
,DIRECTION_MIDDLE
-
action
¶ The action that occurred, as one of the constants
ACTION_PRESSED
,ACTION_RELEASED
, orACTION_HELD
.
-
Constants¶
-
sense_emu.
DIRECTION_UP
¶
-
sense_emu.
DIRECTION_DOWN
¶
-
sense_emu.
DIRECTION_LEFT
¶
-
sense_emu.
DIRECTION_RIGHT
¶
-
sense_emu.
DIRECTION_MIDDLE
¶ Constants representating the direction in which the joystick has been pushed.
DIRECTION_MIDDLE
refers to pressing the joystick as a button.
-
sense_emu.
ACTION_PRESSED
¶
-
sense_emu.
ACTION_RELEASED
¶
-
sense_emu.
ACTION_HELD
¶ Constants representing the actions that can be applied to the joystick.