testing for double to float

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Observer_2_Advertisers by Ames HCI IoT

Committer:
rjoyce
Date:
Sat Jul 18 06:56:25 2015 +0000
Revision:
7:d78080148084
Parent:
6:460ee10d9930
Child:
8:122d77e4fc6a
Child:
9:7360ae6b9b1e
Read acceleration data

Who changed what in which revision?

UserRevisionLine numberNew 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 */
rjoyce 7:d78080148084 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);
rjoyce 7:d78080148084 55 pc.printf("\r\n");
rjoyce 7:d78080148084 56 /*
rjoyce 7:d78080148084 57 unsigned index = 0;
rjoyce 7:d78080148084 58 uint8_t data_size = 0;
rjoyce 7:d78080148084 59 unsigned data_index = 0;
rjoyce 7:d78080148084 60 while (index < params->advertisingDataLen) {
rjoyce 7:d78080148084 61 data_size = params->advertisingData[index];
rjoyce 7:d78080148084 62 data_index = index;
rjoyce 7:d78080148084 63 // TODO: CHECK for size > dataLen
rjoyce 7:d78080148084 64 for (index; index < data_index + data_size; index++) {
rjoyce 7:d78080148084 65 // do something with the data
rjoyce 7:d78080148084 66 }
rjoyce 7:d78080148084 67 }
rjoyce 7:d78080148084 68 */
rgrover1 0:332983584a9c 69 }
rgrover1 0:332983584a9c 70 }
rgrover1 0:332983584a9c 71
rgrover1 0:332983584a9c 72 int main(void)
rgrover1 0:332983584a9c 73 {
rgrover1 0:332983584a9c 74 ble.init();
rgrover1 0:332983584a9c 75
rjoyce 6:460ee10d9930 76 // Set scan to be constant by interval == window
rjoyce 6:460ee10d9930 77 ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
rgrover1 5:103717ce54e5 78 ble.gap().startScan(advertisementCallback);
rjoyce 7:d78080148084 79
rjoyce 7:d78080148084 80 pc.baud(9600);
rjoyce 7:d78080148084 81 wait(8);
rjoyce 7:d78080148084 82 pc.printf("Started scanning...\n\r");
rjoyce 7:d78080148084 83
rgrover1 0:332983584a9c 84
rgrover1 0:332983584a9c 85 while (true) {
rgrover1 0:332983584a9c 86 ble.waitForEvent();
rgrover1 0:332983584a9c 87 }
rgrover1 0:332983584a9c 88 }