From b80abcb62983259cf05fc31516a84d643a76b6cd Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 17:18:07 +0700 Subject: [PATCH] added cplay_ble.ino --- .../Peripheral/cplay_ble/cplay_ble.ino | 189 ++++++++++++++++++ .../Peripheral/cplay_ble/services.txt | 129 ++++++++++++ 2 files changed, 318 insertions(+) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt 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..0f4579a0 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -0,0 +1,189 @@ +/********************************************************************* + 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 + +// 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; + +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 + + // 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); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // 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/cplay_ble/services.txt b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt new file mode 100644 index 00000000..5764c599 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt @@ -0,0 +1,129 @@ +/* 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 + * + * Accelerometer service 0200 + * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z + * - Measurement Period 0001 + * + * Light sensor service 0300 + * - Lux Data 0301 | uint16_t | Read + Notify + * - Measurement Period 0001 + * + * Button service 0600 + * - Pressed 0601 | uint32_t | Read + Notify | slide sw (b2), button B (b1), button A (b0) + * - Measurement Period 0001 + * + * Tone Service 0C00 + * - Frequency 0C01 | uint16_t | frequency in Hz + * - Duration 0C01 | + * + * Sound sensor service 0400 + * - Sound Data 0401 | int16_t | Read + Notify + * - Measurement Period 0001 + * + * Captouch service 0500 + * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately + * - Cap0 0510 | int32_t | Read + Notify + * - ......................... + * - Cap7 0517 | int32_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * + * PIN I/O service 0700 + * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out + * - Pin Data 0702 | uint64_t | Read + Write | + * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle + * + * PIN Analog 0800 + * - A0 0810 | uint16_t | Read + Notify + * ......................... + * - A7 0817 | uint16_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Neopixel Service 0900 + * - Pixel Pin 0901 | uint8_t | Read + Write + * - Pixel Count 0902 | uint16_t | Read + Write + * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. + * - Pixel Data 0904 | RGB array| Write + * + * Gamepad Service 0A00 + * - Gamepad Button 0A01 | uint16_t | Write + * - Left XY 0A02 | int8_t[2] | Write + * - Right XY 0A03 | int8_t[2] | Write + * + * Image Service 0B00 + * - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ... + * - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ? + * - Fill 0B03 | uint8_t[3] | Write | Fill with solid color + * - Load File 0B04 | utf8_t | Write | Load image from qspi flash ? + */ + + + + + { + "_comment": { + "comment": [ + "Add UUIDs for your services, characteristics, and descriptors in this file.", + "The format is the UUID of the item, followed by an object containing the data.", + "'name' is a mandatory handle, while 'format' is optional.", + "Currently the only supported format is 'TEXT', default is 'NO_FORMAT'.", + "The application must be reloaded to access changes in this file." + ] + }, + "uuid16bitServiceDefinitions": { + "180D": { + "name": "Heart Rate" + } + }, + "uuid16bitCharacteristicDefinitions": { + "2A37": { + "name": "Heart Rate Measurement" + }, + "2A29": { + "name": "Manufacturer Name String", + "format": "TEXT" + } + }, + "uuid16bitDescriptorDefinitions": {}, + "uuid128bitServiceDefinitions": { + "6E400001B5A3F393E0A9E50E24DCCA9E": { + "name": "UART over BLE" + }, + + "ADAF0100C33242A893BD25E905756CB8": { "name": "Adafruit Temperature" }, + "ADAF0200C33242A893BD25E905756CB8": { "name": "Adafruit Accelerometer" }, + "ADAF0300C33242A893BD25E905756CB8": { "name": "Adafruit Light" }, + + "ADAF0400C33242A893BD25E905756CB8": { "name": "Adafruit Sound" }, + "ADAF0500C33242A893BD25E905756CB8": { "name": "Adafruit Capacitive Touch" }, + "ADAF0600C33242A893BD25E905756CB8": { "name": "Adafruit Buttons" } + + "ADAF0700C33242A893BD25E905756CB8": { "name": "Adafruit Pin DIO" }, + "ADAF0800C33242A893BD25E905756CB8": { "name": "Adafruit Pin Analog" }, + "ADAF0900C33242A893BD25E905756CB8": { "name": "Adafruit RGB Pixel" } + + "ADAF0A00C33242A893BD25E905756CB8": { "name": "Adafruit Gamepad" }, + "ADAF0B00C33242A893BD25E905756CB8": { "name": "Adafruit Image" }, + "ADAF0C00C33242A893BD25E905756CB8": { "name": "Adafruit Tone" } + + }, + "uuid128bitCharacteristicDefinitions": { + "6E400002B5A3F393E0A9E50E24DCCA9E": { "name": "UART RX" }, + "6E400003B5A3F393E0A9E50E24DCCA9E": { "name": "UART TX" }, + + "ADAF0001C33242A893BD25E905756CB8": { "name": "Measurement Period" }, + "ADAF0101C33242A893BD25E905756CB8": { "name": "Temperature" }, + "ADAF0201C33242A893BD25E905756CB8": { "name": "Accelerometer" }, + "ADAF0301C33242A893BD25E905756CB8": { "name": "Lux" } + }, + "uuid128bitDescriptorDefinitions": {} +}