-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.swift
244 lines (188 loc) · 5.98 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//------------------------------------------------------------------------------
//
// bt cabinet1.swift4a
// Swift For Arduino 3.0+
//
// Created by Carl Peto on 04/16/2020.
//
// NOTE: Modifications to the "Libraries:" comment line below will affect the build.
// Libraries: unsafeAdafruit_Bluefruit_LE.swift
//------------------------------------------------------------------------------
/*
____________________________________________________________________________
Purpose
Configure the Adafruit Bluefruit shield to listen to UART data, then use a
custom protocol to control a string of neopixel lights, setting colour and
brightness.
____________________________________________________________________________
Hardware Configuration
- Adafruit Bluefruit shield must be installed
- D5 goes to the neopixel string to control them
____________________________________________________________________________
Notes
1. Make sure you have up to date community libraries to get the bluetooth
library code.
2. Make sure the number of neopixels on the string matches the pixelCount
in the program.
3. Make sure hasWhite and grbOrdered are correct for your hardware.
4. Requires a matching program on your iphone to control the lights. The code
for that program should be found near this program!
(see swiftforarduino/neopixel-cabinet)
____________________________________________________________________________
Experiments
- update the iphone app to give a better ui
- send the current colour back to the iphone on connect so it
can update its ui accordingly
- add separately controlled white values if supported
____________________________________________________________________________
*/
import AVR
typealias IntegerLiteralType = Pin
let iLEDPin = D5
let pixelCount: UInt16 = 60
// setup serial (for debugging only)
SetupSerial()
// attempt to retrieve previous hue/value from EEPROM
var existingF = readEEPROM(address: 199)
var existingHue = readEEPROM(address: 10)
var existingOn = readEEPROM(address: 299)
var currentValue: UInt8 = existingF
// current hue/value
var currentHue: UInt8 = existingHue
var on: Bool = existingOn != 0
// helper functions
func showSolidColour(hue: UInt8, value: UInt8) {
var col: iLEDFastColor
if on {
col = iLEDFastMakeColor(hue: hue, saturation: 255, value: value, white: 0)
} else {
col = iLEDOff
}
for _ in 1...pixelCount {
iLEDFastWritePixel(color: col)
}
}
func currentState() -> (data: AVRString, length: UInt8) {
stringStartNew()
stringAddCharacter(0x48)
stringAddCharacter(currentHue)
stringAddCharacter(0x56)
stringAddCharacter(currentValue)
stringAddCharacter(0x31)
stringAddCharacter(on ? 1 : 0)
return (data: stringCurrentValue(), length: 6)
}
func sendCurrentState() {
btPrint(buffer: currentState().data)
}
func fadeOn() {
setOn()
let duration: UInt16 = 500
let brightnessStep = duration / UInt16(50)
// fade in to current value
var brightness = 0
while brightness < 255 {
brightness = brightness &+ 5
if brightness >= currentValue {
break
}
showSolidColour(hue: currentHue, value: brightness)
delay(ms: brightnessStep)
}
showSolidColour(hue: currentHue, value: currentValue)
}
func fadeOff() {
let duration: UInt16 = 500
let brightnessStep = duration / UInt16(50)
var brightness = currentValue
while brightness > 0 {
brightness = brightness &- 5
showSolidColour(hue: currentHue, value: brightness)
delay(ms: brightnessStep)
if brightness < 10 {
brightness = 0
}
}
setOff()
showSolidColour(hue: currentHue, value: currentValue)
}
func setOn() {
on = true
writeEEPROMWithoutVerify(address: 299, value: 1)
}
func setOff() {
on = false
writeEEPROMWithoutVerify(address: 299, value: 0)
}
func getBTCommand() -> (data: AVRString, length: UInt8) {
while btAvailable() == 0 {} // block until data
stringStartNew()
var length: UInt8 = 0
while btAvailable() > 0 {
let c = btRead()
stringAddCharacter(c.0)
length = length &+ 1
}
return (stringCurrentValue(), length)
}
func interpretCommand(cmd: (data: AVRString, length: UInt8)) {
guard let data = cmd.data else {
return
}
let len = cmd.length
if len > 0, data[0] == 0x3F {
sendCurrentState()
return
}
if len > 0, data[0] == 0x30 {
setOff()
showSolidColour(hue: currentHue, value: currentValue)
return
}
if len > 0, data[0] == 0x31 {
setOn()
showSolidColour(hue: currentHue, value: currentValue)
return
}
if len > 0, data[0] == 0x32 {
fadeOff()
return
}
if len > 0, data[0] == 0x33 {
fadeOn()
return
}
if len > 1, data[0] == 0x48 {
currentHue = UInt8(bitPattern: data[1])
writeEEPROMWithoutVerify(address: 10, value: currentHue)
}
if len > 3, data[2] == 0x56 {
currentValue = UInt8(bitPattern: data[3])
if currentValue > 100 {
currentValue = 100
}
writeEEPROMWithoutVerify(address: 199, value: currentValue)
}
showSolidColour(hue: currentHue, value: currentValue)
}
// setup neopixels
pinMode(pin: iLEDPin, mode: OUTPUT)
iLEDFastSetup(pin: iLEDPin, pixelCount: pixelCount, hasWhite: true, grbOrdered: true)
delay(microseconds: 6)
showSolidColour(hue: currentHue, value: currentValue)
//setup bluetooth UART...
btStart(verbose: true)
btSetEcho(on: false)
btSetVerbose(on: false)
btSendCommand("AT+GAPDEVNAME=S4A BT Device")
// ...wait for connection, then go to data mode
while !btIsConnected() {}
print("connected")
btSetMode(mode: btDataMode)
//delay(ms: 3000)
// wait for commands to come in over bluetooth and implement them
while(true) {
let cmd = getBTCommand()
interpretCommand(cmd: cmd)
delay(ms: 200)
}