From 74c91a82157318007b714708587571ac06f48406 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 18 Feb 2020 11:07:47 +0700 Subject: [PATCH] Add Gyro sensor --- .../src/BLEAdafruitService.h | 11 +-- .../src/services/BLEAdafruitAccel.h | 4 +- .../src/services/BLEAdafruitGyro.cpp | 74 +++++++++++++++++++ .../src/services/BLEAdafruitGyro.h | 38 ++++++++++ .../src/services/BLEAdafruitLightSensor.h | 2 - 5 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.cpp create mode 100644 libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.h diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index 0fb56bbe..57867ac9 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -28,13 +28,14 @@ #include "bluefruit_common.h" #include "BLECharacteristic.h" #include "BLEService.h" - #include "services/BLEAdafruitSensor.h" -#include "services/BLEAdafruitTemperature.h" + #include "services/BLEAdafruitAccel.h" -#include "services/BLEAdafruitLightSensor.h" -#include "services/BLEAdafruitButton.h" -#include "services/BLEAdafruitTone.h" #include "services/BLEAdafruitAddressablePixel.h" +#include "services/BLEAdafruitButton.h" +#include "services/BLEAdafruitGyro.h" +#include "services/BLEAdafruitLightSensor.h" +#include "services/BLEAdafruitTemperature.h" +#include "services/BLEAdafruitTone.h" #endif /* BLEADAFRUITSERVICE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h index 45ed51cc..28c3ab65 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h @@ -23,7 +23,7 @@ */ #ifndef BLEADAFRUITACCEL_H_ -#define BLEADAFRUITACCEL_H_ +#define BLEADAFRUITGYRO_H_ class BLEAdafruitAccel : public BLEAdafruitSensor { @@ -31,8 +31,6 @@ class BLEAdafruitAccel : public BLEAdafruitSensor static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; - BLECharacteristic Accel; - BLEAdafruitAccel(void); virtual err_t begin(void); }; diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.cpp new file mode 100644 index 00000000..9172a6c3 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.cpp @@ -0,0 +1,74 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "BLEAdafruitService.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + + +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 + * + * Shared Characteristics + * - Measurement Period 0001 | int32_t | Read + Write | + * ms between measurements, -1: stop reading, 0: update when changes + * + * Accelerometer service 0400 + * - Accel 0401 | float[3] | Read + Notify | x, y, z + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitGyro::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x04, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitGyro::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x04, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitGyro::BLEAdafruitGyro(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitGyro::begin (void) +{ + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4*3); // float[3] + + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.h new file mode 100644 index 00000000..9d8f1d24 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitGyro.h @@ -0,0 +1,38 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef BLEADAFRUITACCEL_H_ +#define BLEADAFRUITGYRO_H_ + +class BLEAdafruitGyro : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLEAdafruitGyro(void); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUITACCEL_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h index bcf4200d..cba64da0 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h @@ -31,8 +31,6 @@ class BLEAdafruitLightSensor : public BLEAdafruitSensor static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; - BLECharacteristic Lux; - BLEAdafruitLightSensor(void); virtual err_t begin(void); };