diff --git a/.travis.yml b/.travis.yml index b864978e..2a3fcb26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,7 +56,7 @@ install: - BSP_VERSION=`eval ls $BSP_PATH` - rm -r $BSP_PATH/* - ln -s $TRAVIS_BUILD_DIR $BSP_PATH/$BSP_VERSION - - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library" + - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library","Adafruit Circuit Playground" before_script: diff --git a/boards.txt b/boards.txt index 444b3aee..888d77c0 100644 --- a/boards.txt +++ b/boards.txt @@ -32,7 +32,7 @@ feather52832.upload.use_1200bps_touch=false feather52832.upload.wait_for_upload_port=false feather52832.upload.native_usb=false feather52832.upload.maximum_size=290816 -feather52832.upload.maximum_data_size=51072 +feather52832.upload.maximum_data_size=52224 # Build feather52832.build.mcu=cortex-m4 @@ -81,7 +81,7 @@ feather52840.upload.protocol=nrfutil feather52840.upload.use_1200bps_touch=true feather52840.upload.wait_for_upload_port=true feather52840.upload.maximum_size=815104 -feather52840.upload.maximum_data_size=248832 +feather52840.upload.maximum_data_size=237568 # Build feather52840.build.mcu=cortex-m4 @@ -135,7 +135,7 @@ itsybitsy52840.upload.protocol=nrfutil itsybitsy52840.upload.use_1200bps_touch=true itsybitsy52840.upload.wait_for_upload_port=true itsybitsy52840.upload.maximum_size=815104 -itsybitsy52840.upload.maximum_data_size=248832 +itsybitsy52840.upload.maximum_data_size=237568 # Build itsybitsy52840.build.mcu=cortex-m4 @@ -188,7 +188,7 @@ cplaynrf52840.upload.protocol=nrfutil cplaynrf52840.upload.use_1200bps_touch=true cplaynrf52840.upload.wait_for_upload_port=true cplaynrf52840.upload.maximum_size=815104 -cplaynrf52840.upload.maximum_data_size=248832 +cplaynrf52840.upload.maximum_data_size=237568 # Build cplaynrf52840.build.mcu=cortex-m4 @@ -295,7 +295,7 @@ metro52840.upload.protocol=nrfutil metro52840.upload.use_1200bps_touch=true metro52840.upload.wait_for_upload_port=true metro52840.upload.maximum_size=815104 -metro52840.upload.maximum_data_size=248832 +metro52840.upload.maximum_data_size=237568 # Build metro52840.build.mcu=cortex-m4 @@ -338,7 +338,7 @@ pca10056.upload.protocol=nrfutil pca10056.upload.use_1200bps_touch=true pca10056.upload.wait_for_upload_port=true pca10056.upload.maximum_size=815104 -pca10056.upload.maximum_data_size=248832 +pca10056.upload.maximum_data_size=237568 # Build pca10056.build.mcu=cortex-m4 diff --git a/cores/nRF5/freertos/config/FreeRTOSConfig.h b/cores/nRF5/freertos/config/FreeRTOSConfig.h index c080ec2d..7b03755a 100644 --- a/cores/nRF5/freertos/config/FreeRTOSConfig.h +++ b/cores/nRF5/freertos/config/FreeRTOSConfig.h @@ -91,7 +91,7 @@ #define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY ( 2 ) // Normal #define configTIMER_QUEUE_LENGTH 32 -#define configTIMER_TASK_STACK_DEPTH ( 100 ) +#define configTIMER_TASK_STACK_DEPTH ( 256 ) /* Tickless Idle configuration. */ #define configEXPECTED_IDLE_TIME_BEFORE_SLEEP 2 diff --git a/cores/nRF5/linker/nrf52840_s140_v6.ld b/cores/nRF5/linker/nrf52840_s140_v6.ld index dc718462..d676b605 100755 --- a/cores/nRF5/linker/nrf52840_s140_v6.ld +++ b/cores/nRF5/linker/nrf52840_s140_v6.ld @@ -8,13 +8,13 @@ MEMORY FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xED000 - 0x26000 /* SRAM required by S132 depend on - * - Attribute Table Size + * - Attribute Table Size (Number of Services and Characteristics) * - Vendor UUID count * - Max ATT MTU * - Concurrent connection peripheral + central + secure links * - Event Len, HVN queue, Write CMD queue */ - RAM (rwx) : ORIGIN = 0x20003400, LENGTH = 0x20040000 - 0x20003400 + RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000 } SECTIONS diff --git a/cores/nRF5/utility/AdaCallback.c b/cores/nRF5/utility/AdaCallback.c index 64e51b70..3fb9aa0d 100644 --- a/cores/nRF5/utility/AdaCallback.c +++ b/cores/nRF5/utility/AdaCallback.c @@ -73,15 +73,9 @@ void adafruit_callback_task(void* arg) } } -//! Test if in interrupt mode -static inline bool is_isr(void) -{ - return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; -} - void ada_callback_queue(ada_callback_t* cb_item) { - BaseType_t ret = is_isr() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); + BaseType_t ret = isInISR() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); if ( ret != pdTRUE ) { @@ -91,7 +85,7 @@ void ada_callback_queue(ada_callback_t* cb_item) _cb_qdepth = 2*_cb_qdepth; // try again - if ( is_isr() ) + if ( isInISR() ) { xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL); }else diff --git a/cores/nRF5/utility/SoftwareTimer.cpp b/cores/nRF5/utility/SoftwareTimer.cpp new file mode 100644 index 00000000..84c64c0e --- /dev/null +++ b/cores/nRF5/utility/SoftwareTimer.cpp @@ -0,0 +1,111 @@ +/* + * 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 "SoftwareTimer.h" + +SoftwareTimer::SoftwareTimer() +{ + _handle = NULL; +} + +SoftwareTimer::~SoftwareTimer () +{ + if ( _handle != NULL ) xTimerDelete(_handle, 0); +} + +void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID, bool repeating) +{ + _handle = xTimerCreate(NULL, ms2tick(ms), repeating, timerID, callback); +} + +bool SoftwareTimer::start(void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + }else + { + xTimerStart(_handle, 0); + } + return ret; +} + +bool SoftwareTimer::stop (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerStop (_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::reset (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerReset(_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::setPeriod(uint32_t ms) +{ + BaseType_t active = xTimerIsTimerActive(_handle); + bool ret = true; + + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerChangePeriodFromISR(_handle, ms2tick(ms), &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerChangePeriod(_handle, ms2tick(ms), 0); + } + + // Change period of inactive timer will also start it !! + if ( !active ) stop(); + + return ret; +} diff --git a/cores/nRF5/utility/SoftwareTimer.h b/cores/nRF5/utility/SoftwareTimer.h index e39b7301..24453c49 100644 --- a/cores/nRF5/utility/SoftwareTimer.h +++ b/cores/nRF5/utility/SoftwareTimer.h @@ -44,52 +44,16 @@ class SoftwareTimer TimerHandle_t _handle; public: - SoftwareTimer() { _handle = NULL; } - virtual ~SoftwareTimer() { if(_handle != NULL) xTimerDelete(_handle, 0); } + SoftwareTimer(); + virtual ~SoftwareTimer(); - void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true) - { - _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); - } + void begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID = NULL, bool repeating = true); + TimerHandle_t getHandle(void) { return _handle; } - TimerHandle_t getHandle(void) - { - return _handle; - } - - void start(void) { xTimerStart(_handle, 0); } - void stop (void) { xTimerStop (_handle, 0); } - void reset(void) { xTimerReset(_handle, 0); } - - bool startFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool stopFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool resetFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - void setPeriod(uint32_t ms) - { - BaseType_t active = xTimerIsTimerActive(_handle); - xTimerChangePeriod(_handle, ms2tick(ms), 0); - - // Change period of inactive timer will also start it !! - if ( !active ) xTimerStop(_handle, 0); - } + bool start(void); + bool stop (void); + bool reset (void); + bool setPeriod(uint32_t ms); }; #endif /* SOFTWARETIMER_H_ */ diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index 40bbd734..a557f547 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -36,6 +36,12 @@ void enterUf2Dfu(void); void waitForEvent(void); void systemOff(uint32_t pin, uint8_t wake_logic); +// Test if in interrupt mode +static inline bool isInISR(void) +{ + return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; +} + #ifdef __cplusplus } #endif diff --git a/libraries/BLEAdafruitService/library.properties b/libraries/BLEAdafruitService/library.properties new file mode 100644 index 00000000..9fb87d11 --- /dev/null +++ b/libraries/BLEAdafruitService/library.properties @@ -0,0 +1,10 @@ +name=Adafruit BLE Services +version=1.0 +author=Adafruit +maintainer=Adafruit +sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules +paragraph=Arduino library for nRF52-based Adafruit Bluefruit LE modules +category=Communication +url=https://github.com/adafruit/Adafruit_nRF52_Arduino +architectures=* +includes=BLEAdafruitService.h diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h new file mode 100644 index 00000000..0fb56bbe --- /dev/null +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -0,0 +1,40 @@ +/* + * 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 BLEADAFRUITSERVICE_H_ +#define BLEADAFRUITSERVICE_H_ + +#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" + +#endif /* BLEADAFRUITSERVICE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp new file mode 100644 index 00000000..044163b8 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.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 0200 + * - Accel 0201 | float[3] | Read + Notify | x, y, z + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitAccel::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x02, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x02, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitAccel::BLEAdafruitAccel(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitAccel::begin (void) +{ + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4*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/BLEAdafruitAccel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h new file mode 100644 index 00000000..45ed51cc --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h @@ -0,0 +1,40 @@ +/* + * 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 BLEADAFRUITACCEL_H_ + +class BLEAdafruitAccel : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLECharacteristic Accel; + + BLEAdafruitAccel(void); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUITACCEL_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp new file mode 100644 index 00000000..4501f546 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp @@ -0,0 +1,153 @@ +/* + * 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 "bluefruit_common.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +#include "BLEAdafruitAddressablePixel.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 + * + * Adafruit NeoPixel Service 0900 + * - Pin 0901 | uint8_t | Read + Write | + * - Type 0902 | uint8_t | Read + Write | 0: neopixel, 1: dotstar + * - Data 0903 | { uint16_t start, uint8_t flags, data } + * - Count + */ + +const uint8_t BLEAdafruitAddressablePixel::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x09, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_PIN[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x09, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_TYPE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x09, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x09, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_BUFSIZE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x04, 0x09, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitAddressablePixel::BLEAdafruitAddressablePixel(void) + : BLEService(UUID128_SERVICE), _pin(UUID128_CHR_PIN), _type(UUID128_CHR_TYPE), + _data(UUID128_CHR_DATA), _bufsize(UUID128_CHR_BUFSIZE) +{ + _neo = NULL; +} + +err_t BLEAdafruitAddressablePixel::begin(Adafruit_NeoPixel* neo_pixel) +{ + _neo = neo_pixel; + uint16_t bufsize = _neo->numPixels()*3; // TODO actual bufsize for 3 RGB or 4 RGBW later + return begin((uint8_t) _neo->getPin(), 0, bufsize); // TODO dotstart support +} + +err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t bufsize) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Characteristic + _pin.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _pin.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _pin.setFixedLen(1); + VERIFY_STATUS( _pin.begin() ); + _pin.write8(pin); + + // Add Characteristic + _type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _type.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _type.setFixedLen(1); + VERIFY_STATUS( _type.begin() ); + _type.write8(type); + + // Add Characteristic + _data.setProperties(CHR_PROPS_WRITE); + _data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); + // Change to use VLOC STACK to USER due to lack of memroy + // Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH)); + _data.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX); + VERIFY_STATUS( _data.begin() ); + + // Add Characteristic + _bufsize.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _bufsize.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _bufsize.setFixedLen(2); + VERIFY_STATUS( _bufsize.begin() ); + _bufsize.write16(bufsize); + + _data.setWriteCallback(pixel_data_write_cb, true); + + return ERROR_NONE; +} + +//--------------------------------------------------------------------+ +// Static callbacks +//--------------------------------------------------------------------+ +void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + (void) conn_hdl; + + if (len < 3) return; + + BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService(); + + uint16_t index; + memcpy(&index, data, 2); + uint8_t flag = data[2]; + + uint8_t* buffer = svc._neo->getPixels() + index; + + memcpy(buffer, data+3, len-3); + + // show flag + if ( flag & 0x01 ) + { + svc._neo->show(); + } +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h new file mode 100644 index 00000000..ec666933 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h @@ -0,0 +1,57 @@ +/* + * 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 BLEADAFRUITADDRESSABLEPIXEL_H_ +#define BLEADAFRUITADDRESSABLEPIXEL_H_ + +#include "Adafruit_NeoPixel.h" + +class BLEAdafruitAddressablePixel : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_PIN[16]; + static const uint8_t UUID128_CHR_TYPE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + static const uint8_t UUID128_CHR_BUFSIZE[16]; + + BLEAdafruitAddressablePixel(void); + virtual err_t begin(Adafruit_NeoPixel* neo); + + private: + BLECharacteristic _pin; + BLECharacteristic _type; + BLECharacteristic _data; + BLECharacteristic _bufsize; + + Adafruit_NeoPixel* _neo; + + err_t begin(uint8_t pin, uint8_t type, uint16_t bufsize); + + static void pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); +}; + + + +#endif /* BLEADAFRUITADDRESSABLEPIXEL_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp new file mode 100644 index 00000000..11ff0c19 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp @@ -0,0 +1,73 @@ +/* + * 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 + * + * Board Button service 0600 + * - Button 0601 | uint32_t | Read + Notify | e.g slide (b0), button A (b1), button B (b2) + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitButton::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x06, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitButton::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x06, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitButton::BLEAdafruitButton(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ +} + +err_t BLEAdafruitButton::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(100) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h new file mode 100644 index 00000000..2bdc40e3 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.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 BLE_ADAFRUIT_BUTTON_H_ +#define BLE_ADAFRUIT_BUTTON_H_ + +class BLEAdafruitButton : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLEAdafruitButton(void); + virtual err_t begin(void); +}; + +#endif /* BLE_ADAFRUIT_BUTTON_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp new file mode 100644 index 00000000..1238e194 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.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 + * + * Light Sensor service 0300 + * - Lux 0301 | float | Read + Notify | + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitLightSensor::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x03, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitLightSensor::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x03, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitLightSensor::BLEAdafruitLightSensor(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitLightSensor::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(1000) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h new file mode 100644 index 00000000..bcf4200d --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h @@ -0,0 +1,40 @@ +/* + * 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 BLE_ADAFRUIT_LIGHT_SENSOR_H_ +#define BLE_ADAFRUIT_LIGHT_SENSOR_H_ + +class BLEAdafruitLightSensor : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLECharacteristic Lux; + + BLEAdafruitLightSensor(void); + virtual err_t begin(void); +}; + +#endif /* BLE_ADAFRUIT_LIGHT_SENSOR_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp new file mode 100644 index 00000000..bb54e242 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp @@ -0,0 +1,160 @@ +/* + * 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 +//--------------------------------------------------------------------+ + +BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid) + : BLEService(service_uuid), _measurement(data_uuid), _period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +{ + _measure_cb = NULL; +} + +void BLEAdafruitSensor::setMeasureCallback(measure_callback_t fp) +{ + _measure_cb = fp; +} + +err_t BLEAdafruitSensor::begin(int32_t ms) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + _measurement.setCccdWriteCallback(sensor_data_cccd_cb, true); + + VERIFY_STATUS( _measurement.begin() ); + + _period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _period.setFixedLen(4); + VERIFY_STATUS( _period.begin() ); + _period.write32(ms); + + _period.setWriteCallback(sensor_period_write_cb, true); + + // setup timer + _timer.begin(ms, sensor_timer_cb, this, true); + + return ERROR_NONE; +} + +void BLEAdafruitSensor::setPeriod(int32_t period_ms) +{ + _period.write32(period_ms); + _update_timer(period_ms); +} + + +void BLEAdafruitSensor::startMeasuring(void) +{ + _timer.start(); +} + +void BLEAdafruitSensor::stopMeasuring(void) +{ + _timer.stop(); +} + + +void BLEAdafruitSensor::_update_timer(int32_t ms) +{ + // TODO handle period = 0 which notify on changes ASAP + if ( ms < 0 ) + { + _timer.stop(); + }else if ( ms > 0) + { + _timer.setPeriod(ms); + }else + { + // Period = 0: keeping the current interval, but report on changes only + } +} + +void BLEAdafruitSensor::_timer_callback(void) +{ + if (_measure_cb) + { + uint8_t buf[_measurement.getMaxLen()]; + uint16_t len = _measure_cb(buf, sizeof(buf)); + len = min(len, sizeof(buf)); + + // Period = 0, compare with old data, only update on changes + if ( 0 == _period.read32() ) + { + uint8_t prev_buf[_measurement.getMaxLen()]; + _measurement.read(prev_buf, sizeof(prev_buf)); + + // skip notify if there is no changes + if ( 0 == memcmp(prev_buf, buf, len) ) return; + } + + _measurement.notify(buf, len); + } +} + +//--------------------------------------------------------------------+ +// Static Callbacks +//--------------------------------------------------------------------+ + +void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer) +{ + BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID(xTimer); + svc->_timer_callback(); +} + +// Client update period, adjust timer accordingly +void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + BLEAdafruitSensor& svc = (BLEAdafruitSensor&) chr->parentService(); + + int32_t ms = 0; + memcpy(&ms, data, len); + + svc._update_timer(ms); +} + +void BLEAdafruitSensor::sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value) +{ + BLEAdafruitSensor& svc = (BLEAdafruitSensor&) chr->parentService(); + + // notify enabled + if (value & BLE_GATT_HVX_NOTIFICATION) + { + svc._timer.start(); + }else + { + svc._timer.stop(); + } + + // send initial notification if period = 0 +// if ( 0 == svc._period.read32() ) +// { +// svc._measurement.notify(); +// } +} + diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h new file mode 100644 index 00000000..fe5c5e4f --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h @@ -0,0 +1,57 @@ +/* + * 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 BLE_ADAFRUIT_SENSOR_H_ +#define BLE_ADAFRUIT_SENSOR_H_ + +class BLEAdafruitSensor : public BLEService +{ + public: + typedef uint16_t (*measure_callback_t )(uint8_t* buf, uint16_t bufsize); + + BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid); + virtual err_t begin(int32_t ms); + + void setMeasureCallback(measure_callback_t fp); + void setPeriod(int32_t period_ms); + + void startMeasuring(void); + void stopMeasuring(void); + + protected: + BLECharacteristic _period; + BLECharacteristic _measurement; + + measure_callback_t _measure_cb; + SoftwareTimer _timer; + + void _update_timer(int32_t ms); + void _timer_callback(void); + + static void sensor_timer_cb(TimerHandle_t xTimer); + static void sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); + static void sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value); +}; + +#endif /* BLE_ADAFRUIT_SENSOR_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp new file mode 100644 index 00000000..f9667152 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp @@ -0,0 +1,73 @@ +/* + * 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 + * + * Temperature service 0100 + * - Temperature 0101 | float | Read + Notify | degree in Celsius + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitTemperature::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x01, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitTemperature::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x01, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitTemperature::BLEAdafruitTemperature(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitTemperature::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(1000) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h new file mode 100644 index 00000000..7ab8b837 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.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 BLEADAFRUITTEMPERATURE_H_ +#define BLEADAFRUITTEMPERATURE_H_ + +class BLEAdafruitTemperature : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLEAdafruitTemperature(); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUITTEMPERATURE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp new file mode 100644 index 00000000..45407d01 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp @@ -0,0 +1,89 @@ +/* + * 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 +//--------------------------------------------------------------------+ + +const uint8_t BLEAdafruitTone::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0C, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitTone::UUID128_CHR_TONE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0C, 0xAF, 0xAD +}; + +BLEAdafruitTone::BLEAdafruitTone(void) + : BLEService(UUID128_SERVICE), _tone(UUID128_CHR_TONE) +{ + _pin = -1; +} + +err_t BLEAdafruitTone::begin(int pin) +{ + _pin = pin; + + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Setup Characteristic + _tone.setProperties(CHR_PROPS_WRITE); + _tone.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); + _tone.setFixedLen(6); // uint16_t freq, uint32_t duration + _tone.setWriteCallback(tone_write_cb, true); + VERIFY_STATUS( _tone.begin() ); + + return ERROR_NONE; +} + +void BLEAdafruitTone::tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + BLEAdafruitTone& svc = (BLEAdafruitTone&) chr->parentService(); + + struct ATTR_PACKED { + uint16_t freq; + uint32_t duration; + } tone_data; + + // invalid length + if (len != sizeof(tone_data)) return; + + // extra data + memcpy(&tone_data, data, len); + + // frequency = 0 means no tone + if (tone_data.freq == 0) + { + noTone(svc._pin); + }else + { + tone(svc._pin, tone_data.freq, tone_data.duration); + } +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h new file mode 100644 index 00000000..4ea3b2a4 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h @@ -0,0 +1,44 @@ +/* + * 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 BLEADAFRUITTONE_H_ +#define BLEADAFRUITTONE_H_ + +class BLEAdafruitTone : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_TONE[16]; + + BLEAdafruitTone(void); + virtual err_t begin(int pin); + + private: + int _pin; + BLECharacteristic _tone; + + static void tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); +}; + +#endif /* BLEADAFRUITTONE_H_ */ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.build.cplaynrf52840 b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.build.cplaynrf52840 new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino new file mode 100644 index 00000000..268704b3 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -0,0 +1,212 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include +#include +#include +#include +#include + +// BLE Service +BLEDfu bledfu; // OTA DFU service +BLEDis bledis; // device information +BLEUart bleuart; // uart over ble +BLEBas blebas; // battery + +// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8 +BLEAdafruitTemperature bleTemp; +BLEAdafruitAccel bleAccel; +BLEAdafruitLightSensor bleLight; +BLEAdafruitButton bleButton; +BLEAdafruitTone bleTone; + +BLEAdafruitAddressablePixel blePixel; + +Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, CPLAY_NEOPIXELPIN, NEO_GRB + NEO_KHZ800); + +uint16_t measure_button(uint8_t* buf, uint16_t bufsize) +{ + uint32_t button = 0; + + button |= ( CircuitPlayground.slideSwitch() ? 0x01 : 0x00 ); + button |= ( CircuitPlayground.leftButton() ? 0x02 : 0x00 ); + button |= ( CircuitPlayground.rightButton() ? 0x04 : 0x00 ); + + memcpy(buf, &button, 4); + return 4; +} + +uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize) +{ + float temp = CircuitPlayground.temperature(); + memcpy(buf, &temp, 4); + return 4; +} + +uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) +{ + float lux = CircuitPlayground.lightSensor(); + memcpy(buf, &lux, 4); + return 4; +} + +uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) +{ + float accel[3]; + accel[0] = CircuitPlayground.motionX(); + accel[1] = CircuitPlayground.motionY(); + accel[2] = CircuitPlayground.motionZ(); + + memcpy(buf, accel, sizeof(accel)); + return sizeof(accel); +} + +void setup() +{ + Serial.begin(115200); + //while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLEUART Example"); + Serial.println("---------------------------\n"); + + CircuitPlayground.begin(); + + // 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 + Bluefruit.autoConnLed(false); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setName("Bluefruit52"); + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + + // To be consistent OTA DFU should be added first if it exists + bledfu.begin(); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Start BLE Battery Service + blebas.begin(); + blebas.write(100); + + // Adafruit Service + bleTemp.begin(); + bleTemp.setMeasureCallback(measure_temperature); + + bleAccel.begin(); + bleAccel.setMeasureCallback(measure_accel); + + bleLight.begin(); + bleLight.setMeasureCallback(measure_light_sensor); + + bleButton.begin(); + bleButton.setMeasureCallback(measure_button); + bleButton.setPeriod(0); // only notify if there is changes with buttons + + bleTone.begin(CPLAY_BUZZER); + + strip.begin(); + blePixel.begin(&strip); + + // Set up and start advertising + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); + Serial.println("Once connected, enter character(s) that you wish to send"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + + // Advertising with only board ID + struct ATTR_PACKED { + uint16_t mfr_id; + + uint8_t field_len; + uint16_t field_key; + uint16_t field_value; + } mfr_adv; + + mfr_adv.mfr_id = UUID16_COMPANY_ID_ADAFRUIT; + mfr_adv.field_len = 4; + mfr_adv.field_key = 1; // board id + mfr_adv.field_value = USB_PID; + + Bluefruit.Advertising.addManufacturerData(&mfr_adv, sizeof(mfr_adv)); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + +} + +// callback invoked when central connects +void connect_callback(uint16_t conn_handle) +{ + // Get the reference to current connection + BLEConnection* connection = Bluefruit.Connection(conn_handle); + + char central_name[32] = { 0 }; + connection->getPeerName(central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println(); + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); +} diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 2bb3854a..6d8a217a 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -40,19 +40,25 @@ #define TFT_CS 9 #endif +// Circuit Playground Bluefruit for use with TFT 1.5" GIZMO #ifdef ARDUINO_NRF52840_CIRCUITPLAY - #define TFT_DC A7 - #define TFT_CS A6 + #define TFT_DC 1 + #define TFT_CS 0 + #define TFT_BACKLIGHT A3 #endif #if TFT_IN_USE == TFT_35_FEATHERWING #include "Adafruit_HX8357.h" Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC); + #elif TFT_IN_USE == TFT_24_FEATHERWING #include Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); + #elif TFT_IN_USE == TFT_15_GIZMO - #error "Not supported yet" + #include "Adafruit_ST7789.h" + Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1); + #else #error "TFT display is not supported" #endif @@ -99,7 +105,17 @@ void setup() { Serial.begin(115200); +#if TFT_IN_USE == TFT_15_GIZMO + tft.init(240, 240); + tft.setRotation(2); + pinMode(TFT_BACKLIGHT, OUTPUT); + digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on + +#else tft.begin(); + +#endif + tft.fillScreen(COLOR_BLACK); tft.setTextColor(COLOR_WHITE); tft.setTextSize(1); diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index a06fefcf..5ab0084a 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -53,6 +53,7 @@ void BLECharacteristic::_init(void) _attr_meta.read_perm = _attr_meta.write_perm = BLE_SECMODE_OPEN; _attr_meta.vlen = 1; _attr_meta.vloc = BLE_GATTS_VLOC_STACK; + _userbuf = NULL; _handles.value_handle = BLE_GATT_HANDLE_INVALID; _handles.user_desc_handle = BLE_GATT_HANDLE_INVALID; @@ -121,6 +122,11 @@ void BLECharacteristic::setMaxLen(uint16_t max_len) _max_len = max_len; } +uint16_t BLECharacteristic::getMaxLen(void) +{ + return _max_len; +} + void BLECharacteristic::setFixedLen(uint16_t fixed_len) { if ( fixed_len ) @@ -133,6 +139,14 @@ void BLECharacteristic::setFixedLen(uint16_t fixed_len) } } +// Use application buffer instead of SD stack buffer +void BLECharacteristic::setBuffer(void* buf, uint16_t bufsize) +{ + _max_len = bufsize; + _userbuf = (uint8_t*) buf; + _attr_meta.vloc = buf ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK; +} + void BLECharacteristic::setPermission(BleSecurityMode read_perm, BleSecurityMode write_perm) { memcpy(&_attr_meta.read_perm , &read_perm, 1); @@ -270,7 +284,7 @@ err_t BLECharacteristic::begin(void) .init_len = (_attr_meta.vlen == 1) ? (uint16_t) 0 : _max_len, .init_offs = 0, .max_len = _max_len, - .p_value = NULL + .p_value = _userbuf }; VERIFY_STATUS( sd_ble_gatts_characteristic_add(BLE_GATT_HANDLE_INVALID, &char_md, &attr_char_value, &_handles) ); @@ -545,6 +559,11 @@ uint16_t BLECharacteristic::write32(int num) return write32( (uint32_t) num ); } +uint16_t BLECharacteristic::write32(int32_t num) +{ + return write32( (uint32_t) num ); +} + /*------------------------------------------------------------------*/ /* READ *------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 9135d572..b1ac6d98 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -98,6 +98,9 @@ class BLECharacteristic void setPermission(BleSecurityMode read_perm, BleSecurityMode write_perm); void setMaxLen(uint16_t max_len); void setFixedLen(uint16_t fixed_len); + void setBuffer(void* buf, uint16_t bufsize); + + uint16_t getMaxLen(void); /*------------- Descriptors -------------*/ void setUserDescriptor(const char* descriptor); // aka user descriptor @@ -125,6 +128,7 @@ class BLECharacteristic uint16_t write16 (uint16_t num); uint16_t write32 (uint32_t num); uint16_t write32 (int num); + uint16_t write32 (int32_t num); /*------------- Read -------------*/ uint16_t read (void* buffer, uint16_t bufsize, uint16_t offset = 0); @@ -139,6 +143,7 @@ class BLECharacteristic bool notifyEnabled(uint16_t conn_hdl); /*------------- Notify -------------*/ +// bool notify (void); bool notify (const void* data, uint16_t len); bool notify (const char* str); @@ -148,6 +153,7 @@ class BLECharacteristic bool notify32 (int num); /*------------- Notify multiple connections -------------*/ +// bool notify (uint16_t conn_hdl); bool notify (uint16_t conn_hdl, const void* data, uint16_t len); bool notify (uint16_t conn_hdl, const char* str); @@ -181,17 +187,12 @@ class BLECharacteristic virtual void _eventHandler(ble_evt_t* event); protected: - struct ATTR_PACKED { - uint8_t write : 1; - uint8_t cccd_write : 1; - uint8_t read_authorize : 1; - uint8_t write_authorize : 1; - } _use_ada_cb; - bool _is_temp; uint16_t _max_len; BLEService* _service; // pointer to parent's service + uint8_t* _userbuf; + /*------------- Descriptors -------------*/ const char* _usr_descriptor; struct ATTR_PACKED { @@ -211,11 +212,18 @@ class BLECharacteristic }_long_wr; /*------------- Callback pointers -------------*/ - read_authorize_cb_t _rd_authorize_cb; - write_authorize_cb_t _wr_authorize_cb; + struct ATTR_PACKED { + uint8_t write : 1; + uint8_t cccd_write : 1; + uint8_t read_authorize : 1; + uint8_t write_authorize : 1; + } _use_ada_cb; - write_cb_t _wr_cb; - write_cccd_cb_t _cccd_wr_cb; + read_authorize_cb_t _rd_authorize_cb; + write_authorize_cb_t _wr_authorize_cb; + + write_cb_t _wr_cb; + write_cccd_cb_t _cccd_wr_cb; /*------------- Internal Functions -------------*/ void _init(void); diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index fa7223d9..55101f3b 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -232,7 +232,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) *------------------------------------------------------------------*/ bool BLEGatt::_addCharacteristic(BLECharacteristic* chr) { - if ( _server.chr_count == CFG_GATT_MAX_SERVER_CHARS ) return false; + VERIFY( _server.chr_count < CFG_GATT_MAX_SERVER_CHARS ); _server.chr_list[ _server.chr_count++ ] = chr; return true; diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.cpp b/libraries/Bluefruit52Lib/src/BLEUuid.cpp index 6922afce..882184b3 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.cpp +++ b/libraries/Bluefruit52Lib/src/BLEUuid.cpp @@ -36,13 +36,17 @@ #include "BLEUuid.h" -//void reverse_uuid128(uint8_t const in[16], uint8_t out[16]) -//{ -// for(uint8_t i=0; i<16; i++) -// { -// out[i] = in[15-i]; -// } -//} +const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xAF, 0xAD +}; + +const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x00, 0xAF, 0xAD +}; void BLEUuid::set(uint16_t uuid16) { @@ -98,7 +102,7 @@ size_t BLEUuid::size (void) const return 0; } -err_t BLEUuid::begin(void) +bool BLEUuid::begin(void) { /* Add base uuid and decode to get uuid16 * This should cover the already added base uuid128 previously @@ -106,10 +110,10 @@ err_t BLEUuid::begin(void) if (_uuid.type == BLE_UUID_TYPE_UNKNOWN && _uuid128 != NULL ) { (void) sd_ble_uuid_vs_add( (ble_uuid128_t const*) _uuid128, &_uuid.type ); - VERIFY_STATUS( sd_ble_uuid_decode(16, _uuid128, &_uuid) ); + VERIFY_STATUS( sd_ble_uuid_decode(16, _uuid128, &_uuid), false ); } - return ERROR_NONE; + return true; } bool BLEUuid::operator== (const BLEUuid& uuid) const diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index 9b0b7529..1193cdad 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -59,7 +59,7 @@ class BLEUuid size_t size (void) const; // Add UUID128 if needed, in case of UUID16, no actions is required - err_t begin(void); + bool begin(void); bool operator==(const BLEUuid& uuid) const; bool operator!=(const BLEUuid& uuid) const; @@ -72,6 +72,12 @@ class BLEUuid BLEUuid& operator=(ble_uuid_t uuid); }; +//--------------------------------------------------------------------+ +// Custom UUID Service +//--------------------------------------------------------------------+ +extern const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16]; +extern const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16]; + /*------------------------------------------------------------------*/ /* Service UUID * https://www.bluetooth.com/specifications/gatt/services @@ -101,13 +107,20 @@ class BLEUuid #define UUID16_SVC_BMS 0x181E #define UUID16_SVC_CGM 0x181F #define UUID16_SVC_PLX 0x1822 +#define UUID16_SVC_OTS 0x1825 #define UUID16_SVC_EDDYSTONE 0xFEAA +// +#define UUID16_EXTERNAL_REPORT_REF_DESCR 0x2907 +#define UUID16_REPORT_REF_DESCR 0x2908 + /*------------------------------------------------------------------*/ /* Characteristic UUID * https://www.bluetooth.com/specifications/gatt/characteristics *------------------------------------------------------------------*/ +#define UUID16_CHR_TEMPERATURE_CELSIUS 0x2A1F +#define UUID16_CHR_TEMPERATURE_FAHRENHEIT 0x2A20 #define UUID16_CHR_REMOVABLE 0x2A3A #define UUID16_CHR_SERVICE_REQUIRED 0x2A3B #define UUID16_CHR_ALERT_CATEGORY_ID 0x2A43 @@ -178,32 +191,43 @@ class BLEUuid #define UUID16_CHR_SC_CTRLPT 0x2A55 #define UUID16_CHR_RSC_MEASUREMENT 0x2A53 #define UUID16_CHR_SENSOR_LOCATION 0x2A5D -#define UUID16_EXTERNAL_REPORT_REF_DESCR 0x2907 -#define UUID16_REPORT_REF_DESCR 0x2908 #define UUID16_CHR_LN_FEATURE 0x2A6A #define UUID16_CHR_LN_POSITION_QUALITY 0x2A69 #define UUID16_CHR_LN_LOCATION_AND_SPEED 0x2A67 #define UUID16_CHR_LN_NAVIGATION 0x2A68 #define UUID16_CHR_LN_CONTROL_POINT 0x2A6B -#define UUID16_BMS_CTRLPT 0x2AA4 -#define UUID16_BMS_FEATURE 0x2AA5 -#define UUID16_CGM_MEASUREMENT 0x2AA7 -#define UUID16_CGM_FEATURE 0x2AA8 -#define UUID16_CGM_STATUS 0x2AA9 -#define UUID16_CGM_SESSION_START_TIME 0x2AAA -#define UUID16_CGM_SESSION_RUN_TIME 0x2AAB -#define UUID16_CGM_SPECIFIC_OPS_CTRLPT 0x2AAC -#define UUID16_PLX_SPOT_CHECK_MEAS 0x2A5E -#define UUID16_PLX_CONTINUOUS_MEAS 0x2A5F -#define UUID16_PLX_FEATURES 0x2A60 +#define UUID16_CHR_BMS_CTRLPT 0x2AA4 +#define UUID16_CHR_BMS_FEATURE 0x2AA5 +#define UUID16_CHR_CGM_MEASUREMENT 0x2AA7 +#define UUID16_CHR_CGM_FEATURE 0x2AA8 +#define UUID16_CHR_CGM_STATUS 0x2AA9 +#define UUID16_CHR_CGM_SESSION_START_TIME 0x2AAA +#define UUID16_CHR_CGM_SESSION_RUN_TIME 0x2AAB +#define UUID16_CHR_CGM_SPECIFIC_OPS_CTRLPT 0x2AAC +#define UUID16_CHR_PLX_SPOT_CHECK_MEAS 0x2A5E +#define UUID16_CHR_PLX_CONTINUOUS_MEAS 0x2A5F +#define UUID16_CHR_PLX_FEATURES 0x2A60 +#define UUID16_CHR_TEMPERATURE 0x2A6E #define UUID16_CHR_UV_INDEX 0x2A76 +#define UUID16_CHR_OTS_FEATURES 0x2ABD +#define UUID16_CHR_OTS_OBJECT_NAME 0x2ABE +#define UUID16_CHR_OTS_OBJECT_TYPE 0x2ABF +#define UUID16_CHR_OTS_OBJECT_SIZE 0x2AC0 +#define UUID16_CHR_OTS_OBJECT_FIRST_CREATED 0x2AC1 +#define UUID16_CHR_OTS_OBJECT_LAST_MODIFIED 0x2AC2 +#define UUID16_CHR_OTS_OBJECT_ID 0x2AC3 +#define UUID16_CHR_OTS_OBJECT_PROPERTIES 0x2AC4 +#define UUID16_CHR_OTS_OACP 0x2AC5 +#define UUID16_CHR_OTS_OLCP 0x2AC6 +#define UUID16_CHR_OTS_LF 0x2AC7 +#define UUID16_CHR_OTS_OBJECT_CHANGED 0x2AC8 /*------------------------------------------------------------------*/ /* Company UUID * https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers *------------------------------------------------------------------*/ #define UUID16_COMPANY_ID_APPLE 0x004C - +#define UUID16_COMPANY_ID_ADAFRUIT 0x0822 /*------------------------------------------------------------------*/ /* Unit values ( used in Characteristic Presentation Format ) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index c3c6c603..d17f66e4 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -122,7 +122,10 @@ static void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info) LOG_LV1("SD Err", "assert at %s : %d", assert_info->p_file_name, assert_info->line_num); } - while(1) { } + while(1) + { + yield(); + } #endif } @@ -141,7 +144,7 @@ AdafruitBluefruit::AdafruitBluefruit(void) /*------------------------------------------------------------------*/ varclr(&_sd_cfg); - _sd_cfg.attr_table_size = 0x800; + _sd_cfg.attr_table_size = CFG_SD_ATTR_TABLE_SIZE; _sd_cfg.uuid128_max = BLE_UUID_VS_COUNT_DEFAULT; _sd_cfg.service_changed = 1; @@ -326,7 +329,7 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count) * prph and central connections for optimal SRAM usage. * * - If Peripheral mode is enabled - * - ATTR Table Size = 0x800. + * - ATTR Table Size = CFG_SD_ATTR_TABLE_SIZE. * - HVN TX Queue Size = 3 * * - If Central mode is enabled diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index ffe5280d..94d18dcc 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -36,6 +36,8 @@ #define BLE_GATT_ATT_MTU_MAX 247 #define BLE_MAX_CONNECTION 20 // SD support up to 20 connections +#define CFG_SD_ATTR_TABLE_SIZE 0xC00 + #include "BLEUuid.h" #include "BLEAdvertising.h" #include "BLECharacteristic.h" diff --git a/tools/build_all.py b/tools/build_all.py index 38f1a293..4f6178a4 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -67,7 +67,11 @@ def build_examples(variant): for sketch in glob.iglob('libraries/**/*.ino', recursive=True): start_time = time.monotonic() - if os.path.exists(os.path.dirname(sketch) + '/.skip') or os.path.exists(os.path.dirname(sketch) + '/.skip.' + variant): + # skip if example contains: ".skip" or ".skip.variant" + # however ".build.variant" file can overwrite ".skip", used to build a specific variant only + sketchdir = os.path.dirname(sketch) + if ( (os.path.exists(sketchdir + '/.skip') or os.path.exists(sketchdir + '/.skip.' + variant)) and + not os.path.exists(sketchdir + '/.build.' + variant)): success = "skipped" else: # TODO - preferably, would have STDERR show up in **both** STDOUT and STDERR.