Files
Adafruit_nRF52_Arduino/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino
T
2017-04-10 17:27:30 +07:00

155 lines
4.2 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;
// Software Timer for blinking RED LED
SoftwareTimer blinkTimer;
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 BLEUART Example");
// Initialize Blink Timer for 1000 ms and start it
blinkTimer.begin(1000, blink_timer_callback);
blinkTimer.start();
// 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)");
}
/**
* Software Timer callback is invoked in built-in thread of FreeRTOS with
* minimal stack size. Therefore it should be as simple as possible. If
* a periodically heavy-task is needed, please use Scheduler.startLoop() to
* create an own task for it.
*
* More information http://www.freertos.org/RTOS-software-timer.html
*/
void blink_timer_callback(TimerHandle_t xTimerID)
{
(void) xTimerID;
digitalToggle(LED_RED);
}
/**
* RTOS Idle callback is automatically invoked by FreeRTOS
* when there is no active threads. E.g loop() call delay() and
* there is no bluetooth or hw event. This is a greate place to handle
* background data.
*
* NOTE: After this callback returns, MCU will enter low-power mode
* by waitForEvent() internally. THerefore user SHOULD NOT call
* waitForEvent() in this callback.
*
* WARNING: This function MUST NOT call any blocking FreeRTOS API
* such as delay(), xSemaphoreTake() etc ... for more information
* http://www.freertos.org/a00016.html
*/
void rtos_idle_callback(void)
{
// Background task here
// waitForEvent() will be invoked by internal code when this callback return.
// Don't call waitForEvent() in this function nor any other blocking API()
}