-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.c
297 lines (228 loc) · 8.95 KB
/
main.c
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
/*
UWB Localization Tag
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_sdm.h"
#include "nrf_delay.h"
#include "ble.h"
#include "ble_db_discovery.h"
#include "softdevice_handler.h"
#include "app_util.h"
#include "app_error.h"
#include "ble_advdata_parser.h"
#include "ble_conn_params.h"
#include "ble_hci.h"
#include "boards.h"
#include "nrf_gpio.h"
#include "pstorage.h"
#include "app_trace.h"
#include "ble_hrs_c.h"
#include "ble_bas_c.h"
#include "app_util.h"
#include "app_timer.h"
#include "simple_ble.h"
#include "eddystone.h"
#include "led.h"
#include "boards.h"
#include "ble_config.h"
#include "tripoint_interface.h"
/*******************************************************************************
* Configuration and settings
******************************************************************************/
#define TRITAG_SHORT_UUID 0x3152
#define TRITAG_CHAR_LOCATION_SHORT_UUID 0x3153
#define TRITAG_CHAR_RANGING_ENABLE_SHORT_UUID 0x3154
#define TRITAG_CHAR_CALIBRATION_SHORT_UUID 0x3159
#define TRITAG_CHAR_STATUS_SHORT_UUID 0x3155
// Randomly generated UUID
simple_ble_service_t service_handle = {
.uuid128 = {{0x2e, 0x5d, 0x5e, 0x39, 0x31, 0x52, 0x45, 0x0c,
0x90, 0xee, 0x3f, 0xa2, 0x31, 0x52, 0x8c, 0xd6}}
};
simple_ble_char_t char_range_handle = {.uuid16 = TRITAG_CHAR_LOCATION_SHORT_UUID};
simple_ble_char_t char_calibration_index_handle = {.uuid16 = TRITAG_CHAR_CALIBRATION_SHORT_UUID};
simple_ble_char_t char_ranging_enable_handle = {.uuid16 = TRITAG_CHAR_RANGING_ENABLE_SHORT_UUID};
simple_ble_char_t char_status_handle = {.uuid16 = TRITAG_CHAR_STATUS_SHORT_UUID};
// Intervals for advertising and connections
static simple_ble_config_t ble_config = {
// c0:98:e5:45:xx:xx
.platform_id = 0x45, // used as 4th octect in device BLE address
.device_id = DEVICE_ID_DEFAULT,
.adv_name = DEVICE_NAME, // used in advertisements if there is room
.adv_interval = MSEC_TO_UNITS(1000, UNIT_0_625_MS),
// .min_conn_interval = MSEC_TO_UNITS(500, UNIT_1_25_MS),
// .max_conn_interval = MSEC_TO_UNITS(1000, UNIT_1_25_MS),
.min_conn_interval = MSEC_TO_UNITS(8, UNIT_1_25_MS),
.max_conn_interval = MSEC_TO_UNITS(10, UNIT_1_25_MS),
};
// Copy address from flash
uint8_t _ble_address[6];
/*******************************************************************************
* State for this application
******************************************************************************/
// Main application state
simple_ble_app_t* simple_ble_app;
static ble_app_t app;
// GP Timer. Used to retry initializing the TriPoint.
static app_timer_id_t app_timer;
// Whether or not we successfully got through to the TriPoint module
// and got it configured properly.
bool tripoint_inited = false;
/*******************************************************************************
* nRF CALLBACKS - In response to various BLE/hardware events.
******************************************************************************/
// Function for handling the WRITE CHARACTERISTIC BLE event.
void ble_evt_write (ble_evt_t* p_ble_evt) {
ble_gatts_evt_write_t* p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
if (simple_ble_is_char_event(p_ble_evt, &char_ranging_enable_handle)) {
// Handle a write to the characteristic that starts and stops
// TriPoint ranging.
app.app_ranging = p_evt_write->data[0];
// Stop or start the tripoint based on the value we just got
if (app.app_ranging == 1) {
tripoint_resume();
} else {
tripoint_sleep();
}
} else if (simple_ble_is_char_event(p_ble_evt, &char_calibration_index_handle)) {
// Handle a write to the characteristic that starts calibration
app.calibration_index = p_evt_write->data[0];
// Configure this node for calibration and set the calibration node
// index. If 0, this node will immediately start calibration.
tripoint_start_calibration(app.calibration_index);
}
}
/*******************************************************************************
* PolyPoint Callbacks
******************************************************************************/
uint8_t updated = 0;
uint32_t blobLen;
uint8_t dataBlob[256];
void updateData (uint8_t * data, uint32_t len) {
uint32_t copy_len = MIN(len, 256);
memcpy(app.app_raw_response_buffer, data, copy_len);
blobLen = copy_len;
updated = 1;
}
void tripointDataUpdate () {
//update the data value and notify on the data
if (blobLen >= 5) {
led_on(LED_0);
nrf_delay_us(1000);
led_off(LED_0);
}
if(simple_ble_app->conn_handle != BLE_CONN_HANDLE_INVALID) {
ble_gatts_hvx_params_t notify_params;
uint16_t len = blobLen;
notify_params.handle = char_range_handle.char_handle.value_handle;
notify_params.type = BLE_GATT_HVX_NOTIFICATION;
notify_params.offset = 0;
notify_params.p_len = &len;
notify_params.p_data = app.app_raw_response_buffer;
// volatile uint32_t err_code = 0;
// err_code = sd_ble_gatts_hvx(simple_ble_app->conn_handle, ¬ify_params);
sd_ble_gatts_hvx(simple_ble_app->conn_handle, ¬ify_params);
// APP_ERROR_CHECK(err_code);
}
updated = 0;
}
static void timer_handler (void* p_context) {
uint32_t err_code;
if (!tripoint_inited) {
err_code = tripoint_init(updateData);
if (err_code == NRF_SUCCESS) {
tripoint_inited = true;
tripoint_start_ranging(true, 10);
}
}
}
/*******************************************************************************
* INIT FUNCTIONS
******************************************************************************/
void initialize_app_timer (void) {
APP_TIMER_INIT(TRITAG_TIMER_PRESCALER, TRITAG_MAX_TIMERS, TRITAG_OP_QUEUE_SIZE, false);
}
static void timers_init (void) {
uint32_t err_code;
err_code = app_timer_create(&app_timer,
APP_TIMER_MODE_REPEATED,
timer_handler);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(app_timer, UPDATE_RATE, NULL);
APP_ERROR_CHECK(err_code);
}
// Init services
void services_init (void) {
// Add main TriTag service
simple_ble_add_service(&service_handle);
//add the characteristic that exposes a blob of interrupt response
simple_ble_add_characteristic(1, 0, 1, 0, // read, write, notify, vlen
128, app.app_raw_response_buffer,
&service_handle,
&char_range_handle);
// Add the characteristic that enables/disables ranging
simple_ble_add_characteristic(1, 1, 0, 0, // read, write, notify, vlen
1, &app.calibration_index,
&service_handle,
&char_calibration_index_handle);
// Add the characteristic that sets the index of a node during
// calibration. Writing a 0 to this characteristic will start the
// calibration.
simple_ble_add_characteristic(1, 1, 0, 0, // read, write, notify, vlen
1, &app.app_ranging,
&service_handle,
&char_ranging_enable_handle);
// Status
simple_ble_add_characteristic(1, 0, 0, 0, // read, write, notify, vlen
1,(uint8_t*) &tripoint_inited,
&service_handle,
&char_status_handle);
}
int main (void) {
uint32_t err_code;
//
// Initialization
//
led_init(LED_0);
// We default to doing ranging at the start
app.app_ranging = 1;
// Set to effective -1
app.calibration_index = 255;
// Get stored address
memcpy(_ble_address, (uint8_t*) ADDRESS_FLASH_LOCATION, 6);
// And use it to setup the BLE
ble_config.device_id = (_ble_address[1] << 8) | _ble_address[0];
// Setup BLE
simple_ble_app = simple_ble_init(&ble_config);
// Setup the advertisement to use the Eddystone format.
// We include the device name in the scan response
ble_advdata_t srdata;
memset(&srdata, 0, sizeof(srdata));
srdata.name_type = BLE_ADVDATA_FULL_NAME;
eddystone_adv(PHYSWEB_URL, &srdata);
// Need a timer to make sure we have inited the tripoint
timers_init();
// Init the nRF hardware to work with the tripoint module.
err_code = tripoint_hw_init();
APP_ERROR_CHECK(err_code);
// Init the state machine on the tripoint
err_code = tripoint_init(updateData);
if (err_code == NRF_SUCCESS) {
tripoint_inited = true;
}
// Start the ranging!!!
if (tripoint_inited) {
tripoint_start_ranging(true, 10);
}
led_on(LED_0);
while (1) {
power_manage();
if (updated) {
tripointDataUpdate();
}
}
}