
Kod new
Dependencies: BLE_API MMA8452Q mbed nRF51822
Service.h@2:e78a5ce9f1d7, 2016-04-10 (annotated)
- Committer:
- Radoj
- Date:
- Sun Apr 10 17:47:09 2016 +0000
- Revision:
- 2:e78a5ce9f1d7
- Parent:
- 0:6bd1a61571d0
asdw
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Radoj | 0:6bd1a61571d0 | 1 | /* mbed Microcontroller Library |
Radoj | 0:6bd1a61571d0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Radoj | 0:6bd1a61571d0 | 3 | * |
Radoj | 0:6bd1a61571d0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Radoj | 0:6bd1a61571d0 | 5 | * you may not use this file except in compliance with the License. |
Radoj | 0:6bd1a61571d0 | 6 | * You may obtain a copy of the License at |
Radoj | 0:6bd1a61571d0 | 7 | * |
Radoj | 0:6bd1a61571d0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Radoj | 0:6bd1a61571d0 | 9 | * |
Radoj | 0:6bd1a61571d0 | 10 | * Unless required by applicable law or agreed to in writing, software |
Radoj | 0:6bd1a61571d0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Radoj | 0:6bd1a61571d0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Radoj | 0:6bd1a61571d0 | 13 | * See the License for the specific language governing permissions and |
Radoj | 0:6bd1a61571d0 | 14 | * limitations under the License. |
Radoj | 0:6bd1a61571d0 | 15 | */ |
Radoj | 0:6bd1a61571d0 | 16 | |
Radoj | 0:6bd1a61571d0 | 17 | #ifndef __BLE_SERVICE_H__ |
Radoj | 0:6bd1a61571d0 | 18 | #define __BLE_SERVICE_H__ |
Radoj | 0:6bd1a61571d0 | 19 | |
Radoj | 0:6bd1a61571d0 | 20 | class Service { |
Radoj | 0:6bd1a61571d0 | 21 | public: |
Radoj | 0:6bd1a61571d0 | 22 | const static uint16_t SERVICE_UUID = 0x0010; |
Radoj | 0:6bd1a61571d0 | 23 | const static uint16_t X_CHARACTERISTIC_UUID = 0x0011; |
Radoj | 0:6bd1a61571d0 | 24 | const static uint16_t Y_CHARACTERISTIC_UUID = 0x0012; |
Radoj | 0:6bd1a61571d0 | 25 | const static uint16_t Z_CHARACTERISTIC_UUID = 0x0013; |
Radoj | 0:6bd1a61571d0 | 26 | const static uint16_t ALL_CHARACTERISTIC_UUID = 0x0014; |
Radoj | 0:6bd1a61571d0 | 27 | |
Radoj | 0:6bd1a61571d0 | 28 | Service(BLE &_ble) : |
Radoj | 0:6bd1a61571d0 | 29 | 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 | 30 | { |
Radoj | 0:6bd1a61571d0 | 31 | GattCharacteristic *charTable[] = {&xState,&yState, &zState, &allState}; |
Radoj | 0:6bd1a61571d0 | 32 | GattService Service(Service::SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
Radoj | 0:6bd1a61571d0 | 33 | ble.gattServer().addService(Service); |
Radoj | 0:6bd1a61571d0 | 34 | } |
Radoj | 0:6bd1a61571d0 | 35 | |
Radoj | 0:6bd1a61571d0 | 36 | void updateXState(float newState) { |
Radoj | 2:e78a5ce9f1d7 | 37 | |
Radoj | 2:e78a5ce9f1d7 | 38 | int length = sizeof(float); |
Radoj | 2:e78a5ce9f1d7 | 39 | |
Radoj | 2:e78a5ce9f1d7 | 40 | uint8_t bytes[sizeof(float)]; |
Radoj | 2:e78a5ce9f1d7 | 41 | for(int i = 0; i < length; i++){ |
Radoj | 2:e78a5ce9f1d7 | 42 | bytes[i] = ((uint8_t*)&newState)[i]; |
Radoj | 2:e78a5ce9f1d7 | 43 | //printf("%x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 44 | // printf("%d;",length); |
Radoj | 2:e78a5ce9f1d7 | 45 | } |
Radoj | 2:e78a5ce9f1d7 | 46 | |
Radoj | 2:e78a5ce9f1d7 | 47 | |
Radoj | 2:e78a5ce9f1d7 | 48 | |
Radoj | 2:e78a5ce9f1d7 | 49 | int n = sizeof(bytes) / sizeof(bytes[0]); |
Radoj | 2:e78a5ce9f1d7 | 50 | //printf("%d",sizeof(bytes[0])); |
Radoj | 2:e78a5ce9f1d7 | 51 | ble.gattServer().write(xState.getValueHandle(), (uint8_t *)&bytes, n); |
Radoj | 2:e78a5ce9f1d7 | 52 | |
Radoj | 2:e78a5ce9f1d7 | 53 | |
Radoj | 2:e78a5ce9f1d7 | 54 | |
Radoj | 2:e78a5ce9f1d7 | 55 | //uint8_t *p = (uint8_t*)&newState; |
Radoj | 2:e78a5ce9f1d7 | 56 | |
Radoj | 2:e78a5ce9f1d7 | 57 | //for (int i = 0; i < sizeof(newState); i++) printf("wwww 0x%02x ", p[i]); |
Radoj | 2:e78a5ce9f1d7 | 58 | //ble.gattServer().write(xState.getValueHandle(), (uint8_t *)&newState, sizeof(newState)); |
Radoj | 0:6bd1a61571d0 | 59 | } |
Radoj | 0:6bd1a61571d0 | 60 | |
Radoj | 0:6bd1a61571d0 | 61 | void updateYState(float newState) { |
Radoj | 2:e78a5ce9f1d7 | 62 | //ble.gattServer().write(yState.getValueHandle(), (uint8_t *)&newState, sizeof(newState)); |
Radoj | 2:e78a5ce9f1d7 | 63 | int length = sizeof(float); |
Radoj | 2:e78a5ce9f1d7 | 64 | uint8_t bytes[sizeof(float)]; |
Radoj | 2:e78a5ce9f1d7 | 65 | for(int i = 0; i < length; i++){ |
Radoj | 2:e78a5ce9f1d7 | 66 | bytes[i] = ((uint8_t*)&newState)[i]; |
Radoj | 2:e78a5ce9f1d7 | 67 | //printf("%x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 68 | } |
Radoj | 2:e78a5ce9f1d7 | 69 | //printf("%d",sizeof(bytes[0])); |
Radoj | 2:e78a5ce9f1d7 | 70 | |
Radoj | 2:e78a5ce9f1d7 | 71 | int n = sizeof(bytes) / sizeof(bytes[0]); |
Radoj | 2:e78a5ce9f1d7 | 72 | ble.gattServer().write(yState.getValueHandle(), (uint8_t *)&bytes, n); |
Radoj | 0:6bd1a61571d0 | 73 | } |
Radoj | 0:6bd1a61571d0 | 74 | |
Radoj | 0:6bd1a61571d0 | 75 | void updateZState(float newState) { |
Radoj | 2:e78a5ce9f1d7 | 76 | //ble.gattServer().write(zState.getValueHandle(), (uint8_t *)&newState, sizeof(newState)); |
Radoj | 2:e78a5ce9f1d7 | 77 | int length = sizeof(float); |
Radoj | 2:e78a5ce9f1d7 | 78 | uint8_t bytes[sizeof(float)]; |
Radoj | 2:e78a5ce9f1d7 | 79 | for(int i = 0; i < length; i++){ |
Radoj | 2:e78a5ce9f1d7 | 80 | bytes[i] = ((uint8_t*)&newState)[i]; |
Radoj | 2:e78a5ce9f1d7 | 81 | //printf("%x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 82 | } |
Radoj | 2:e78a5ce9f1d7 | 83 | //printf("%d",sizeof(bytes[0])); |
Radoj | 2:e78a5ce9f1d7 | 84 | int n = sizeof(bytes) / sizeof(bytes[0]); |
Radoj | 2:e78a5ce9f1d7 | 85 | ble.gattServer().write(zState.getValueHandle(), (uint8_t *)&bytes, n); |
Radoj | 0:6bd1a61571d0 | 86 | } |
Radoj | 0:6bd1a61571d0 | 87 | |
Radoj | 2:e78a5ce9f1d7 | 88 | void updateALLState(float newState,float newStatey,float newStatez) { |
Radoj | 2:e78a5ce9f1d7 | 89 | |
Radoj | 2:e78a5ce9f1d7 | 90 | int length = 14; |
Radoj | 2:e78a5ce9f1d7 | 91 | uint8_t bytes[14]; |
Radoj | 2:e78a5ce9f1d7 | 92 | |
Radoj | 2:e78a5ce9f1d7 | 93 | printf("dane: %f;%f;%f\n",newState, newStatey,newStatez); |
Radoj | 2:e78a5ce9f1d7 | 94 | |
Radoj | 2:e78a5ce9f1d7 | 95 | /* |
Radoj | 2:e78a5ce9f1d7 | 96 | for(int i = 0; i < length; i++){ |
Radoj | 2:e78a5ce9f1d7 | 97 | if(i<=3){ |
Radoj | 2:e78a5ce9f1d7 | 98 | bytes[i] = ((uint8_t*)&newState)[i]; |
Radoj | 2:e78a5ce9f1d7 | 99 | printf("X: %x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 100 | } |
Radoj | 2:e78a5ce9f1d7 | 101 | else if(i==4){ |
Radoj | 2:e78a5ce9f1d7 | 102 | bytes[i] = 0xff; |
Radoj | 2:e78a5ce9f1d7 | 103 | printf("P: %x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 104 | } |
Radoj | 2:e78a5ce9f1d7 | 105 | else if(i>4&&i<=8){ |
Radoj | 2:e78a5ce9f1d7 | 106 | bytes[i] = ((uint8_t*)&newStatey)[i]; |
Radoj | 2:e78a5ce9f1d7 | 107 | printf("Y: %x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 108 | } |
Radoj | 2:e78a5ce9f1d7 | 109 | else if(i==9){ |
Radoj | 2:e78a5ce9f1d7 | 110 | bytes[i] = 0xff; |
Radoj | 2:e78a5ce9f1d7 | 111 | printf("P: %x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 112 | } |
Radoj | 2:e78a5ce9f1d7 | 113 | else if(i>9&&i<14){ |
Radoj | 2:e78a5ce9f1d7 | 114 | bytes[i] = ((uint8_t*)&newStatez)[i]; |
Radoj | 2:e78a5ce9f1d7 | 115 | printf("Z: %x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 116 | } |
Radoj | 2:e78a5ce9f1d7 | 117 | } |
Radoj | 2:e78a5ce9f1d7 | 118 | */ |
Radoj | 2:e78a5ce9f1d7 | 119 | |
Radoj | 2:e78a5ce9f1d7 | 120 | bytes[0] = ((uint8_t*)&newState)[0]; |
Radoj | 2:e78a5ce9f1d7 | 121 | bytes[1] = ((uint8_t*)&newState)[1]; |
Radoj | 2:e78a5ce9f1d7 | 122 | bytes[2] = ((uint8_t*)&newState)[2]; |
Radoj | 2:e78a5ce9f1d7 | 123 | bytes[3] = ((uint8_t*)&newState)[3]; |
Radoj | 2:e78a5ce9f1d7 | 124 | bytes[4] = 0xff; |
Radoj | 2:e78a5ce9f1d7 | 125 | bytes[5] = ((uint8_t*)&newStatey)[0]; |
Radoj | 2:e78a5ce9f1d7 | 126 | bytes[6] = ((uint8_t*)&newStatey)[1]; |
Radoj | 2:e78a5ce9f1d7 | 127 | bytes[7] = ((uint8_t*)&newStatey)[2]; |
Radoj | 2:e78a5ce9f1d7 | 128 | bytes[8] = ((uint8_t*)&newStatey)[3]; |
Radoj | 2:e78a5ce9f1d7 | 129 | bytes[9] = 0xff; |
Radoj | 2:e78a5ce9f1d7 | 130 | bytes[10] = ((uint8_t*)&newStatez)[0]; |
Radoj | 2:e78a5ce9f1d7 | 131 | bytes[11] = ((uint8_t*)&newStatez)[1]; |
Radoj | 2:e78a5ce9f1d7 | 132 | bytes[12] = ((uint8_t*)&newStatez)[2]; |
Radoj | 2:e78a5ce9f1d7 | 133 | bytes[13] = ((uint8_t*)&newStatez)[3]; |
Radoj | 2:e78a5ce9f1d7 | 134 | |
Radoj | 2:e78a5ce9f1d7 | 135 | for(int i=0;i<length;i++){ |
Radoj | 2:e78a5ce9f1d7 | 136 | printf("%x ",bytes[i]); |
Radoj | 2:e78a5ce9f1d7 | 137 | } |
Radoj | 2:e78a5ce9f1d7 | 138 | |
Radoj | 2:e78a5ce9f1d7 | 139 | uint16_t n = sizeof(bytes) / sizeof(bytes[0]); |
Radoj | 2:e78a5ce9f1d7 | 140 | printf("%d",n); |
Radoj | 2:e78a5ce9f1d7 | 141 | |
Radoj | 2:e78a5ce9f1d7 | 142 | ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); |
Radoj | 0:6bd1a61571d0 | 143 | } |
Radoj | 0:6bd1a61571d0 | 144 | |
Radoj | 0:6bd1a61571d0 | 145 | private: |
Radoj | 0:6bd1a61571d0 | 146 | BLE &ble; |
Radoj | 0:6bd1a61571d0 | 147 | ReadOnlyGattCharacteristic<float> xState; |
Radoj | 0:6bd1a61571d0 | 148 | ReadOnlyGattCharacteristic<float> yState; |
Radoj | 0:6bd1a61571d0 | 149 | ReadOnlyGattCharacteristic<float> zState; |
Radoj | 0:6bd1a61571d0 | 150 | ReadOnlyGattCharacteristic<float> allState; |
Radoj | 0:6bd1a61571d0 | 151 | }; |
Radoj | 0:6bd1a61571d0 | 152 | |
Radoj | 0:6bd1a61571d0 | 153 | #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */ |