* use notify isntead of updateCharacteristicValue

* typo

* auto-start service
This commit is contained in:
Peli de Halleux 2018-10-02 10:11:34 -07:00 коммит произвёл GitHub
Родитель e5199542b3
Коммит 120441c29d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 35 добавлений и 30 удалений

Просмотреть файл

@ -35,10 +35,10 @@ const uint8_t midiServiceUuid[] = {
BluetoothMIDIService::BluetoothMIDIService(BLEDevice *dev): ble(*dev) {
timestamp = 0;
memset(midi, 0, sizeof(midi));
memset(midiBuffer, 0, sizeof(midiBuffer));
firstRead = true;
GattCharacteristic midiCharacteristic(midiCharacteristicUuid, midi, 0, sizeof(midi),
GattCharacteristic midiCharacteristic(midiCharacteristicUuid, midiBuffer, 0, sizeof(midiBuffer),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
);
@ -64,7 +64,7 @@ void BluetoothMIDIService::onDataRead(const GattReadCallbackParams* params)
{
if (firstRead) {
// send empty payload upon first connect
ble.updateCharacteristicValue(midiCharacteristicHandle, midi, 0);
ble.gattServer().notify(midiCharacteristicHandle, (uint8_t *)midiBuffer, 0);
firstRead = false;
}
}
@ -82,35 +82,35 @@ bool BluetoothMIDIService::connected() {
void BluetoothMIDIService::sendMidiMessage(uint8_t data0) {
if (connected()) {
unsigned int ticks = tick.read_ms() & 0x1fff;
midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
midiBuffer[0] = 0x80 | ((ticks >> 7) & 0x3f);
midiBuffer[1] = 0x80 | (ticks & 0x7f);
midiBuffer[2] = data0;
ble.updateCharacteristicValue(midiCharacteristicHandle, midi, 3);
ble.gattServer().notify(midiCharacteristicHandle, (uint8_t *)midiBuffer, 3);
}
}
void BluetoothMIDIService::sendMidiMessage(uint8_t data0, uint8_t data1) {
if (connected()) {
unsigned int ticks = tick.read_ms() & 0x1fff;
midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
midi[3] = data1;
midiBuffer[0] = 0x80 | ((ticks >> 7) & 0x3f);
midiBuffer[1] = 0x80 | (ticks & 0x7f);
midiBuffer[2] = data0;
midiBuffer[3] = data1;
ble.updateCharacteristicValue(midiCharacteristicHandle, midi, 4);
ble.gattServer().notify(midiCharacteristicHandle, (uint8_t *)midiBuffer, 4);
}
}
void BluetoothMIDIService::sendMidiMessage(uint8_t data0, uint8_t data1, uint8_t data2) {
if (connected()) {
unsigned int ticks = tick.read_ms() & 0x1fff;
midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
midi[3] = data1;
midi[4] = data2;
midiBuffer[0] = 0x80 | ((ticks >> 7) & 0x3f);
midiBuffer[1] = 0x80 | (ticks & 0x7f);
midiBuffer[2] = data0;
midiBuffer[3] = data1;
midiBuffer[4] = data2;
ble.updateCharacteristicValue(midiCharacteristicHandle, midi, 5);
ble.gattServer().notify(midiCharacteristicHandle, (uint8_t *)midiBuffer, 5);
}
}

Просмотреть файл

@ -47,7 +47,7 @@ private:
void onDisconnection(const Gap::DisconnectionCallbackParams_t* params);
uint16_t timestamp;
uint8_t midi[5];
uint8_t midiBuffer[5];
bool firstRead;
BLEDevice &ble;

Просмотреть файл

@ -15,21 +15,20 @@ For another device like a smartphone to use any of the Bluetooth "services" whic
This package allows the @boardname@ to act as a MIDI peripherical, like a piano. It requires to connect to a BLE MIDI device to receive the commands and play them.
Place a ``||bluetooth start midi service||`` block in your program to enable MDI over Bluetooth low energy.
```blocks
bluetooth.startMidiService();
```
This library uses the [MIDI package](/pkg/microsoft/pxt-midi).
Please refer to that project documentation for further details.
Please refer to that project documentation for further details on using MIDI commands.
## Supported Platforms
This package is currently only supported on iOS (iPad/iPhone).
Any app that supports a MIDI keyboard or instrument should work.
* iPhone/iPad: [Apple GarageBand](https://itunes.apple.com/us/app/garageband/id408709785?mt=8)
### Apple GarageBand
* [iPhone/iPad App]](https://itunes.apple.com/us/app/garageband/id408709785?mt=8)
Go to settings (gearwheel), click **Advanced**, click **Bluetooth MIDI device** and connect to the @boardname@.
If the @boardname@ is marked as offline, click **Edit** and **Forget** the device.
## Supported targets

Просмотреть файл

@ -4,12 +4,13 @@ namespace bluetooth {
*/
//% blockId=bluetooth_start_midi block="bluetooth start midi service"
//% part=bluetooth
//% blockHidden=1 deprecated=true
export function startMidiService() {
function send(buffer: Buffer) {
bluetooth.midiSendMessage(buffer);
}
midi.setTransport(send);
bluetooth.midiSendMessage(pins.createBuffer(0));
bluetooth.midiSendMessage(pins.createBuffer(0)); // does nothing but starts service lazily
}
/**
@ -20,4 +21,7 @@ namespace bluetooth {
export function midiSendMessage(data: Buffer) {
return;
}
}
}
// automatically start midi service
bluetooth.startMidiService();

Просмотреть файл

@ -4,7 +4,9 @@ bluetooth.onBluetoothConnected(() => {
bluetooth.onBluetoothDisconnected(() => {
basic.showString("D")
})
bluetooth.startMidiService();
// this is automatic
// bluetooth.startMidiService();
basic.showString("S")
input.onButtonPressed(Button.A, () => {