microbyte
Version:
A wrapper for bluetooth and USB interactivity between browsers and micro:bits
393 lines (382 loc) • 82.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.microbyte = {}));
})(this, (function (exports) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
/**
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
*
* SPDX-License-Identifier: MIT
*/
/* eslint-disable @typescript-eslint/no-namespace */
/**
* References to the Bluetooth Profile UUIDs.
*/
var MBSpecs;
(function (MBSpecs) {
/**
* The UUIDs of the services available on the micro:bit.
*/
let Services;
(function (Services) {
/**
* The UUID of the micro:bit's UART service.
*/
Services.UART_SERVICE = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
/**
* The micro:bits accelerometer service.
*/
Services.ACCEL_SERVICE = 'e95d0753-251d-470a-a062-fa1922dfa9a8';
/**
* The device information service. Exposes information about manufacturer, vendor, and firmware version.
*/
Services.DEVICE_INFO_SERVICE = '0000180a-0000-1000-8000-00805f9b34fb';
/**
* Used for controlling the LEDs on the micro:bit.
*/
Services.LED_SERVICE = 'e95dd91d-251d-470a-a062-fa1922dfa9a8';
/**
* The UUID of the micro:bit's IO service.
*/
Services.IO_SERVICE = 'e95d127b-251d-470a-a062-fa1922dfa9a8';
/**
* Service for buttons on the micro:bit.
*/
Services.BUTTON_SERVICE = 'e95d9882-251d-470a-a062-fa1922dfa9a8';
})(Services = MBSpecs.Services || (MBSpecs.Services = {}));
/**
* The UUIDs of the characteristics available on the micro:bit.
*/
let Characteristics;
(function (Characteristics) {
/**
* Characteristic for the A button.
*/
Characteristics.BUTTON_A = 'e95dda90-251d-470a-a062-fa1922dfa9a8';
/**
* Characteristic for the B button.
*/
Characteristics.BUTTON_B = 'e95dda91-251d-470a-a062-fa1922dfa9a8';
/**
* The accelerometer data characteristic.
*/
Characteristics.ACCEL_DATA = 'e95dca4b-251d-470a-a062-fa1922dfa9a8';
/**
* IO data characteristic. Used for controlling IO pins on the micro:bit.
*/
Characteristics.IO_DATA = 'e95d8d00-251d-470a-a062-fa1922dfa9a8';
/**
* Allows the state of any|all LEDs in the 5x5 grid to be set to on or off with a single GATT operation.
*
* Octet 0, LED Row 1: bit4 bit3 bit2 bit1 bit0
*
* Octet 1, LED Row 2: bit4 bit3 bit2 bit1 bit0
*
* Octet 2, LED Row 3: bit4 bit3 bit2 bit1 bit0
*
* Octet 3, LED Row 4: bit4 bit3 bit2 bit1 bit0
*
* Octet 4, LED Row 5: bit4 bit3 bit2 bit1 bit0
*/
Characteristics.LED_MATRIX_STATE = 'e95d7b77-251d-470a-a062-fa1922dfa9a8';
/**
* The model number of the micro:bit as a string.
*/
Characteristics.MODEL_NUMBER = '00002a24-0000-1000-8000-00805f9b34fb';
/**
* The UUID of the micro:bit's UART TX characteristic.
* Used to listen for data from the micro:bit.
*/
Characteristics.UART_DATA_TX = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
/**
* The UUID of the micro:bit's UART RX characteristic.
* Used for sending data to the micro:bit.
*/
Characteristics.UART_DATA_RX = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
})(Characteristics = MBSpecs.Characteristics || (MBSpecs.Characteristics = {}));
let USBSpecs;
(function (USBSpecs) {
USBSpecs.PRODUCT_ID = 516;
USBSpecs.VENDOR_ID = 3368;
USBSpecs.FICR = 0x10000000;
USBSpecs.DEVICE_ID_1 = 0x064;
USBSpecs.MICROBIT_NAME_LENGTH = 5;
USBSpecs.MICROBIT_NAME_CODE_LETTERS = 5;
})(USBSpecs = MBSpecs.USBSpecs || (MBSpecs.USBSpecs = {}));
(function (Button) {
Button[Button["A"] = 0] = "A";
Button[Button["B"] = 1] = "B";
})(MBSpecs.Button || (MBSpecs.Button = {}));
(function (ButtonStates) {
ButtonStates[ButtonStates["Released"] = 0] = "Released";
ButtonStates[ButtonStates["Pressed"] = 1] = "Pressed";
ButtonStates[ButtonStates["LongPressed"] = 2] = "LongPressed";
})(MBSpecs.ButtonStates || (MBSpecs.ButtonStates = {}));
/**
* Ordered list of all IO pins. Such as 0, 2, '3V', 'GND', etc.
*/
MBSpecs.IO_PIN_LAYOUT = [
3,
0,
4,
5,
6,
7,
1,
8,
9,
10,
11,
12,
2,
13,
14,
15,
16,
17,
'3V',
18,
19,
20,
21,
'GND',
24,
];
/**
* Utilities for working with the micro:bit's Bluetooth Profile.
*/
class Utility {
/**
* Fetches the model number of the micro:bit.
* @param {BluetoothRemoteGATTServer} gattServer The GATT server to read from.
* @return {Promise<number>} The model number of the micro:bit. 1 for the original, 2 for the new.
*/
static getModelNumber(gattServer) {
return __awaiter(this, void 0, void 0, function* () {
try {
const deviceInfo = yield gattServer.getPrimaryService(Services.DEVICE_INFO_SERVICE);
// TODO: Next line has been observed to fail. Proper error handling needed.
// Triage the issue, if no cause can be found please remove this todo.
const modelNumber = yield deviceInfo.getCharacteristic(Characteristics.MODEL_NUMBER);
// Read the value and convert it to UTF-8 (as specified in the Bluetooth specification).
const modelNumberValue = yield modelNumber.readValue();
const decodedModelNumber = new TextDecoder().decode(modelNumberValue);
// The model number either reads "BBC micro:bit" or "BBC micro:bit V2.0". Still unsure if those are the only cases.
if (decodedModelNumber.toLowerCase() === 'BBC micro:bit'.toLowerCase()) {
return 1;
}
if (decodedModelNumber.toLowerCase().includes('BBC micro:bit v2'.toLowerCase())) {
return 2;
}
}
catch (e) {
console.log(e);
}
throw new Error('Could not read model number');
});
}
/**
* Converts a micro:bit serial number to it's corresponding friendly name
* @param {number} serialNo The serial number of the micro:bit
* @returns {string} the name of the micro:bit.
*/
static serialNumberToName(serialNo) {
let d = USBSpecs.MICROBIT_NAME_CODE_LETTERS;
let ld = 1;
let name = '';
for (let i = 0; i < USBSpecs.MICROBIT_NAME_LENGTH; i++) {
const h = Math.floor((serialNo % d) / ld);
serialNo -= h;
d *= USBSpecs.MICROBIT_NAME_CODE_LETTERS;
ld *= USBSpecs.MICROBIT_NAME_CODE_LETTERS;
name = Utility.CODEBOOK_USB[i][h] + name;
}
return name;
}
static messageToDataview(message, delimiter = '#') {
if (delimiter.length != 1) {
throw new Error('The delimiter must be 1 character long');
}
const fullMessage = `${message}${delimiter}`;
const view = new DataView(new ArrayBuffer(fullMessage.length));
for (let i = 0; i < fullMessage.length; i++) {
view.setUint8(i, fullMessage.charCodeAt(i));
}
return view;
}
/**
* Converts a pairing pattern to a name.
* See guide on microbit names to understand how a pattern is turned into a name
* https://support.microbit.org/support/solutions/articles/19000067679-how-to-find-the-name-of-your-micro-bit
* @param {boolean[]} pattern The pattern to convert.
* @returns {string} The name of the micro:bit.
*/
static patternToName(pattern) {
const code = [' ', ' ', ' ', ' ', ' '];
for (let col = 0; col < USBSpecs.MICROBIT_NAME_LENGTH; col++) {
for (let row = 0; row < USBSpecs.MICROBIT_NAME_LENGTH; row++) {
if (pattern[row * USBSpecs.MICROBIT_NAME_LENGTH + col]) {
// Find the first vertical on/true in each column
code[col] = this.CODEBOOK_BLUETOOTH[row][col]; // Use code-book to find char
break; // Rest of column is irrelevant
}
// If we get to here the pattern is not legal, and the returned name
// will not match any microbit.
}
}
return code.join('');
}
static isPairingPattermValid(pattern) {
for (let col = 0; col < USBSpecs.MICROBIT_NAME_LENGTH; col++) {
let isAnyHighlighted = false;
for (let row = 0; row < USBSpecs.MICROBIT_NAME_LENGTH; row++) {
if (pattern[row * USBSpecs.MICROBIT_NAME_LENGTH + col]) {
isAnyHighlighted = true;
}
}
if (!isAnyHighlighted) {
return false;
}
}
return true;
}
/**
* Converts a name to a pairing pattern.
* IMPORTANT: Assumes correct microbit name. Not enough error handling for
* incorrect names.
* @param {string} name The name of the micro:bit
* @returns {boolean[]} The pairing pattern
*/
static nameToPattern(name) {
const pattern = new Array(25).fill(true);
// if wrong name length, return empty pattern
if (name.length != USBSpecs.MICROBIT_NAME_LENGTH) {
return pattern.map(() => false);
}
for (let column = 0; column < USBSpecs.MICROBIT_NAME_LENGTH; column++) {
for (let row = 0; row < USBSpecs.MICROBIT_NAME_LENGTH; row++) {
if (this.CODEBOOK_BLUETOOTH[row][column] === name.charAt(column)) {
break;
}
pattern[5 * row + column] = false;
}
}
return pattern;
}
/**
* Converts a binary number represented as an array of numbers into an octet.
*
* @param array Bitmap array to convert.
* @returns {number} The octet.
*/
static arrayToOctet(array) {
let numArray = array;
if (typeof array[0] === 'boolean') {
const typedArray = array;
numArray = typedArray.map((value) => (value ? 1 : 0));
}
let sum = 0;
// We track the bit pos because the value of the octet, will just be the decimal value of the bit representation.
let bitPos = 0;
// Walk in reverse order to match the order of the micro:bit.
for (let i = numArray.length - 1; i >= 0; i--) {
// Just calculate the decimal value of the bit position and add it to the sum.
sum += numArray[i] * Math.pow(2, bitPos);
bitPos++;
}
return sum;
}
}
Utility.CODEBOOK_USB = [
['z', 'v', 'g', 'p', 't'],
['u', 'o', 'i', 'e', 'a'],
['z', 'v', 'g', 'p', 't'],
['u', 'o', 'i', 'e', 'a'],
['z', 'v', 'g', 'p', 't'],
];
/**
* This is a version of the microbit codebook, where the original codebook is transposed
* and the rows are flipped. This gives an easier to use version for the bluetooth pattern
* connection.
* This could be done programatically, but having it typed out hopefully helps
* with the understanding of pattern <-> friendly name conversion
*/
Utility.CODEBOOK_BLUETOOTH = [
['t', 'a', 't', 'a', 't'],
['p', 'e', 'p', 'e', 'p'],
['g', 'i', 'g', 'i', 'g'],
['v', 'o', 'v', 'o', 'v'],
['z', 'u', 'z', 'u', 'z'],
];
MBSpecs.Utility = Utility;
})(MBSpecs || (MBSpecs = {}));
var MBSpecs$1 = MBSpecs;
/**
* The state of the Microbit device
*/
exports.MicrobitDeviceState = void 0;
(function (MicrobitDeviceState) {
/**
* The device is fully connected
*/
MicrobitDeviceState["CONNECTED"] = "CONNECTED";
/**
* The device is disconnected
*/
MicrobitDeviceState["DISCONNECTED"] = "DISCONNECTED";
/**
* The device is connecting
*/
MicrobitDeviceState["CONNECTING"] = "CONNECTING";
/**
* The device has been connected, and is being initialized
*/
MicrobitDeviceState["INITIALIZING"] = "INITIALIZING";
/**
* The device is reconnecting
*/
MicrobitDeviceState["RECONNECTING"] = "RECONNECTING";
/**
* The device is closed
*/
MicrobitDeviceState["CLOSED"] = "CLOSED";
})(exports.MicrobitDeviceState || (exports.MicrobitDeviceState = {}));
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var t=function(e,r){return (t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e;}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);})(e,r)};function e(e,r){function n(){this.constructor=e;}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n);}function r(t,e,r,n){return new(r||(r=Promise))((function(i,s){function o(t){try{u(n.next(t));}catch(t){s(t);}}function c(t){try{u(n.throw(t));}catch(t){s(t);}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e);}))).then(o,c);}u((n=n.apply(t,[])).next());}))}function n(t,e){var r,n,i,s,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return s={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function c(s){return function(c){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return o.label++,{value:s[1],done:!1};case 5:o.label++,n=s[1],s=[0];continue;case 7:s=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){o=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){o.label=s[1];break}if(6===s[0]&&o.label<i[1]){o.label=i[1],i=s;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(s);break}i[2]&&o.ops.pop(),o.trys.pop();continue}s=e.call(t,o);}catch(t){s=[6,t],n=0;}finally{r=i=0;}if(5&s[0])throw s[1];return {value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function i(){}function s(){s.init.call(this);}function o(t){return void 0===t._maxListeners?s.defaultMaxListeners:t._maxListeners}function c(t,e,r){if(e)t.call(r);else for(var n=t.length,i=v(t,n),s=0;s<n;++s)i[s].call(r);}function u(t,e,r,n){if(e)t.call(r,n);else for(var i=t.length,s=v(t,i),o=0;o<i;++o)s[o].call(r,n);}function a(t,e,r,n,i){if(e)t.call(r,n,i);else for(var s=t.length,o=v(t,s),c=0;c<s;++c)o[c].call(r,n,i);}function h(t,e,r,n,i,s){if(e)t.call(r,n,i,s);else for(var o=t.length,c=v(t,o),u=0;u<o;++u)c[u].call(r,n,i,s);}function f(t,e,r,n){if(e)t.apply(r,n);else for(var i=t.length,s=v(t,i),o=0;o<i;++o)s[o].apply(r,n);}function l(t,e,r,n){var s,c,u,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((c=t._events)?(c.newListener&&(t.emit("newListener",e,r.listener?r.listener:r),c=t._events),u=c[e]):(c=t._events=new i,t._eventsCount=0),u){if("function"==typeof u?u=c[e]=n?[r,u]:[u,r]:n?u.unshift(r):u.push(r),!u.warned&&(s=o(t))&&s>0&&u.length>s){u.warned=!0;var h=new Error("Possible EventEmitter memory leak detected. "+u.length+" "+e+" listeners added. Use emitter.setMaxListeners() to increase limit");h.name="MaxListenersExceededWarning",h.emitter=t,h.type=e,h.count=u.length,a=h,"function"==typeof console.warn?console.warn(a):console.log(a);}}else u=c[e]=r,++t._eventsCount;return t}function d(t,e,r){var n=!1;function i(){t.removeListener(e,i),n||(n=!0,r.apply(t,arguments));}return i.listener=r,i}function p(t){var e=this._events;if(e){var r=e[t];if("function"==typeof r)return 1;if(r)return r.length}return 0}function v(t,e){for(var r=new Array(e);e--;)r[e]=t[e];return r}i.prototype=Object.create(null),s.EventEmitter=s,s.usingDomains=!1,s.prototype.domain=void 0,s.prototype._events=void 0,s.prototype._maxListeners=void 0,s.defaultMaxListeners=10,s.init=function(){this.domain=null,s.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new i,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0;},s.prototype.setMaxListeners=function(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},s.prototype.getMaxListeners=function(){return o(this)},s.prototype.emit=function(t){var e,r,n,i,s,o,l,d="error"===t;if(o=this._events)d=d&&null==o.error;else if(!d)return !1;if(l=this.domain,d){if(e=arguments[1],!l){if(e instanceof Error)throw e;var p=new Error('Uncaught, unspecified "error" event. ('+e+")");throw p.context=e,p}return e||(e=new Error('Uncaught, unspecified "error" event')),e.domainEmitter=this,e.domain=l,e.domainThrown=!1,l.emit("error",e),!1}if(!(r=o[t]))return !1;var v="function"==typeof r;switch(n=arguments.length){case 1:c(r,v,this);break;case 2:u(r,v,this,arguments[1]);break;case 3:a(r,v,this,arguments[1],arguments[2]);break;case 4:h(r,v,this,arguments[1],arguments[2],arguments[3]);break;default:for(i=new Array(n-1),s=1;s<n;s++)i[s-1]=arguments[s];f(r,v,this,i);}return !0},s.prototype.addListener=function(t,e){return l(this,t,e,!1)},s.prototype.on=s.prototype.addListener,s.prototype.prependListener=function(t,e){return l(this,t,e,!0)},s.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,d(this,t,e)),this},s.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,d(this,t,e)),this},s.prototype.removeListener=function(t,e){var r,n,s,o,c;if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(r=n[t]))return this;if(r===e||r.listener&&r.listener===e)0==--this._eventsCount?this._events=new i:(delete n[t],n.removeListener&&this.emit("removeListener",t,r.listener||e));else if("function"!=typeof r){for(s=-1,o=r.length;o-- >0;)if(r[o]===e||r[o].listener&&r[o].listener===e){c=r[o].listener,s=o;break}if(s<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new i,this;delete n[t];}else !function(t,e){for(var r=e,n=r+1,i=t.length;n<i;r+=1,n+=1)t[r]=t[n];t.pop();}(r,s);n.removeListener&&this.emit("removeListener",t,c||e);}return this},s.prototype.removeAllListeners=function(t){var e,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new i,this._eventsCount=0):r[t]&&(0==--this._eventsCount?this._events=new i:delete r[t]),this;if(0===arguments.length){for(var n,s=Object.keys(r),o=0;o<s.length;++o)"removeListener"!==(n=s[o])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new i,this._eventsCount=0,this}if("function"==typeof(e=r[t]))this.removeListener(t,e);else if(e)do{this.removeListener(t,e[e.length-1]);}while(e[0]);return this},s.prototype.listeners=function(t){var e,r=this._events;return r&&(e=r[t])?"function"==typeof e?[e.listener||e]:function(t){for(var e=new Array(t.length),r=0;r<e.length;++r)e[r]=t[r].listener||t[r];return e}(e):[]},s.listenerCount=function(t,e){return "function"==typeof t.listenerCount?t.listenerCount(e):p.call(t,e)},s.prototype.listenerCount=p,s.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var w,m=function(){function t(){this.locked=!1;}return t.prototype.lock=function(){return r(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return this.locked?[4,new Promise((function(t){return setTimeout(t,1)}))]:[3,2];case 1:return t.sent(),[3,0];case 2:return this.locked=!0,[2]}}))}))},t.prototype.unlock=function(){this.locked=!1;},t}(),b=function(t){function i(e,r,n){void 0===r&&(r=0),void 0===n&&(n=1e7);var i=t.call(this)||this;i.transport=e,i.mode=r,i.clockFrequency=n,i.connected=!1,i.sendMutex=new m,i.blockSize=i.transport.packetSize-4-1;var s=i.transport.packetSize-2-1;return i.operationCount=Math.floor(s/5),i}return e(i,t),i.prototype.bufferSourceToUint8Array=function(t,e){if(!e)return new Uint8Array([t]);var r=void 0!==e.buffer?e.buffer:e,n=new Uint8Array(r.byteLength+1);return n.set([t]),n.set(new Uint8Array(r),1),n},i.prototype.selectProtocol=function(t){return r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return e=2===t?59196:59294,[4,this.swjSequence(new Uint8Array([255,255,255,255,255,255,255]))];case 1:return r.sent(),[4,this.swjSequence(new Uint16Array([e]))];case 2:return r.sent(),[4,this.swjSequence(new Uint8Array([255,255,255,255,255,255,255]))];case 3:return r.sent(),[4,this.swjSequence(new Uint8Array([0]))];case 4:return r.sent(),[2]}}))}))},i.prototype.send=function(t,e){return r(this,void 0,void 0,(function(){var r,i;return n(this,(function(n){switch(n.label){case 0:return r=this.bufferSourceToUint8Array(t,e),[4,this.sendMutex.lock()];case 1:n.sent(),n.label=2;case 2:return n.trys.push([2,,5,6]),[4,this.transport.write(r)];case 3:return n.sent(),[4,this.transport.read()];case 4:if((i=n.sent()).getUint8(0)!==t)throw new Error("Bad response for "+t+" -> "+i.getUint8(0));switch(t){case 3:case 8:case 9:case 10:case 17:case 18:case 19:case 29:case 23:case 24:case 26:case 21:case 22:case 4:if(0!==i.getUint8(1))throw new Error("Bad status for "+t+" -> "+i.getUint8(1))}return [2,i];case 5:return this.sendMutex.unlock(),[7];case 6:return [2]}}))}))},i.prototype.clearAbort=function(t){return void 0===t&&(t=30),r(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.send(8,new Uint8Array([0,t]))];case 1:return e.sent(),[2]}}))}))},i.prototype.dapInfo=function(t){return r(this,void 0,void 0,(function(){var e,r,i,s;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,this.send(0,new Uint8Array([t]))];case 1:if(e=n.sent(),0===(r=e.getUint8(1)))return [2,""];switch(t){case 240:case 254:case 255:case 253:if(1===r)return [2,e.getUint8(2)];if(2===r)return [2,e.getUint16(2)];if(4===r)return [2,e.getUint32(2)]}return i=Array.prototype.slice.call(new Uint8Array(e.buffer,2,r)),[2,String.fromCharCode.apply(null,i)];case 2:return s=n.sent(),[4,this.clearAbort()];case 3:throw n.sent(),s;case 4:return [2]}}))}))},i.prototype.swjSequence=function(t,e){return void 0===e&&(e=8*t.byteLength),r(this,void 0,void 0,(function(){var r,i;return n(this,(function(n){switch(n.label){case 0:r=this.bufferSourceToUint8Array(e,t),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(18,r)];case 2:return n.sent(),[3,5];case 3:return i=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),i;case 5:return [2]}}))}))},i.prototype.swjClock=function(t){return r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,4]),[4,this.send(17,new Uint8Array([255&t,(65280&t)>>8,(16711680&t)>>16,(4278190080&t)>>24]))];case 1:return r.sent(),[3,4];case 2:return e=r.sent(),[4,this.clearAbort()];case 3:throw r.sent(),e;case 4:return [2]}}))}))},i.prototype.swjPins=function(t,e,i){return r(this,void 0,void 0,(function(){var r;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,this.send(16,new Uint8Array([t,e,255&i,(65280&i)>>8,(16711680&i)>>16,(4278190080&i)>>24]))];case 1:return [2,n.sent().getUint8(1)];case 2:return r=n.sent(),[4,this.clearAbort()];case 3:throw n.sent(),r;case 4:return [2]}}))}))},i.prototype.dapDelay=function(t){return r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,4]),[4,this.send(9,new Uint8Array([255&t,(65280&t)>>8]))];case 1:return r.sent(),[3,4];case 2:return e=r.sent(),[4,this.clearAbort()];case 3:throw r.sent(),e;case 4:return [2]}}))}))},i.prototype.configureTransfer=function(t,e,i){return r(this,void 0,void 0,(function(){var r,s,o;return n(this,(function(n){switch(n.label){case 0:r=new Uint8Array(5),(s=new DataView(r.buffer)).setUint8(0,t),s.setUint16(1,e,!0),s.setUint16(3,i,!0),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(4,r)];case 2:return n.sent(),[3,5];case 3:return o=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),o;case 5:return [2]}}))}))},i.prototype.connect=function(){return r(this,void 0,void 0,(function(){var t,e,r;return n(this,(function(n){switch(n.label){case 0:return !0===this.connected?[2]:[4,this.transport.open()];case 1:n.sent(),n.label=2;case 2:return n.trys.push([2,5,,8]),[4,this.send(17,new Uint32Array([this.clockFrequency]))];case 3:return n.sent(),[4,this.send(2,new Uint8Array([this.mode]))];case 4:if(0===(t=n.sent()).getUint8(1)||0!==this.mode&&t.getUint8(1)!==this.mode)throw new Error("Mode not enabled.");return [3,8];case 5:return e=n.sent(),[4,this.clearAbort()];case 6:return n.sent(),[4,this.transport.close()];case 7:throw n.sent(),e;case 8:return n.trys.push([8,11,,13]),[4,this.configureTransfer(0,100,0)];case 9:return n.sent(),[4,this.selectProtocol(1)];case 10:return n.sent(),[3,13];case 11:return r=n.sent(),[4,this.transport.close()];case 12:throw n.sent(),r;case 13:return this.connected=!0,[2]}}))}))},i.prototype.disconnect=function(){return r(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:if(!1===this.connected)return [2];e.label=1;case 1:return e.trys.push([1,3,,5]),[4,this.send(3)];case 2:return e.sent(),[3,5];case 3:return t=e.sent(),[4,this.clearAbort()];case 4:throw e.sent(),t;case 5:return [4,this.transport.close()];case 6:return e.sent(),this.connected=!1,[2]}}))}))},i.prototype.reconnect=function(){return r(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return [4,this.disconnect()];case 1:return t.sent(),[4,new Promise((function(t){return setTimeout(t,100)}))];case 2:return t.sent(),[4,this.connect()];case 3:return t.sent(),[2]}}))}))},i.prototype.reset=function(){return r(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,4]),[4,this.send(10)];case 1:return [2,1===e.sent().getUint8(2)];case 2:return t=e.sent(),[4,this.clearAbort()];case 3:throw e.sent(),t;case 4:return [2]}}))}))},i.prototype.transfer=function(t,e,i,s){return void 0===e&&(e=2),void 0===i&&(i=0),void 0===s&&(s=0),r(this,void 0,void 0,(function(){var r,o,c,u,a,h,f;return n(this,(function(n){switch(n.label){case 0:r="number"==typeof t?[{port:t,mode:e,register:i,value:s}]:t,o=new Uint8Array(2+5*r.length),(c=new DataView(o.buffer)).setUint8(0,0),c.setUint8(1,r.length),r.forEach((function(t,e){var r=2+5*e;c.setUint8(r,t.port|t.mode|t.register),c.setUint32(r+1,t.value||0,!0);})),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(5,o)];case 2:if((u=n.sent()).getUint8(1)!==r.length)throw new Error("Transfer count mismatch");if(2===(a=u.getUint8(2)))throw new Error("Transfer response WAIT");if(4===a)throw new Error("Transfer response FAULT");if(8===a)throw new Error("Transfer response PROTOCOL_ERROR");if(16===a)throw new Error("Transfer response VALUE_MISMATCH");if(7===a)throw new Error("Transfer response NO_ACK");return "number"==typeof t?[2,u.getUint32(3,!0)]:(h=4*r.length,[2,new Uint32Array(u.buffer.slice(3,3+h))]);case 3:return f=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),f;case 5:return [2]}}))}))},i.prototype.transferBlock=function(t,e,i){return r(this,void 0,void 0,(function(){var r,s,o,c,u,a,h,f;return n(this,(function(n){switch(n.label){case 0:o=4,"number"==typeof i?(r=i,s=2):(r=i.length,s=0,o+=i.byteLength),c=new Uint8Array(o),(u=new DataView(c.buffer)).setUint8(0,0),u.setUint16(1,r,!0),u.setUint8(3,t|s|e),"number"!=typeof i&&i.forEach((function(t,e){var r=4+4*e;u.setUint32(r,t,!0);})),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(6,u)];case 2:if((a=n.sent()).getUint16(1,!0)!==r)throw new Error("Transfer count mismatch");if(2===(h=a.getUint8(3)))throw new Error("Transfer response WAIT");if(4===h)throw new Error("Transfer response FAULT");if(8===h)throw new Error("Transfer response PROTOCOL_ERROR");if(7===h)throw new Error("Transfer response NO_ACK");return "number"==typeof i?[2,new Uint32Array(a.buffer.slice(4,4+4*r))]:[3,5];case 3:return f=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),f;case 5:return [2,void 0]}}))}))},i}(s),g=/[\xc0-\xff][\x80-\xbf]*$/g,A=/[\xc0-\xff][\x80-\xbf]*/g,C=function(){function t(){}return t.prototype.decode=function(t){var e=Array.prototype.slice.call(new Uint8Array(t)),r=String.fromCodePoint.apply(void 0,e);this.partialChar&&(r=""+this.partialChar+r,this.partialChar=void 0);var n=r.match(g);if(n){var i=n[0].length;this.partialChar=r.slice(-i),r=r.slice(0,-i);}return r.replace(A,this.decoderReplacer)},t.prototype.decoderReplacer=function(t){var e=t.codePointAt(0)<<24,r=Math.clz32(~e),n=0,i=t.length,s="";if(r<5&&i>=r){for(e=e<<r>>>24+r,n=1;n<r;n+=1)e=e<<6|63&t.codePointAt(n);e<=65535?s+=String.fromCodePoint(e):e<=1114111?(e-=65536,s+=String.fromCodePoint(55296+(e>>10),56320+(1023&e))):n=0;}for(;n<i;n+=1)s+="�";return s},t}(),U=new C,E=function(t){function i(e,s,o){void 0===s&&(s=0),void 0===o&&(o=1e7);var c=t.call(this,e,s,o)||this;return c.serialPolling=!1,c.serialListeners=!1,c.on("newListener",(function(t){return r(c,void 0,void 0,(function(){return n(this,(function(e){return t===i.EVENT_SERIAL_DATA&&0===this.listenerCount(t)&&(this.serialListeners=!0),[2]}))}))})),c.on("removeListener",(function(t){t===i.EVENT_SERIAL_DATA&&(0===c.listenerCount(t)&&(c.serialListeners=!1));})),c}return e(i,t),i.prototype.isBufferBinary=function(t){for(var e=Array.prototype.slice.call(new Uint16Array(t,0,50)),r=String.fromCharCode.apply(null,e),n=0;n<r.length;n++){var i=r.charCodeAt(n);if(65533===i||i<=8)return !0}return !1},i.prototype.writeBuffer=function(t,e,s){return void 0===s&&(s=0),r(this,void 0,void 0,(function(){var r,o,c,u;return n(this,(function(n){switch(n.label){case 0:r=Math.min(t.byteLength,s+e),o=t.slice(s,r),(c=new Uint8Array(o.byteLength+1)).set([o.byteLength]),c.set(new Uint8Array(o),1),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(140,c)];case 2:return n.sent(),[3,5];case 3:return u=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),u;case 5:return this.emit(i.EVENT_PROGRESS,s/t.byteLength),r<t.byteLength?[2,this.writeBuffer(t,e,r)]:[2]}}))}))},i.prototype.flash=function(t,e){return void 0===e&&(e=62),r(this,void 0,void 0,(function(){var r,s,o;return n(this,(function(n){switch(n.label){case 0:r=function(t){return void 0!==t.buffer}(t)?t.buffer:t,s=this.isBufferBinary(r)?0:1,n.label=1;case 1:return n.trys.push([1,6,,8]),[4,this.send(138,new Uint32Array([s]))];case 2:if(0!==n.sent().getUint8(1))throw new Error("Flash error");return [4,this.writeBuffer(r,e)];case 3:return n.sent(),this.emit(i.EVENT_PROGRESS,1),[4,this.send(139)];case 4:if(0!==n.sent().getUint8(1))throw new Error("Flash error");return [4,this.send(137)];case 5:return n.sent(),[3,8];case 6:return o=n.sent(),[4,this.clearAbort()];case 7:throw n.sent(),o;case 8:return [2]}}))}))},i.prototype.getSerialBaudrate=function(){return r(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,4]),[4,this.send(129)];case 1:return [2,e.sent().getUint32(1,!0)];case 2:return t=e.sent(),[4,this.clearAbort()];case 3:throw e.sent(),t;case 4:return [2]}}))}))},i.prototype.setSerialBaudrate=function(t){return void 0===t&&(t=9600),r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,4]),[4,this.send(130,new Uint32Array([t]))];case 1:return r.sent(),[3,4];case 2:return e=r.sent(),[4,this.clearAbort()];case 3:throw r.sent(),e;case 4:return [2]}}))}))},i.prototype.serialWrite=function(t){return r(this,void 0,void 0,(function(){var e,r;return n(this,(function(n){switch(n.label){case 0:(e=t.split("").map((function(t){return t.charCodeAt(0)}))).unshift(e.length),n.label=1;case 1:return n.trys.push([1,3,,5]),[4,this.send(132,new Uint8Array(e).buffer)];case 2:return n.sent(),[3,5];case 3:return r=n.sent(),[4,this.clearAbort()];case 4:throw n.sent(),r;case 5:return [2]}}))}))},i.prototype.serialRead=function(){return r(this,void 0,void 0,(function(){var t,e,r;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,this.send(131)];case 1:return 0===(t=n.sent()).byteLength||(131!==t.getUint8(0)||0===(e=t.getUint8(1)))?[2,void 0]:([2,t.buffer.slice(2,2+e)]);case 2:return r=n.sent(),[4,this.clearAbort()];case 3:throw n.sent(),r;case 4:return [2]}}))}))},i.prototype.startSerialRead=function(t,e){return void 0===t&&(t=100),void 0===e&&(e=!0),r(this,void 0,void 0,(function(){var r,s,o;return n(this,(function(n){switch(n.label){case 0:this.serialPolling=!0,n.label=1;case 1:return this.serialPolling?this.serialListeners?(r=this.connected,!1!==this.connected||!0!==e?[3,3]:[4,this.connect()]):[3,7]:[3,9];case 2:n.sent(),n.label=3;case 3:return [4,this.serialRead()];case 4:return s=n.sent(),!1!==r||!0!==e?[3,6]:[4,this.disconnect()];case 5:n.sent(),n.label=6;case 6:void 0!==s&&(o=U.decode(s),this.emit(i.EVENT_SERIAL_DATA,o)),n.label=7;case 7:return [4,new Promise((function(e){return setTimeout(e,t)}))];case 8:return n.sent(),[3,1];case 9:return [2]}}))}))},i.prototype.stopSerialRead=function(){this.serialPolling=!1;},i.EVENT_PROGRESS="progress",i.EVENT_SERIAL_DATA="serial",i}(b),P=function(){function t(t,e,r){void 0===e&&(e=0),void 0===r&&(r=1e7);this.proxy=void 0!==t.open?new b(t,e,r):t;}return t.prototype.waitDelay=function(t,e,i){return void 0===e&&(e=0),void 0===i&&(i=100),r(this,void 0,void 0,(function(){var r;return n(this,(function(n){switch(n.label){case 0:r=!0,e>0&&setTimeout((function(){if(r)throw r=!1,new Error("Wait timed out")}),e),n.label=1;case 1:return r?[4,t()]:[3,5];case 2:return !0===n.sent()?(r=!1,[2]):i>0?[4,new Promise((function(t){return setTimeout(t,e)}))]:[3,4];case 3:n.sent(),n.label=4;case 4:return [3,1];case 5:return [2]}}))}))},t.prototype.concatTypedArray=function(t){if(1===t.length)return t[0];for(var e=0,r=0,n=t;r<n.length;r++){e+=n[r].length;}for(var i=new Uint32Array(e),s=0,o=0;s<t.length;s++)i.set(t[s],o),o+=t[s].length;return i},t.prototype.readDPCommand=function(t){return [{mode:2,port:0,register:t}]},t.prototype.writeDPCommand=function(t,e){if(8===t){if(e===this.selectedAddress)return [];this.selectedAddress=e;}return [{mode:0,port:0,register:t,value:e}]},t.prototype.readAPCommand=function(t){var e=4278190080&t|240&t;return this.writeDPCommand(8,e).concat({mode:2,port:1,register:t})},t.prototype.writeAPCommand=function(t,e){if(0===t){if(e===this.cswValue)return [];this.cswValue=e;}var r=4278190080&t|240&t;return this.writeDPCommand(8,r).concat({mode:0,port:1,register:t,value:e})},t.prototype.readMem16Command=function(t){return this.writeAPCommand(0,587202641).concat(this.writeAPCommand(4,t)).concat(this.readAPCommand(12))},t.prototype.writeMem16Command=function(t,e){return this.writeAPCommand(0,587202641).concat(this.writeAPCommand(4,t)).concat(this.writeAPCommand(12,e))},t.prototype.readMem32Command=function(t){return this.writeAPCommand(0,587202642).concat(this.writeAPCommand(4,t)).concat(this.readAPCommand(12))},t.prototype.writeMem32Command=function(t,e){return this.writeAPCommand(0,587202642).concat(this.writeAPCommand(4,t)).concat(this.writeAPCommand(12,e))},t.prototype.transferSequence=function(t){return r(this,void 0,void 0,(function(){var e,r,i,s;return n(this,(function(n){switch(n.label){case 0:e=(e=[]).concat.apply(e,t),r=[],n.label=1;case 1:return e.length?(i=e.splice(0,this.proxy.operationCount),[4,this.proxy.transfer(i)]):[3,3];case 2:return s=n.sent(),r.push(s),[3,1];case 3:return [2,this.concatTypedArray(r)]}}))}))},t.prototype.connect=function(){return r(this,void 0,void 0,(function(){var t,e=this;return n(this,(function(i){switch(i.label){case 0:return t=-1610612736,[4,this.proxy.connect()];case 1:return i.sent(),[4,this.readDP(0)];case 2:return i.sent(),[4,this.transferSequence([this.writeDPCommand(0,4),this.writeDPCommand(8,0),this.writeDPCommand(4,1342177280)])];case 3:return i.sent(),[4,this.waitDelay((function(){return r(e,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.readDP(4)];case 1:return [2,(e.sent()&t)===t]}}))}))}))];case 4:return i.sent(),[2]}}))}))},t.prototype.disconnect=function(){return this.proxy.disconnect()},t.prototype.reconnect=function(){return r(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return [4,this.disconnect()];case 1:return t.sent(),[4,new Promise((function(t){return setTimeout(t,100)}))];case 2:return t.sent(),[4,this.connect()];case 3:return t.sent(),[2]}}))}))},t.prototype.reset=function(){return this.proxy.reset()},t.prototype.readDP=function(t){return r(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.proxy.transfer(this.readDPCommand(t))];case 1:return [2,e.sent()[0]]}}))}))},t.prototype.writeDP=function(t,e){return r(this,void 0,void 0,(function(){return n(this,(function(r){switch(r.label){case 0:return [4,this.proxy.transfer(this.writeDPCommand(t,e))];case 1:return r.sent(),[2]}}))}))},t.prototype.readAP=function(t){return r(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.proxy.transfer(this.readAPCommand(t))];case 1:return [2,e.sent()[0]]}}))}))},t.prototype.writeAP=function(t,e){return r(this,void 0,void 0,(function(){return n(this,(function(r){switch(r.label){case 0:return [4,this.proxy.transfer(this.writeAPCommand(t,e))];case 1:return r.sent(),[2]}}))}))},t.prototype.readMem16=function(t){return r(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.proxy.transfer(this.readMem16Command(t))];case 1:return [2,e.sent()[0]]}}))}))},t.prototype.writeMem16=function(t,e){return r(this,void 0,void 0,(function(){return n(this,(function(r){switch(r.label){case 0:return e<<=(2&t)<<3,[4,this.proxy.transfer(this.writeMem16Command(t,e))];case 1:return r.sent(),[2]}}))}))},t.prototype.readMem32=function(t){return r(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return [4,this.proxy.transfer(this.readMem32Command(t))];case 1:return [2,e.sent()[0]]}}))}))},t.prototype.writeMem32=function(t,e){return r(this,void 0,void 0,(function(){return n(this,(function(r){switch(r.label){case 0:return [4,this.proxy.transfer(this.writeMem32Command(t,e))];case 1:return r.sent(),[2]}}))}))},t.prototype.readBlock=function(t,e){return r(this,void 0,void 0,(function(){var r,i,s,o;return n(this,(function(n){switch(n.label){case 0:return [4,this.transferSequence([this.writeAPCommand(0,587202642),this.writeAPCommand(4,t)])];case 1:n.sent(),r=[],i=e,n.label=2;case 2:return i>0?(s=Math.min(i,Math.floor(this.proxy.blockSize/4)),[4,this.proxy.transferBlock(1,12,s)]):[3,4];case 3:return o=n.sent(),r.push(o),i-=s,[3,2];case 4:return [2,this.concatTypedArray(r)]}}))}))},t.prototype.writeBlock=function(t,e){return r(this,void 0,void 0,(function(){var r,i;return n(this,(function(n){switch(n.label){case 0:return [4,this.transferSequence([this.writeAPCommand(0,587202642),this.writeAPCommand(4,t)])];case 1:n.sent(),r=0,n.label=2;case 2:return r<e.length?(i=e.slice(r,r+Math.floor(this.proxy.blockSize/4)),[4,this.proxy.transferBlock(1,12,i)]):[3,4];case 3:return n.sent(),r+=Math.floor(this.proxy.blockSize/4),[3,2];case 4:return [2]}}))}))},t}(),T=function(t){function i(){return null!==t&&t.apply(this,arguments)||this}return e(i,t),i.prototype.enableDebug=function(){return this.writeMem32(3758157296,-1604386815)},i.prototype.readCoreRegisterCommand=function(t){return this.writeMem32Command(3758157300,t).concat(this.readMem32Command(3758157296)).concat(this.readMem32Command(3758157304))},i.prototype.writeCoreRegisterCommand=function(t,e){return this.writeMem32Command(3758157304,e).concat(this.writeMem32Command(3758157300,65536|t))},i.prototype.getState=function(){return r(this,void 0,void 0,(function(){var t,e,r;return n(this,(function(n){switch(n.label){case 0:return [4,this.readMem32(3758157296)];case 1:return t=n.sent(),e=524288&t?1:262144&t?2:131072&t?3:4,33554432&t?[4,this.readMem32(3758157296)]:[3,3];case 2:return 33554432&(r=n.sent())&&!(16777216&r)?[2,0]:[2,e];case 3:return [2,e];case 4:return [2]}}))}))},i.prototype.isHalted=function(){return r(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return [4,this.readMem32(3758157296)];case 1:return [2,!!(131072&t.sent())]}}))}))},i.prototype.halt=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=0),r(this,void 0,void 0,(function(){var r=this;return n(this,(function(n){switch(n.label){case 0:return [4,this.isHalted()];case 1:return n.sent()?[2]:[4,this.writeMem32(3758157296,-1604386813)];case 2:return n.sent(),t?[2,this.waitDelay((function(){return r.isHalted()}),e)]:[2]}}))}))},i.prototype.resume=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=0),r(this,void 0,void 0,(function(){var i=this;return n(this,(function(s){switch(s.label){case 0:return [4,this.isHalted()];case 1:return s.sent()?[4,this.writeMem32(3758157104,7)]:[2];case 2:return s.sent(),[4,this.enableDebug()];case 3:return s.sent(),t?[2,this.waitDelay((function(){return r(i,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return [4,this.isHalted()];case 1:return [2,!t.sent()]}}))}))}),e)]:[2]}}))}))},i.prototype.readCoreRegister=function(t){return r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return [4,this.transferSequence([this.writeMem32Command(3758157300,t),this.readMem32Command(3758157296)])];case 1:if(e=r.sent(),!(65536&e[0]))throw new Error("Register not ready");return [2,this.readMem32(3758157304)]}}))}))},i.prototype.readCoreRegisters=function(t){return r(this,void 0,void 0,(function(){var e,r,i,s,o;return n(this,(function(n){switch(n.label){case 0:e=[],r=0,i=t,n.label=1;case 1:return r<i.length?(s=i[r],[4,this.readCoreRegister(s)]):[3,4];case 2:o=n.sent(),e.push(o),n.label=3;case 3:return r++,[3,1];case 4:return [2,e]}}))}))},i.prototype.writeCoreRegister=function(t,e){return r(this,void 0,void 0,(function(){var r;return n(this,(function(n){switch(n.label){case 0:return [4,this.transferSequence([this.writeMem32Command(3758157304,e),this.writeMem32Command(3758157300,65536|t),this.readMem32Command(3758157296)])];case 1:if(r=n.sent(),!(65536&r[0]))throw new Error("Register not ready");return [2]}}))}))},i.prototype.execute=function(t,e,i,s,o){void 0===o&&(o=t+1);for(var c=[],u=5;u<arguments.length;u++)c[u-5]=arguments[u];return r(this,void 0,void 0,(function(){var r,u,a,h=this;return n(this,(function(n){switch(n.label){case 0:for(48682!==e[e.length-1]&&((r=new Uint32Array(e.length+1)).set(e),r.set([48682],e.length-1),e=r),u=[this.writeCoreRegisterCommand(13,i),this.writeCoreRegisterCommand(15,s),this.writeCoreRegisterCommand(14,o)],a=0;a<Math.min(c.length,12);a++)u.push(this.writeCoreRegisterCommand(a,c[a]));return u.push(this.writeCoreRegisterCommand(16,16777216)),[4,this.halt()];case 1:return n.sent(),[4,this.transferSequence(u)];case 2:return n.sent(),[4,this.writeBlock(t,e)];case 3:return n.sent(),[4,this.resume(!1)];case 4:return n.sent(),[4,this.waitDelay((function(){return h.isHalted()}),1e4)];case 5:return n.sent(),[2]}}))}))},i.prototype.softReset=function(){return r(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:return [4,this.writeMem32(3758157308,0)];case 1:return t.sent(),[2,this.writeMem32(3758157068,100270084)]}}))}))},i.prototype.setTargetResetState=function(t){return void 0===t&&(t=!0),r(this,void 0,void 0,(function(){var e;return n(this,(function(r){switch(r.label){case 0:return [4,this.writeMem32(3758157308,1)];case 1:return r.sent(),!0!==t?[3,3]:[4,this.reset()];case 2:return r.sent(),[3,6];case 3:return [4,this.readMem32(3758157068)];case 4:return e=r.sent(),[4,this.writeMem32(3758157068,100270084|e)];case 5:r.sent(),r.label=6;case 6:return [4,this.writeMem32(3758157308,0)];case 7:return r.sent(),[2]}}))}))},i}(P);!function(t){t[t.ENABLE=1]="ENABLE",t[t.KEY=2]="KEY";}(w||(w={}));var x=function(){function t(t,e,r,n){void 0===e&&(e=255),void 0===r&&(r=1),void 0===n&&(n=!1),this.device=t,this.interfaceClass=e,this.configuration=r,this.alwaysControlTransfer=n,this.packetSize=64;}return t.prototype.extendBuffer=function(t,e){var r=void 0!==t.buffer?t.buffer:t,n=Math.min(r.byteLength,e),i=new Uint8Array(n);return i.set(new Uint8Array(r)),i},t.prototype.open=function(){return r(this,void 0,void 0,(function(){var t,e,r,i,s,o,c=this;return n(this,(function(n){switch(n.label){case 0:return [4,this.device.open()];case 1:return n.sent(),[4,this.device.selectConfiguration(this.configuration)];case