This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pwrd.power.fail.demo.sb
432 lines (365 loc) · 19.2 KB
/
pwrd.power.fail.demo.sb
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Simple program that reads the supply volts in mV and prints it to the UART
// and will then wait for BLE_EVBLEMSGID_POWER_FAILURE_WARNING event
//
// 02/03/2013
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
//Set this to 0 to disable all debugging messages
#define ENABLE_DEBUG_PRINTS 1
//******************************************************************************
// Library Import
//******************************************************************************
// -- lib\ble.sblib -- START
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ###### #######
// ###### Library File meant to be d in files #######
// ###### #######
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This library contains all the BLE related #defines that will make your app
// more readable
//
//******************************************************************************
//******************************************************************************
// Library Import
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define BLE_CONN_HANDLE_INVALID 0xFFFF
#define BLE_INVALID_UUID_HANDLE 0
#define BLE_INVALID_CHAR_HANDLE 0
#define BLE_INVALID_SERVICE_HANDLE 0
#define CIRCBUFFER_INVALID_HANDLE -1
//Set BLE_DISCOVERABILITY_NONE if whitelist is enabled in BleAdvertStart()
//as the 4.0 spec says if whitelist is enabled then discoverability is not allowed
#define BLE_DISCOVERABILITY_NONE 0
#define BLE_DISCOVERABILITY_LIMITED 1
#define BLE_DISCOVERABILITY_GENERAL 2
#define BLE_DISCOVERABILITY_BOTH 3
#define BLE_APPEARANCE_UNKNOWN 0
#define BLE_APPEARANCE_GENERIC_PHONE 64
#define BLE_APPEARANCE_GENERIC_COMPUTER 128
#define BLE_APPEARANCE_GENERIC_WATCH 192
#define BLE_APPEARANCE_WATCH_SPORTS_WATCH 193
#define BLE_APPEARANCE_GENERIC_CLOCK 256
#define BLE_APPEARANCE_GENERIC_DISPLAY 320
#define BLE_APPEARANCE_GENERIC_REMOTE_CONTROL 384
#define BLE_APPEARANCE_GENERIC_EYE_GLASSES 448
#define BLE_APPEARANCE_GENERIC_TAG 512
#define BLE_APPEARANCE_GENERIC_KEYRING 576
#define BLE_APPEARANCE_GENERIC_MEDIA_PLAYER 640
#define BLE_APPEARANCE_GENERIC_BARCODE_SCANNER 704
#define BLE_APPEARANCE_GENERIC_THERMOMETER 768
#define BLE_APPEARANCE_THERMOMETER_EAR 769
#define BLE_APPEARANCE_GENERIC_HEART_RATE_SENSOR 832
#define BLE_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT 833
#define BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE 896
#define BLE_APPEARANCE_BLOOD_PRESSURE_ARM 897
#define BLE_APPEARANCE_BLOOD_PRESSURE_WRIST 898
#define BLE_APPEARANCE_HUMAN_INTERFACE_DEVICE_HID_GENERIC 960
#define BLE_APPEARANCE_KEYBOARD_HID_SUBTYPE 961
#define BLE_APPEARANCE_MOUSE_HID_SUBTYPE 962
#define BLE_APPEARANCE_JOYSTICK_HID_SUBTYPE 963
#define BLE_APPEARANCE_GAMEPAD_HID_SUBTYPE 964
#define BLE_APPEARANCE_DIGITIZER_TABLET_HID_SUBTYPE 965
#define BLE_APPEARANCE_CARD_READER_HID_SUBTYPE 966
#define BLE_APPEARANCE_DIGITAL_PEN_HID_SUBTYPE 967
#define BLE_APPEARANCE_BARCODE_SCANNER_HID_SUBTYPE 968
#define BLE_APPEARANCE_GENERIC_GLUCOSE_METER 1024
#define UUID_ALERT_NOTIFICATION_SERVICE 0x1811
#define UUID_BATTERY_SERVICE 0x180F
#define UUID_BLOOD_PRESSURE_SERVICE 0x1810
#define UUID_CURRENT_TIME_SERVICE 0x1805
#define UUID_DEVICE_INFORMATION_SERVICE 0x180A
#define UUID_GLUCOSE_SERVICE 0x1808
#define UUID_HEALTH_THERMOMETER_SERVICE 0x1809
#define UUID_HEART_RATE_SERVICE 0x180D
#define UUID_HUMAN_INTERFACE_DEVICE_SERVICE 0x1812
#define UUID_IMMEDIATE_ALERT_SERVICE 0x1802
#define UUID_LINK_LOSS_SERVICE 0x1803
#define UUID_NEXT_DST_CHANGE_SERVICE 0x1807
#define UUID_PHONE_ALERT_STATUS_SERVICE 0x180E
#define UUID_REFERENCE_TIME_UPDATE_SERVICE 0x1806
#define UUID_SCAN_PARAMETERS_SERVICE 0x1813
#define UUID_TX_POWER_SERVICE 0x1804
#define BLE_SERVICE_SECONDARY 0
#define BLE_SERVICE_PRIMARY 1
#define BLE_CHAR_METADATA_ATTR_NOT_PRESENT 0
#define BLE_ATTR_ACCESS_NONE 0
#define BLE_ATTR_ACCESS_OPEN 1
#define BLE_ATTR_ACCESS_ENC_NO_MITM 2
#define BLE_ATTR_ACCESS_ENC_WITH_MITM 3
#define BLE_ATTR_ACCESS_SIGNED_NO_MITM 4
#define BLE_ATTR_ACCESS_SIGNED_WITH_MITM 5
#define BLE_CHAR_PROPERTIES_BROADCAST 0x01
#define BLE_CHAR_PROPERTIES_READ 0x02
#define BLE_CHAR_PROPERTIES_WRITE_WO_RESPONSE 0x04
#define BLE_CHAR_PROPERTIES_WRITE 0x08
#define BLE_CHAR_PROPERTIES_NOTIFY 0x10
#define BLE_CHAR_PROPERTIES_INDICATE 0x20
#define BLE_CHAR_PROPERTIES_AUTH_SIGNED_WR 0x40
#define BLE_CHAR_PROPERTIES_RELIABLE_WRITE 0x80
//Body Sensor Location values
#define BLE_HRS_BODY_SENSOR_LOCATION_OTHER 0
#define BLE_HRS_BODY_SENSOR_LOCATION_CHEST 1
#define BLE_HRS_BODY_SENSOR_LOCATION_WRIST 2
#define BLE_HRS_BODY_SENSOR_LOCATION_FINGER 3
#define BLE_HRS_BODY_SENSOR_LOCATION_HAND 4
#define BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE 5
#define BLE_HRS_BODY_SENSOR_LOCATION_FOOT 6
//Types of advert packets
#define ADV_IND 0
#define ADV_DIRECT_IND 1
#define ADV_SCAN_IND 2
#define ADV_NONCONN_IND 3
//Types of filter policy
#define ADV_FILTERPOLICY_ANY 0
#define ADV_FILTERPOLICY_SCANREQ 1
#define ADV_FILTERPOLICY_CONNREQ 2
#define ADV_FILTERPOLICY_BOTH 3
//BLE EVENT MSG IDs
#define BLE_EVBLEMSGID_CONNECT 0 //msgCtx = connection handle
#define BLE_EVBLEMSGID_DISCONNECT 1 //msgCtx = connection handle
#define BLE_EVBLEMSGID_IMMEDIATE_ALERT_SERVICE_ALERT 2 //msgCtx = alert level
#define BLE_EVBLEMSGID_LINKLOSS_SERVICE_ALERT 3 //msgCtx = alert level
#define BLE_EVBLEMSGID_SERVICE_ERROR 4 //msgCtx = resultcode
#define BLE_EVBLEMSGID_HTS_INDICATION_STATE 5 //msgCtx = new indication state 0=off, 1=on
#define BLE_EVBLEMSGID_HTS_INDICATION_CNF 6 //msgCtx = not used
#define BLE_EVBLEMSGID_BPS_INDICATION_STATE 7 //msgCtx = new indication state 0=off, 1=on
#define BLE_EVBLEMSGID_BPS_INDICATION_CNF 8 //msgCtx = not used
#define BLE_EVBLEMSGID_DISPLAY_PASSKEY 9 //msgCtx = passcode to display
#define BLE_EVBLEMSGID_NEW_BOND 10 //msgCtx = connection handle
#define BLE_EVBLEMSGID_AUTH_KEY_REQUEST 11 //msgCtx = 1 for passkey, 2 for 16 byte OOB data
#define BLE_EVBLEMSGID_HRS_NOTIFICATION_STATE 12 //msgCtx = new notification state 0=off, 1=on
//#define FUTURE_USE 13
#define BLE_EVBLEMSGID_CONN_PARMS_UPDATE 14 //msgCtx = connection handle
#define BLE_EVBLEMSGID_CONN_PARMS_UPDATE_FAIL 15 //msgCtx = connection handle
#define BLE_EVBLEMSGID_CONN_TO_BONDED_MASTER 16 //msgCtx = connection handle
#define BLE_EVBLEMSGID_UPDATED_BOND 17 //msgCtx = connection handle
#define BLE_EVBLEMSGID_ENCRYPTED 18 //msgCtx = connection handle
#define BLE_EVBLEMSGID_POWER_FAILURE_WARNING 19 //msgCtx = current supply voltage
#define BLE_EVBLEMSGID_UNENCRYPTED 20 //msgCtx = connection handle
#define BLE_EVBLEMSGID_DEVICENAME_WRITE 21 //msgCtx = connection handle
//BLE Alert Levels
#define BLE_ALERT_LEVEL_NO_ALERT 0
#define BLE_ALERT_LEVEL_MILD_ALERT 1
#define BLE_ALERT_LEVEL_HIGH_ALERT 2
//Blood Pressure Feature Bits
#define BLE_BPS_FEATURE_BODY_MOVEMENT_BIT 1
#define BLE_BPS_FEATURE_CUFF_FIT_BIT 2
#define BLE_BPS_FEATURE_IRREGULAR_PULSE_BIT 4
#define BLE_BPS_FEATURE_PULSE_RATE_RANGE_BIT 8
#define BLE_BPS_FEATURE_MEASUREMENT_POSITION_BIT 16
#define BLE_BPS_FEATURE_MULTIPLE_BOND_BIT 32
//Blood Pressure Measurement Status flags
#define BLE_BPS_MEAS_STAT_BODY_MOVEMENT 1
#define BLE_BPS_MEAS_STAT_CUFF_FIT 2
#define BLE_BPS_MEAS_STAT_IRREGULAR_PULSE 4
#define BLE_BPS_MEAS_STAT_PULSE_RATE_RANGE 8
#define BLE_BPS_MEAS_STAT_MEASUREMENT_POSITION 16
//Pairing i/o capability
#define BLE_IOCAPABILITY_NONE 0
#define BLE_IOCAPABILITY_DISPLAY_YESNO 1
#define BLE_IOCAPABILITY_KEYBOARD_ONLY 2
#define BLE_IOCAPABILITY_DISPLAY_ONLY 3
#define BLE_IOCAPABILITY_KEYBOARD_DISPLAY 4
#define BLE_IOCAPABILITY_ILLEGAL 5
//Health thermometer sensor location
#define BLE_HTS_TEMP_TYPE_ARMPIT 1
#define BLE_HTS_TEMP_TYPE_BODY 2
#define BLE_HTS_TEMP_TYPE_EAR 3
#define BLE_HTS_TEMP_TYPE_FINGER 4
#define BLE_HTS_TEMP_TYPE_GI_TRACT 5
#define BLE_HTS_TEMP_TYPE_MOUTH 6
#define BLE_HTS_TEMP_TYPE_RECTUM 7
#define BLE_HTS_TEMP_TYPE_TOE 8
#define BLE_HTS_TEMP_TYPE_EAR_DRUM 9
//HCI Status Codes
#define BLE_HCI_AUTHENTICATION_FAILURE 0x05
#define BLE_HCI_CONNECTION_TIMEOUT 0x08
//Some Useful Result Codes to help with documentation
#define RESCODE_BLE_SVC_REGISTERED 0x6018
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//******************************************************************************
// Handler definitions
//******************************************************************************
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
// -- lib\ble.sblib -- END
// -- lib\debugging.routines.sblib -- START
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Debugging and Tracing Routines - BEFORE includes
//
// Have either the following
//
// #define ENABLE_DEBUG_PRINTS 1
//
// or
//
// #define ENABLE_DEBUG_PRINTS 0
//
// in the source file that includes this
//
//******************************************************************************
// "lib\debugging.routines.sblib"
//==============================================================================
//==============================================================================
sub AssertResCode(byval rc as integer,byval tag as integer)
if (ENABLE_DEBUG_PRINTS!=0) && (rc!=0) then
print "Failed with ";integer.h' rc;" at tag ";tag;"\n"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgMsg(byval msg$ as string)
if (ENABLE_DEBUG_PRINTS!=0) then
print msg$;"\n"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgMsgVal(byval msg$ as string, byval vl as integer)
if (ENABLE_DEBUG_PRINTS!=0) then
print msg$;" ";vl;"\n"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgPrnAdvReport(byref rp$ as string)
if (ENABLE_DEBUG_PRINTS!=0) then
print "Rpt=";strhexize$(rp$);"\n"
endif
endsub
// -- lib\debugging.routines.sblib -- END
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim rc
dim mv
dim lastmv
dim count
count = 0
lastmv = -1
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//******************************************************************************
// Handler definitions
//******************************************************************************
//==============================================================================
// This handler is called when TIMER 0 expires
//==============================================================================
function HandlerTimer0() as integer
mv=ReadPwrSupplyMv()
if lastmv != mv then
print "\n --- Supply voltage is ";mv;"mV"
lastmv = mv
endif
endfunc 1
//==============================================================================
// This handler is called when there is a BLE message
//==============================================================================
function HandlerBleMsg(BYVAL nMsgId AS INTEGER, BYVAL nCtx AS INTEGER) as integer
select nMsgId
case BLE_EVBLEMSGID_POWER_FAILURE_WARNING
DbgMsgVal("\n --- Power Fail Warning ",nCtx)
mv=ReadPwrSupplyMv()
print "\n --- Supply voltage is ";mv;"mV"
count = count + 1
print "\n --- count is ";count
if count > 3 then
exitfunc 0
endif
case else
DbgMsgVal("Unknown Ble Msg", nMsgId)
endselect
endfunc 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
OnEvent EVBLEMSG call HandlerBleMsg
OnEvent EVTMR0 call HandlerTimer0
mv=ReadPwrSupplyMv()
print "\n\nSupply voltage is ";mv;"mV\n"
mv=2700
rc=SetPwrSupplyThreshMv(mv)
AssertResCode(rc,1100)
print "\nWaiting for power supply to fall below ";mv;"mV"
//Poll sensor on a timer
TimerStart(0,500,1)
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
waitevent