pxt-calliope/libs/bluetooth/bluetooth.cpp

147 lines
5.0 KiB
C++
Raw Normal View History

2016-06-14 15:30:07 +02:00
#include "pxt.h"
#include "MESEvents.h"
2016-06-23 09:05:49 +02:00
#include "MicroBitUARTService.h"
2016-06-14 15:30:07 +02:00
using namespace pxt;
/**
* Support for additional Bluetooth services.
*/
//% color=#0082FB weight=20
2016-06-14 15:30:07 +02:00
namespace bluetooth {
2016-06-24 17:12:52 +02:00
MicroBitUARTService *uart = NULL;
/**
* Starts the Bluetooth accelerometer service
*/
//% help=bluetooth/start-accelerometer-service
//% blockId=bluetooth_start_accelerometer_service block="bluetooth accelerometer service"
//% parts="bluetooth" weight=90 blockGap=8
void startAccelerometerService() {
new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);
}
/**
* Starts the Bluetooth button service
*/
//% help=bluetooth/start-button-service
//% blockId=bluetooth_start_button_service block="bluetooth button service" blockGap=8
//% parts="bluetooth" weight=89
void startButtonService() {
new MicroBitButtonService(*uBit.ble);
}
2016-06-14 15:30:07 +02:00
/**
* Starts the Bluetooth IO pin service.
2016-06-14 15:30:07 +02:00
*/
//% help=bluetooth/start-io-pin-service
//% blockId=bluetooth_start_io_pin_service block="bluetooth io pin service" blockGap=8
//% parts="bluetooth" weight=88
2016-06-14 15:30:07 +02:00
void startIOPinService() {
new MicroBitIOPinService(*uBit.ble, uBit.io);
}
/**
* Starts the Bluetooth LED service
*/
//% help=bluetooth/start-led-service
//% blockId=bluetooth_start_led_service block="bluetooth led service" blockGap=8
//% parts="bluetooth" weight=87
void startLEDService() {
new MicroBitLEDService(*uBit.ble, uBit.display);
}
/**
* Starts the Bluetooth temperature service
*/
//% help=bluetooth/start-temperature-service
//% blockId=bluetooth_start_temperature_service block="bluetooth temperature service" blockGap=8
//% parts="bluetooth" weight=86
void startTemperatureService() {
new MicroBitTemperatureService(*uBit.ble, uBit.thermometer);
}
/**
* Starts the Bluetooth magnetometer service
*/
//% help=bluetooth/start-magnetometer-service
//% blockId=bluetooth_start_magnetometer_service block="bluetooth magnetometer service"
//% parts="bluetooth" weight=85
void startMagnetometerService() {
new MicroBitMagnetometerService(*uBit.ble, uBit.compass);
}
2016-06-23 09:05:49 +02:00
/**
* Starts the Bluetooth UART service
*/
2016-08-09 17:10:53 +02:00
//% help=bluetooth/start-uart-service
//% blockId=bluetooth_start_uart_service block="bluetooth uart service"
//% parts="bluetooth" advanced=true
2016-06-23 09:05:49 +02:00
void startUartService() {
2016-06-24 17:12:52 +02:00
if (uart) return;
// 61 octet buffer size is 3 x (MTU - 3) + 1
// MTU on nRF51822 is 23 octets. 3 are used by Attribute Protocol header data leaving 20 octets for payload
// So we allow a RX buffer that can contain 3 x max length messages plus one octet for a terminator character
uart = new MicroBitUARTService(*uBit.ble, 61, 60);
2016-06-23 09:05:49 +02:00
}
//%
void uartWriteString(StringData *data) {
2016-06-24 17:12:52 +02:00
startUartService();
uart->send(ManagedString(data));
}
//%
StringData* uartReadUntil(StringData *del) {
2016-06-24 17:12:52 +02:00
startUartService();
return uart->readUntil(ManagedString(del)).leakData();
}
/**
* Register code to run when the micro:bit is connected to over Bluetooth
* @param body Code to run when a Bluetooth connection is established
*/
2016-06-24 17:12:52 +02:00
//% help=bluetooth/on-bluetooth-connected weight=20
//% blockId=bluetooth_on_connected block="on bluetooth connected" blockGap=8
//% parts="bluetooth"
void onBluetoothConnected(Action body) {
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, body);
}
/**
* Register code to run when a bluetooth connection to the micro:bit is lost
* @param body Code to run when a Bluetooth connection is lost
*/
2016-06-24 17:12:52 +02:00
//% help=bluetooth/on-bluetooth-disconnected weight=19
//% blockId=bluetooth_on_disconnected block="on bluetooth disconnected"
//% parts="bluetooth"
void onBluetoothDisconnected(Action body) {
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body);
}
const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
/**
* Advertise an Eddystone URL
* @param url the url to transmit. Must be no longer than the supported eddystone url length
* @param power power level between 0 and 7, e.g.: 7
*/
2016-11-30 13:24:52 +01:00
//% blockId=eddystone_advertise_url block="bluetooth advertise url %url|with power %power"
//% parts=bluetooth weight=11 blockGap=8
//% help=bluetooth/advertise-url
void advertiseUrl(StringData* url, int power) {
int8_t level = CALIBRATED_POWERS[min(7, max(0, power))];
uBit.bleManager.advertiseEddystoneUrl(ManagedString(url), level);
}
/**
* Stops advertising Eddystone end points
*/
2016-11-30 13:24:52 +01:00
//% blockId=eddystone_stop_advertising block="bluetooth stop advertising"
2016-11-30 13:19:17 +01:00
//% parts=bluetooth weight=10
//% help=bluetooth/stop-advertising
void stopAdvertising() {
uBit.bleManager.stopAdvertising();
}
}