You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.6 KiB
189 lines
4.6 KiB
/********************************************************************* |
|
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 |
|
*********************************************************************/ |
|
|
|
/* |
|
* This sketch demonstrate the central API(). A additional bluefruit |
|
* that has bleuart as peripheral is required for the demo. |
|
*/ |
|
#include <bluefruit.h> |
|
|
|
#define RIGHT 2 |
|
#define FOG 3 |
|
#define LEFT 4 |
|
|
|
BLEClientDis clientDis; |
|
BLEClientUart clientUart; |
|
|
|
void setup() |
|
{ |
|
Serial.begin(115200); |
|
|
|
Serial.println("Bluefruit52 Central BLEUART Example"); |
|
Serial.println("-----------------------------------\n"); |
|
|
|
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 |
|
// SRAM usage required by SoftDevice will increase dramatically with number of connections |
|
Bluefruit.begin(0, 1); |
|
|
|
Bluefruit.setName("Bluefruit52 Central"); |
|
|
|
// Configure DIS client |
|
clientDis.begin(); |
|
|
|
// Init BLE Central Uart Serivce |
|
clientUart.begin(); |
|
clientUart.setRxCallback(bleuart_rx_callback); |
|
|
|
// Increase Blink rate to different from PrPh advertising mode |
|
Bluefruit.setConnLedInterval(250); |
|
|
|
// Callbacks for Central |
|
Bluefruit.Central.setConnectCallback(connect_callback); |
|
Bluefruit.Central.setDisconnectCallback(disconnect_callback); |
|
|
|
/* Start Central Scanning |
|
* - Enable auto scan if disconnected |
|
* - Interval = 100 ms, window = 80 ms |
|
* - Don't use active scan |
|
* - Start(timeout) with timeout = 0 will scan forever (until connected) |
|
*/ |
|
Bluefruit.Scanner.setRxCallback(scan_callback); |
|
Bluefruit.Scanner.restartOnDisconnect(true); |
|
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms |
|
Bluefruit.Scanner.useActiveScan(false); |
|
Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds |
|
|
|
// Setup buttons |
|
pinMode(RIGHT, INPUT_PULLUP); |
|
pinMode(FOG, INPUT_PULLUP); |
|
pinMode(LEFT, INPUT_PULLUP); |
|
} |
|
|
|
/** |
|
* Callback invoked when scanner pick up an advertising data |
|
* @param report Structural advertising data |
|
*/ |
|
void scan_callback(ble_gap_evt_adv_report_t* report) |
|
{ |
|
// Check if advertising contain BleUart service |
|
if ( Bluefruit.Scanner.checkReportForService(report, clientUart) ) |
|
{ |
|
Serial.print("BLE UART service detected. Connecting ... "); |
|
|
|
// Connect to device with bleuart service in advertising |
|
Bluefruit.Central.connect(report); |
|
} |
|
} |
|
|
|
/** |
|
* Callback invoked when an connection is established |
|
* @param conn_handle |
|
*/ |
|
void connect_callback(uint16_t conn_handle) |
|
{ |
|
Serial.println("Connected"); |
|
|
|
Serial.print("Dicovering DIS ... "); |
|
if ( clientDis.discover(conn_handle) ) |
|
{ |
|
Serial.println("Found it"); |
|
char buffer[32+1]; |
|
|
|
// read and print out Manufacturer |
|
memset(buffer, 0, sizeof(buffer)); |
|
if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ) |
|
{ |
|
Serial.print("Manufacturer: "); |
|
Serial.println(buffer); |
|
} |
|
|
|
// read and print out Model Number |
|
memset(buffer, 0, sizeof(buffer)); |
|
if ( clientDis.getModel(buffer, sizeof(buffer)) ) |
|
{ |
|
Serial.print("Model: "); |
|
Serial.println(buffer); |
|
} |
|
|
|
Serial.println(); |
|
} |
|
|
|
Serial.print("Discovering BLE Uart Service ... "); |
|
|
|
if ( clientUart.discover(conn_handle) ) |
|
{ |
|
Serial.println("Found it"); |
|
|
|
Serial.println("Enable TXD's notify"); |
|
clientUart.enableTXD(); |
|
|
|
Serial.println("Ready to receive from peripheral"); |
|
}else |
|
{ |
|
Serial.println("Found NONE"); |
|
|
|
// disconect since we couldn't find bleuart service |
|
Bluefruit.Central.disconnect(conn_handle); |
|
} |
|
} |
|
|
|
/** |
|
* Callback invoked when a connection is dropped |
|
* @param conn_handle |
|
* @param reason |
|
*/ |
|
void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
|
{ |
|
(void) conn_handle; |
|
(void) reason; |
|
|
|
Serial.println("Disconnected"); |
|
} |
|
|
|
/** |
|
* Callback invoked when uart received data |
|
* @param uart_svc Reference object to the service where the data |
|
* arrived. In this example it is clientUart |
|
*/ |
|
void bleuart_rx_callback(BLEClientUart& uart_svc) |
|
{ |
|
Serial.print("[RX]: "); |
|
|
|
while ( uart_svc.available() ) |
|
{ |
|
Serial.print( (char) uart_svc.read() ); |
|
} |
|
|
|
Serial.println(); |
|
} |
|
|
|
void loop() |
|
{ |
|
if (Bluefruit.Central.connected()) |
|
{ |
|
if (clientUart.discovered()) |
|
{ |
|
if (!digitalRead(LEFT)) { |
|
clientUart.print('L'); |
|
} else if (!digitalRead(FOG)) { |
|
clientUart.print('F'); |
|
} else if (!digitalRead(RIGHT)) { |
|
clientUart.print('R'); |
|
} |
|
} |
|
} |
|
|
|
delay(10); |
|
} |
|
|
|
|