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