Increased default power to 6, added UART write block

This commit is contained in:
Martin Woolley 2016-06-23 11:38:23 +01:00
parent 7bf00ff139
commit 76005841fa
6 changed files with 139 additions and 4 deletions

View File

@ -27,7 +27,7 @@ bluetooth.startAccelerometerService();
### Video - Accelerometer service demo - Starts at 0:18
http://www.youtube.com/watch?v=aep_GVowKfs
http://www.youtube.com/watch?v=aep_GVowKfs#t=18s
### Advanced

View File

@ -0,0 +1,44 @@
# Bluetooth UART Service
### ~hint
![](/static/bluetooth/Bluetooth_SIG.png)
For another device like a smartphone to use any of the Bluetooth "services" which the micro:bit has, it must first connect to the micro:bit.
### ~
The Bluetooth UART service allows another device such as a smartphone to exchange any data it wants to with the micro:bit, in small chunks which are intended to be joined together. [UART[(https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter) stands for Universal Asynchronous Receiver Transmitter and is one way in which serial data communications can be performed, usually between two devices connected by a physical, wired connection. The Bluetooth UART service emulates the behaviour of a physical UART system and allows the exchange of a maximum of 20 bytes of data at a time in either direction.
When this service is used, the micro:bit sets up a 60 byte buffer and data it receives will be accumulated in the buffer until it is full. When using the UART service from your micro:bit code, you can indicate a special character which will be used to mean that the entire message in at most three chunks has now been sent by the other, connected device, at which point the micro:bit will release the entire contents of its buffer to any code trying to read it. In other words this special character, known as a 'delimiter' is used by the device connected to the micro:bit to mean "I've sent my whole message, you can now use it".
You could use the UART service for many things. It doesn't care what you put in messages which makes it very flexible. You could create a guessing game, with questions and answers passing between micro:bit and a smartphone or you could connect a camera to the micro:bit and transmit image data obtained from the edge connector, in chunks over Bluetooth to a smartphone. There are a great many possibilities.
To use the Bluetooth UART service from another device you'll need additional micro:bit code which reads and uses data from the UART buffer and / or writes data to the buffer for transmission over Bluetooth to another device.
```sig
bluetooth.startUartService();
```
### Example: Starting the Bluetooth UART service
The following code shows the Bluetooth UART service being started:
```blocks
bluetooth.startUartService();
```
### Video - UART service guessing game
https://www.youtube.com/watch?v=PgGeWddMAZ0
### Advanced
For more advanced information on the micro:bit Bluetooth UART service including information on using a smartphone, see the [Lancaster University micro:bit runtime technical documentation](http://lancaster-university.github.io/microbit-docs/ble/uart-service/)
### See also
[Bluetooth SIG](https://www.bluetooth.com), [Bluetooth on micro:bit resources](http://bluetooth-mdw.blogspot.co.uk/p/bbc-microbit.html)
```package
microbit-bluetooth
```

View File

@ -0,0 +1,52 @@
# UART Write
### ~hint
![](/static/bluetooth/Bluetooth_SIG.png)
For another device like a smartphone to use any of the Bluetooth "services" which the micro:bit has, it must first connect to the micro:bit.
### ~
The [Bluetooth UART service](start-uart-service.md) allows another device such as a smartphone to exchange any data it wants to with the micro:bit, in small chunks.
With the Bluetooth UART service running, this block allows a micro:bit to send data to a Bluetooth connected device.
```sig
bluetooth.uartWrite("");
```
### Example: Starting the Bluetooth UART service and then sending "HELLO" whenever button A is pressed and another device has connected over Bluetooth
```blocks
let connected = 0;
bluetooth.onBluetoothConnected(() => {
basic.showString("C");
connected = 1;
});
bluetooth.onBluetoothDisconnected(() => {
basic.showString("D");
connected = 0;
});
bluetooth.startUartService();
input.onButtonPressed(Button.A, () => {
if (connected == 1) {
bluetooth.uartWrite("HELLO");
}
});
```
### Video - UART service guessing game
https://www.youtube.com/watch?v=PgGeWddMAZ0
### Advanced
For more advanced information on the micro:bit Bluetooth UART service including information on using a smartphone, see the [Lancaster University micro:bit runtime technical documentation](http://lancaster-university.github.io/microbit-docs/ble/uart-service/)
### See also
[Bluetooth SIG](https://www.bluetooth.com), [Bluetooth on micro:bit resources](http://bluetooth-mdw.blogspot.co.uk/p/bbc-microbit.html)
```package
microbit-bluetooth
```

View File

@ -91,8 +91,32 @@ namespace bluetooth {
//% blockId=bluetooth_start_uart_service block="bluetooth|uart|service" blockGap=8
void startUartService() {
// 32 octet buffer size is
uart = new MicroBitUARTService(*uBit.ble, 32, 32);
// 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 buffer that can contain 3 x max length messages plus one octet for a terminator character
uart = new MicroBitUARTService(*uBit.ble, 61, 60);
}
/**
* Reads the Bluetooth UART service buffer, returning when a specified 'end of message' delimiter character is encountered
* @param eom End of Message delimiter character
*/
//% help=bluetooth/uart-read
//% blockId=bluetooth_uart_read block="bluetooth|uart|read %eom" blockGap=8
StringData* uartRead(char* eom) {
// temp hard coding of : as eom delimiter
char delim[6] = {':', 0};
return uart->readUntil(delim).leakData();
}
/**
* 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
void uartWrite(StringData* data) {
uart->send(ManagedString(data));
}
}

View File

@ -21,7 +21,7 @@
"open": 0,
"whitelist": 1,
"advertising_timeout": 0,
"tx_power": 0,
"tx_power": 6,
"dfu_service": 1,
"event_service": 1,
"device_info_service": 1

View File

@ -71,6 +71,21 @@ declare namespace bluetooth {
//% help=bluetooth/start-uart-service
//% blockId=bluetooth_start_uart_service block="bluetooth|uart|service" blockGap=8 shim=bluetooth::startUartService
function startUartService(): void;
/**
* Reads the Bluetooth UART service buffer, returning when a specified 'end of message' delimiter character is encountered
* @param eom End of Message delimiter character
*/
//% help=bluetooth/uart-read
//% blockId=bluetooth_uart_read block="bluetooth|uart|read %eom" blockGap=8 shim=bluetooth::uartRead
function uartRead(eom: char*): string;
/**
* 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 shim=bluetooth::uartWrite
function uartWrite(data: string): void;
}
// Auto-generated. Do not edit. Really.