test uploading
Dependencies: BLE_API mbed nRF51822
ble_meter.cpp@1:3a1494d478bd, 2015-06-08 (annotated)
- Committer:
- ReneM92
- Date:
- Mon Jun 08 13:08:15 2015 +0000
- Revision:
- 1:3a1494d478bd
- Parent:
- 0:ac8f3df14a42
testing ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ReneM92 | 0:ac8f3df14a42 | 1 | #include "ble_meter.h" |
ReneM92 | 0:ac8f3df14a42 | 2 | |
ReneM92 | 0:ac8f3df14a42 | 3 | GonioService::GonioService(BLEDevice &_ble, uint16_t hoek, uint16_t acc, uint16_t gyro): |
ReneM92 | 0:ac8f3df14a42 | 4 | ble(_ble), |
ReneM92 | 0:ac8f3df14a42 | 5 | G_ServiceUUID(0x9000), |
ReneM92 | 0:ac8f3df14a42 | 6 | G_CharUUID(0x9001), |
ReneM92 | 0:ac8f3df14a42 | 7 | G_ControlUUID(0x9002), |
ReneM92 | 0:ac8f3df14a42 | 8 | gValueBytes(hoek,acc,gyro), |
ReneM92 | 0:ac8f3df14a42 | 9 | dataChar(G_CharUUID, gValueBytes.getPointer(), |
ReneM92 | 0:ac8f3df14a42 | 10 | gValueBytes.getNumValueBytes(), GonioValueBytes::MAX_VALUE_BYTES, |
ReneM92 | 0:ac8f3df14a42 | 11 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ReneM92 | 0:ac8f3df14a42 | 12 | batteryChar(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryValue, |
ReneM92 | 0:ac8f3df14a42 | 13 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ReneM92 | 0:ac8f3df14a42 | 14 | controlPoint(G_ControlUUID, &controlPointValue) |
ReneM92 | 0:ac8f3df14a42 | 15 | { |
ReneM92 | 0:ac8f3df14a42 | 16 | setupService(); |
ReneM92 | 0:ac8f3df14a42 | 17 | //; |
ReneM92 | 0:ac8f3df14a42 | 18 | } |
ReneM92 | 0:ac8f3df14a42 | 19 | |
ReneM92 | 0:ac8f3df14a42 | 20 | void GonioService::setupService(){ |
ReneM92 | 0:ac8f3df14a42 | 21 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
ReneM92 | 0:ac8f3df14a42 | 22 | if (serviceAdded) { |
ReneM92 | 0:ac8f3df14a42 | 23 | return; |
ReneM92 | 0:ac8f3df14a42 | 24 | } |
ReneM92 | 0:ac8f3df14a42 | 25 | init(); |
ReneM92 | 0:ac8f3df14a42 | 26 | //batteryLevel(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
ReneM92 | 0:ac8f3df14a42 | 27 | GattCharacteristic *charTable[] = {&dataChar, &controlPoint,&batteryChar}; |
ReneM92 | 0:ac8f3df14a42 | 28 | GattService GonioService(G_ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
ReneM92 | 0:ac8f3df14a42 | 29 | |
ReneM92 | 0:ac8f3df14a42 | 30 | ble.addService(GonioService); |
ReneM92 | 0:ac8f3df14a42 | 31 | serviceAdded = true; |
ReneM92 | 0:ac8f3df14a42 | 32 | |
ReneM92 | 0:ac8f3df14a42 | 33 | ble.onDataWritten(this, &GonioService::onDataWritten); |
ReneM92 | 0:ac8f3df14a42 | 34 | setupAdvertising(); |
ReneM92 | 0:ac8f3df14a42 | 35 | } |
ReneM92 | 0:ac8f3df14a42 | 36 | |
ReneM92 | 0:ac8f3df14a42 | 37 | void GonioService::updateBattery(uint8_t newLevel) { |
ReneM92 | 0:ac8f3df14a42 | 38 | batteryValue = newLevel; |
ReneM92 | 0:ac8f3df14a42 | 39 | ble.updateCharacteristicValue(batteryChar.getValueAttribute().getHandle(), &batteryValue, 1); |
ReneM92 | 0:ac8f3df14a42 | 40 | } |
ReneM92 | 0:ac8f3df14a42 | 41 | |
ReneM92 | 0:ac8f3df14a42 | 42 | uint8_t GonioService::getBatteryLevel(){ |
ReneM92 | 0:ac8f3df14a42 | 43 | return batteryValue; |
ReneM92 | 0:ac8f3df14a42 | 44 | } |
ReneM92 | 0:ac8f3df14a42 | 45 | |
ReneM92 | 0:ac8f3df14a42 | 46 | void GonioService::updateGonio(uint16_t params, uint16_t acc, uint16_t gyro){ |
ReneM92 | 0:ac8f3df14a42 | 47 | gValueBytes.updateGonio(params,acc, gyro); |
ReneM92 | 0:ac8f3df14a42 | 48 | ble.updateCharacteristicValue(dataChar.getValueAttribute().getHandle(), gValueBytes.getPointer(), gValueBytes.getNumValueBytes()); |
ReneM92 | 0:ac8f3df14a42 | 49 | } |
ReneM92 | 0:ac8f3df14a42 | 50 | |
ReneM92 | 0:ac8f3df14a42 | 51 | void GonioService::onDataWritten(const GattCharacteristicWriteCBParams *params){ |
ReneM92 | 0:ac8f3df14a42 | 52 | writeValue = 200; |
ReneM92 | 0:ac8f3df14a42 | 53 | if (params->charHandle == controlPoint.getValueAttribute().getHandle()) { |
ReneM92 | 0:ac8f3df14a42 | 54 | writeValue = params->data[0]; |
ReneM92 | 0:ac8f3df14a42 | 55 | } |
ReneM92 | 0:ac8f3df14a42 | 56 | } |
ReneM92 | 0:ac8f3df14a42 | 57 | |
ReneM92 | 0:ac8f3df14a42 | 58 | uint8_t GonioService::getGServiceUUID(){ |
ReneM92 | 0:ac8f3df14a42 | 59 | return G_ServiceUUID; |
ReneM92 | 0:ac8f3df14a42 | 60 | } |
ReneM92 | 0:ac8f3df14a42 | 61 | |
ReneM92 | 0:ac8f3df14a42 | 62 | /* |
ReneM92 | 0:ac8f3df14a42 | 63 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
ReneM92 | 0:ac8f3df14a42 | 64 | { |
ReneM92 | 0:ac8f3df14a42 | 65 | GonioService::ble.startAdvertising(); // restart advertising |
ReneM92 | 0:ac8f3df14a42 | 66 | } |
ReneM92 | 0:ac8f3df14a42 | 67 | |
ReneM92 | 0:ac8f3df14a42 | 68 | void GonioService::periodicCallback(){ |
ReneM92 | 0:ac8f3df14a42 | 69 | // do nothing yet |
ReneM92 | 0:ac8f3df14a42 | 70 | }*/ |
ReneM92 | 0:ac8f3df14a42 | 71 | |
ReneM92 | 0:ac8f3df14a42 | 72 | void GonioService::init(){ |
ReneM92 | 0:ac8f3df14a42 | 73 | ble.init(); |
ReneM92 | 0:ac8f3df14a42 | 74 | //ble.setTxPower(4); |
ReneM92 | 0:ac8f3df14a42 | 75 | //ble.onDisconnection(disconnectionCallback); |
ReneM92 | 0:ac8f3df14a42 | 76 | } |
ReneM92 | 0:ac8f3df14a42 | 77 | |
ReneM92 | 0:ac8f3df14a42 | 78 | void GonioService::setupAdvertising(){ |
ReneM92 | 0:ac8f3df14a42 | 79 | const static char DEVICE_NAME[] = "GonioMeter"; |
ReneM92 | 0:ac8f3df14a42 | 80 | static const uint16_t uuid16_list[] = {G_ServiceUUID}; |
ReneM92 | 0:ac8f3df14a42 | 81 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ReneM92 | 0:ac8f3df14a42 | 82 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ReneM92 | 0:ac8f3df14a42 | 83 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
ReneM92 | 0:ac8f3df14a42 | 84 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ReneM92 | 0:ac8f3df14a42 | 85 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ReneM92 | 0:ac8f3df14a42 | 86 | ble.setAdvertisingInterval(50); |
ReneM92 | 0:ac8f3df14a42 | 87 | ble.startAdvertising(); |
ReneM92 | 0:ac8f3df14a42 | 88 | } |
ReneM92 | 0:ac8f3df14a42 | 89 | |
ReneM92 | 0:ac8f3df14a42 | 90 |