forked from LairdCP/RM1xx-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.server.notify.bl600.sb
260 lines (240 loc) · 10.7 KB
/
temp.server.notify.bl600.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
//******************************************************************************
// Copyright (c) 2016, 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 ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// **Temperature Notifications**
// This application notifies the temperature when the CCCD is written to.
// This will run on a BL600 but will need SIO and formula changes to run
// and report the temperature correctly on a BL652/BT900
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
//******************************************************************************
// Library Import
//******************************************************************************
#include "lib/ble.sblib"
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
DIM rc
DIM addr$
DIM hSvc //Service handle
DIM mdAttr
DIM mdCccd
DIM mdSccd
DIM chProp
DIM attr1$
DIM attr2$
DIM hUuidS1 //Handles for uuid of Service 1
DIM hUuidC1 //Handles for uuid of characteristic 1 in Service 1
DIM hUuidC2 //Handles for uuid of characteristic 2 in Service 1
DIM dvcNme$
DIM nmeWrtble
DIM MinConnInt
DIM MaxConnInt
DIM ConnSupTO
DIM sL
DIM uuid$
DIM tempIntvl
DIM charVal$
DIM ntfyEnabled
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
addr$ = ""
dvcNme$ = "Laird Temp"
nmeWrtble = 0 //Device name will not be writable by peer
MinConnInt = 250000 //Minimum acceptable connection interval is 0.25 seconds
MaxConnInt = 750000 //Maximum acceptable connection interval is 0.75 seconds
ConnSupTO = 4000000 //Connection supervisory timeout is 4 seconds
sL = 0
uuid$ = "\56\9a\00\00\b8\7f\49\0c\92\cb\11\ba\5e\a5\16\7c" //Laird's base UUID
tempIntvl = 2000 //Interval for temperature measurements in ms
ntfyEnabled = 0
//******************************************************************************
// Debugging resources
//******************************************************************************
sub AssertRC(rc, line)
if(rc!=0)then
print "Failed with ";integer.h' rc;" on line ";line;"\n"
endif
endsub
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
sub GattInit()
//Converts the Laird base UUID into a new service handle and initialize that service
hUuidS1 = BleHandleUuid128(uuid$)
rc = BleServiceNew(BLE_SERVICE_PRIMARY, hUuidS1, hSvc)
AssertRC(rc, 82)
//Creates the first characteristic, which is used to send the temperatue value
mdAttr = BleAttrMetadataEx(BLE_ATTR_ACCESS_OPEN, BLE_ATTR_ACCESS_NONE, 10, 0, rc)
AssertRC(rc, 86)
mdCccd = BleAttrMetadataEx(BLE_ATTR_ACCESS_OPEN, BLE_ATTR_ACCESS_OPEN, 2, 0, rc)
AssertRC(rc, 88)
mdSccd = BLE_CHAR_METADATA_ATTR_NOT_PRESENT
chProp = BLE_CHAR_PROPERTIES_NOTIFY + BLE_CHAR_PROPERTIES_READ
hUuidC1 = BleHandleUuidSibling(hUuidS1,0x2A6E)
rc = BleCharNew(chProp,hUuidC1,mdAttr,mdCccd,mdSccd)
AssertRC(rc, 93)
attr1$ = "\00\00\00\00"
rc = BleCharCommit(hSvc,attr1$,hUuidC1)
PRINT "Service UUID: "; INTEGER.H' hUuidS1;"\n"
PRINT "Char UUID: "; INTEGER.H' hUuidC1;"\n"
endsub
//*******************************************************************************
//*******************************************************************************
sub InitTempSensor()
//Initializes the temperature sensor, starts the timer and sets the temp sense gpio
//Poll sensor on a timer
TimerStart(0, tempIntvl, 0)
print "Timer0 started\n"
print "Temperature polling begins\n"
rc = GpioSetFunc(4, 3, 0) //set as analog in
AssertRC(rc, 110)
endsub
//*******************************************************************************
//*******************************************************************************
function Adc2Mv(adc)
//Sets the adc formula
//==============================================================================
// Bandgap = 1200mv
// Max Adc = 1024
// Input scaled by 2/3
//
// Hence mV = (((adc/MaxADC) * Bandgap)*3)/2
// refactoring for integer maths gives
// mv = (adc * Bandgap * 3)/(MaxADC*2)
// mv = (adc * 1200 * 3)/(1024 * 2)
// mv = (adc * 600 * 3)/1024
// mv = (adc * 150 * 3)/256
// mv = (adc * 75 * 3)/128
// mv = (adc * 225)/128
//==============================================================================
adc = (adc * 225)/128
endfunc adc
//*******************************************************************************
//*******************************************************************************
function Mv2Temperature(mv)
//Function that converts the voltage into a temperature
mv = -1*(mv-1858)/12 //Calculation for the LM20BIM7 on the BL600
endfunc mv
//******************************************************************************
// Handler definitions
//******************************************************************************
FUNCTION HndlrBlrAdvTimOut()
PRINT "Advert stopped via timeout\n"
PRINT "Restarting\n"
rc = BleAdvertStart(0, addr$, 500, 0, 0)
AssertRC(rc, 148)
ENDFUNC 1
//*******************************************************************************
//*******************************************************************************
FUNCTION HndlrBleMsg(BYVAL nMsgId AS INTEGER, BYVAL nCtx AS INTEGER)
SELECT nMsgId
CASE BLE_EVBLEMSGID_CONNECT
PRINT "BLE Connection ";nCtx;"\n"
CASE BLE_EVBLEMSGID_DISCONNECT
PRINT "Disconnected ";nCtx;"\n"
rc = BleAdvertStart(0, addr$, 500, 0, 0)
AssertRC(rc, 160)
CASE BLE_EVBLEMSGID_HTS_INDICATION_STATE
PRINT "Thermometer CCD value has changed. New value is ";nCtx;"\n"
CASE BLE_EVBLEMSGID_ENCRYPTED
PRINT "Connection ";nCtx;" is now encrypted\n"
CASE BLE_EVBLEMSGID_CONN_TO_BONDED_MASTER
PRINT "Connected to a bonded master\n"
CASE BLE_EVBLEMSGID_UPDATED_BOND
PRINT "A new pairing has replaced the old key\n";
CASE ELSE
PRINT "Unknown Ble Msg\n"
ENDSELECT
ENDFUNC 1
//*******************************************************************************
//*******************************************************************************
function HandlerTimer0() as integer
//Prints and notifies the temperature value after the timer triggers
dim mV,tmp
mv = Adc2Mv(GpioRead(4))
tmp = Mv2Temperature(mv)
print "Temperature=" ; tmp;"\n"
sprint #attr1$, tmp
//rc = BleCharValueNotIFy(hUuidC1,attr1$)
//Checks the status charVal to see if notifications have been requested and continues/stops
IF ntfyEnabled == 1 THEN
rc=BleCharValueNotify(hUuidC1,attr1$)
//rc = BleCharValueWrite(hUuidC2,attr1$)
TimerStart(0,tempIntvl,0)
ENDIF
endfunc 1
//*******************************************************************************
//*******************************************************************************
sub SetupAdverts()
//Setup advert report and scan response
dim advRpt$
dim scanRpt$
rc = BleAdvRptInit(advRpt$, 2, 1, 10)
AssertRC(rc, 199)
rc = BleScanRptInit(scanRpt$)
AssertRC(rc, 201)
rc = BleAdvRptAddUuid128(scanRpt$, BleHandleUuid128(uuid$))
AssertRC(rc, 203)
rc = BleAdvRptsCommit(advRpt$, scanRpt$)
AssertRC(rc, 205)
endsub
//*******************************************************************************
// CCCD descriptor written handler
//*******************************************************************************
FUNCTION HndlrCharCccd(BYVAL charHandle, BYVAL nVal) AS INTEGER
DIM value$
IF charHandle==hUuidC1 THEN
IF nVal == 1 THEN
PRINT "Notifications have been enabled by client\n"
ntfyEnabled=1
InitTempSensor()
ELSEIF nVal == 0 THEN
PRINT "Notifications been disabled by client\n"
ntfyEnabled=0
ENDIF
ENDIF
ENDFUNC 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
GattInit()
rc=BleGapSvcInit(dvcNme$, nmeWrtble, BLE_APPEARANCE_GENERIC_THERMOMETER, MinConnInt, MaxConnInt, ConnSupTO, sL)
AssertRC(rc, 230)
SetupAdverts()
rc = BleAdvertStart(0, addr$, 500, 0, 0)
AssertRC(rc, 233)
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
ONEVENT EVBLEMSG CALL HndlrBleMsg
ONEVENT EVBLE_ADV_TIMEOUT CALL HndlrBlrAdvTimOut
ONEVENT EVTMR0 CALL HandlerTimer0
ONEVENT EVCHARCCCD CALL HndlrCharCccd
waitevent