Let me know what you think

Dependencies:   BLE_API mbed nRF51822

Fork of SDP_Version3_Abdul by Michael Galis

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AcclerationService.h Source File

AcclerationService.h

00001 /* Senior Project Bluetooth bicycle speedometer
00002 Author: Michael Galis
00003 This header file describes the Acceleration Service that I created to be able
00004 to send the x, y, and z accelerations as the bicycle is moving. These 
00005 accelerations will be used by the phone to calculate the speed of the bicycle.
00006 */
00007 
00008 #ifndef __BLE_SERVICE_H__
00009 #define __BLE_SERVICE_H__
00010 
00011 class AccelerationService {
00012 public:
00013     const static uint16_t ACCELERATION_SERVICE_UUID = 0xA010;
00014     const static uint16_t X_CHARACTERISTIC_UUID     = 0xA011;
00015     const static uint16_t Y_CHARACTERISTIC_UUID     = 0xA012;
00016     const static uint16_t Z_CHARACTERISTIC_UUID     = 0xA013;
00017     const static uint16_t ALL_CHARACTERISTIC_UUID   = 0x0014;
00018 
00019     AccelerationService(BLE &_ble) :
00020         ble(_ble), 
00021         xData(X_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00022         yData(Y_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00023         zData(Z_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00024         allState(ALL_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00025     {
00026         GattCharacteristic *charTable[] = {&xData,&yData, &zData, &allState};
00027         GattService         accelerationService(AccelerationService::ACCELERATION_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00028         ble.gattServer().addService(accelerationService);
00029     }
00030 
00031     void updateXData(float newData) 
00032     {
00033         int length = sizeof(float);
00034         uint8_t bytes[sizeof(float)]; 
00035         for(int i = 0; i < length; i++)
00036         {
00037             bytes[i] = ((uint8_t*)&newData)[i];
00038         }
00039         int n = sizeof(bytes); 
00040         ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&bytes, n);
00041         //int n = sizeof(uint8_t); 
00042         //ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&newData, n); 
00043     }
00044     
00045     void updateYData(float newData)
00046     {
00047         int length = sizeof(float);
00048         uint8_t bytes[sizeof(float)];
00049         for(int i = 0; i < length; i++)
00050         {
00051             bytes[i] = ((uint8_t*)&newData)[i];
00052         } 
00053         int n = sizeof(bytes);
00054         ble.gattServer().write(yData.getValueHandle(), (uint8_t *)&bytes, n); 
00055     }
00056     
00057     void updateZData(float newData) 
00058     {
00059         int length = sizeof(float);
00060         uint8_t bytes[sizeof(float)];
00061         for(int i = 0; i < length; i++)
00062         {
00063             bytes[i] = ((uint8_t*)&newData)[i];
00064         }
00065         int n = sizeof(bytes);
00066         ble.gattServer().write(zData.getValueHandle(), (uint8_t *)&bytes, n); 
00067     }
00068     
00069     void updateALLState(float newState,float newStatey,float newStatez) 
00070     {
00071         
00072         int length = 14;
00073         uint8_t bytes[length]; 
00074         
00075         bytes[0] = ((uint8_t*)&newState)[0];
00076         bytes[1] = ((uint8_t*)&newState)[1];
00077         bytes[2] = ((uint8_t*)&newState)[2];
00078         bytes[3] = ((uint8_t*)&newState)[3];
00079         bytes[4] = 0xff;
00080         bytes[5] = ((uint8_t*)&newStatey)[0];
00081         bytes[6] = ((uint8_t*)&newStatey)[1];
00082         bytes[7] = ((uint8_t*)&newStatey)[2];
00083         bytes[8] = ((uint8_t*)&newStatey)[3];
00084         bytes[9] = 0xff;
00085         bytes[10] = ((uint8_t*)&newStatez)[0];
00086         bytes[11] = ((uint8_t*)&newStatez)[1];
00087         bytes[12] = ((uint8_t*)&newStatez)[2];
00088         bytes[13] = ((uint8_t*)&newStatez)[3];
00089              
00090         uint16_t n = sizeof(bytes) / sizeof(bytes[0]);
00091         ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
00092     }
00093 
00094 private:
00095     BLE                              &ble;
00096     ReadOnlyGattCharacteristic<float>  xData;
00097     ReadOnlyGattCharacteristic<float>  yData;
00098     ReadOnlyGattCharacteristic<float>  zData;
00099     ReadOnlyArrayGattCharacteristic<uint8_t, 14> allState;
00100 };
00101 
00102 #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */