Odometry Pedometer using nRF51822 and ADXL345

Dependencies:   ADXL345 BLE_API mbed nRF51822

Fork of BLE_CycleSpeedCadence by Robert Walker

Committer:
asyrofi
Date:
Fri Aug 25 01:50:45 2017 +0000
Revision:
76:b56d8e15bdc6
Parent:
75:7e334e81da21
need correction please... want to using nrf51822 + ADXL345;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 /* mbed Microcontroller Library
rgrover1 67:b2d2dee347c0 2 * Copyright (c) 2006-2015 ARM Limited
ktownsend 0:87a7fc231fae 3 *
ktownsend 0:87a7fc231fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:87a7fc231fae 5 * you may not use this file except in compliance with the License.
ktownsend 0:87a7fc231fae 6 * You may obtain a copy of the License at
ktownsend 0:87a7fc231fae 7 *
ktownsend 0:87a7fc231fae 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:87a7fc231fae 9 *
ktownsend 0:87a7fc231fae 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:87a7fc231fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:87a7fc231fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:87a7fc231fae 13 * See the License for the specific language governing permissions and
ktownsend 0:87a7fc231fae 14 * limitations under the License.
ktownsend 0:87a7fc231fae 15 */
ktownsend 0:87a7fc231fae 16
ktownsend 0:87a7fc231fae 17 #include "mbed.h"
rgrover1 67:b2d2dee347c0 18 #include "ble/BLE.h"
tenfoot 71:7b6a488af957 19 #include "CyclingSpeedAndCadenceService.h"
rgrover1 67:b2d2dee347c0 20 #include "ble/services/BatteryService.h"
rgrover1 67:b2d2dee347c0 21 #include "ble/services/DeviceInformationService.h"
ktownsend 0:87a7fc231fae 22
rgrover1 67:b2d2dee347c0 23 BLE ble;
tenfoot 75:7e334e81da21 24 Timer timer;
ktownsend 0:87a7fc231fae 25
tenfoot 71:7b6a488af957 26 const static char DEVICE_NAME[] = "CSC1";
tenfoot 71:7b6a488af957 27 static const uint16_t uuid16_list[] = {GattService::UUID_CYCLING_SPEED_AND_CADENCE,
rgrover1 42:06ebef2e0e44 28 GattService::UUID_DEVICE_INFORMATION_SERVICE};
tenfoot 75:7e334e81da21 29
tenfoot 75:7e334e81da21 30 uint32_t t = 0;
tenfoot 75:7e334e81da21 31 uint32_t nextWheel = 0;
tenfoot 75:7e334e81da21 32 uint32_t nextCrank = 0;
tenfoot 75:7e334e81da21 33 static volatile bool triggerWheel = false;
tenfoot 75:7e334e81da21 34 static volatile bool triggerCrank = false;
Rohit Grover 36:ea2a1b4f51c1 35
rgrover1 41:9cef0129da5f 36 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
ktownsend 0:87a7fc231fae 37 {
rgrover1 66:c09ddf226b9c 38 ble.gap().startAdvertising(); // restart advertising
rgrover1 7:daab8ba5139e 39 }
Rohit Grover 3:24e2b056d229 40
tenfoot 75:7e334e81da21 41
tenfoot 75:7e334e81da21 42 void onTick(void)
Rohit Grover 11:1d9aafee4984 43 {
tenfoot 75:7e334e81da21 44 ++t;
tenfoot 75:7e334e81da21 45
tenfoot 75:7e334e81da21 46 if (t >= nextWheel)
tenfoot 75:7e334e81da21 47 {
tenfoot 75:7e334e81da21 48 triggerWheel = true;
tenfoot 75:7e334e81da21 49 nextWheel += 7 + (rand() % 10);
tenfoot 75:7e334e81da21 50 }
tenfoot 75:7e334e81da21 51
tenfoot 75:7e334e81da21 52 if (t >= nextCrank)
tenfoot 75:7e334e81da21 53 {
tenfoot 75:7e334e81da21 54 triggerCrank = true;
tenfoot 75:7e334e81da21 55 nextCrank += 8 + (rand() % 20);
tenfoot 75:7e334e81da21 56 }
Rohit Grover 11:1d9aafee4984 57 }
Rohit Grover 11:1d9aafee4984 58
ktownsend 0:87a7fc231fae 59 int main(void)
ktownsend 0:87a7fc231fae 60 {
Rohit Grover 11:1d9aafee4984 61 Ticker ticker;
tenfoot 75:7e334e81da21 62 ticker.attach(onTick, 0.1);
tenfoot 75:7e334e81da21 63 timer.start();
ktownsend 0:87a7fc231fae 64
Rohit Grover 15:7ba28817e31e 65 ble.init();
rgrover1 65:cb76569f74f6 66 ble.gap().onDisconnection(disconnectionCallback);
ktownsend 0:87a7fc231fae 67
rgrover1 45:98c5a34b07a4 68 /* Setup primary service. */
tenfoot 71:7b6a488af957 69 uint32_t wheelCounter = 100; // init Wheel to 100revs
tenfoot 71:7b6a488af957 70 uint16_t crankCounter = 10; // init crank to 10revs
tenfoot 73:bae88c99c2ae 71 CyclingSpeedAndCadenceService cscService(ble,
tenfoot 73:bae88c99c2ae 72 CyclingSpeedAndCadenceService::MODE_SPEED_CADENCE,
tenfoot 73:bae88c99c2ae 73 CyclingSpeedAndCadenceService::LOCATION_CHAINSTAY);
rgrover1 45:98c5a34b07a4 74
mbedAustin 55:3a7d497a3e03 75 /* Setup auxiliary service. */
tenfoot 71:7b6a488af957 76 DeviceInformationService deviceInfo(ble, "ROB", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
rgrover1 45:98c5a34b07a4 77
rgrover1 45:98c5a34b07a4 78 /* Setup advertising. */
rgrover1 65:cb76569f74f6 79 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 65:cb76569f74f6 80 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
tenfoot 71:7b6a488af957 81 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::CYCLING_SPEED_AND_CADENCE_SENSOR);
rgrover1 65:cb76569f74f6 82 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 65:cb76569f74f6 83 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 67:b2d2dee347c0 84 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
rgrover1 65:cb76569f74f6 85 ble.gap().startAdvertising();
tenfoot 75:7e334e81da21 86
tenfoot 75:7e334e81da21 87 nextWheel = 7 + (rand() % 10);
tenfoot 75:7e334e81da21 88 nextCrank = 8 + (rand() % 20);
Rohit Grover 3:24e2b056d229 89
mbedAustin 55:3a7d497a3e03 90 // infinite loop
mbedAustin 55:3a7d497a3e03 91 while (1) {
mbedAustin 55:3a7d497a3e03 92 // check for trigger from periodicCallback()
tenfoot 75:7e334e81da21 93 if (ble.getGapState().connected)
tenfoot 75:7e334e81da21 94 {
tenfoot 75:7e334e81da21 95 if (triggerCrank && triggerWheel)
tenfoot 75:7e334e81da21 96 {
tenfoot 75:7e334e81da21 97 uint16_t when = (timer.read() * 1024);
tenfoot 75:7e334e81da21 98 cscService.updateCounters(++wheelCounter, ++crankCounter, when);
tenfoot 75:7e334e81da21 99 triggerWheel = false;
tenfoot 75:7e334e81da21 100 triggerCrank = false;
tenfoot 75:7e334e81da21 101 }
tenfoot 75:7e334e81da21 102 else if (triggerWheel)
tenfoot 75:7e334e81da21 103 {
tenfoot 75:7e334e81da21 104 uint16_t when = (timer.read() * 1024);
tenfoot 75:7e334e81da21 105 cscService.updateWheelCounter(++wheelCounter, when);
tenfoot 75:7e334e81da21 106 triggerWheel = false;
tenfoot 75:7e334e81da21 107 }
tenfoot 75:7e334e81da21 108 else if (triggerCrank)
tenfoot 75:7e334e81da21 109 {
tenfoot 75:7e334e81da21 110 uint16_t when = (timer.read() * 1024);
tenfoot 75:7e334e81da21 111 cscService.updateCrankCounter(++crankCounter, when);
tenfoot 75:7e334e81da21 112 triggerCrank = false;
tenfoot 75:7e334e81da21 113 }
tenfoot 75:7e334e81da21 114 else
tenfoot 75:7e334e81da21 115 {
tenfoot 75:7e334e81da21 116 ble.waitForEvent(); // low power wait for event
tenfoot 75:7e334e81da21 117 }
Rohit Grover 36:ea2a1b4f51c1 118 } else {
mbedAustin 55:3a7d497a3e03 119 ble.waitForEvent(); // low power wait for event
Rohit Grover 36:ea2a1b4f51c1 120 }
ktownsend 0:87a7fc231fae 121 }
ktownsend 0:87a7fc231fae 122 }