merging ble changes
This commit is contained in:
@ -4,22 +4,6 @@
|
||||
|
||||
using namespace pxt;
|
||||
|
||||
enum Delimiters {
|
||||
//% block="new line"
|
||||
NewLine = 1,
|
||||
//% block=","
|
||||
Comma = 2,
|
||||
//% block="$"
|
||||
Dollar = 3,
|
||||
//% block=":"
|
||||
Colon = 4,
|
||||
//% block="."
|
||||
Fullstop = 5,
|
||||
//% block="#"
|
||||
Hash = 6,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Support for additional Bluetooth services.
|
||||
*/
|
||||
@ -27,12 +11,33 @@ enum Delimiters {
|
||||
namespace bluetooth {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth IO pin service.
|
||||
*/
|
||||
//% help=bluetooth/start-io-pin-service
|
||||
//% blockId=bluetooth_start_io_pin_service block="bluetooth io pin service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//% parts="bluetooth" weight=88
|
||||
void startIOPinService() {
|
||||
new MicroBitIOPinService(*uBit.ble, uBit.io);
|
||||
}
|
||||
@ -42,7 +47,7 @@ namespace bluetooth {
|
||||
*/
|
||||
//% help=bluetooth/start-led-service
|
||||
//% blockId=bluetooth_start_led_service block="bluetooth led service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//% parts="bluetooth" weight=87
|
||||
void startLEDService() {
|
||||
new MicroBitLEDService(*uBit.ble, uBit.display);
|
||||
}
|
||||
@ -52,7 +57,7 @@ namespace bluetooth {
|
||||
*/
|
||||
//% help=bluetooth/start-temperature-service
|
||||
//% blockId=bluetooth_start_temperature_service block="bluetooth temperature service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//% parts="bluetooth" weight=86
|
||||
void startTemperatureService() {
|
||||
new MicroBitTemperatureService(*uBit.ble, uBit.thermometer);
|
||||
}
|
||||
@ -61,38 +66,19 @@ namespace bluetooth {
|
||||
* Starts the Bluetooth magnetometer service
|
||||
*/
|
||||
//% help=bluetooth/start-magnetometer-service
|
||||
//% blockId=bluetooth_start_magnetometer_service block="bluetooth magnetometer service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//% blockId=bluetooth_start_magnetometer_service block="bluetooth magnetometer service"
|
||||
//% parts="bluetooth" weight=85
|
||||
void startMagnetometerService() {
|
||||
new MicroBitMagnetometerService(*uBit.ble, uBit.compass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth accelerometer service
|
||||
*/
|
||||
//% help=bluetooth/start-accelerometer-service
|
||||
//% blockId=bluetooth_start_accelerometer_service block="bluetooth accelerometer service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
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"
|
||||
void startButtonService() {
|
||||
new MicroBitButtonService(*uBit.ble);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth UART service
|
||||
*/
|
||||
//% help=bluetooth/start-uart-service
|
||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service"
|
||||
//% parts="bluetooth" advanced=true
|
||||
void startUartService() {
|
||||
if (uart) return;
|
||||
// 61 octet buffer size is 3 x (MTU - 3) + 1
|
||||
@ -102,13 +88,13 @@ namespace bluetooth {
|
||||
}
|
||||
|
||||
//%
|
||||
void uartWrite(StringData *data) {
|
||||
void uartWriteString(StringData *data) {
|
||||
startUartService();
|
||||
uart->send(ManagedString(data));
|
||||
}
|
||||
|
||||
//%
|
||||
StringData* uartRead(StringData *del) {
|
||||
StringData* uartReadUntil(StringData *del) {
|
||||
startUartService();
|
||||
return uart->readUntil(ManagedString(del)).leakData();
|
||||
}
|
||||
@ -133,7 +119,5 @@ namespace bluetooth {
|
||||
//% parts="bluetooth"
|
||||
void onBluetoothDisconnected(Action body) {
|
||||
registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,42 +1,48 @@
|
||||
/**
|
||||
* Support for additional Bluetooth services.
|
||||
*/
|
||||
//% color=#0082FB weight=20
|
||||
namespace bluetooth {
|
||||
/**
|
||||
* Returns the delimiter corresponding string
|
||||
*/
|
||||
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||
//% weight=1 parts="bluetooth"
|
||||
export function delimiters(del: Delimiters): string {
|
||||
// even though it might not look like, this is more
|
||||
// (memory) efficient than the C++ implementation, because the
|
||||
// strings are statically allocated and take no RAM
|
||||
switch (del) {
|
||||
case Delimiters.NewLine: return "\n"
|
||||
case Delimiters.Comma: return ","
|
||||
case Delimiters.Dollar: return "$"
|
||||
case Delimiters.Colon: return ":"
|
||||
case Delimiters.Fullstop: return "."
|
||||
case Delimiters.Hash: return "#"
|
||||
default: return "\n"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
//% help=bluetooth/uart-write
|
||||
//% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartWrite
|
||||
export function uartWrite(data: string): void {
|
||||
//% help=bluetooth/uart-write-string weight=80
|
||||
//% 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a numeric value to the serial
|
||||
*/
|
||||
//% help=bluetooth/uart-write-number weight=79
|
||||
//% weight=89 blockGap=8 advanced=true
|
||||
//% blockId=bluetooth_uart_writenumber block="bluetooth uart|write number %value"
|
||||
export function uartWriteNumber(value: number): void {
|
||||
uartWriteString(value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a ``name: value`` pair line to the serial.
|
||||
* @param name name of the value stream, eg: x
|
||||
* @param value to write
|
||||
*/
|
||||
//% weight=88 weight=78
|
||||
//% 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");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||
*/
|
||||
//% help=bluetooth/uart-read
|
||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartRead
|
||||
export function uartRead(del: string): string {
|
||||
//% help=bluetooth/uart-read-until weight=75
|
||||
//% blockId=bluetooth_uart_read block="bluetooth uart|read until %del=serial_delimiter_conv"
|
||||
//% parts="bluetooth" shim=bluetooth::uartReadUntil advanced=true
|
||||
export function uartReadUntil(del: string): string {
|
||||
// dummy implementation for simulator
|
||||
return "???"
|
||||
}
|
||||
|
16
libs/bluetooth/enums.d.ts
vendored
16
libs/bluetooth/enums.d.ts
vendored
@ -1,20 +1,4 @@
|
||||
// Auto-generated. Do not edit.
|
||||
|
||||
|
||||
declare enum Delimiters {
|
||||
//% block="new line"
|
||||
NewLine = 1,
|
||||
//% block=","
|
||||
Comma = 2,
|
||||
//% block="$"
|
||||
Dollar = 3,
|
||||
//% block=":"
|
||||
Colon = 4,
|
||||
//% block="."
|
||||
Fullstop = 5,
|
||||
//% block="#"
|
||||
Hash = 6,
|
||||
}
|
||||
declare namespace bluetooth {
|
||||
}
|
||||
|
||||
|
74
libs/bluetooth/shims.d.ts
vendored
74
libs/bluetooth/shims.d.ts
vendored
@ -7,44 +7,12 @@
|
||||
//% color=#0082FB weight=20
|
||||
declare namespace bluetooth {
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth IO pin service.
|
||||
*/
|
||||
//% help=bluetooth/start-io-pin-service
|
||||
//% blockId=bluetooth_start_io_pin_service block="bluetooth io pin service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startIOPinService
|
||||
function startIOPinService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth LED service
|
||||
*/
|
||||
//% help=bluetooth/start-led-service
|
||||
//% blockId=bluetooth_start_led_service block="bluetooth led service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startLEDService
|
||||
function startLEDService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth temperature service
|
||||
*/
|
||||
//% help=bluetooth/start-temperature-service
|
||||
//% blockId=bluetooth_start_temperature_service block="bluetooth temperature service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startTemperatureService
|
||||
function startTemperatureService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth magnetometer service
|
||||
*/
|
||||
//% help=bluetooth/start-magnetometer-service
|
||||
//% blockId=bluetooth_start_magnetometer_service block="bluetooth magnetometer service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startMagnetometerService
|
||||
function startMagnetometerService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth accelerometer service
|
||||
*/
|
||||
//% help=bluetooth/start-accelerometer-service
|
||||
//% blockId=bluetooth_start_accelerometer_service block="bluetooth accelerometer service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startAccelerometerService
|
||||
//% blockId=bluetooth_start_accelerometer_service block="bluetooth accelerometer service"
|
||||
//% parts="bluetooth" weight=90 blockGap=8 shim=bluetooth::startAccelerometerService
|
||||
function startAccelerometerService(): void;
|
||||
|
||||
/**
|
||||
@ -52,15 +20,47 @@ declare namespace bluetooth {
|
||||
*/
|
||||
//% help=bluetooth/start-button-service
|
||||
//% blockId=bluetooth_start_button_service block="bluetooth button service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startButtonService
|
||||
//% parts="bluetooth" weight=89 shim=bluetooth::startButtonService
|
||||
function startButtonService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth IO pin service.
|
||||
*/
|
||||
//% help=bluetooth/start-io-pin-service
|
||||
//% blockId=bluetooth_start_io_pin_service block="bluetooth io pin service" blockGap=8
|
||||
//% parts="bluetooth" weight=88 shim=bluetooth::startIOPinService
|
||||
function startIOPinService(): void;
|
||||
|
||||
/**
|
||||
* 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 shim=bluetooth::startLEDService
|
||||
function startLEDService(): void;
|
||||
|
||||
/**
|
||||
* 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 shim=bluetooth::startTemperatureService
|
||||
function startTemperatureService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth magnetometer service
|
||||
*/
|
||||
//% help=bluetooth/start-magnetometer-service
|
||||
//% blockId=bluetooth_start_magnetometer_service block="bluetooth magnetometer service"
|
||||
//% parts="bluetooth" weight=85 shim=bluetooth::startMagnetometerService
|
||||
function startMagnetometerService(): void;
|
||||
|
||||
/**
|
||||
* Starts the Bluetooth UART service
|
||||
*/
|
||||
//% help=bluetooth/start-uart-service
|
||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::startUartService
|
||||
//% blockId=bluetooth_start_uart_service block="bluetooth uart service"
|
||||
//% parts="bluetooth" advanced=true shim=bluetooth::startUartService
|
||||
function startUartService(): void;
|
||||
|
||||
/**
|
||||
|
16
libs/core/enums.d.ts
vendored
16
libs/core/enums.d.ts
vendored
@ -311,6 +311,22 @@ declare namespace motors {
|
||||
//% block=9600
|
||||
BaudRate9600 = 9600,
|
||||
}
|
||||
|
||||
|
||||
declare enum Delimiters {
|
||||
//% block="new line"
|
||||
NewLine = 1,
|
||||
//% block=","
|
||||
Comma = 2,
|
||||
//% block="$"
|
||||
Dollar = 3,
|
||||
//% block=":"
|
||||
Colon = 4,
|
||||
//% block="."
|
||||
Fullstop = 5,
|
||||
//% block="#"
|
||||
Hash = 6,
|
||||
}
|
||||
declare namespace serial {
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "ksbit.h"
|
||||
|
||||
enum class SerialPin {
|
||||
enum SerialPin {
|
||||
P0 = MICROBIT_ID_IO_P0,
|
||||
P1 = MICROBIT_ID_IO_P1,
|
||||
P2 = MICROBIT_ID_IO_P2,
|
||||
@ -12,36 +12,52 @@ enum class SerialPin {
|
||||
//P16 = MICROBIT_ID_IO_P16
|
||||
};
|
||||
|
||||
enum class BaudRate {
|
||||
enum BaudRate {
|
||||
//% block=115200
|
||||
BaudRate115200 = 115200,
|
||||
//% block=9600
|
||||
BaudRate9600 = 9600
|
||||
};
|
||||
|
||||
enum Delimiters {
|
||||
//% block="new line"
|
||||
NewLine = 1,
|
||||
//% block=","
|
||||
Comma = 2,
|
||||
//% block="$"
|
||||
Dollar = 3,
|
||||
//% block=":"
|
||||
Colon = 4,
|
||||
//% block="."
|
||||
Fullstop = 5,
|
||||
//% block="#"
|
||||
Hash = 6,
|
||||
};
|
||||
|
||||
//% weight=2 color=30
|
||||
//% advanced=true
|
||||
namespace serial {
|
||||
// note that at least one // followed by % is needed per declaration!
|
||||
|
||||
/**
|
||||
* Reads a line of text from the serial port.
|
||||
* Reads a line of text from the serial port and returns the buffer when the delimiter is met.
|
||||
* @param delimiter text delimiter that separates each text chunk
|
||||
*/
|
||||
//% help=serial/read-line
|
||||
//% blockId=serial_read_line block="serial read line"
|
||||
//% weight=20
|
||||
StringData* readLine() {
|
||||
return uBit.serial.readUntil(ManagedString("\n")).leakData();
|
||||
//% help=serial/read-until
|
||||
//% blockId=serial_read_until block="serial|read until %delimiter=serial_delimiter_conv"
|
||||
//% weight=19
|
||||
StringData* readUntil(StringData* delimiter) {
|
||||
return uBit.serial.readUntil(ManagedString(delimiter)).leakData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a piece of text through Serial connection.
|
||||
* Reads a line of text from the serial port.
|
||||
*/
|
||||
//% help=serial/write-string
|
||||
//% weight=87
|
||||
//% blockId=serial_writestring block="serial write string %text"
|
||||
void writeString(StringData *text) {
|
||||
uBit.serial.send(ManagedString(text));
|
||||
//% help=serial/read-line
|
||||
//% blockId=serial_read_line block="serial|read line"
|
||||
//% weight=20 blockGap=8
|
||||
StringData* readLine() {
|
||||
return readUntil(ManagedString("\n").leakData());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,12 +65,22 @@ namespace serial {
|
||||
* @param delimiters the characters to match received characters against. eg:"\n"
|
||||
*/
|
||||
// help=serial/on-data-received
|
||||
// weight=19
|
||||
// weight=18
|
||||
void onDataReceived(StringData* delimiters, Action body) {
|
||||
uBit.serial.eventOn(ManagedString(delimiters));
|
||||
registerWithDal(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a piece of text through Serial connection.
|
||||
*/
|
||||
//% help=serial/write-string
|
||||
//% weight=87
|
||||
//% blockId=serial_writestring block="serial|write string %text"
|
||||
void writeString(StringData *text) {
|
||||
uBit.serial.send(ManagedString(text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically configuring the serial instance to use pins other than USBTX and USBRX.
|
||||
* @param tx the new transmission pins
|
||||
@ -63,7 +89,7 @@ namespace serial {
|
||||
*/
|
||||
//% weight=10
|
||||
//% help=serial/redirect-to
|
||||
//% blockId=serial_redirect block="serial redirect to|TX %tx|RX %rx|at baud rate %rate"
|
||||
//% blockId=serial_redirect block="serial|redirect to|TX %tx|RX %rx|at baud rate %rate"
|
||||
//% blockExternalInputs=1
|
||||
void redirect(SerialPin tx, SerialPin rx, BaudRate rate) {
|
||||
uBit.serial.redirect((PinName)tx, (PinName)rx);
|
||||
|
@ -46,4 +46,24 @@ namespace serial {
|
||||
export function onLineReceived(body: Action): void {
|
||||
// serial.onDataReceived("\n", body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the delimiter corresponding string
|
||||
*/
|
||||
//% blockId="serial_delimiter_conv" block="%del"
|
||||
//% weight=1
|
||||
export function delimiters(del: Delimiters): string {
|
||||
// even though it might not look like, this is more
|
||||
// (memory) efficient than the C++ implementation, because the
|
||||
// strings are statically allocated and take no RAM
|
||||
switch (del) {
|
||||
case Delimiters.NewLine: return "\n"
|
||||
case Delimiters.Comma: return ","
|
||||
case Delimiters.Dollar: return "$"
|
||||
case Delimiters.Colon: return ":"
|
||||
case Delimiters.Fullstop: return "."
|
||||
case Delimiters.Hash: return "#"
|
||||
default: return "\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
libs/core/shims.d.ts
vendored
17
libs/core/shims.d.ts
vendored
@ -671,12 +671,21 @@ declare namespace pins {
|
||||
//% advanced=true
|
||||
declare namespace serial {
|
||||
|
||||
/**
|
||||
* Reads a line of text from the serial port and returns the buffer when the delimiter is met.
|
||||
* @param delimiter text delimiter that separates each text chunk
|
||||
*/
|
||||
//% help=serial/read-until
|
||||
//% blockId=serial_read_until block="serial|read until %delimiter=serial_delimiter_conv"
|
||||
//% weight=19 shim=serial::readUntil
|
||||
function readUntil(delimiter: string): string;
|
||||
|
||||
/**
|
||||
* Reads a line of text from the serial port.
|
||||
*/
|
||||
//% help=serial/read-line
|
||||
//% blockId=serial_read_line block="serial read line"
|
||||
//% weight=20 shim=serial::readLine
|
||||
//% blockId=serial_read_line block="serial|read line"
|
||||
//% weight=20 blockGap=8 shim=serial::readLine
|
||||
function readLine(): string;
|
||||
|
||||
/**
|
||||
@ -684,7 +693,7 @@ declare namespace serial {
|
||||
*/
|
||||
//% help=serial/write-string
|
||||
//% weight=87
|
||||
//% blockId=serial_writestring block="serial write string %text" shim=serial::writeString
|
||||
//% blockId=serial_writestring block="serial|write string %text" shim=serial::writeString
|
||||
function writeString(text: string): void;
|
||||
|
||||
/**
|
||||
@ -695,7 +704,7 @@ declare namespace serial {
|
||||
*/
|
||||
//% weight=10
|
||||
//% help=serial/redirect-to
|
||||
//% blockId=serial_redirect block="serial redirect to|TX %tx|RX %rx|at baud rate %rate"
|
||||
//% blockId=serial_redirect block="serial|redirect to|TX %tx|RX %rx|at baud rate %rate"
|
||||
//% blockExternalInputs=1 shim=serial::redirect
|
||||
function redirect(tx: SerialPin, rx: SerialPin, rate: BaudRate): void;
|
||||
}
|
||||
|
Reference in New Issue
Block a user