diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index deee3264..a2b34e58 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -32,6 +32,7 @@ #include "services/BLEAdafruitAccel.h" #include "services/BLEAdafruitAddressablePixel.h" +#include "services/BLEAdafruitBaro.h" #include "services/BLEAdafruitButton.h" #include "services/BLEAdafruitGyro.h" #include "services/BLEAdafruitHumid.h" diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.cpp new file mode 100644 index 00000000..dae8a210 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.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 + * + * Barometric service 0800 + * - Pressure 0801 | float[3] | Read + Notify | x, y, z + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitBaro::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x08, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitBaro::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x08, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitBaro::BLEAdafruitBaro(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitBaro::begin (void) +{ + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4); + + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(DEFAULT_PERIOD) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.h new file mode 100644 index 00000000..8dea7266 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitBaro.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_BARO_H_ +#define BLEADAFRUIT_BARO_H_ + +class BLEAdafruitBaro : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + static const int32_t DEFAULT_PERIOD = 1000; + + BLEAdafruitBaro(void); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUIT_BARO_H_ */ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino index 3e802b48..0bd0960b 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino @@ -34,16 +34,13 @@ BLEBas blebas; // battery BLEAdafruitTemperature bleTemp; BLEAdafruitAccel bleAccel; BLEAdafruitLightSensor bleLight; -BLEAdafruitGyro bleGyro; -BLEAdafruitMagnetic bleMagnetic; - BLEAdafruitButton bleButton; BLEAdafruitTone bleTone; BLEAdafruitAddressablePixel blePixel; -#if defined(ARDUINO_NRF52840_CIRCUITPLAY) //------------- Circuit Playground Bluefruit -------------// +#if defined(ARDUINO_NRF52840_CIRCUITPLAY) #include @@ -68,7 +65,7 @@ uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) return 3*sizeof(float); // 12 } -uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) +uint16_t measure_light(uint8_t* buf, uint16_t bufsize) { float lux; lux = CircuitPlayground.lightSensor(); @@ -98,12 +95,14 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize) return 4; } -#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) //------------- CLUE & Feather Sense -------------// +#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) #include #include +#include #include +#include #if defined(ARDUINO_NRF52840_CLUE) #define DEVICE_NAME "CLUE" @@ -113,9 +112,17 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize) #define NEOPIXEL_COUNT 1 -Adafruit_APDS9960 apds9960; -Adafruit_LSM6DS33 lsm6ds33; -Adafruit_BMP280 bmp280; + +BLEAdafruitGyro bleGyro; +BLEAdafruitMagnetic bleMagnetic; +BLEAdafruitHumid bleHumid; +BLEAdafruitBaro bleBaro; + +Adafruit_APDS9960 apds9960; // Proximity, Light, Gesture, Color +Adafruit_LSM6DS33 lsm6ds33; // Gyro and Accel +Adafruit_LIS3MDL lis3mdl; // Magnetometer +Adafruit_BMP280 bmp280; // Temperature, Barometric +Adafruit_SHT31 sht30; // Humid uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize) { @@ -126,21 +133,21 @@ uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize) uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) { - float* accel_buf = (float*) buf; + float* float_buf = (float*) buf; sensors_event_t accel, gyro, temp; (void) gyro; (void) temp; lsm6ds33.getEvent(&accel, &gyro, &temp); - accel_buf[0] = accel.acceleration.x; - accel_buf[1] = accel.acceleration.y; - accel_buf[2] = accel.acceleration.z; + float_buf[0] = accel.acceleration.x; + float_buf[1] = accel.acceleration.y; + float_buf[2] = accel.acceleration.z; - return 3*sizeof(float); // 12 + return 12; } -uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) +uint16_t measure_light(uint8_t* buf, uint16_t bufsize) { float lux; @@ -155,12 +162,32 @@ uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) uint16_t measure_gyro(uint8_t* buf, uint16_t bufsize) { + float* float_buf = (float*) buf; + sensors_event_t accel, gyro, temp; + (void) accel; (void) temp; + + lsm6ds33.getEvent(&accel, &gyro, &temp); + + float_buf[0] = gyro.gyro.x; + float_buf[1] = gyro.gyro.y; + float_buf[2] = gyro.gyro.z; + + return 12; } uint16_t measure_magnetic(uint8_t* buf, uint16_t bufsize) { + float* float_buf = (float*) buf; + sensors_event_t mag; + lis3mdl.getEvent(&mag); + + float_buf[0] = mag.magnetic.x; + float_buf[1] = mag.magnetic.y; + float_buf[2] = mag.magnetic.z; + + return 12; } uint16_t measure_button(uint8_t* buf, uint16_t bufsize) @@ -176,6 +203,20 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize) return 4; } +uint16_t measure_humid(uint8_t* buf, uint16_t bufsize) +{ + float humid = sht30.readHumidity(); + memcpy(buf, &humid, 4); + return 4; +} + +uint16_t measure_baro(uint8_t* buf, uint16_t bufsize) +{ + float baro = bmp280.readPressure()/100; + memcpy(buf, &baro, 4); + return 4; +} + #else #error "Board is not supported" #endif @@ -185,11 +226,6 @@ Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXEL_COUNT, PIN_NEOPIXEL, NEO_GR //------------- Setup -------------// void setup() { - Serial.begin(115200); - - Serial.println("Bluefruit52 BLEUART Example"); - Serial.println("---------------------------\n"); - #if defined ARDUINO_NRF52840_CIRCUITPLAY CircuitPlayground.begin(); @@ -202,17 +238,21 @@ void setup() // Buzzer Speaker pinMode(PIN_BUZZER, OUTPUT); - // Light sensor apds9960.begin(); apds9960.enableColor(true); - // Accelerometer lsm6ds33.begin_I2C(); - - // Pressure + Temperature bmp280.begin(); + lis3mdl.begin_I2C(); + sht30.begin(0x44); #endif + Serial.begin(115200); + while(!Serial) delay(10); // wait for native USB + + Serial.println("Bluefruit Playground Example"); + Serial.println("---------------------------\n"); + // Setup the BLE LED to be enabled on CONNECT // Note: This is actually the default behaviour, but provided // here in case you want to control this LED manually via PIN 19 @@ -252,7 +292,7 @@ void setup() bleAccel.setMeasureCallback(measure_accel); bleLight.begin(); - bleLight.setMeasureCallback(measure_light_sensor); + bleLight.setMeasureCallback(measure_light); bleButton.begin(); bleButton.setMeasureCallback(measure_button); @@ -265,11 +305,17 @@ void setup() // CPB doesn't support these on-board sensor #ifndef ARDUINO_NRF52840_CIRCUITPLAY -// bleGyro.begin(); -// bleGyro.setMeasureCallback(measure_gyro); -// -// bleMagnetic.begin(); -// bleMagnetic.setMeasureCallback(measure_magnetic); + bleGyro.begin(); + bleGyro.setMeasureCallback(measure_gyro); + + bleMagnetic.begin(); + bleMagnetic.setMeasureCallback(measure_magnetic); + + bleHumid.begin(); + bleHumid.setMeasureCallback(measure_humid); + + bleBaro.begin(); + bleBaro.setMeasureCallback(measure_baro); #endif // Set up and start advertising