132 lines
3.4 KiB
Arduino
132 lines
3.4 KiB
Arduino
/*********************************************************************
|
|
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 <bluefruit.h>
|
|
|
|
// BLE Service
|
|
BLEDis bledis;
|
|
BLEUart bleuart;
|
|
BLEBas blebas;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.println("Bluefruit52 BLEUART Example");
|
|
|
|
// 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 manually via PIN 19
|
|
Bluefruit.autoConnLed(true);
|
|
|
|
Bluefruit.begin();
|
|
Bluefruit.setName("Bluefruit52");
|
|
Bluefruit.setConnectCallback(connect_callback);
|
|
Bluefruit.setDisconnectCallback(disconnect_callback);
|
|
|
|
// 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);
|
|
|
|
// Set up Advertising Packet
|
|
setupAdv();
|
|
|
|
// Start Advertising
|
|
Bluefruit.Advertising.start();
|
|
|
|
Serial.println("Please use Adafruit Bluefruit LE app to connect in UART mode");
|
|
Serial.println("Then Enter characters to send");
|
|
}
|
|
|
|
void setupAdv(void)
|
|
{
|
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
|
Bluefruit.Advertising.addTxPower();
|
|
|
|
// Include bleuart 128-bit uuid
|
|
Bluefruit.Advertising.addService(bleuart);
|
|
|
|
// There is no room for Name in Advertising packet
|
|
// Use Scan response for Name
|
|
Bluefruit.ScanResponse.addName();
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// forward from Serial to BLEUART
|
|
while (Serial.available())
|
|
{
|
|
// delay to get enough inputs, since we have limited number of transmission buffer
|
|
delay(2);
|
|
|
|
uint8_t buf[64];
|
|
int count = Serial.readBytes(buf, sizeof(buf));
|
|
bleuart.write( buf, count );
|
|
}
|
|
|
|
// forward from BLEUART to Serial
|
|
while ( bleuart.available() )
|
|
{
|
|
uint8_t ch;
|
|
ch = (uint8_t) bleuart.read();
|
|
Serial.write(ch);
|
|
}
|
|
|
|
// Instruct CPU to enter low-power state until an event/interrupt occured
|
|
// Note: This will turn off hardware PWM and maybe other peripherlas !!
|
|
waitForEvent();
|
|
}
|
|
|
|
void connect_callback(void)
|
|
{
|
|
Serial.println("Connected");
|
|
}
|
|
|
|
void disconnect_callback(uint8_t reason)
|
|
{
|
|
(void) reason;
|
|
|
|
Serial.println();
|
|
Serial.println("Disconnected");
|
|
Serial.println("Bluefruit will auto start advertising (default)");
|
|
}
|
|
|
|
/**
|
|
* RTOS Idle callback is automatically invoked by FreeRTOS
|
|
* when there is no active threads. This function must not
|
|
* call any blocking FreeRTOS API such as delay(), xSemaphoreTake() etc ..
|
|
*
|
|
* Notes: Should be used for background work. Great place to call
|
|
* waitForEvent() (see comment below)
|
|
*/
|
|
void rtos_idle_callback(void)
|
|
{
|
|
// Background task here
|
|
|
|
// Instruct CPU to enter low-power state until an event/interrupt occured
|
|
// Note: This will turn off hardware PWM and maybe other peripherlas !!
|
|
// Should only call there is no active PWM devices
|
|
if ( !(PWM0.enabled() || PWM1.enabled() || PWM2.enabled() ) )
|
|
{
|
|
waitForEvent();
|
|
}
|
|
}
|
|
|