Frank Duignan / Mbed 2 deprecated CJMCU-8223-Oled

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers accelService.h Source File

accelService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_ACCEL_SERVICE_H__
00018 #define __BLE_ACCEL_SERVICE_H__
00019 #include <mbed.h>
00020 #include <lis3dh.h>
00021 lis3dh Lis3dh;
00022 class accelService {
00023 public:
00024     const static uint16_t ACCEL_SERVICE_UUID = 0xA012;
00025     const static uint16_t ACCEL_X_CHARACTERISTIC_UUID = 0xA013;
00026     const static uint16_t ACCEL_Y_CHARACTERISTIC_UUID = 0xA014;
00027     const static uint16_t ACCEL_Z_CHARACTERISTIC_UUID = 0xA015;
00028 
00029     accelService(BLEDevice &_ble, int16_t initialValueForACCELCharacteristic) :
00030         ble(_ble), AccelX(ACCEL_X_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelY(ACCEL_Y_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelZ(ACCEL_Z_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic)
00031     {
00032         GattCharacteristic *charTable[] = {&AccelX,&AccelY,&AccelZ};
00033         GattService         AccelService(ACCEL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00034         ble.addService(AccelService);
00035         Lis3dh.begin();
00036     }
00037 
00038     GattAttribute::Handle_t getValueHandle() const {
00039         return AccelX.getValueHandle();
00040     }
00041     void updateAccelX(uint16_t newValue) {
00042         ble.gattServer().write(AccelX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
00043     }
00044     void updateAccelY(uint16_t newValue) {
00045         ble.gattServer().write(AccelY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
00046     }
00047     void updateAccelZ(uint16_t newValue) {
00048         ble.gattServer().write(AccelZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
00049     }
00050     void poll()
00051     {                
00052         int X,Y,Z;
00053         Lis3dh.read(X,Y,Z);        
00054         updateAccelX(X);
00055         updateAccelY(Y);
00056         updateAccelZ(Z);        
00057         
00058     }
00059 private:
00060     BLEDevice &ble;
00061     ReadOnlyGattCharacteristic<int16_t>  AccelX;
00062     ReadOnlyGattCharacteristic<int16_t>  AccelY;
00063     ReadOnlyGattCharacteristic<int16_t>  AccelZ;
00064 };
00065 
00066 #endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */