No changes
Dependencies: BLE_API mbed nRF51822
Fork of SDP_Version3_Abdul by
AcclerationService.h@5:fd83a2ac378e, 2017-02-26 (annotated)
- Committer:
- galism
- Date:
- Sun Feb 26 03:24:30 2017 +0000
- Revision:
- 5:fd83a2ac378e
- Parent:
- 4:caab577334f0
No changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
galism | 4:caab577334f0 | 1 | /* Senior Project Bluetooth bicycle speedometer |
galism | 4:caab577334f0 | 2 | Author: Michael Galis |
galism | 4:caab577334f0 | 3 | This header file describes the Acceleration Service that I created to be able |
galism | 4:caab577334f0 | 4 | to send the x, y, and z accelerations as the bicycle is moving. These |
galism | 4:caab577334f0 | 5 | accelerations will be used by the phone to calculate the speed of the bicycle. |
galism | 4:caab577334f0 | 6 | */ |
galism | 4:caab577334f0 | 7 | |
galism | 4:caab577334f0 | 8 | #ifndef __BLE_SERVICE_H__ |
galism | 4:caab577334f0 | 9 | #define __BLE_SERVICE_H__ |
galism | 4:caab577334f0 | 10 | |
galism | 4:caab577334f0 | 11 | class AccelerationService { |
galism | 4:caab577334f0 | 12 | public: |
galism | 4:caab577334f0 | 13 | const static uint16_t ACCELERATION_SERVICE_UUID = 0xA010; |
galism | 4:caab577334f0 | 14 | const static uint16_t X_CHARACTERISTIC_UUID = 0xA011; |
galism | 4:caab577334f0 | 15 | const static uint16_t Y_CHARACTERISTIC_UUID = 0xA012; |
galism | 4:caab577334f0 | 16 | const static uint16_t Z_CHARACTERISTIC_UUID = 0xA013; |
galism | 4:caab577334f0 | 17 | const static uint16_t ALL_CHARACTERISTIC_UUID = 0x0014; |
galism | 4:caab577334f0 | 18 | |
galism | 4:caab577334f0 | 19 | AccelerationService(BLE &_ble) : |
galism | 4:caab577334f0 | 20 | ble(_ble), |
galism | 4:caab577334f0 | 21 | xData(X_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
galism | 4:caab577334f0 | 22 | yData(Y_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
galism | 4:caab577334f0 | 23 | zData(Z_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
galism | 4:caab577334f0 | 24 | allState(ALL_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
galism | 4:caab577334f0 | 25 | { |
galism | 4:caab577334f0 | 26 | GattCharacteristic *charTable[] = {&xData,&yData, &zData, &allState}; |
galism | 4:caab577334f0 | 27 | GattService accelerationService(AccelerationService::ACCELERATION_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
galism | 4:caab577334f0 | 28 | ble.gattServer().addService(accelerationService); |
galism | 4:caab577334f0 | 29 | } |
galism | 4:caab577334f0 | 30 | |
galism | 4:caab577334f0 | 31 | void updateXData(float newData) |
galism | 4:caab577334f0 | 32 | { |
galism | 4:caab577334f0 | 33 | int length = sizeof(float); |
galism | 4:caab577334f0 | 34 | uint8_t bytes[sizeof(float)]; |
galism | 4:caab577334f0 | 35 | for(int i = 0; i < length; i++) |
galism | 4:caab577334f0 | 36 | { |
galism | 4:caab577334f0 | 37 | bytes[i] = ((uint8_t*)&newData)[i]; |
galism | 4:caab577334f0 | 38 | } |
galism | 4:caab577334f0 | 39 | int n = sizeof(bytes); |
galism | 4:caab577334f0 | 40 | ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&bytes, n); |
galism | 4:caab577334f0 | 41 | //int n = sizeof(uint8_t); |
galism | 4:caab577334f0 | 42 | //ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&newData, n); |
galism | 4:caab577334f0 | 43 | } |
galism | 4:caab577334f0 | 44 | |
galism | 4:caab577334f0 | 45 | void updateYData(float newData) |
galism | 4:caab577334f0 | 46 | { |
galism | 4:caab577334f0 | 47 | int length = sizeof(float); |
galism | 4:caab577334f0 | 48 | uint8_t bytes[sizeof(float)]; |
galism | 4:caab577334f0 | 49 | for(int i = 0; i < length; i++) |
galism | 4:caab577334f0 | 50 | { |
galism | 4:caab577334f0 | 51 | bytes[i] = ((uint8_t*)&newData)[i]; |
galism | 4:caab577334f0 | 52 | } |
galism | 4:caab577334f0 | 53 | int n = sizeof(bytes); |
galism | 4:caab577334f0 | 54 | ble.gattServer().write(yData.getValueHandle(), (uint8_t *)&bytes, n); |
galism | 4:caab577334f0 | 55 | } |
galism | 4:caab577334f0 | 56 | |
galism | 4:caab577334f0 | 57 | void updateZData(float newData) |
galism | 4:caab577334f0 | 58 | { |
galism | 4:caab577334f0 | 59 | int length = sizeof(float); |
galism | 4:caab577334f0 | 60 | uint8_t bytes[sizeof(float)]; |
galism | 4:caab577334f0 | 61 | for(int i = 0; i < length; i++) |
galism | 4:caab577334f0 | 62 | { |
galism | 4:caab577334f0 | 63 | bytes[i] = ((uint8_t*)&newData)[i]; |
galism | 4:caab577334f0 | 64 | } |
galism | 4:caab577334f0 | 65 | int n = sizeof(bytes); |
galism | 4:caab577334f0 | 66 | ble.gattServer().write(zData.getValueHandle(), (uint8_t *)&bytes, n); |
galism | 4:caab577334f0 | 67 | } |
galism | 4:caab577334f0 | 68 | |
galism | 4:caab577334f0 | 69 | void updateALLState(float newState,float newStatey,float newStatez) |
galism | 4:caab577334f0 | 70 | { |
galism | 4:caab577334f0 | 71 | |
galism | 4:caab577334f0 | 72 | int length = 14; |
galism | 4:caab577334f0 | 73 | uint8_t bytes[length]; |
galism | 4:caab577334f0 | 74 | |
galism | 4:caab577334f0 | 75 | bytes[0] = ((uint8_t*)&newState)[0]; |
galism | 4:caab577334f0 | 76 | bytes[1] = ((uint8_t*)&newState)[1]; |
galism | 4:caab577334f0 | 77 | bytes[2] = ((uint8_t*)&newState)[2]; |
galism | 4:caab577334f0 | 78 | bytes[3] = ((uint8_t*)&newState)[3]; |
galism | 4:caab577334f0 | 79 | bytes[4] = 0xff; |
galism | 4:caab577334f0 | 80 | bytes[5] = ((uint8_t*)&newStatey)[0]; |
galism | 4:caab577334f0 | 81 | bytes[6] = ((uint8_t*)&newStatey)[1]; |
galism | 4:caab577334f0 | 82 | bytes[7] = ((uint8_t*)&newStatey)[2]; |
galism | 4:caab577334f0 | 83 | bytes[8] = ((uint8_t*)&newStatey)[3]; |
galism | 4:caab577334f0 | 84 | bytes[9] = 0xff; |
galism | 4:caab577334f0 | 85 | bytes[10] = ((uint8_t*)&newStatez)[0]; |
galism | 4:caab577334f0 | 86 | bytes[11] = ((uint8_t*)&newStatez)[1]; |
galism | 4:caab577334f0 | 87 | bytes[12] = ((uint8_t*)&newStatez)[2]; |
galism | 4:caab577334f0 | 88 | bytes[13] = ((uint8_t*)&newStatez)[3]; |
galism | 4:caab577334f0 | 89 | |
galism | 4:caab577334f0 | 90 | uint16_t n = sizeof(bytes) / sizeof(bytes[0]); |
galism | 4:caab577334f0 | 91 | ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki |
galism | 4:caab577334f0 | 92 | } |
galism | 4:caab577334f0 | 93 | |
galism | 4:caab577334f0 | 94 | private: |
galism | 4:caab577334f0 | 95 | BLE &ble; |
galism | 4:caab577334f0 | 96 | ReadOnlyGattCharacteristic<float> xData; |
galism | 4:caab577334f0 | 97 | ReadOnlyGattCharacteristic<float> yData; |
galism | 4:caab577334f0 | 98 | ReadOnlyGattCharacteristic<float> zData; |
galism | 4:caab577334f0 | 99 | ReadOnlyArrayGattCharacteristic<uint8_t, 14> allState; |
galism | 4:caab577334f0 | 100 | }; |
galism | 4:caab577334f0 | 101 | |
galism | 4:caab577334f0 | 102 | #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */ |