用于 Raspberry Pi Sense Hat 的 Swift 软件包 SenseHat

Photo

功能

待办

用法

实例化

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

参数 orientation 可用于其他方向,例如 SenseHat(orientation: .left)。参数 frameBufferDevice 可用于指定帧缓冲区设备,例如 SenseHat(frameBufferDevice: "/dev/fb0")。两个参数可以同时使用:SenseHat(frameBufferDevice: "/dev/fb0", orientation: .down)

参数 orientation 定义 LED 矩阵的顶部在哪里。以下是用不同方向显示相同字符 "1" 的示例

.up .left .right .down
1 up 1 left 1 right 1 down

将矩阵的所有 LED 设置为特定颜色

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

将矩阵的特定 LED 设置为特定颜色

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

坐标 xy 应属于 0..<7 范围。

在 LED 矩阵上显示字符

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

A

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

pi

在 LED 矩阵上显示字符串

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

读取湿度传感器

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

读取压力传感器

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

实用链接

遗憾的是,Raspberry Pi Sense Hat 的数据手册或程序员手册不存在,或者我未能找到。这里有一些实用链接

Buy Me A Coffee

Blinking