From 8a402ab6e0c83360e681f3dc156cf04ae6fd8390 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 Mar 2020 21:40:33 +0700 Subject: [PATCH] added gesture sensor, however attr memory is overflow --- cores/nRF5/main.cpp | 2 +- .../src/BLEAdafruitService.h | 1 + .../src/services/BLEAdafruitGesture.cpp | 64 +++++++++++++++++++ .../src/services/BLEAdafruitGesture.h | 39 +++++++++++ .../src/services/BLEAdafruitSensor.cpp | 2 +- .../.feather52840sense.test.only | 0 .../bluefruit_playground.ino | 27 ++++++++ 7 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.cpp create mode 100644 libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.h create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/.feather52840sense.test.only diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index fc190f04..62f3ca30 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -46,7 +46,7 @@ static void loop_task(void* arg) #if CFG_DEBUG // If Serial is not begin(), call it to avoid hard fault - if ( !Serial ) Serial.begin(115200); + Serial.begin(115200); // Wait for Serial connection in debug mode while ( !Serial ) yield(); diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index 1c4f632c..ec04e703 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -35,6 +35,7 @@ #include "services/BLEAdafruitBaro.h" #include "services/BLEAdafruitButton.h" #include "services/BLEAdafruitColor.h" +#include "services/BLEAdafruitGesture.h" #include "services/BLEAdafruitGyro.h" #include "services/BLEAdafruitHumid.h" #include "services/BLEAdafruitLightSensor.h" diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.cpp new file mode 100644 index 00000000..ebac4321 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.cpp @@ -0,0 +1,64 @@ +/* + * 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 + * + * Gesture service 06F00 + * - Button 0F01 | unt8_t | Read + Notify | e.g 0: no gesture, 1: Up, 2: Down, 3: Left, 4: Right + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitGesture::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0F, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitGesture::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0F, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitGesture::BLEAdafruitGesture(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(1); +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.h new file mode 100644 index 00000000..db55bfab --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitGesture.h @@ -0,0 +1,39 @@ +/* + * 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 BLEADAFRUIT_GESTURE_H_ +#define BLEADAFRUIT_GESTURE_H_ + +class BLEAdafruitGesture : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLEAdafruitGesture(void); + + using BLEAdafruitSensor::begin; +}; + +#endif /* BLEADAFRUIT_GESTURE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp index bfc931e0..be3b7a86 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp @@ -42,8 +42,8 @@ err_t BLEAdafruitSensor::_begin(int32_t ms) VERIFY_STATUS( BLEService::begin() ); _measurement.setCccdWriteCallback(sensor_data_cccd_cb, true); - VERIFY_STATUS( _measurement.begin() ); + _measurement.write32(0); // zero 4 bytes could help to init some services _period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); _period.setPermission(SECMODE_OPEN, SECMODE_OPEN); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/.feather52840sense.test.only b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/.feather52840sense.test.only new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino index 24fec34c..c346cf26 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino @@ -85,6 +85,7 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize) BLEAdafruitBaro bleBaro; BLEAdafruitColor bleColor; +BLEAdafruitGesture bleGesture; BLEAdafruitHumid bleHumid; BLEAdafruitProximity bleProximity; BLEAdafruitQuaternion bleQuater; @@ -149,6 +150,28 @@ uint16_t measure_color(uint8_t* buf, uint16_t bufsize) return sizeof(rgb); } + + +void gesture_enable_callback(uint16_t conn_hdl, bool enabled) +{ + (void) conn_hdl; + apds9960.enableProximity(enabled); + apds9960.enableGesture(enabled); +} + +uint16_t measure_gesture(uint8_t* buf, uint16_t bufsize) +{ + uint8_t gesture = apds9960.readGesture(); + if (gesture == 0) return 0; // skip no gesture value + + // APDS9960 sensor position is rotated 90 degree left on CLUE + // We will need to correct that by rotating right for user convenience + uint8_t const clue_rotation[] = { 0, APDS9960_LEFT, APDS9960_RIGHT, APDS9960_DOWN, APDS9960_UP }; + buf[0] = clue_rotation[gesture]; + + return 1; +} + void proximity_enable_callback(uint16_t conn_hdl, bool enabled) { (void) conn_hdl; @@ -350,6 +373,10 @@ void setup() bleColor.begin(measure_color, 100); bleColor.setNotifyCallback(color_enable_callback); + bleGesture.begin(measure_gesture, 10); // sampling is 10 ms + bleGesture.setPeriod(0); // notify on changes only + bleGesture.setNotifyCallback(gesture_enable_callback); + bleHumid.begin(measure_humid, 100); bleLight.begin(measure_light, 100);;