BLE HF2 log service (#1549)
* basic hf2 service * service to send hf2 log messages * use common console.log * updated shims * adding config * adding simulator * fix hf2 logging * hide console blocks
This commit is contained in:
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_READ | 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
|
@ -1,6 +1,7 @@
|
||||
#include "pxt.h"
|
||||
#include "MESEvents.h"
|
||||
#include "MicroBitUARTService.h"
|
||||
#include "BLEHF2Service.h"
|
||||
|
||||
using namespace pxt;
|
||||
|
||||
@ -10,6 +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->data, msg->length, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth accelerometer service
|
||||
|
@ -1,8 +1,18 @@
|
||||
/// <reference no-default-lib="true"/>
|
||||
/**
|
||||
* Support for additional Bluetooth services.
|
||||
*/
|
||||
//% color=#007EF4 weight=96 icon="\uf294"
|
||||
namespace bluetooth {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -6,7 +6,9 @@
|
||||
"enums.d.ts",
|
||||
"shims.d.ts",
|
||||
"bluetooth.ts",
|
||||
"bluetooth.cpp"
|
||||
"bluetooth.cpp",
|
||||
"BLEHF2Service.h",
|
||||
"BLEHF2Service.cpp"
|
||||
],
|
||||
"icon": "./static/packages/bluetooth/icon.png",
|
||||
"public": true,
|
||||
|
Reference in New Issue
Block a user