-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.swift
207 lines (175 loc) · 5.1 KB
/
Device.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
struct PicoDevice {
var defaultLED: LED
var redLed: LED?
var blueLed: LED?
var greenLed: LED?
var button: Button?
var lcd: LCD?
var allOn: Bool = false {
didSet {
defaultLED.isOn = allOn
redLed?.isOn = allOn
blueLed?.isOn = allOn
greenLed?.isOn = allOn
}
}
init(defaultLED: LED, redLed: LED?, blueLed: LED?, greenLed: LED?, button: Button?, lcd: LCD?) {
self.defaultLED = defaultLED
self.redLed = redLed
self.blueLed = blueLed
self.greenLed = greenLed
self.button = button
self.lcd = lcd
}
}
struct LED {
private let pin: UInt32
init(pin: UInt32) {
self.pin = pin
gpio_init(pin)
gpio_set_dir(pin, true)
}
var isOn: Bool = false {
didSet {
gpio_put(pin, isOn)
}
}
}
struct Button {
let pin: UInt32
// pressed -> false (LOW)
var isPressed: Bool {
!gpio_get(pin)
}
init(pin: UInt32) {
self.pin = pin
gpio_init(pin)
// input
gpio_set_dir(pin, false)
}
}
struct LCD {
let addr: UInt8 = 0x27
var i2c0Pointer: UnsafeMutablePointer<i2c_inst_t> = get_pointer()
init() {
i2c_init(i2c0Pointer, 100 * 1000)
gpio_set_function(UInt32(4), GPIO_FUNC_I2C) // SDA
gpio_set_function(UInt32(5), GPIO_FUNC_I2C) // SCL
gpio_pull_up(4) // SDA
gpio_pull_up(5) // SCL
sleep_ms(40)
setup()
}
func clear() {
send(Command.clearDisplay, mode: .command)
setCursor(line: 0, pos: 0)
}
func setCursor(line: UInt8, pos: UInt8) {
let value = (line == 0) ? 0x80 + pos : 0xC0 + pos
send(value, mode: .command)
}
func displayString(_ value: StaticString) {
let maxCharPerLine = 16
value.withUTF8Buffer { buffer in
var idx = 0
var line = 0
for char in buffer {
idx += 1
// break line
if idx > maxCharPerLine, line == 0 {
setCursor(line: 1, pos: 0)
line += 1
}
send(char, mode: .character)
}
}
}
func displayStringWithStreaming(_ value: StaticString) {
let maxCharPerLine = 16
value.withUTF8Buffer { buffer in
var idx = 0
var line = 0
for char in buffer {
idx += 1
// break line
if idx > maxCharPerLine, line == 0 {
setCursor(line: 1, pos: 0)
line += 1
}
send(char, mode: .character)
sleep_ms(200)
}
}
}
private func setup() {
send(0x03, mode: .command)
send(0x03, mode: .command)
send(0x03, mode: .command)
send(0x02, mode: .command)
send(Command.entryModeSet | EntryMode.left, mode: .command)
send(Command.functionSet | FunctionSet.lcd2Line, mode: .command)
send(Command.displayControl | CursorContol.displayON, mode: .command)
send(Command.returnHome, mode: .command)
clear()
}
private func toggleEnable(value: UInt8) {
sleep_us(600)
write(value: UInt8(value | Flag.enable))
sleep_us(600)
write(value: UInt8(value & ~Flag.enable))
sleep_us(600)
}
private func write(value: UInt8) {
var copiedValue = value
withUnsafeMutablePointer(to: &copiedValue) { pointer in
_ = i2c_write_blocking(i2c0Pointer, addr, pointer, 1, false)
}
}
private func send(_ bytes: UInt8, mode: Mode) {
let high = (mode.rawValue) | (bytes & 0xF0) | Backlight.on
let low = (mode.rawValue) | ((bytes << 4) & 0xF0) | Backlight.on
write(value: high)
toggleEnable(value: high)
write(value: low)
toggleEnable(value: low)
}
enum Mode: UInt8 {
case command = 0
case character = 1
}
enum Command {
static let clearDisplay: UInt8 = 0x01
static let returnHome: UInt8 = 0x02
static let entryModeSet: UInt8 = 0x04
static let displayControl: UInt8 = 0x08
static let cursorShift: UInt8 = 0x10
static let functionSet: UInt8 = 0x20
static let setCGRamAddr: UInt8 = 0x40
static let setDDRamAddr: UInt8 = 0x80
}
enum EntryMode {
static let shiftIncrement: UInt8 = 0x01
static let left: UInt8 = 0x02
}
enum CursorContol {
static let blinkON: UInt8 = 0x01
static let cursorON: UInt8 = 0x02
static let displayON: UInt8 = 0x04
}
enum CursorShift {
static let moveRight: UInt8 = 0x04
static let displayMove: UInt8 = 0x08
}
enum FunctionSet {
static let lcd5x10Dots: UInt8 = 0x04
static let lcd2Line: UInt8 = 0x08
static let lcd8BitMode: UInt8 = 0x10
}
enum Backlight {
static let on: UInt8 = 0x08
static let off: UInt8 = 0x00
}
enum Flag {
static let enable: UInt8 = 0x04
}
}