Michael Galis / Mbed 2 deprecated SDP_Version2

Dependencies:   BLE_API MMA8452Q mbed nRF51822

Fork of BLE_Accelerometer_final by Praktyki

Committer:
Radoj
Date:
Sun Apr 10 19:05:36 2016 +0000
Revision:
3:7dc284221369
Parent:
2:e78a5ce9f1d7
BLE Accel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Radoj 0:6bd1a61571d0 1 #ifndef __BLE_SERVICE_H__
Radoj 0:6bd1a61571d0 2 #define __BLE_SERVICE_H__
Radoj 0:6bd1a61571d0 3
Radoj 0:6bd1a61571d0 4 class Service {
Radoj 0:6bd1a61571d0 5 public:
Radoj 0:6bd1a61571d0 6 const static uint16_t SERVICE_UUID = 0x0010;
Radoj 0:6bd1a61571d0 7 const static uint16_t X_CHARACTERISTIC_UUID = 0x0011;
Radoj 0:6bd1a61571d0 8 const static uint16_t Y_CHARACTERISTIC_UUID = 0x0012;
Radoj 0:6bd1a61571d0 9 const static uint16_t Z_CHARACTERISTIC_UUID = 0x0013;
Radoj 0:6bd1a61571d0 10 const static uint16_t ALL_CHARACTERISTIC_UUID = 0x0014;
Radoj 0:6bd1a61571d0 11
Radoj 0:6bd1a61571d0 12 Service(BLE &_ble) :
Radoj 0:6bd1a61571d0 13 ble(_ble), xState(X_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),yState(Y_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),zState(Z_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),allState(ALL_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
Radoj 0:6bd1a61571d0 14 {
Radoj 0:6bd1a61571d0 15 GattCharacteristic *charTable[] = {&xState,&yState, &zState, &allState};
Radoj 0:6bd1a61571d0 16 GattService Service(Service::SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
Radoj 0:6bd1a61571d0 17 ble.gattServer().addService(Service);
Radoj 0:6bd1a61571d0 18 }
Radoj 0:6bd1a61571d0 19
Radoj 3:7dc284221369 20 /*
Radoj 3:7dc284221369 21 * Update wartosci charakterystyk
Radoj 3:7dc284221369 22 */
Radoj 3:7dc284221369 23
Radoj 0:6bd1a61571d0 24 void updateXState(float newState) {
Radoj 2:e78a5ce9f1d7 25 int length = sizeof(float);
Radoj 2:e78a5ce9f1d7 26
Radoj 3:7dc284221369 27 uint8_t bytes[sizeof(float)]; //tablica podzielonego float'a
Radoj 3:7dc284221369 28
Radoj 3:7dc284221369 29 for(int i = 0; i < length; i++){
Radoj 2:e78a5ce9f1d7 30 bytes[i] = ((uint8_t*)&newState)[i];
Radoj 3:7dc284221369 31 }
Radoj 3:7dc284221369 32
Radoj 3:7dc284221369 33 int n = sizeof(bytes) / sizeof(bytes[0]);
Radoj 3:7dc284221369 34 ble.gattServer().write(xState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
Radoj 0:6bd1a61571d0 35 }
Radoj 0:6bd1a61571d0 36
Radoj 0:6bd1a61571d0 37 void updateYState(float newState) {
Radoj 2:e78a5ce9f1d7 38 int length = sizeof(float);
Radoj 3:7dc284221369 39
Radoj 3:7dc284221369 40 uint8_t bytes[sizeof(float)];//tablica podzielonego float'a
Radoj 3:7dc284221369 41
Radoj 3:7dc284221369 42 for(int i = 0; i < length; i++){
Radoj 2:e78a5ce9f1d7 43 bytes[i] = ((uint8_t*)&newState)[i];
Radoj 3:7dc284221369 44 }
Radoj 2:e78a5ce9f1d7 45
Radoj 3:7dc284221369 46 int n = sizeof(bytes) / sizeof(bytes[0]);
Radoj 3:7dc284221369 47 ble.gattServer().write(yState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
Radoj 0:6bd1a61571d0 48 }
Radoj 0:6bd1a61571d0 49
Radoj 0:6bd1a61571d0 50 void updateZState(float newState) {
Radoj 2:e78a5ce9f1d7 51 int length = sizeof(float);
Radoj 3:7dc284221369 52
Radoj 3:7dc284221369 53 uint8_t bytes[sizeof(float)];//tablica podzielonego float'a
Radoj 3:7dc284221369 54
Radoj 3:7dc284221369 55 for(int i = 0; i < length; i++){
Radoj 2:e78a5ce9f1d7 56 bytes[i] = ((uint8_t*)&newState)[i];
Radoj 3:7dc284221369 57 }
Radoj 3:7dc284221369 58
Radoj 3:7dc284221369 59 int n = sizeof(bytes) / sizeof(bytes[0]);
Radoj 3:7dc284221369 60 ble.gattServer().write(zState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
Radoj 0:6bd1a61571d0 61 }
Radoj 0:6bd1a61571d0 62
Radoj 2:e78a5ce9f1d7 63 void updateALLState(float newState,float newStatey,float newStatez) {
Radoj 2:e78a5ce9f1d7 64
Radoj 2:e78a5ce9f1d7 65 int length = 14;
Radoj 2:e78a5ce9f1d7 66
Radoj 3:7dc284221369 67 uint8_t bytes[14]; //tablica podzielonych float'ow
Radoj 2:e78a5ce9f1d7 68
Radoj 2:e78a5ce9f1d7 69 /*
Radoj 3:7dc284221369 70 * dzielenie float'ow x y z do tablicy
Radoj 3:7dc284221369 71 * mona bylo zrobic forem ;)
Radoj 3:7dc284221369 72 */
Radoj 3:7dc284221369 73
Radoj 3:7dc284221369 74 bytes[0] = ((uint8_t*)&newState)[0];
Radoj 3:7dc284221369 75 bytes[1] = ((uint8_t*)&newState)[1];
Radoj 3:7dc284221369 76 bytes[2] = ((uint8_t*)&newState)[2];
Radoj 3:7dc284221369 77 bytes[3] = ((uint8_t*)&newState)[3];
Radoj 3:7dc284221369 78 bytes[4] = 0xff;
Radoj 3:7dc284221369 79 bytes[5] = ((uint8_t*)&newStatey)[0];
Radoj 3:7dc284221369 80 bytes[6] = ((uint8_t*)&newStatey)[1];
Radoj 3:7dc284221369 81 bytes[7] = ((uint8_t*)&newStatey)[2];
Radoj 3:7dc284221369 82 bytes[8] = ((uint8_t*)&newStatey)[3];
Radoj 3:7dc284221369 83 bytes[9] = 0xff;
Radoj 3:7dc284221369 84 bytes[10] = ((uint8_t*)&newStatez)[0];
Radoj 3:7dc284221369 85 bytes[11] = ((uint8_t*)&newStatez)[1];
Radoj 3:7dc284221369 86 bytes[12] = ((uint8_t*)&newStatez)[2];
Radoj 3:7dc284221369 87 bytes[13] = ((uint8_t*)&newStatez)[3];
Radoj 2:e78a5ce9f1d7 88
Radoj 2:e78a5ce9f1d7 89
Radoj 3:7dc284221369 90 uint16_t n = sizeof(bytes) / sizeof(bytes[0]);
Radoj 2:e78a5ce9f1d7 91
Radoj 3:7dc284221369 92 ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
Radoj 0:6bd1a61571d0 93 }
Radoj 0:6bd1a61571d0 94
Radoj 0:6bd1a61571d0 95 private:
Radoj 0:6bd1a61571d0 96 BLE &ble;
Radoj 0:6bd1a61571d0 97 ReadOnlyGattCharacteristic<float> xState;
Radoj 0:6bd1a61571d0 98 ReadOnlyGattCharacteristic<float> yState;
Radoj 0:6bd1a61571d0 99 ReadOnlyGattCharacteristic<float> zState;
Radoj 3:7dc284221369 100 ReadOnlyArrayGattCharacteristic<uint8_t, 14> allState;
Radoj 0:6bd1a61571d0 101 };
Radoj 0:6bd1a61571d0 102
Radoj 0:6bd1a61571d0 103 #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */