-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.bgs
More file actions
357 lines (271 loc) · 10 KB
/
Copy pathscript.bgs
File metadata and controls
357 lines (271 loc) · 10 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
dim result
#pin
dim pin(2)
dim pin_len
dim auth
#Sleep Pin read
dim portReadResult
dim portReadPort
dim portReadData
# endpoint data in
dim in(20)
dim in_len
# endpoint data out
dim out(20)
dim out_len
# Serial RX Buffer
dim srx_buffer(256)
dim srx_index
dim srx_write_index
dim srx_write_len
dim srx_indicate
dim srx_indicated
# Atmega 32u4 Interrupt
dim at_interrupt
# Is the device connected
dim connected
#delay counter
dim delay
procedure delay(d)
let delay = d
while delay>0
delay = delay-1
end while
end
procedure mcu_interrupt()
at_interrupt = 1
call system_endpoint_tx(system_endpoint_uart1, 20, "{e:'mcu_interrupt'}\n")
call hardware_io_port_write(0, $02, $02)
call hardware_set_soft_timer(32, 1, 1) # Call timer 1 once after 1 millisec, to set the interrupt pin low
end
procedure mcu_interrupt_complete()
at_interrupt = 0
call attributes_write(xgatt_mcu_interrupt, 0, 1, 0) # Set char back to 0 (off)
call hardware_io_port_write(0, $02, $00)
end
procedure mcu_wake_test()
call hardware_io_port_read(1, $01)(portReadResult, portReadPort, portReadData )
if portReadData != 01
call mcu_interrupt()
end if
end
procedure set_sensor_gatt( id, value )
if id = $01
call attributes_write(xgatt_sensor1, 0, 2, value)
end if
if id = $02
call attributes_write(xgatt_sensor2, 0, 2, value)
end if
if id = $03
call attributes_write(xgatt_sensor3, 0, 2, value)
end if
if id = $04
call attributes_write(xgatt_sensor4, 0, 2, value)
end if
if id = $05
call attributes_write(xgatt_sensor5, 0, 2, value)
end if
if id = $06
call attributes_write(xgatt_sensor6, 0, 2, value)
end if
if id = $07
call attributes_write(xgatt_sensor7, 0, 2, value)
end if
if id = $08
call attributes_write(xgatt_sensor8, 0, 2, value)
end if
end
# Boot event listener (gets executed when the device is powered)
event system_boot(major, minor, patch, build, ll_version, protocol_version, hw)
call flash_ps_load($8000)(result, pin_len, pin(0:2))
if pin(0:2) = 0000
auth = 1
else
auth = 0
end if
#call attributes_write(xgatt_debug, 0, 2, pin(0:2))
connected = 0 # We are not connected
at_interrupt = 0 # Set MCU reset pin low, so so low.
# Reset Serial RX Buffer Index
srx_index = 0
srx_write_index = 0
#call system_endpoint_set_watermarks(system_endpoint_uart1, 0, 0) # Disable watermarks
call system_endpoint_set_watermarks(system_endpoint_uart1, 1, $ff) # Enable watermarks
# set advertisement interval to 1-2 seconds, all three channels
call gap_set_adv_parameters(1600, 3200, 7)
call gap_set_mode(gap_general_discoverable, gap_undirected_connectable) # Start advertisement
# set all GPIO pins to input/high (pull), except P1_0 and P1_1 to output HIGH (no internal pull-ups)
# P1_0/1 will leak current if not externally pulled or set as outputs
call hardware_io_port_config_direction(0, $0)
call hardware_io_port_write(0, $ff, $ff)
call hardware_io_port_config_direction(1, $2)
call hardware_io_port_write(1, $ff, $ff)
call hardware_io_port_config_direction(2, $0)
call hardware_io_port_write(2, $ff, $ff)
# configure P0_0 and P0_1 as output. Connected LED and MCU Interrupt respectively
call hardware_io_port_config_direction(0, $03)
call hardware_io_port_write(0, $03, 0)
#Set timer to generate event every 2s
#call hardware_set_soft_timer(32768*2, 1, 0)
end
#Timer event listener
event hardware_soft_timer(handle)
if handle = 1
call mcu_interrupt_complete()
end if
if handle = 2
if srx_index > srx_write_index && connected = 1
if srx_indicate = 1 && srx_indicated = 0
return
end if
srx_write_len = (srx_index - srx_write_index)
if srx_write_len > 20 then
srx_write_len = 20
end if
if srx_indicate
call attributes_write(xgatt_data_rx, 0, srx_write_len, srx_buffer(srx_write_index:srx_write_len))
srx_indicated = 0
else
call attributes_write(xgatt_data, 0, srx_write_len, srx_buffer(srx_write_index:srx_write_len))
end if
srx_write_index = srx_write_index + srx_write_len
if srx_write_index >= srx_index then
srx_index = 0
srx_write_index = 0
end if
end if
end if
if handle = 3
call system_reset(1)
end if
# Delayed mcu sleep disable command
if handle = 4
call system_endpoint_tx(system_endpoint_uart1, 2, $004E)
end if
end
# Connection event listener
event connection_status(connection, flags, address, address_type, conn_interval, timeout, latency, bonding)
connected = 1 #Device is connected
call hardware_io_port_write(0, $01, 1)
#call system_endpoint_tx(system_endpoint_uart1, 18, "\n\r++ Connected ++\n")
call hardware_set_soft_timer((32768/1000) * conn_interval / 4, 2, 0)
#call attributes_write(xgatt_debug, 0, 2, conn_interval)
call mcu_wake_test()
call hardware_set_soft_timer(3200, 4, 1) # Delayed sleep disable mcu command
end
# Disconnection event listener
event connection_disconnected(connection, reason)
connected = 0 # We are not connected
call hardware_io_port_write(0, $01, 0)
#call system_endpoint_set_watermarks(system_endpoint_uart1, 0, 0) # Disable watermarks
call gap_set_adv_parameters(1600, 3200, 7) # set advertisement interval to 1-2 seconds, all three channels
call gap_set_mode(gap_general_discoverable, gap_undirected_connectable) # Start advertisement
#call system_endpoint_tx(system_endpoint_uart1, 18, "\n\r++ Disconnected ++\n")
call system_endpoint_tx(system_endpoint_uart1, 2, $014E)
end
# Incoming data from Bluetooth: Attributes value event listener
event attributes_value(connection, reason, handle, offset, value_len, value_data)
if handle = xgatt_pin then
if pin(0:2) = value_data(0:2)
auth = 1
else
auth = 0
end if
end if
if auth = 1 # start auth block
if handle = xgatt_set_pin then
call flash_ps_save($8000, 2, value_data(0:2))
call attributes_write(xgatt_pin, 0, 2, value_data(0:2))
end if
if handle = xgatt_data || handle = xgatt_data_rx then
out(0:value_len) = value_data(0:value_len) # Copy data from GATT
out_len = value_len # Store data length
call system_endpoint_set_watermarks(system_endpoint_uart1, $ff, out_len) # Set TX watermark to data length
end if
if handle = xgatt_mcu_interrupt then
if value_data(0:1) & $01
call mcu_interrupt()
else
call mcu_interrupt_complete()
end if
end if
if handle = xgatt_dfu_reset then
if value_data(0:1) & $01
# Put MCU into UART pass through mode
call system_endpoint_tx(system_endpoint_uart1, 2, $0208)
# Reboot to UART DFU mode with a timer after one second
call hardware_set_soft_timer(32768*1, 3, 1)
end if
end if
else
if handle = xgatt_data || handle = xgatt_data_rx then
call attributes_user_write_response(0,0)
end if
end if # end authd block
end
# System endpoint watermark TX event listener
event system_endpoint_watermark_tx(endpoint, size)
if (endpoint = system_endpoint_uart1) then
call mcu_wake_test()
call system_endpoint_set_watermarks(system_endpoint_uart1, $ff, 0) # Disable TX watermark
call system_endpoint_tx(system_endpoint_uart1, out_len, out(0:out_len)) # Write data to UART
call attributes_user_write_response(0,0)
out_len = 0
end if
end
# Attribute status event listener
# Event occurs if attribute's Client Characteristic Configuration value changes.
event attributes_status(handle, flags)
if handle = xgatt_data_rx then
if flags & 2 then
srx_indicate = 1
srx_indicated = 1
end if
end if
if handle = xgatt_data
if flags & 1 then
srx_indicate = 0
srx_indicated = 0
end if
end if
end
# System endpoint watermark event listener
# Generated when there is data available from UART
event system_endpoint_watermark_rx(endpoint, size)
if (endpoint = system_endpoint_uart1) then
call hardware_io_port_write(0, $01, 1)
call system_endpoint_set_watermarks(system_endpoint_uart1, 0, $ff) # disable RX watermark
in_len = size
if srx_index + in_len > 210
in_len = 200 - srx_index
call system_endpoint_rx(system_endpoint_uart1, in_len)(result, in_len, srx_buffer(srx_index:in_len))
srx_index = 0
in_len = size - in_len
else
call system_endpoint_rx(system_endpoint_uart1, in_len)(result, in_len, srx_buffer(srx_index:in_len))
## Check buffer for sensor command 0xe783
if srx_buffer(srx_index:2) = $83e7
call set_sensor_gatt( srx_buffer(srx_index+2:1), srx_buffer(srx_index+3:2) )
else
if srx_buffer(srx_index:2) = $84e8 || srx_buffer(srx_index-1:2) = $84e8
call mcu_interrupt()
else
if connected = 1
srx_index = srx_index + in_len
end if
end if
end if
end if
call system_endpoint_set_watermarks(system_endpoint_uart1, 1, $ff) # enable RX watermark
#call attributes_write(xgatt_debug, 0, 2, in_len)
call hardware_io_port_write(0, $01, 0)
end if
end
# Attribute client indicated event listener
event attclient_indicated(connection, attrhandle)
if attrhandle = xgatt_data_rx then
call system_endpoint_set_watermarks(system_endpoint_uart1, 1, $ff) # set RX watermark to 1 byte
srx_indicated = 1
in_len = 0
end if
end