rakha asyrofi / Mbed 2 deprecated BLE_CSCADXL345

Dependencies:   ADXL345 BLE_API mbed nRF51822

Fork of BLE_CycleSpeedCadence by Robert Walker

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "CyclingSpeedAndCadenceService.h"
00020 #include "ble/services/BatteryService.h"
00021 #include "ble/services/DeviceInformationService.h"
00022 
00023 BLE  ble;
00024 Timer timer;
00025 
00026 const static char     DEVICE_NAME[]        = "CSC1";
00027 static const uint16_t uuid16_list[]        = {GattService::UUID_CYCLING_SPEED_AND_CADENCE,
00028                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
00029 
00030 uint32_t t = 0;
00031 uint32_t nextWheel = 0;
00032 uint32_t nextCrank = 0;
00033 static volatile bool  triggerWheel = false;
00034 static volatile bool  triggerCrank = false;
00035 
00036 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00037 {
00038     ble.gap().startAdvertising(); // restart advertising
00039 }
00040 
00041 
00042 void onTick(void)
00043 {
00044     ++t;
00045     
00046     if (t >= nextWheel)
00047     {
00048         triggerWheel = true;
00049         nextWheel += 7 + (rand() % 10);
00050     }
00051     
00052     if (t >= nextCrank)
00053     {
00054         triggerCrank = true;    
00055         nextCrank += 8 + (rand() % 20);
00056     }
00057 }
00058 
00059 int main(void)
00060 {
00061     Ticker ticker;
00062     ticker.attach(onTick, 0.1);
00063     timer.start();
00064 
00065     ble.init();
00066     ble.gap().onDisconnection(disconnectionCallback);
00067 
00068     /* Setup primary service. */
00069     uint32_t wheelCounter = 100; // init Wheel to 100revs
00070     uint16_t crankCounter = 10; // init crank to 10revs
00071     CyclingSpeedAndCadenceService cscService(ble,
00072         CyclingSpeedAndCadenceService::MODE_SPEED_CADENCE,
00073         CyclingSpeedAndCadenceService::LOCATION_CHAINSTAY );
00074 
00075     /* Setup auxiliary service. */
00076     DeviceInformationService deviceInfo(ble, "ROB", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00077 
00078     /* Setup advertising. */
00079     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00080     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00081     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::CYCLING_SPEED_AND_CADENCE_SENSOR);
00082     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00083     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00084     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00085     ble.gap().startAdvertising();
00086     
00087     nextWheel = 7 + (rand() % 10);
00088     nextCrank = 8 + (rand() % 20);
00089 
00090     // infinite loop
00091     while (1) {
00092         // check for trigger from periodicCallback()
00093         if (ble.getGapState().connected)
00094         {
00095             if (triggerCrank && triggerWheel)
00096             {
00097                 uint16_t when = (timer.read() * 1024);
00098                 cscService.updateCounters(++wheelCounter, ++crankCounter, when);
00099                 triggerWheel = false;
00100                 triggerCrank = false;
00101             }
00102             else if (triggerWheel)
00103             {
00104                 uint16_t when = (timer.read() * 1024);
00105                 cscService.updateWheelCounter(++wheelCounter, when);
00106                 triggerWheel = false;
00107             }
00108             else if (triggerCrank)
00109             {
00110                 uint16_t when = (timer.read() * 1024);
00111                 cscService.updateCrankCounter(++crankCounter, when);
00112                 triggerCrank = false;
00113             }
00114             else
00115             {
00116                 ble.waitForEvent(); // low power wait for event
00117             }
00118         } else {
00119             ble.waitForEvent(); // low power wait for event
00120         }
00121     }
00122 }