2.1.28, initiation update to PXT v5.28.24 (#54)
This commit is contained in:
committed by
Peli de Halleux
parent
38a964516e
commit
5c114a0c57
50
libs/bluetooth/BLEHF2Service.cpp
Normal file
50
libs/bluetooth/BLEHF2Service.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "MicroBitConfig.h"
|
||||
#include "ble/UUID.h"
|
||||
#include "BLEHF2Service.h"
|
||||
#include "MicroBitEvent.h"
|
||||
|
||||
BLEHF2Service::BLEHF2Service(BLEDevice &_ble) :
|
||||
ble(_ble)
|
||||
{
|
||||
GattCharacteristic txCharacteristic(BLEHF2TxCharacteristicUUID, (uint8_t *)&txCharacteristicMessage, 0,
|
||||
sizeof(txCharacteristicMessage), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
memset(&txCharacteristicMessage, 0, sizeof(txCharacteristicMessage));
|
||||
|
||||
// Set default security requirements
|
||||
txCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
|
||||
// setup GATT table
|
||||
GattCharacteristic *characteristics[] = {&txCharacteristic};
|
||||
GattService service(BLEHF2ServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
|
||||
ble.addService(service);
|
||||
|
||||
// retreive handles
|
||||
txCharacteristicHandle = txCharacteristic.getValueHandle();
|
||||
|
||||
// initialize data
|
||||
ble.gattServer().write(txCharacteristicHandle,(uint8_t *)&txCharacteristicMessage, sizeof(txCharacteristicMessage));
|
||||
}
|
||||
|
||||
void BLEHF2Service::sendSerial(const char *data, int len, bool isError) {
|
||||
if (ble.getGapState().connected)
|
||||
{
|
||||
int32_t sent = 0;
|
||||
while(sent < len) {
|
||||
int32_t n = min(BLEHF2_DATA_LENGTH, len - sent);
|
||||
txCharacteristicMessage.command = (isError ? BLEHF2_FLAG_SERIAL_OUT : BLEHF2_FLAG_SERIAL_ERR) | n;
|
||||
memcpy(&txCharacteristicMessage.data, data + sent, n);
|
||||
ble.gattServer().notify(txCharacteristicHandle,(uint8_t *)&txCharacteristicMessage, sizeof(txCharacteristicMessage));
|
||||
sent += n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t BLEHF2ServiceUUID[] = {
|
||||
0xb1,0x12,0xf5,0xe6,0x26,0x79,0x30,0xda,0xa2,0x6e,0x02,0x73,0xb6,0x04,0x38,0x49
|
||||
};
|
||||
|
||||
const uint8_t BLEHF2TxCharacteristicUUID[] = {
|
||||
0xb1,0x12,0xf5,0xe6,0x26,0x79,0x30,0xda,0xa2,0x6e,0x02,0x73,0xb6,0x04,0x38,0x4a
|
||||
};
|
54
libs/bluetooth/BLEHF2Service.h
Normal file
54
libs/bluetooth/BLEHF2Service.h
Normal file
@ -0,0 +1,54 @@
|
||||
#ifndef BLE_HF2_SERVICE_H
|
||||
#define BLE_HF2_SERVICE_H
|
||||
|
||||
#include "MicroBitConfig.h"
|
||||
#include "ble/BLE.h"
|
||||
#include "MicroBitThermometer.h"
|
||||
#include "EventModel.h"
|
||||
#include "pxt.h"
|
||||
|
||||
#define HF2_ID 9501
|
||||
|
||||
#define BLEHF2_FLAG_SERIAL_OUT 0x80
|
||||
#define BLEHF2_FLAG_SERIAL_ERR 0xC0
|
||||
#define BLEHF2_DATA_LENGTH 19
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t BLEHF2ServiceUUID[];
|
||||
extern const uint8_t BLEHF2TxCharacteristicUUID[];
|
||||
|
||||
struct BLEHF2Packet {
|
||||
uint8_t command;
|
||||
uint8_t data[BLEHF2_DATA_LENGTH];
|
||||
};
|
||||
|
||||
class BLEHF2Service
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the TemperatureService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
BLEHF2Service(BLEDevice &_ble);
|
||||
|
||||
/**
|
||||
* Sends text
|
||||
*/
|
||||
void sendSerial(const char *data, int len, bool isError);
|
||||
|
||||
private:
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
// memory for buffers.
|
||||
BLEHF2Packet txCharacteristicMessage;
|
||||
|
||||
// Handles to access each characteristic when they are held by Soft Device.
|
||||
GattAttribute::Handle_t txCharacteristicHandle;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
@ -29,7 +29,10 @@
|
||||
"bluetooth.startTemperatureService": "Starts the Bluetooth temperature service",
|
||||
"bluetooth.startUartService": "Starts the Bluetooth UART service",
|
||||
"bluetooth.stopAdvertising": "Stops advertising Eddystone end points",
|
||||
"bluetooth.uartReadBuffer": "Reads buffered UART data into a buffer",
|
||||
"bluetooth.uartReadUntil": "Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.",
|
||||
"bluetooth.uartWriteBuffer": "Sends a buffer of data via Bluetooth UART",
|
||||
"bluetooth.uartWriteLine": "Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.",
|
||||
"bluetooth.uartWriteNumber": "Prints a numeric value to the serial",
|
||||
"bluetooth.uartWriteString": "Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.",
|
||||
"bluetooth.uartWriteValue": "Writes a ``name: value`` pair line to the serial.",
|
||||
|
@ -14,6 +14,7 @@
|
||||
"bluetooth.startUartService|block": "bluetooth uart service",
|
||||
"bluetooth.stopAdvertising|block": "bluetooth stop advertising",
|
||||
"bluetooth.uartReadUntil|block": "bluetooth uart|read until %del=serial_delimiter_conv",
|
||||
"bluetooth.uartWriteLine|block": "bluetooth uart|write line %data",
|
||||
"bluetooth.uartWriteNumber|block": "bluetooth uart|write number %value",
|
||||
"bluetooth.uartWriteString|block": "bluetooth uart|write string %data",
|
||||
"bluetooth.uartWriteValue|block": "bluetooth uart|write value %name|= %value",
|
||||
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"bluetooth": "Unterstützung für zusätzliche Bluetooth-Dienste.",
|
||||
"bluetooth.onBluetoothConnected|param|body": "Code, der ausgeführt wird, wenn eine Bluetooth-Verbindung aufgebaut wurde",
|
||||
"bluetooth.uartWriteNumber": "Gibt einen numerischen Wert an die serielle",
|
||||
"bluetooth.uartWriteValue": "Schreibt ein ``Namen: Wert`` Wertepaar auf die serielle Schnittstelle.",
|
||||
"bluetooth.uartWriteValue|param|name": "Name des Wertestreams, z.B.: x",
|
||||
"bluetooth.uartWriteValue|param|value": "Schreiben"
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#include "pxt.h"
|
||||
#include "MESEvents.h"
|
||||
#include "MicroBitUARTService.h"
|
||||
#include "BLEHF2Service.h"
|
||||
|
||||
using namespace pxt;
|
||||
|
||||
@ -10,7 +11,14 @@ using namespace pxt;
|
||||
//% color=#0082FB weight=96 icon="\uf294"
|
||||
namespace bluetooth {
|
||||
MicroBitUARTService *uart = NULL;
|
||||
BLEHF2Service* pHF2 = NULL;
|
||||
|
||||
//%
|
||||
void __log(String msg) {
|
||||
if (NULL == pHF2)
|
||||
pHF2 = new BLEHF2Service(*uBit.ble);
|
||||
pHF2->sendSerial(msg->getUTF8Data(), msg->getUTF8Size(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth accelerometer service
|
||||
@ -88,26 +96,59 @@ namespace bluetooth {
|
||||
}
|
||||
|
||||
//%
|
||||
void uartWriteString(StringData *data) {
|
||||
void uartWriteString(String data) {
|
||||
startUartService();
|
||||
uart->send(ManagedString(data));
|
||||
uart->send(MSTR(data));
|
||||
}
|
||||
|
||||
//%
|
||||
StringData* uartReadUntil(StringData *del) {
|
||||
String uartReadUntil(String del) {
|
||||
startUartService();
|
||||
return uart->readUntil(ManagedString(del)).leakData();
|
||||
return PSTR(uart->readUntil(MSTR(del)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a buffer of data via Bluetooth UART
|
||||
*/
|
||||
//%
|
||||
void uartWriteBuffer(Buffer buffer) {
|
||||
startUartService();
|
||||
uart->send(buffer->data, buffer->length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads buffered UART data into a buffer
|
||||
*/
|
||||
//%
|
||||
Buffer uartReadBuffer() {
|
||||
startUartService();
|
||||
int bytes = uart->rxBufferedSize();
|
||||
auto buffer = mkBuffer(NULL, bytes);
|
||||
int read = uart->read(buffer->data, buffer->length);
|
||||
// read failed
|
||||
if (read < 0) {
|
||||
decrRC(buffer);
|
||||
return mkBuffer(NULL, 0);
|
||||
}
|
||||
// could not fill the buffer
|
||||
if (read != buffer->length) {
|
||||
auto tmp = mkBuffer(buffer->data, read);
|
||||
decrRC(buffer);
|
||||
buffer = tmp;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an event to be fired when one of the delimiter is matched.
|
||||
* @param delimiters the characters to match received characters against.
|
||||
*/
|
||||
//% help=bluetooth/on-uart-data-received
|
||||
//% weight=18 blockId=bluetooth_on_data_received block="bluetooth|on data received %delimiters=serial_delimiter_conv"
|
||||
void onUartDataReceived(StringData* delimiters, Action body) {
|
||||
void onUartDataReceived(String delimiters, Action body) {
|
||||
startUartService();
|
||||
uart->eventOn(ManagedString(delimiters));
|
||||
uart->eventOn(MSTR(delimiters));
|
||||
registerWithDal(MICROBIT_ID_BLE_UART, MICROBIT_UART_S_EVT_DELIM_MATCH, body);
|
||||
}
|
||||
|
||||
@ -131,7 +172,7 @@ namespace bluetooth {
|
||||
//% 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};
|
||||
/**
|
||||
@ -143,11 +184,13 @@ namespace bluetooth {
|
||||
//% blockId=eddystone_advertise_url block="bluetooth advertise url %url|with power %power|connectable %connectable"
|
||||
//% parts=bluetooth weight=11 blockGap=8
|
||||
//% help=bluetooth/advertise-url blockExternalInputs=1
|
||||
void advertiseUrl(StringData* url, int power, bool connectable) {
|
||||
void advertiseUrl(String url, int power, bool connectable) {
|
||||
#if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
|
||||
power = min(MICROBIT_BLE_POWER_LEVELS-1, max(0, power));
|
||||
int8_t level = CALIBRATED_POWERS[power];
|
||||
uBit.bleManager.advertiseEddystoneUrl(ManagedString(url), level, connectable);
|
||||
uBit.bleManager.advertiseEddystoneUrl(MSTR(url), level, connectable);
|
||||
uBit.bleManager.setTransmitPower(power);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -158,14 +201,14 @@ namespace bluetooth {
|
||||
*/
|
||||
//% parts=bluetooth weight=12 advanced=true
|
||||
void advertiseUidBuffer(Buffer nsAndInstance, int power, bool connectable) {
|
||||
ManagedBuffer buf(nsAndInstance);
|
||||
if (buf.length() != 16) return;
|
||||
#if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID)
|
||||
auto buf = nsAndInstance;
|
||||
if (buf->length != 16) return;
|
||||
|
||||
power = min(MICROBIT_BLE_POWER_LEVELS-1, max(0, power));
|
||||
int8_t level = CALIBRATED_POWERS[power];
|
||||
uint8_t uidNs[10]; buf.readBytes(uidNs, 0, 10);
|
||||
uint8_t uidInst[6]; buf.readBytes(uidInst, 10, 6);
|
||||
uBit.bleManager.advertiseEddystoneUid((const char*)uidNs, (const char*)uidInst, level, connectable);
|
||||
uBit.bleManager.advertiseEddystoneUid((const char*)buf->data, (const char*)buf->data + 10, level, connectable);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -186,5 +229,5 @@ namespace bluetooth {
|
||||
//% help=bluetooth/stop-advertising advanced=true
|
||||
void stopAdvertising() {
|
||||
uBit.bleManager.stopAdvertising();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,20 @@
|
||||
/// <reference no-default-lib="true"/>
|
||||
/**
|
||||
* Support for additional Bluetooth services.
|
||||
*/
|
||||
//% color=#0082FB weight=96 icon="\uf294"
|
||||
namespace bluetooth {
|
||||
export let NEW_LINE = "\r\n";
|
||||
|
||||
/**
|
||||
* Internal use
|
||||
*/
|
||||
//% shim=bluetooth::__log
|
||||
export function __log(msg: string) {
|
||||
return;
|
||||
}
|
||||
console.addListener(function (msg) { __log(msg) });
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
@ -10,8 +22,17 @@ namespace bluetooth {
|
||||
//% blockId=bluetooth_uart_write block="bluetooth uart|write string %data" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartWriteString advanced=true
|
||||
export function uartWriteString(data: string): void {
|
||||
// dummy implementation for simulator
|
||||
console.log("UART Write: " + data)
|
||||
console.log(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
//% help=bluetooth/uart-write-line weight=79
|
||||
//% blockId=bluetooth_uart_line block="bluetooth uart|write line %data" blockGap=8
|
||||
//% parts="bluetooth" advanced=true
|
||||
export function uartWriteLine(data: string): void {
|
||||
uartWriteString(data + serial.NEW_LINE);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,7 +54,7 @@ namespace bluetooth {
|
||||
//% help=bluetooth/uart-write-value advanced=true
|
||||
//% blockId=bluetooth_uart_writevalue block="bluetooth uart|write value %name|= %value"
|
||||
export function uartWriteValue(name: string, value: number): void {
|
||||
uartWriteString(name + ":" + value + "\r\n");
|
||||
uartWriteString((name ? name + ":" : "") + value + NEW_LINE);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +65,7 @@ namespace bluetooth {
|
||||
//% parts="bluetooth" shim=bluetooth::uartReadUntil advanced=true
|
||||
export function uartReadUntil(del: string): string {
|
||||
// dummy implementation for simulator
|
||||
return "???"
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,8 +7,10 @@
|
||||
"shims.d.ts",
|
||||
"bluetooth.ts",
|
||||
"bluetooth.cpp",
|
||||
"_locales/de/bluetooth-jsdoc-strings.json"
|
||||
"BLEHF2Service.h",
|
||||
"BLEHF2Service.cpp"
|
||||
],
|
||||
"icon": "./static/packages/bluetooth/icon.png",
|
||||
"public": true,
|
||||
"dependencies": {
|
||||
"core": "file:../core"
|
||||
@ -23,50 +25,10 @@
|
||||
},
|
||||
"optionalConfig": {
|
||||
"microbit-dal": {
|
||||
"stack_size": 1280,
|
||||
"gatt_table_size": "0x700"
|
||||
}
|
||||
},
|
||||
"userConfigs": [
|
||||
{
|
||||
"description": "No Pairing Required: Anyone can connect via Bluetooth.",
|
||||
"config": {
|
||||
"microbit-dal": {
|
||||
"bluetooth": {
|
||||
"open": 1,
|
||||
"pairing_mode": 0,
|
||||
"whitelist": 0,
|
||||
"security_level": null
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "JustWorks pairing (default): Pairing is automatic once the pairing is initiated.",
|
||||
"config": {
|
||||
"microbit-dal": {
|
||||
"bluetooth": {
|
||||
"open": null,
|
||||
"pairing_mode": null,
|
||||
"whitelist": null,
|
||||
"security_level": null
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Passkey pairing: Pairing requires 6 digit key to pair.",
|
||||
"config": {
|
||||
"microbit-dal": {
|
||||
"bluetooth": {
|
||||
"open": 0,
|
||||
"pairing_mode": 1,
|
||||
"whitelist": 1,
|
||||
"security_level": "SECURITY_MODE_ENCRYPTION_WITH_MITM"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"installedVersion": "vzlhfd"
|
||||
}
|
18
libs/bluetooth/shims.d.ts
vendored
18
libs/bluetooth/shims.d.ts
vendored
@ -63,6 +63,18 @@ declare namespace bluetooth {
|
||||
//% parts="bluetooth" advanced=true shim=bluetooth::startUartService
|
||||
function startUartService(): void;
|
||||
|
||||
/**
|
||||
* Sends a buffer of data via Bluetooth UART
|
||||
*/
|
||||
//% shim=bluetooth::uartWriteBuffer
|
||||
function uartWriteBuffer(buffer: Buffer): void;
|
||||
|
||||
/**
|
||||
* Reads buffered UART data into a buffer
|
||||
*/
|
||||
//% shim=bluetooth::uartReadBuffer
|
||||
function uartReadBuffer(): Buffer;
|
||||
|
||||
/**
|
||||
* Registers an event to be fired when one of the delimiter is matched.
|
||||
* @param delimiters the characters to match received characters against.
|
||||
@ -98,7 +110,7 @@ declare namespace bluetooth {
|
||||
//% blockId=eddystone_advertise_url block="bluetooth advertise url %url|with power %power|connectable %connectable"
|
||||
//% parts=bluetooth weight=11 blockGap=8
|
||||
//% help=bluetooth/advertise-url blockExternalInputs=1 shim=bluetooth::advertiseUrl
|
||||
function advertiseUrl(url: string, power: number, connectable: boolean): void;
|
||||
function advertiseUrl(url: string, power: int32, connectable: boolean): void;
|
||||
|
||||
/**
|
||||
* Advertise an Eddystone UID
|
||||
@ -107,7 +119,7 @@ declare namespace bluetooth {
|
||||
* @param connectable true to keep bluetooth connectable for other services, false otherwise.
|
||||
*/
|
||||
//% parts=bluetooth weight=12 advanced=true shim=bluetooth::advertiseUidBuffer
|
||||
function advertiseUidBuffer(nsAndInstance: Buffer, power: number, connectable: boolean): void;
|
||||
function advertiseUidBuffer(nsAndInstance: Buffer, power: int32, connectable: boolean): void;
|
||||
|
||||
/**
|
||||
* Sets the bluetooth transmit power between 0 (minimal) and 7 (maximum).
|
||||
@ -115,7 +127,7 @@ declare namespace bluetooth {
|
||||
*/
|
||||
//% parts=bluetooth weight=5 help=bluetooth/set-transmit-power advanced=true
|
||||
//% blockId=bluetooth_settransmitpower block="bluetooth set transmit power %power" shim=bluetooth::setTransmitPower
|
||||
function setTransmitPower(power: number): void;
|
||||
function setTransmitPower(power: int32): void;
|
||||
|
||||
/**
|
||||
* Stops advertising Eddystone end points
|
||||
|
Reference in New Issue
Block a user