Features:
- ✅ blink with LED - that what everyone starts from playing with Raspberry Pi;
- ✅ get/set color to individual pixels;
- ✅ set color to all pixels in one shot;
- ✅ get/set all pixels in one shot (to/from Data);
- ✅ show 8x8 characters on LCD matrix (supported ascii, extended latin, box drawings elements, block elements, Hiragana, Greek, sga);
- ✅ show arbitrary text (8x8 font, horizontal scroll);
- ✅ rotating of LED matrix 0º/90º/180º/270º;
- ✅ set orientation of LED matrix 0º/90º/180º/270º and make all get/set primitives respect it;
- ✅ read joystick;
- ✅ read humidity and pressure sensors;
- ✅ read gyroscope/accelerometer/magnitometer sensors;
- ✅ snake game;
- ✅ life game;
TODO:
- improve gyroscope/accelerometer/magnetometer sensors reading;
- add conversion of RGB color to Rgb565;
- add Cyrillic font 8x8;
- add 3x5 font;
- replace C fonts with Swift ones;
- show arbitrary image on LED matrix;
- show preprocessed video;
- menu to show readings from any of available sensors;
- mode for rotating screen depending on gyro readings;
- emulator of SenseHat LED matrix for Web;
- Kalman filtering for accelerometer/gyro/magnetometer;
- add analog clock demo app.
// Look over all frame buffer devices in `/dev/` for one of Sense Hat.
// Use default orientation `.up`
guard let senseHat = SenseHat() else {
fatalError("Can't initialise Raspberry Pi Sense Hat")
}
Parameter orientation
could be used for other orientations" SenseHat(orientation: .left)
.
Parameter frameBufferDevice
could be use for specific frame buffer device: SenseHat(frameBufferDevice: "/dev/fb0")
.
Both parameters could be used: SenseHat(frameBufferDevice: "/dev/fb0", orientation: .down)
.
Parameter orientation defines where top of the LED matrix will be. Here are example of the same character "1"
shown with different orientations:
.up |
.left |
.right |
.down |
---|---|---|---|
senseHat.set(color: .red) // sets all LEDs of matrix to red
senseHat.set(color: .black) // sets all LEDs of matrix to black, literally turns them off
senseHat.set(color: .black) // clear
senseHat.set(x: 0, y: 0, color: .white) // set most top left LED to white using function syntax
senseHat[7, 7] = .green // set most bottom right LED to green using subscript syntax
Coordinates x
and y
should belong to 0..<7
range.
senseHat.show(character: Character("A"), color: .blue)
senseHat.show(character: Character("π"), color: .yellow, background: .blue)
senseHat.show(string: "Hello! ", secPerChar: 0.5, color: .yellow, background: .blue)
senseHat.orientation = .left
senseHat.show(string: "Απόλλων ", secPerChar: 0.5, color: .red, background: .darkGray)
senseHat.orientation = .right
senseHat.show(string: "ここからそこまで ", secPerChar: 0.5, color: .white, background: .brown)
senseHat.orientation = .down
senseHat.show(string: "Fußgängerübergänge ", secPerChar: 0.5, color: .white, background: .purple)
if let h = senseHat.humidity() {
let strH = String(format: "%.1lf", h.H_rH)
senseHat.show(string: "Humidity \(strH)% rH ", secPerChar: 0.5, color: .yellow, background: .black)
let strT = String(format: "%.1lf", h.T_DegC)
senseHat.show(string: "Temperature \(strT)ºC", secPerChar: 0.5, color: .yellow, background: .black)
} else {
print("Cannot read humidity sensor")
}
if let p = senseHat.pressure() {
let strP = String(format: "%.1lf", p.P_hPa)
let strT = String(format: "%.1lf", p.T_DegC)
senseHat.show(string: "Pressure \(strP) hPa ", secPerChar: 0.5, color: .yellow, background: .black)
senseHat.show(string: "Temperature \(strT)ºC", secPerChar: 0.5, color: .yellow, background: .black)
} else {
print("Cannot read pressure sensor")
}
Unfortunately Data sheet or Programmer's manual for Raspberry Pi Sense Hat doesn't exist or I have failed to find it. Here are some useful links:
- Official page on raspberrypi.org Sense HAT;
- Astro Pi: Flight Hardware Tech Specs names all sensors of SenseHat with links on datasheets;
- Official documentation Documentation for Sense HAT;
- Official Python module sense-hat;
- Source of Python module sense-hat;
- Getting started with the Sense HAT;
- Rust library sensehat-screen where it's explained how RGB is packed in two bytes in so called
Rgb565
; - Linux The Frame Buffer Device API used to access pixel buffer;
- Data sheet for LSM9DS1, this might be useful Reading from FIFO;
- Guide to install Swift on Raspberry Pi buildSwiftOnARM used to install prebuilt Swift 5.1.5 TODO: describe what I've done differently;
- 3D printing Astro Pi case.