9. API - The Sense HAT¶
pisense module is the main namespace for the pisense package; it
imports (and exposes) all publically accessible classes, functions, and
constants from all the modules beneath it for convenience. It also defines
SenseHAT(settings='/etc/RTIMULib.ini', *, fps=15, easing=<function linear>, max_events=100, flush_input=True, emulate=None)¶
An instance of this class represents the Sense HAT as a whole. It provides attributes for objects that represent each component of the HAT, including:
stickfor the joystick
screenfor the display
environfor the environmental sensors
imufor the Inertial Measurement Unit (IMU)
The settings parameter can be used to point to alternate settings files but it is strongly recommended you leave this at the default as this can affect the calibration of the IMU. Other keyword arguments are used in the initialization of the subordinate objects; see the documentation for their classes for further information.
One particular keyword argument, emulate, takes its default from an environment variable:
PISENSE_EMULATE. If set, this must be an integer number, typically 0 or 1 (0 is assumed if the variable is not set). This argument indicates whether the instance should attach to the “real” Sense HAT or the desktop Sense HAT emulator. The environment variable is particularly useful as it means scripts can be tested against the emulator without alteration. For example:
$ PISENSE_EMULATE=1 python examples/rainbow.py
Your script should not attempt to create more than one instance of this class (given it represents a single piece of hardware). If you attempt to do so a
SenseHATReinitwarning will be raised and the existing instance will be returned.
close()method to close the Sense HAT interface and free up any background resources. The method is idempotent (you can call it multiple times without error) and after it is called, any operations on the Sense HAT may return an error (but are not guaranteed to do so).
Gets or sets the rotation (around the Z-axis) of the Sense HAT. When querying this attribute, only the screen’s rotation is queried. When set, the attribute affects the screen, joystick, and IMU.