trying to add correlation

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Trial by Ames HCI IoT

Committer:
mchan
Date:
Thu Jul 30 16:40:03 2015 +0000
Revision:
19:5b952d4f1024
Parent:
10:0733d4800ed1
correlation test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rjoyce 10:0733d4800ed1 1 /* mbed Microcontroller Library
rjoyce 10:0733d4800ed1 2 * Copyright (c) 2006-2015 ARM Limited
rjoyce 10:0733d4800ed1 3 *
rjoyce 10:0733d4800ed1 4 * Licensed under the Apache License, Version 2.0 (the "License");
rjoyce 10:0733d4800ed1 5 * you may not use this file except in compliance with the License.
rjoyce 10:0733d4800ed1 6 * You may obtain a copy of the License at
rjoyce 10:0733d4800ed1 7 *
rjoyce 10:0733d4800ed1 8 * http://www.apache.org/licenses/LICENSE-2.0
rjoyce 10:0733d4800ed1 9 *
rjoyce 10:0733d4800ed1 10 * Unless required by applicable law or agreed to in writing, software
rjoyce 10:0733d4800ed1 11 * distributed under the License is distributed on an "AS IS" BASIS,
rjoyce 10:0733d4800ed1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rjoyce 10:0733d4800ed1 13 * See the License for the specific language governing permissions and
rjoyce 10:0733d4800ed1 14 * limitations under the License.
rjoyce 10:0733d4800ed1 15 */
rjoyce 10:0733d4800ed1 16
rjoyce 10:0733d4800ed1 17 #include "mbed.h"
rjoyce 10:0733d4800ed1 18 #include "BLE.h"
rjoyce 10:0733d4800ed1 19
rjoyce 10:0733d4800ed1 20 BLE ble;
rjoyce 10:0733d4800ed1 21
rjoyce 10:0733d4800ed1 22 typedef union _accleration_data {
rjoyce 10:0733d4800ed1 23 float f;
rjoyce 10:0733d4800ed1 24 uint8_t b[4];
rjoyce 10:0733d4800ed1 25 } acceleration_data;
rjoyce 10:0733d4800ed1 26
rjoyce 10:0733d4800ed1 27 typedef enum _parse_state {
rjoyce 10:0733d4800ed1 28 PARSE_SIZE,
rjoyce 10:0733d4800ed1 29 PARSE_TYPE,
rjoyce 10:0733d4800ed1 30 PARSE_DATA
rjoyce 10:0733d4800ed1 31 } ParseState;
rjoyce 10:0733d4800ed1 32
rjoyce 10:0733d4800ed1 33 Serial pc(USBTX, USBRX);
rjoyce 10:0733d4800ed1 34
rjoyce 10:0733d4800ed1 35 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
rjoyce 10:0733d4800ed1 36 {
rjoyce 10:0733d4800ed1 37 if (params->peerAddr[5] == 0xe5 && params->peerAddr[4] == 0x4f) {
rjoyce 10:0733d4800ed1 38
rjoyce 10:0733d4800ed1 39 pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
rjoyce 10:0733d4800ed1 40 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
rjoyce 10:0733d4800ed1 41 params->rssi, params->isScanResponse, params->type);
rjoyce 10:0733d4800ed1 42 for (unsigned index = 0; index < params->advertisingDataLen; index++) {
rjoyce 10:0733d4800ed1 43 pc.printf("%02x ", params->advertisingData[index]);
rjoyce 10:0733d4800ed1 44 }
rjoyce 10:0733d4800ed1 45 pc.printf("\r\n");
rjoyce 10:0733d4800ed1 46
rjoyce 10:0733d4800ed1 47 /* Quick and dirty naive way */
rjoyce 10:0733d4800ed1 48 /*acceleration_data acceleration;
rjoyce 10:0733d4800ed1 49 acceleration.f = 4.5;
rjoyce 10:0733d4800ed1 50 if (params->advertisingDataLen != 6) return;
rjoyce 10:0733d4800ed1 51 for (unsigned i = 0; i < 4; i++) {
rjoyce 10:0733d4800ed1 52 acceleration.b[i] = params->advertisingData[i+2];
rjoyce 10:0733d4800ed1 53 }
rjoyce 10:0733d4800ed1 54 pc.printf("Acceleration: %.2f", acceleration.f);
rjoyce 10:0733d4800ed1 55 pc.printf("\r\n");*/
rjoyce 10:0733d4800ed1 56
rjoyce 10:0733d4800ed1 57
rjoyce 10:0733d4800ed1 58 /* Printing the boolean for being picked up or not*/
rjoyce 10:0733d4800ed1 59 bool p = params->advertisingData[2];
rjoyce 10:0733d4800ed1 60 pc.printf("Picked up: %p",p);
rjoyce 10:0733d4800ed1 61 pc.printf("\r\n");
rjoyce 10:0733d4800ed1 62
rjoyce 10:0733d4800ed1 63 /*
rjoyce 10:0733d4800ed1 64
rjoyce 10:0733d4800ed1 65 unsigned index = 0;
rjoyce 10:0733d4800ed1 66 uint8_t data_size = 0;
rjoyce 10:0733d4800ed1 67 unsigned data_index = 0;
rjoyce 10:0733d4800ed1 68 while (index < params->advertisingDataLen) {
rjoyce 10:0733d4800ed1 69 data_size = params->advertisingData[index];
rjoyce 10:0733d4800ed1 70 data_index = index;
rjoyce 10:0733d4800ed1 71 // TODO: CHECK for size > dataLen
rjoyce 10:0733d4800ed1 72 for (index; index < data_index + data_size; index++) {
rjoyce 10:0733d4800ed1 73 // do something with the data
rjoyce 10:0733d4800ed1 74 }
rjoyce 10:0733d4800ed1 75 }
rjoyce 10:0733d4800ed1 76 */
rjoyce 10:0733d4800ed1 77 }
rjoyce 10:0733d4800ed1 78 }
rjoyce 10:0733d4800ed1 79
rjoyce 10:0733d4800ed1 80 int main(void)
rjoyce 10:0733d4800ed1 81 {
rjoyce 10:0733d4800ed1 82 ble.init();
rjoyce 10:0733d4800ed1 83
rjoyce 10:0733d4800ed1 84 // Set scan to be constant by interval == window
rjoyce 10:0733d4800ed1 85 ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
rjoyce 10:0733d4800ed1 86 ble.gap().startScan(advertisementCallback);
rjoyce 10:0733d4800ed1 87
rjoyce 10:0733d4800ed1 88 pc.baud(9600);
rjoyce 10:0733d4800ed1 89 wait(8);
rjoyce 10:0733d4800ed1 90 pc.printf("Started scanning...\n\r");
rjoyce 10:0733d4800ed1 91
rjoyce 10:0733d4800ed1 92
rjoyce 10:0733d4800ed1 93 while (true) {
rjoyce 10:0733d4800ed1 94 ble.waitForEvent();
rjoyce 10:0733d4800ed1 95 }
rjoyce 10:0733d4800ed1 96 }