-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathluis.tbs
More file actions
366 lines (315 loc) · 9.69 KB
/
luis.tbs
File metadata and controls
366 lines (315 loc) · 9.69 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
358
359
360
361
362
363
364
365
366
'***********************************************************************************************************
' LUIS LIBRARY
'***********************************************************************************************************
include "global.tbh"
'--------------------------------------------------------------------------------
const LUIS_STAMP="LUIS> "
const LUIS_CR_LF=chr(13)+chr(10)
const LUIS_INIT_SIGNATURE=&hB5A4
const LUIS_CMD_ESC_CHAR = "@"
const LUIS_PLACEHOLDER_STR="####"
const LUIS_HTML_FILE="luis.html"
const LUIS_STX=2
const LUIS_CR=13
const LUIS_MAX_BT_CMD_LEN=30
const LUIS_MAX_BT_RPL_LEN=30
'--------------------------------------------------------------------------------
#if LUIS_DEBUG_PRINT
declare sub luis_debugprint(byref print_data as string)
#endif
declare sub luis_delay_ms(delay as integer)
declare function luis_dot_decimal_to_hex(stored_mac as string) as string
'--------------------------------------------------------------------------------
dim luis_init_flag as word=0
dim luis_GOP_result as string
dim luis_current_send_chunk as string
dim luis_html_placeholder_start as dword
dim luis_html_placeholder_end as dword
dim luis_html_read_pointer as dword
dim luis_html_send_state as en_luis_html_send_states
'================================================================================
function luis_start() as en_luis_status_codes
luis_start=EN_LUIS_STATUS_OK
#if LUIS_DEBUG_PRINT
luis_debugprint("---START---")
#endif
if wln.enabled = NO then
wln.csmap=LUIS_BT_CS
io.num=LUIS_BT_CS
io.enabled=YES
wln.dimap=LUIS_BT_DI
wln.domap=LUIS_BT_DO
io.num=LUIS_BT_DO
io.enabled=YES
wln.clkmap=LUIS_BT_CLK
io.num=LUIS_BT_CLK
io.enabled=YES
io.num=LUIS_BT_RST
io.enabled=YES
wln.disable
io.num=LUIS_BT_RST
io.state=LOW
luis_delay_ms(1)
io.state=HIGH
sys.buffalloc()
wln.boot(0)
else
#if LUIS_DEBUG_PRINT
luis_debugprint("WA2000 module already enabled")
#endif
end if
if wln.enabled = NO then
#if LUIS_DEBUG_PRINT
luis_debugprint("WA2000 module failed to start")
#endif
luis_init_flag=0
luis_start=EN_LUIS_BT_FAILURE
else
dim hex_mac as string = luis_dot_decimal_to_hex(bt.mac)
dim advertise_name as string = BT_NAME + " " + mid(hex_mac,9, 2) + ":" + mid(hex_mac, 11, 2)
bt.name=advertise_name
bt.emulation=PL_WLN_BT_EMULATION_MODE_NORDIC
bt.txbuffrq(LUIS_BT_TX_BUF_SIZE)
bt.rxbuffrq(LUIS_BT_RX_BUF_SIZE)
sys.buffalloc()
bt.enable()
end if
romfile.open(LUIS_HTML_FILE)
if romfile.pointer32<>1 then
luis_start=EN_LUIS_FILE_HTML_ERROR
#if LUIS_DEBUG_PRINT
luis_debugprint("Failed to open HTML file")
#endif
end if
dim html_size as integer = romfile.size
romfile.open(LUIS_CONFIG_FILE)
if romfile.pointer32<>1 then
luis_start=EN_LUIS_FILE_CONFIG_ERROR
#if LUIS_DEBUG_PRINT
luis_debugprint("Failed to open LUIS Library file")
#endif
end if
dim config_size as integer = romfile.size
LUIS_GOP_result="{\x22type\x22:\x22spa\x22,\x22length\x22:"+lstr(html_size + config_size - len(LUIS_PLACEHOLDER_STR))+"}"
if luis_start=EN_LUIS_STATUS_OK then
luis_init_flag=LUIS_INIT_SIGNATURE
end if
end function
sub luis_on_bt_event(bt_event as enum pl_bt_events)
if luis_init_flag<>LUIS_INIT_SIGNATURE or wln.enabled=NO then
exit sub
end if
select case bt_event
case PL_BT_EVENT_CONNECTED:
#if LUIS_DEBUG_PRINT
luis_debugprint("Device connected")
#endif
callback_luis_bt_connected()
case PL_BT_EVENT_DISCONNECTED:
#if LUIS_DEBUG_PRINT
luis_debugprint("Device disconnected")
#endif
callback_luis_bt_disconnected()
case PL_BT_EVENT_ENABLED:
#if LUIS_DEBUG_PRINT
luis_debugprint("Bluetooth enabled")
luis_debugprint("Connect to device named " + bt.name + " from the LUIS mobile app")
#endif
bt.advertise = YES
callback_luis_bt_enabled()
case PL_BT_EVENT_DISABLED:
#if LUIS_DEBUG_PRINT
luis_debugprint("Bluetooth disabled")
#endif
luis_init_flag=0
callback_luis_bt_disabled()
end select
end sub
sub luis_on_bt_data_sent()
if luis_init_flag<>LUIS_INIT_SIGNATURE then
#if LUIS_DEBUG_PRINT
luis_debugprint("got bluetooth data when LUIS not initialized")
#endif
exit sub
end if
dim chunk_size as dword = 255
if len(luis_current_send_chunk)<>0 and bt.txfree-bt.newtxlen >=len(luis_current_send_chunk) then
bt.setdata(luis_current_send_chunk)
bt.send()
end if
select case luis_html_send_state
case EN_LUIS_HTML_GOP_RESULT:
luis_html_send_state=EN_LUIS_HTML_SEND_PART1
romfile.open(LUIS_HTML_FILE)
luis_html_placeholder_start = romfile.find32(0, LUIS_PLACEHOLDER_STR, 0)
luis_html_placeholder_end = luis_html_placeholder_start + len(LUIS_PLACEHOLDER_STR)
luis_current_send_chunk=romfile.getdata(chunk_size)
luis_html_read_pointer=romfile.pointer32
case EN_LUIS_HTML_SEND_PART1:
romfile.open(LUIS_HTML_FILE)
romfile.pointer32=luis_html_read_pointer
if luis_html_read_pointer + chunk_size > luis_html_placeholder_start then
chunk_size = luis_html_placeholder_start - luis_html_read_pointer
luis_html_send_state=EN_LUIS_HTML_SEND_CONFIG
end if
luis_current_send_chunk=romfile.getdata(chunk_size)
if luis_html_send_state<>EN_LUIS_HTML_SEND_PART1 then
luis_html_read_pointer=1
else
luis_html_read_pointer=romfile.pointer32
end if
case EN_LUIS_HTML_SEND_CONFIG:
romfile.open(LUIS_CONFIG_FILE)
romfile.pointer32=luis_html_read_pointer
luis_current_send_chunk=romfile.getdata(chunk_size)
luis_html_read_pointer=romfile.pointer32
if luis_html_read_pointer>=romfile.size then
luis_html_send_state=EN_LUIS_HTML_SEND_PART2
luis_html_read_pointer=luis_html_placeholder_end
end if
case EN_LUIS_HTML_SEND_PART2:
romfile.open(LUIS_HTML_FILE)
romfile.pointer32=luis_html_read_pointer
luis_current_send_chunk=romfile.getdata(chunk_size)
luis_html_read_pointer=romfile.pointer32
end select
if len(luis_current_send_chunk) > 0 then
bt.notifysent(bt.txbuffsize-len(luis_current_send_chunk))
end if
end sub
'====================================================================
public sub luis_proc_bt_cmd(byref cmd_str as string)
dim reply_str as string(LUIS_MAX_BT_RPL_LEN)
dim x as byte
dim setting_name as string(STG_MAX_SETTING_NAME_LEN)
dim index as byte
dim s2 as string(64)
dim stg_data as string(32)
dim cs,f,pos as byte
dim mac_num as word
dim i as word
select case left(cmd_str,1)
case "G": 'get setting --------------------------------------
setting_name=mid(cmd_str,2,STG_MAX_SETTING_NAME_LEN)
if setting_name = "OP" then
luis_html_send_state=EN_LUIS_HTML_GOP_RESULT
luis_current_send_chunk=luis_GOP_result
bt.notifysent(bt.txbuffsize-len(luis_current_send_chunk))
exit sub
end if
select case stg_sg(setting_name,0,reply_str,EN_STG_GET)
case EN_STG_STATUS_OK:
goto reply_ok_have_data
case EN_STG_STATUS_UNKNOWN:
goto reply_err
case EN_STG_STATUS_INVALID_INDEX:
goto reply_err
case else:
goto reply_failed
end select
case "S": 'set setting --------------------------------------
setting_name=mid(cmd_str,2,2)
stg_data=right(cmd_str,len(cmd_str)-3)
select case stg_sg(setting_name,0,stg_data,EN_STG_SET)
case EN_STG_STATUS_OK:
goto reply_ok_have_data
case EN_STG_STATUS_INVALID:
goto reply_err
case EN_STG_STATUS_INVALID_INDEX:
goto reply_err
case EN_STG_STATUS_UNKNOWN:
goto reply_err
case else:
goto reply_failed
end select
case "B": 'buzz the device ----------------------------------
callback_luis_buzz_command()
goto reply_ok
case "I": 'initialize settings --------------------------
if stg_restore_multiple(EN_STG_INIT_MODE_NORMAL)<>EN_STG_STATUS_OK then goto reply_failed
goto reply_ok
case "E": 'reboot -------------------------------------------
wln.disable()
io.num=LUIS_BT_RST
io.state=LOW
sys.reboot()
case else
goto reply_err
end select
reply_rejected:
reply_str="R"
goto send
reply_err:
reply_str="C"
goto send
reply_failed:
reply_str="F"
goto send
reply_ok:
reply_str="A"
goto send
reply_ok_have_data:
reply_str="A"+reply_str
goto send
send:
reply_str=chr(2)+reply_str+chr(13) 'add encapsulation
bt.setdata(reply_str)
bt.send
reply_null:
end sub
function luis_bt_data_arrival() as en_luis_status_codes
dim x,y as byte
dim bt_cmd_str as string(LUIS_MAX_BT_CMD_LEN)
luis_bt_data_arrival=EN_LUIS_STATUS_OK
if luis_init_flag<>LUIS_INIT_SIGNATURE then
luis_bt_data_arrival=EN_LUIS_STATUS_NOT_STARTED
exit function
end if
bt_cmd_str=bt_cmd_str+bt.getdata(255-len(bt_cmd_str))
x=instr(1,bt_cmd_str,chr(LUIS_STX),1)
'this will prevent the string to overflow with garbage
if len(bt_cmd_str)=LUIS_MAX_BT_CMD_LEN then
y=instr(1,bt_cmd_str,chr(LUIS_CR),1)
if x=0 or y=0 then
bt_cmd_str=""
exit function
end if
end if
while x<>0
'we've got STX
bt_cmd_str=right(bt_cmd_str,len(bt_cmd_str)+1-x) 'cut out everything to the left of STX
x=instr(1,bt_cmd_str,chr(LUIS_CR),1) 'look for command end
if x=0 then exit function
luis_proc_bt_cmd(mid(bt_cmd_str,2,x-2))
bt_cmd_str=right(bt_cmd_str,len(bt_cmd_str)-x)
x=instr(1,bt_cmd_str,chr(LUIS_STX),1)
wend
end function
#if LUIS_DEBUG_PRINT
sub luis_debugprint(byref print_data as string)
sys.debugprint(LUIS_STAMP+print_data+LUIS_CR_LF)
end sub
#endif
sub luis_delay_ms(delay as integer)
dim end_time as dword=sys.timercountms+delay
while sys.timercountms<end_time
wend
end sub
function luis_dot_decimal_to_hex(stored_mac as string) as string
dim i as integer = 0
dim startpos as integer = 1
dim endpos as integer = 0
dim num as word = ""
dim hex_mac as string = ""
for i = 0 to 5
endpos = instr(startpos,stored_mac,".",0)
num = val( mid(stored_mac,startpos, endpos-startpos) )
if num<>0 then
hex_mac = hex_mac + right(hex( num ),2)
else
hex_mac = hex_mac + "00"
end if
startpos=endpos+1
next i
luis_dot_decimal_to_hex = hex_mac
end function