https://github.com/valeriyvan/raspberrypisensehat
Swift package for Raspberry Pi Sense Hat
https://github.com/valeriyvan/raspberrypisensehat
diy hacktoberfest raspberry-pi sense-hat spm swift swiftpackage
Last synced: about 1 month ago
JSON representation
Swift package for Raspberry Pi Sense Hat
- Host: GitHub
- URL: https://github.com/valeriyvan/raspberrypisensehat
- Owner: valeriyvan
- License: mit
- Created: 2020-10-30T22:43:27.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-04-19T17:10:18.000Z (about 3 years ago)
- Last Synced: 2025-03-01T15:04:21.214Z (over 1 year ago)
- Topics: diy, hacktoberfest, raspberry-pi, sense-hat, spm, swift, swiftpackage
- Language: Swift
- Homepage:
- Size: 8.89 MB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Swift package `SenseHat` for Raspberry Pi Sense Hat

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](https://en.wikipedia.org/wiki/Conway%27s_Game_of_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.
# Usage
## Instantiating
``` Swift
// 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`
--- | --- | --- | ---
 |  |  | 
## Set all LEDs of matrix to specific color
``` Swift
senseHat.set(color: .red) // sets all LEDs of matrix to red
```

``` Swift
senseHat.set(color: .black) // sets all LEDs of matrix to black, literally turns them off
```

## Set specific LED of matrix to specific color
``` Swift
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.
## Show character on LED matrix
``` Swift
senseHat.show(character: Character("A"), color: .blue)
```

``` Swift
senseHat.show(character: Character("π"), color: .yellow, background: .blue)
```

## Show string on LED matrix
``` Swift
senseHat.show(string: "Hello! ", secPerChar: 0.5, color: .yellow, background: .blue)
```

``` Swift
senseHat.orientation = .left
senseHat.show(string: "Απόλλων ", secPerChar: 0.5, color: .red, background: .darkGray)
```

``` Swift
senseHat.orientation = .right
senseHat.show(string: "ここからそこまで ", secPerChar: 0.5, color: .white, background: .brown)
```

``` Swift
senseHat.orientation = .down
senseHat.show(string: "Fußgängerübergänge ", secPerChar: 0.5, color: .white, background: .purple)
```

## Reading humidity sensor
``` Swift
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")
}
```

## Reading pressure sensor
``` Swift
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")
}
```

# Useful links
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](https://www.raspberrypi.org/products/sense-hat/);
* [Astro Pi: Flight Hardware Tech Specs](https://www.raspberrypi.org/blog/astro-pi-tech-specs/) names all sensors of SenseHat with links on datasheets;
* Official documentation [Documentation for Sense HAT](https://www.raspberrypi.org/documentation/hardware/sense-hat/);
* Official Python module [sense-hat](https://pythonhosted.org/sense-hat/);
* Source of Python module [sense-hat](https://github.com/astro-pi/python-sense-hat);
* [Getting started with the Sense HAT](https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat);
* Rust library [sensehat-screen](https://docs.rs/sensehat-screen/) where it's explained how RGB is packed in two bytes in so called `Rgb565`;
* Linux [The Frame Buffer Device API](https://www.kernel.org/doc/Documentation/fb/api.txt) used to access pixel buffer;
* Data sheet for [LSM9DS1](https://www.st.com/resource/en/datasheet/lsm9ds1.pdf), this might be useful [Reading from FIFO](https://github.com/kriswiner/LSM9DS1/issues/5);
* Guide to install Swift on Raspberry Pi [buildSwiftOnARM](https://github.com/uraimo/buildSwiftOnARM) used to install prebuilt Swift 5.1.5 TODO: describe what I've done differently;
* [3D printing Astro Pi case](https://projects.raspberrypi.org/en/projects/astro-pi-flight-case).

