The Sense HAT is an add-on board for the Raspberry Pi family. It features an LED matrix, a joystick, a gyroscope, an accelerometer, a magnetometer, and temperature, barometric pressure, and humidity sensors.
This library pulls in hardware specific modules for the Sense HAT and exports them in a single module.
The LED library is the sense-hat-led module. Example
It supports setting individual pixels, flipping the display horizontally or vertically, displaying characters, and batch pixel jobs (clear, fill). We recommend you check the documentation for the library above for the entire API documentation.
// Using the standalone hw-specific library
const matrix = require('sense-hat-led');
// Using this library
const matrix = require('node-sense-hat').Leds;
const x = 3;
const y = 3;
const red = [255, 0, 0];
// Set a single pixel
matrix.setPixel(x, y, red);
const matrix = require('node-sense-hat').Leds;
const x = 3;
const y = 3;
const off = [0, 0, 0];
matrix.setPixel(x, y, off);
const matrix = require('node-sense-hat').Leds;
const O = [0, 0, 0];
const X = [255, 0, 0];
const cross = [
X, O, O, O, O, O, O, X,
O, X, O, O, O, O, X, O,
O, O, X, O, O, X, O, O,
O, O, O, X, X, O, O, O,
O, O, O, X, X, O, O, O,
O, O, X, O, O, X, O, O,
O, X, O, O, O, O, X, O,
X, O, O, O, O, O, O, X,
];
matrix.setPixels(cross);
// To fill with a single color instead
matrix.clear([127, 0, 0]);
const matrix = require('node-sense-hat').Leds
matrix.clear()
The joystick library used is the sense-joystick module. Example
The joystick library exposes an EventEmitter
interface, so that you
can register a function to be called when a joystick event occurs.
Possible joystick events are 'press'
, 'release'
and 'hold'
, and the
possible direction values for these events are 'up'
, 'down'
, 'left'
,
'right'
, and 'click'
.
When using the joystick, the first thing that needs to be done is retrieving a
handle to the device. Behind the scenes this involves finding the correct Linux
/dev
file and reading the descriptions. As this can be quite a long process,
the getJoystick
function returns a promise.
const JoystickLib = require("node-sense-hat").Joystick;
JoystickLib.getJoystick().then(joystick => {
// We now have a handle the joystick hardware
});
const JoystickLib = require("node-sense-hat").Joystick;
JoystickLib.getJoystick().then(joystick => {
joystick.on("press", direction => {
console.log("Joystick pressed in " + direction + " direction");
});
joystick.on("release", direction => {
console.log("Joystick released in " + direction + " direction");
});
joystick.on("hold", direction => {
console.log("The joystick is being held in the " + direction + " direction");
});
});
The Inertial Measurement Unit library used is the nodeimu module. Example
The use of the IMU is slightly more involved—we recommend you check the example above. We've provided some snippets below to detail the simple cases.
const imu = require("node-sense-hat").Imu;
const IMU = new imu.IMU();
IMU.getValue((err, data) => {
if (err !== null) {
console.error("Could not read sensor data: ", err);
return;
}
console.log("Accelleration is: ", JSON.stringify(data.accel, null, " "));
console.log("Gyroscope is: ", JSON.stringify(data.gyro, null, " "));
console.log("Compass is: ", JSON.stringify(data.compass, null, " "));
console.log("Fusion data is: ", JSON.stringify(data.fusionPose, null, " "));
console.log("Temp is: ", data.temperature);
console.log("Pressure is: ", data.pressure);
console.log("Humidity is: ", data.humidity);
});
Getting the tilt correction is more involved again:
const imu = require("node-sense-hat").Imu;
const IMU = new imu.IMU();
const headingCorrection = (heading, offset = 0) => {
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
const declinationAngle = 0.03106686;
heading += declinationAngle + offset;
// Correct for when signs are reversed.
if (heading < 0) {
heading += 2 * Math.PI;
}
// Check for wrap due to addition of declination.
if (heading > 2 * Math.PI) {
heading -= 2 * Math.PI;
}
return heading;
};
const headingToDegree = heading => {
// Convert radians to degrees for readability.
return heading * 180 / Math.PI;
};
IMU.getValue((err, data) => {
if (err !== null) {
console.error("Could not read data: ", err);
}
console.log("Tilt heading is: ", headingToDegree(headingCorrection(data.tiltHeading, Math.PI / 2)));
});
These modules are exposed by the following entries:
Joystick: joystick library
Leds: LED library
Imu: nodeimu library