javier cifuentes / Mbed 2 deprecated IMOB_with_encryption

Dependencies:   AccelSensor BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AccelSensorService.h Source File

AccelSensorService.h

00001 #ifndef __BLE_ACCEL_SENSOR_SERVICE_H__
00002 #define __BLE_ACCEL_SENSOR_SERVICE_H__
00003 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 #include "ble/Gap.h"
00007 #include "AccelSensor/AccelSensor.h"
00008 
00009 #define ACCEL_DETECTION_THRESHOLD 85
00010 
00011 class AccelSensorService {
00012 public:
00013     const static uint16_t ACCEL_SENSOR_SERVICE_UUID = 0xD000;
00014     const static uint16_t ACCEL_DETECTION_UUID = 0xD001;
00015 
00016     AccelSensorService(BLEDevice &_ble) : 
00017         ble(_ble), 
00018         accelerometer(P0_22, P0_20),// waveshare //accelerometer(P0_24, P0_21),// nuevo
00019         accelDetection(0),
00020         AccelDetectionCharacteristic(ACCEL_DETECTION_UUID, &accelDetection, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00021     {
00022         GattCharacteristic *charTable[] = {&AccelDetectionCharacteristic};
00023         GattService accelSensorService(ACCEL_SENSOR_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00024 
00025         ble.addService(accelSensorService);
00026         
00027         accelerometer.init();
00028         updateAccel();       
00029     }
00030             
00031     void updateAccel() {
00032         int aux_accel[3];
00033         accelerometer.readData(aux_accel);
00034         
00035         for(int i = 0; i < 3; i++)
00036             accel[i] = (uint8_t)aux_accel[i];
00037     }
00038     
00039     bool updateAccelDetection() {
00040         uint8_t passAccel[3] = {accel[0], accel[1], accel[2]};      
00041         updateAccel();
00042         if( abs(accel[0] - passAccel[0]) > ACCEL_DETECTION_THRESHOLD || abs(accel[1] - passAccel[1]) > ACCEL_DETECTION_THRESHOLD || abs(accel[2] - passAccel[2]) > ACCEL_DETECTION_THRESHOLD )
00043         {
00044             accelDetection = 1;
00045             ble.gattServer().write(AccelDetectionCharacteristic.getValueHandle(), &accelDetection, 1);            
00046         }
00047         else
00048         {
00049             accelDetection = 0;
00050             ble.gattServer().write(AccelDetectionCharacteristic.getValueHandle(), &accelDetection, 1);        
00051         }
00052         
00053         return ((accelDetection > 0) ? true: false);
00054     }
00055 
00056 private:
00057     BLEDevice &ble;
00058     
00059     AccelSensor accelerometer;
00060     
00061     uint8_t accel[3];
00062     uint8_t accelDetection;
00063     
00064     ReadOnlyGattCharacteristic < uint8_t > AccelDetectionCharacteristic;    
00065 };
00066 
00067 #endif /* #ifndef __BLE_ACCEL_SENSOR_SERVICE_H__ */