-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patheZ430-Chronos_driver.tcl
More file actions
executable file
·358 lines (294 loc) · 8.02 KB
/
Copy patheZ430-Chronos_driver.tcl
File metadata and controls
executable file
·358 lines (294 loc) · 8.02 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
set vcp 0
set vcp_reply ""
set vcp_len 0
set vcp_pending 0
set vcp_debug 0
if { $vcp_debug == 1 } {
set fh [open "log.txt" w]
} else {
set fh 0
}
# Send string through UART
proc SendCmd { command } {
global vcp vcp_reply vcp_len vcp_pending vcp_debug fh
set timeout 0
while { $vcp_pending == 1 } {
if { $vcp_debug == 1 } { puts $fh "Waiting for reply. Delaying command $command." }
after 5
update
incr timeout
if {$timeout > 100} {
set vcp_pending 0
if { $vcp_debug == 1 } { puts $fh "Terminating delay loop." }
}
}
update
if { $vcp_debug == 1 } { puts $fh "Sending $command" }
# Send command
set vcp_reply ""
set vcp_pending 1
set len [string length $command]
set vcp_len [expr $len / 2]
for {set i 0} {$i < $len} {incr i} {
set hex 0x[string range $command $i [expr $i+1]]
set letter [format %c $hex]
puts -nonewline $vcp $letter
incr i
}
puts -nonewline $vcp "\n"
flush $vcp
}
# Get UART buffer content
proc GetReply {} {
global vcp vcp_reply vcp_len vcp_pending vcp_debug fh
set cap [read $vcp]
binary scan $cap cu* valueList
foreach value $valueList {
append vcp_reply [format "0x%02X " $value]
}
if { $vcp_debug == 1 } { puts $fh "Received: $vcp_reply" }
flush $vcp
set vcp_pending 0
}
# Open COM port
proc BM_OpenCOM { port baudrate timeout dts rts } {
global vcp
catch { open $port w+ } vcp
if { [string first "couldn't" $vcp] != -1 } {
set vcp 0
} else {
fconfigure $vcp -mode 115200,n,8,1
fconfigure $vcp -blocking 0
fconfigure $vcp -buffering full -encoding binary -translation binary
fileevent $vcp readable [list GetReply]
}
}
# Close COM port
proc BM_CloseCOM {} {
global vcp fh
close $vcp
flush $fh
close $fh
}
# Reset hardware
proc BM_Reset {} {
SendCmd "FF0103"
after 15
}
# Set BlueRobin TX ID
proc BM_BR_SetID { id } {
SendCmd "FF0307[format %02X [expr $id & 0xFF]][format %02X [expr ($id>>8) & 0xFF]][format %02X [expr ($id>>16) & 0xFF]][format %02X [expr ($id>>24) & 0xFF]]"
after 15
}
# Start BlueRobin TX
proc BM_BR_Start {} {
SendCmd "FF0203"
after 15
}
# Stop BlueRobin TX
proc BM_BR_Stop {} {
SendCmd "FF0603"
after 15
}
# Set BlueRobin heart rate value
proc BM_BR_SetHeartrate { hr } {
SendCmd "FF0504[format %02X [expr $hr & 0xFF]]"
after 15
}
# Set BlueRobin speed and distance value
proc BM_BR_SetSpeed { spd dist } {
SendCmd "FF0A06[format %02X [expr $spd & 0xFF]][format %02X [expr $dist & 0xFF]][format %02X [expr ($dist>>8) & 0xFF]]"
after 15
}
# Start SimpliciTI stack
proc BM_SPL_Start {} {
SendCmd "FF0703"
after 15
}
# Stop SimpliciTI stack
proc BM_SPL_Stop {} {
SendCmd "FF0903"
after 15
}
# Get 4 bytes payload from SimpliciTI stack
proc BM_SPL_GetData {} {
global vcp_reply
SendCmd "FF080700000000"
after 15
update
while { [llength $vcp_reply] <= 6 } {
lappend vcp_reply 0x00
}
return [format "0x%02X" [expr ([lindex $vcp_reply 6]<<24) + ([lindex $vcp_reply 5]<<16) + ([lindex $vcp_reply 4]<<8) + [lindex $vcp_reply 3]]]
}
# Get HW status
proc BM_GetStatus {} {
global vcp_reply vcp_pending vcp_debug fh
SendCmd "FF000400"
after 15
update
if { $vcp_debug == 1 } { puts $fh "BM_GetStatus = [lindex $vcp_reply 3]" }
return [lindex $vcp_reply 3]
}
# Simulate mouse clicks
proc BM_SetMouseClick { btn } {
global w
set X [expr [winfo pointerx .]]
set Y [expr [winfo pointery .]]
set path [winfo containing $X $Y]
switch $btn {
1 { exec xdotool mousedown 1
after 10
exec xdotool mouseup 1 }
2 { exec xdotool mousedown 3
after 10
exec xdotool mouseup 3 }
3 { exec xdotool mousedown 1
after 10
exec xdotool mouseup 1
after 10
exec xdotool mousedown 1
after 10
exec xdotool mouseup 1 }
}
}
# Simulate complex key events
proc BM_SetKey { keysymbol win alt ctrl shift } {
global w
# Press keys and release them immediately
if { $win == 1 } { exec xdotool keydown "Super_L" }
if { $alt == 1 } { exec xdotool keydown "Alt_L" }
if { $ctrl == 1 } { exec xdotool keydown "Control_L" }
if { $shift == 1 } { exec xdotool keydown "Shift_L" }
if { $keysymbol > 0 } { exec xdotool keydown $keysymbol }
after 10
if { $keysymbol > 0 } { exec xdotool keyup $keysymbol }
if { $shift == 1 } { exec xdotool keyup "Shift_L" }
if { $ctrl == 1 } { exec xdotool keyup "Control_L" }
if { $alt == 1 } { exec xdotool keyup "Alt_L" }
if { $win == 1 } { exec xdotool keyup "Super_L" }
}
# Start SimpliciTI sync mode
proc BM_SYNC_Start {} {
SndCmd "FF3003"
after 15
}
# Send sync command
proc BM_SYNC_SendCommand { ldata } {
# take data from list and append it to command
set str "FF3116"
set i 0
foreach value $ldata {
append str [format "%02X" $value]
incr i
}
# byte stuff with 0x00 until 19 bytes are set
for {set j $i} {$j < 19} {incr j} {
append str "00"
}
SendCmd $str
after 15
}
# Get sync buffer
proc BM_SYNC_ReadBuffer {} {
global vcp_reply
SendCmd "FF3303"
after 15
update
return [lrange $vcp_reply 3 22]
}
# Get sync buffer state (empty/full)
proc BM_SYNC_GetBufferStatus {} {
global vcp_reply
SendCmd "FF320400"
after 15
update
return [lindex $vcp_reply 3]
}
# Get HW status
proc BM_GetStatus1 {} {
global vcp_reply vcp_pending vcp_debug fh
SendCmd "FF000400"
while { $vcp_pending == 1 } { update }
set reply [lindex $vcp_reply 3]
if { $vcp_debug == 1 } { puts $fh "BM_GetStatus1 = $reply" }
return $reply
}
# BM_WBSL_Start
proc BM_WBSL_Start { } {
SendCmd "FF4003"
after 15
}
# BM_WBSL_Stop
proc BM_WBSL_Stop { } {
SendCmd "FF4603"
after 15
}
# BM_WBSL_GetMaxPayload
proc BM_WBSL_GetMaxPayload { } {
global vcp_reply vcp_pending vcp_debug fh
SendCmd "FF490400"
while { $vcp_pending == 1 } { update }
if { $vcp_debug == 1 } { puts $fh "BM_WBSL_GetMaxPayload = [lindex $vcp_reply 3]" }
return [lindex $vcp_reply 3]
}
# BM_WBSL_GetPacketStatus
proc BM_WBSL_GetPacketStatus { } {
global vcp_reply vcp_pending vcp_debug fh
SendCmd "FF480400"
while { $vcp_pending == 1 } { update }
if { $vcp_debug == 1 } { puts $fh "BM_WBSL_GetPacketStatus = [lindex $vcp_reply 3]" }
return [lindex $vcp_reply 3]
}
# BM_WBSL_GetStatus
proc BM_WBSL_GetStatus { } {
global vcp_reply vcp_pending vcp_debug fh
SendCmd "FF410400"
while { $vcp_pending == 1 } { update }
if { $vcp_debug == 1 } { puts $fh "BM_WBSL_GetStatus = [lindex $vcp_reply 3]" }
return [lindex $vcp_reply 3]
}
# BM_WBSL_SendData
proc BM_WBSL_SendData { data_or_info data } {
global vcp_reply vcp_pending vcp_debug fh
if { $data_or_info == 0 } {
set byte0 "00"
set byte1 [format %02X [expr $data&0xFF]]
set byte2 [format %02X [expr $data>>8]]
set str "FF4706$byte0$byte1$byte2"
SendCmd $str
return 3
} else {
# Send several packets until data has been transmitted
if { $data_or_info == 1 } {
set byte0 "01"
} else {
set byte0 "02"
}
# Convert ASCII data string to hex values
set len 0
set data_to_send ""
binary scan $data cu* valueList
foreach value $valueList {
append data_to_send [format "%02X" $value]
incr len
}
set len1 [expr $len + 2]
set byte1 [format %02X [expr $len&0xFF]]
set data_to_send "$byte0$byte1$data_to_send"
if { $vcp_debug == 1 } { puts $fh "\nBM_WBSL_SendData $data_to_send\n" }
while { $len1 > 28 } {
# Send 28 bytes
SendCmd "FF471F[string range $data_to_send 0 55]"
# Cut away data that has been sent
set data_to_send [string range $data_to_send 56 [string length $data_to_send]]
set len1 [expr $len1-28]
update
}
# Send last bytes
set len1 [expr [string length $data_to_send]/2 + 3]
SendCmd "FF47[format %02X $len1]$data_to_send"
update
return $len1
}
}