Felix Rüdiger / Mbed 2 deprecated BLE_Nano_MPU6050Service

Dependencies:   BLE_API mbed nRF51822 MPU6050_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PowerService.h Source File

PowerService.h

00001 #pragma once
00002 #ifndef __POWER_SERVICE_H__
00003 #define __POWER_SERVICE_H__
00004 
00005 #include "BLE.h"
00006 
00007 class PowerService
00008 {
00009     public:
00010         enum
00011         {
00012             UUID_POWER_SERVICE          = 0xA300,
00013             UUID_POWER_VOLTAGE_CHAR     = 0xA301,
00014             UUID_POWER_CURRENT_CHAR     = 0xA302,
00015             UUID_POWER_DISCHARGE_CHAR   = 0xA303
00016         };
00017     
00018         PowerService
00019         (
00020             BLE    &ble,
00021             float   initialVoltage = 0.0f,
00022             float   initialCurrent = 0.0f,
00023             float   initialDischarge = 0.0f
00024         ) :
00025             gattServer(ble.gattServer()),
00026             voltageCharacteristic
00027             (
00028                 UUID_POWER_VOLTAGE_CHAR,
00029                 (float*)&initialVoltage,
00030                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
00031             ),
00032             currentCharacteristic
00033             (
00034                 UUID_POWER_CURRENT_CHAR,
00035                 (float*)&initialCurrent,
00036                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
00037             ),
00038             dischargeCharacteristic
00039             (
00040                 UUID_POWER_DISCHARGE_CHAR,
00041                 (float*)&initialDischarge,
00042                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
00043             )
00044         {
00045             static bool serviceAdded = false;
00046             if (serviceAdded)
00047                 return;
00048                 
00049             GattCharacteristic *charTable[] =
00050             {
00051                 &voltageCharacteristic,
00052                 &currentCharacteristic,
00053                 &dischargeCharacteristic
00054             };
00055             
00056             GattService powerService(UUID_POWER_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic*));
00057             
00058             gattServer.addService(powerService);
00059             
00060             serviceAdded = true;
00061         }
00062         
00063         void updateVoltage(float voltage)
00064         {
00065             gattServer.write(voltageCharacteristic.getValueHandle(), (uint8_t*)&voltage, sizeof(float));
00066         }
00067         
00068         void updateCurrent(float current)
00069         {
00070             gattServer.write(currentCharacteristic.getValueHandle(), (uint8_t*)&current, sizeof(float));
00071         }
00072         
00073         void updateDischarge(float discharge)
00074         {
00075             gattServer.write(dischargeCharacteristic.getValueHandle(), (uint8_t*)&discharge, sizeof(float));
00076         }
00077     
00078     private:
00079         GattServer                         &gattServer;
00080         ReadOnlyGattCharacteristic<float>   voltageCharacteristic;
00081         ReadOnlyGattCharacteristic<float>   currentCharacteristic;
00082         ReadOnlyGattCharacteristic<float>   dischargeCharacteristic;
00083 };
00084 
00085 #endif