Skip to content

valeriyvan/RaspberryPiSenseHat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift package SenseHat for Raspberry Pi Sense Hat

Photo

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.

Usage

Instantiating

// 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
1 up 1 left 1 right 1 down

Set all LEDs of matrix to specific color

senseHat.set(color: .red) // sets all LEDs of matrix to red

Red

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

Black

Set specific LED of matrix to specific color

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

White and green

Coordinates x and y should belong to 0..<7 range.

Show character on LED matrix

senseHat.show(character: Character("A"), color: .blue)

A

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

pi

Show string on LED matrix

senseHat.show(string: "Hello! ", secPerChar: 0.5, color: .yellow, background: .blue)

Hello!

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

Greek

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

Hiragana

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

deutsch

Reading humidity sensor

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")
}

humidity

Reading pressure 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")
}

pressure

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:

Buy Me A Coffee

Blinking