mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 15:02:52 -05:00
Adding promises to return on BLE write. Some refactoring.
This commit is contained in:
parent
701f430e56
commit
73cc149783
3 changed files with 22 additions and 19 deletions
|
@ -111,20 +111,22 @@ class MicroBit {
|
|||
|
||||
/**
|
||||
* @param {string} text - the text to display.
|
||||
* @return {Promise} - a Promise that resolves when writing to device.
|
||||
*/
|
||||
displayText (text) {
|
||||
const output = new Uint8Array(text.length);
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
output[i] = text.charCodeAt(i);
|
||||
}
|
||||
this._writeSessionData(BLECommand.CMD_DISPLAY_TEXT, output);
|
||||
return this._writeSessionData(BLECommand.CMD_DISPLAY_TEXT, output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint8Array} matrix - the matrix to display.
|
||||
* @return {Promise} - a Promise that resolves when writing to device.
|
||||
*/
|
||||
displayMatrix (matrix) {
|
||||
this._writeSessionData(BLECommand.CMD_DISPLAY_LED, matrix);
|
||||
return this._writeSessionData(BLECommand.CMD_DISPLAY_LED, matrix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -212,6 +214,7 @@ class MicroBit {
|
|||
* Write a message to the device BLE session.
|
||||
* @param {number} command - the BLE command hex.
|
||||
* @param {Uint8Array} message - the message to write.
|
||||
* @return {Promise} - a Promise that resolves when writing to device.
|
||||
* @private
|
||||
*/
|
||||
_writeSessionData (command, message) {
|
||||
|
@ -220,8 +223,8 @@ class MicroBit {
|
|||
for (let i = 0; i < message.length; i++) {
|
||||
output[i + 1] = message[i];
|
||||
}
|
||||
const b64enc = Base64Util.uint8ArrayToBase64(output);
|
||||
this._ble.write(BLEUUID.service, BLEUUID.txChar, b64enc, 'base64');
|
||||
const data = Base64Util.uint8ArrayToBase64(output);
|
||||
return this._ble.write(BLEUUID.service, BLEUUID.txChar, data, 'base64');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -494,17 +497,18 @@ class Scratch3MicroBitBlocks {
|
|||
/**
|
||||
* Display text on the 5x5 LED matrix.
|
||||
* @param {object} args - the block's arguments.
|
||||
* @return {Promise} - a Promise that resolves when writing to device.
|
||||
* Note the limit is 19 characters
|
||||
*/
|
||||
displayText (args) {
|
||||
const text = String(args.TEXT).substring(0, 19);
|
||||
this._device.displayText(text);
|
||||
return;
|
||||
return this._device.displayText(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a predefined symbol on the 5x5 LED matrix.
|
||||
* @param {object} args - the block's arguments.
|
||||
* @return {Promise} - a Promise that resolves when writing to device.
|
||||
*/
|
||||
displaySymbol (args) {
|
||||
const hex = symbols2hex[args.SYMBOL];
|
||||
|
@ -514,8 +518,7 @@ class Scratch3MicroBitBlocks {
|
|||
this._device.ledMatrixState[2] = (hex >> 10) & 0x1F;
|
||||
this._device.ledMatrixState[3] = (hex >> 5) & 0x1F;
|
||||
this._device.ledMatrixState[4] = hex & 0x1F;
|
||||
this._device.displayMatrix(this._device.ledMatrixState);
|
||||
return;
|
||||
return this._device.displayMatrix(this._device.ledMatrixState);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,7 @@ class BLESession extends JSONRPCWebSocket {
|
|||
this._socketPromise = new Promise((resolve, reject) => {
|
||||
this._ws.onopen = resolve;
|
||||
this._ws.onerror = this._sendError(); // TODO: socket error?
|
||||
// TODO: generally handle socket disconnects as errors
|
||||
});
|
||||
|
||||
this._availablePeripherals = {};
|
||||
|
@ -37,18 +38,14 @@ class BLESession extends JSONRPCWebSocket {
|
|||
requestDevice () {
|
||||
// TODO: add timeout for 'no devices yet found' ?
|
||||
if (this._ws.readyState === 1) {
|
||||
this.sendRemoteRequest('discover', this._deviceOptions)
|
||||
.catch(e => {
|
||||
// TODO: what if discover doesn't initiate?
|
||||
this._sendError(e);
|
||||
});
|
||||
this.sendRemoteRequest('discover', this._deviceOptions)
|
||||
.catch(e => this._sendError(e));
|
||||
} else {
|
||||
// Try again to connect to the websocket
|
||||
this._socketPromise(this.sendRemoteRequest('discover', this._deviceOptions))
|
||||
.catch(e => {
|
||||
// TODO: what if discover doesn't initiate?
|
||||
this._sendError(e);
|
||||
});
|
||||
this._socketPromise(this.sendRemoteRequest('discover', this._deviceOptions))
|
||||
.catch(e => this._sendError(e));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +61,7 @@ class BLESession extends JSONRPCWebSocket {
|
|||
this._connectCallback();
|
||||
})
|
||||
.catch(e => {
|
||||
// TODO: what if the peripheral loses power?
|
||||
// TODO: what if the peripheral loses power? (web socket closes?)
|
||||
// TODO: what if tries to connect to an unknown peripheral id?
|
||||
this._sendError(e);
|
||||
});
|
||||
|
@ -77,8 +74,7 @@ class BLESession extends JSONRPCWebSocket {
|
|||
* @return {object} - optional return value.
|
||||
*/
|
||||
didReceiveCall (method, params) {
|
||||
// TODO: does didReceiveCall receive any errors?
|
||||
// TODO: Add peripheral 'undiscover' handling
|
||||
// TODO: Add peripheral 'undiscover' handling with timeout?
|
||||
switch (method) {
|
||||
case 'didDiscoverPeripheral':
|
||||
this._availablePeripherals[params.peripheralId] = params;
|
||||
|
|
|
@ -27,6 +27,10 @@ class JSONRPCWebSocket extends JSONRPC {
|
|||
|
||||
_onSocketMessage (e) {
|
||||
const json = JSON.parse(e.data);
|
||||
if (json.method !== 'characteristicDidChange') {
|
||||
console.log('received message: ');
|
||||
console.log(json);
|
||||
}
|
||||
this._handleMessage(json);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue