UNPKG

microbit-web-bluetooth

Version:
17 lines (14 loc) 34 kB
/*! ***************************************************************************** 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. ***************************************************************************** */ var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},e(t,n)};function t(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}function n(e,t,n,r){return new(n||(n=Promise))((function(i,a){function o(e){try{s(r.next(e))}catch(e){a(e)}}function u(e){try{s(r.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,u)}s((r=r.apply(e,t||[])).next())}))}function r(e,t){var n,r,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){o.label=a[1];break}if(6===a[0]&&o.label<i[1]){o.label=i[1],i=a;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(a);break}i[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}var i,a=function(){function e(e){void 0===e&&(e=1),this.concurrent=e,this.queue=[],this.running=0}return e.prototype.pump=function(){return n(this,void 0,void 0,(function(){var e,t,n;return r(this,(function(r){switch(r.label){case 0:if(this.running>=this.concurrent)return[2];if(!(e=this.queue.shift()))return[2];this.running++,r.label=1;case 1:return r.trys.push([1,3,,4]),[4,e.fn()];case 2:return t=r.sent(),e.resolve(t),[3,4];case 3:return n=r.sent(),e.reject(n),[3,4];case 4:return this.running--,[2,this.pump()]}}))}))},e.prototype.add=function(e){var t=this;return new Promise((function(n,r){return t.queue.push({fn:e,resolve:n,reject:r}),t.pump()}))},e}(),o=function(){function e(e,t){this.service=e,this.emitter=t}return e.prototype.getCharacteristic=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return this.characteristics?[3,2]:(t=this,[4,this.service.getCharacteristics()]);case 1:t.characteristics=n.sent(),n.label=2;case 2:return[2,this.characteristics.find((function(t){return t.uuid===e}))]}}))}))},e.prototype.getCharacteristicValue=function(t){return n(this,void 0,void 0,(function(){var i,a=this;return r(this,(function(o){switch(o.label){case 0:return[4,this.getCharacteristic(t)];case 1:if(!(i=o.sent()))throw new Error("Unable to locate characteristic");return[4,e.queue.add((function(){return n(a,void 0,void 0,(function(){return r(this,(function(e){return[2,i.readValue()]}))}))}))];case 2:return[2,o.sent()]}}))}))},e.prototype.setCharacteristicValue=function(t,i){return n(this,void 0,void 0,(function(){var a,o=this;return r(this,(function(u){switch(u.label){case 0:return[4,this.getCharacteristic(t)];case 1:if(!(a=u.sent()))throw new Error("Unable to locate characteristic");return[4,e.queue.add((function(){return n(o,void 0,void 0,(function(){return r(this,(function(e){return[2,a.writeValue(i)]}))}))}))];case 2:return u.sent(),[2]}}))}))},e.prototype.handleListener=function(t,i,a){return n(this,void 0,void 0,(function(){var o,u=this;return r(this,(function(s){switch(s.label){case 0:return[4,this.getCharacteristic(i)];case 1:return(o=s.sent())?[4,e.queue.add((function(){return n(u,void 0,void 0,(function(){return r(this,(function(e){return[2,o.startNotifications()]}))}))}))]:[2];case 2:return s.sent(),this.emitter.on("newListener",(function(i){if(!(i!==t||u.emitter.listenerCount(t)>0))return e.queue.add((function(){return n(u,void 0,void 0,(function(){return r(this,(function(e){return[2,o.addEventListener("characteristicvaluechanged",a)]}))}))}))})),this.emitter.on("removeListener",(function(i){if(!(i!==t||u.emitter.listenerCount(t)>0))return e.queue.add((function(){return n(u,void 0,void 0,(function(){return r(this,(function(e){return[2,o.removeEventListener("characteristicvaluechanged",a)]}))}))}))})),[2]}}))}))},e.queue=new a,e}();!function(e){e.modelNumber="00002a24-0000-1000-8000-00805f9b34fb",e.serialNumber="00002a25-0000-1000-8000-00805f9b34fb",e.firmwareRevision="00002a26-0000-1000-8000-00805f9b34fb",e.hardwareRevision="00002a27-0000-1000-8000-00805f9b34fb",e.manufacturer="00002a29-0000-1000-8000-00805f9b34fb"}(i||(i={}));var u=function(){function e(e){this.helper=new o(e)}return e.create=function(t){return n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,new e(t)]}))}))},e.prototype.readDeviceInformation=function(){return n(this,void 0,void 0,(function(){var e,t,n,a,o,u;return r(this,(function(r){switch(r.label){case 0:return e={},[4,this.readStringCharacteristic(i.modelNumber)];case 1:return(t=r.sent())&&(e.modelNumber=t),[4,this.readStringCharacteristic(i.serialNumber)];case 2:return(n=r.sent())&&(e.serialNumber=n),[4,this.readStringCharacteristic(i.firmwareRevision)];case 3:return(a=r.sent())&&(e.firmwareRevision=a),[4,this.readStringCharacteristic(i.hardwareRevision)];case 4:return(o=r.sent())&&(e.hardwareRevision=o),[4,this.readStringCharacteristic(i.manufacturer)];case 5:return(u=r.sent())&&(e.manufacturer=u),[2,e]}}))}))},e.prototype.readStringCharacteristic=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this.helper.getCharacteristicValue(e)];case 1:return t=n.sent(),[2,(new TextDecoder).decode(t.buffer)];case 2:return n.sent(),[2,void 0];case 3:return[2]}}))}))},e.uuid="0000180a-0000-1000-8000-00805f9b34fb",e}();function s(){}function c(){c.init.call(this)}function h(e){return void 0===e._maxListeners?c.defaultMaxListeners:e._maxListeners}function f(e,t,n,r){var i,a,o,u;if("function"!=typeof n)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,n.listener?n.listener:n),a=e._events),o=a[t]):(a=e._events=new s,e._eventsCount=0),o){if("function"==typeof o?o=a[t]=r?[n,o]:[o,n]:r?o.unshift(n):o.push(n),!o.warned&&(i=h(e))&&i>0&&o.length>i){o.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=o.length,u=c,"function"==typeof console.warn?console.warn(u):console.log(u)}}else o=a[t]=n,++e._eventsCount;return e}function d(e,t,n){var r=!1;function i(){e.removeListener(t,i),r||(r=!0,n.apply(e,arguments))}return i.listener=n,i}function l(e){var t=this._events;if(t){var n=t[e];if("function"==typeof n)return 1;if(n)return n.length}return 0}function v(e,t){for(var n=new Array(t);t--;)n[t]=e[t];return n}s.prototype=Object.create(null),c.EventEmitter=c,c.usingDomains=!1,c.prototype.domain=void 0,c.prototype._events=void 0,c.prototype._maxListeners=void 0,c.defaultMaxListeners=10,c.init=function(){this.domain=null,c.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new s,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},c.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},c.prototype.getMaxListeners=function(){return h(this)},c.prototype.emit=function(e){var t,n,r,i,a,o,u,s="error"===e;if(o=this._events)s=s&&null==o.error;else if(!s)return!1;if(u=this.domain,s){if(t=arguments[1],!u){if(t instanceof Error)throw t;var c=new Error('Uncaught, unspecified "error" event. ('+t+")");throw c.context=t,c}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=u,t.domainThrown=!1,u.emit("error",t),!1}if(!(n=o[e]))return!1;var h="function"==typeof n;switch(r=arguments.length){case 1:!function(e,t,n){if(t)e.call(n);else for(var r=e.length,i=v(e,r),a=0;a<r;++a)i[a].call(n)}(n,h,this);break;case 2:!function(e,t,n,r){if(t)e.call(n,r);else for(var i=e.length,a=v(e,i),o=0;o<i;++o)a[o].call(n,r)}(n,h,this,arguments[1]);break;case 3:!function(e,t,n,r,i){if(t)e.call(n,r,i);else for(var a=e.length,o=v(e,a),u=0;u<a;++u)o[u].call(n,r,i)}(n,h,this,arguments[1],arguments[2]);break;case 4:!function(e,t,n,r,i,a){if(t)e.call(n,r,i,a);else for(var o=e.length,u=v(e,o),s=0;s<o;++s)u[s].call(n,r,i,a)}(n,h,this,arguments[1],arguments[2],arguments[3]);break;default:for(i=new Array(r-1),a=1;a<r;a++)i[a-1]=arguments[a];!function(e,t,n,r){if(t)e.apply(n,r);else for(var i=e.length,a=v(e,i),o=0;o<i;++o)a[o].apply(n,r)}(n,h,this,i)}return!0},c.prototype.addListener=function(e,t){return f(this,e,t,!1)},c.prototype.on=c.prototype.addListener,c.prototype.prependListener=function(e,t){return f(this,e,t,!0)},c.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,d(this,e,t)),this},c.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,d(this,e,t)),this},c.prototype.removeListener=function(e,t){var n,r,i,a,o;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(r=this._events))return this;if(!(n=r[e]))return this;if(n===t||n.listener&&n.listener===t)0==--this._eventsCount?this._events=new s:(delete r[e],r.removeListener&&this.emit("removeListener",e,n.listener||t));else if("function"!=typeof n){for(i=-1,a=n.length;a-- >0;)if(n[a]===t||n[a].listener&&n[a].listener===t){o=n[a].listener,i=a;break}if(i<0)return this;if(1===n.length){if(n[0]=void 0,0==--this._eventsCount)return this._events=new s,this;delete r[e]}else!function(e,t){for(var n=t,r=n+1,i=e.length;r<i;n+=1,r+=1)e[n]=e[r];e.pop()}(n,i);r.removeListener&&this.emit("removeListener",e,o||t)}return this},c.prototype.off=function(e,t){return this.removeListener(e,t)},c.prototype.removeAllListeners=function(e){var t,n;if(!(n=this._events))return this;if(!n.removeListener)return 0===arguments.length?(this._events=new s,this._eventsCount=0):n[e]&&(0==--this._eventsCount?this._events=new s:delete n[e]),this;if(0===arguments.length){for(var r,i=Object.keys(n),a=0;a<i.length;++a)"removeListener"!==(r=i[a])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=new s,this._eventsCount=0,this}if("function"==typeof(t=n[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},c.prototype.listeners=function(e){var t,n=this._events;return n&&(t=n[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),n=0;n<t.length;++n)t[n]=e[n].listener||e[n];return t}(t):[]},c.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):l.call(e,t)},c.prototype.listenerCount=l,c.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var p,g,m=function(e){function n(){var t=null!==e&&e.apply(this,arguments)||this;return t.isEventListenerObject=function(e){return void 0!==e.handleEvent},t}return t(n,e),n.prototype.addEventListener=function(t,n){if(n){var r=this.isEventListenerObject(n)?n.handleEvent:n;e.prototype.addListener.call(this,t,r)}},n.prototype.removeEventListener=function(t,n){if(n){var r=this.isEventListenerObject(n)?n.handleEvent:n;e.prototype.removeListener.call(this,t,r)}},n.prototype.dispatchEvent=function(t,n){var r;return r="string"==typeof t?new CustomEvent(t,{detail:n}):t,e.prototype.emit.call(this,r.type,r)},n}(c);!function(e){e.buttonAState="e95dda90-251d-470a-a062-fa1922dfa9a8",e.buttonBState="e95dda91-251d-470a-a062-fa1922dfa9a8"}(p||(p={})),function(e){e[e.Release=0]="Release",e[e.ShortPress=1]="ShortPress",e[e.LongPress=2]="LongPress"}(g||(g={}));var w,y=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("buttonastatechanged",p.buttonAState,this.buttonAStateChangedHandler.bind(this))];case 1:return e.sent(),[4,this.helper.handleListener("buttonbstatechanged",p.buttonBState,this.buttonBStateChangedHandler.bind(this))];case 2:return e.sent(),[2]}}))}))},i.prototype.readButtonAState=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(p.buttonAState)];case 1:return[2,e.sent().getUint8(0)]}}))}))},i.prototype.readButtonBState=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(p.buttonBState)];case 1:return[2,e.sent().getUint8(0)]}}))}))},i.prototype.buttonAStateChangedHandler=function(e){var t=e.target.value;this.dispatchEvent("buttonastatechanged",t.getUint8(0))},i.prototype.buttonBStateChangedHandler=function(e){var t=e.target.value;this.dispatchEvent("buttonbstatechanged",t.getUint8(0))},i.uuid="e95d9882-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.ledMatrixState="e95d7b77-251d-470a-a062-fa1922dfa9a8",e.ledText="e95d93ee-251d-470a-a062-fa1922dfa9a8",e.scrollingDelay="e95d0d2d-251d-470a-a062-fa1922dfa9a8"}(w||(w={}));var b,C=function(){function e(e){this.helper=new o(e)}return e.create=function(t){return n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,new e(t)]}))}))},e.prototype.writeText=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.encodeString(e),[2,this.helper.setCharacteristicValue(w.ledText,t)]}))}))},e.prototype.readMatrixState=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(w.ledMatrixState)];case 1:return e=t.sent(),[2,this.viewToLedMatrix(e)]}}))}))},e.prototype.writeMatrixState=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.ledMatrixToView(e),[2,this.helper.setCharacteristicValue(w.ledMatrixState,t)]}))}))},e.prototype.getScrollingDelay=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(w.scrollingDelay)];case 1:return[2,e.sent().getUint16(0,!0)]}}))}))},e.prototype.setScrollingDelay=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return(t=new DataView(new ArrayBuffer(2))).setUint16(0,e,!0),[2,this.helper.setCharacteristicValue(w.scrollingDelay,t)]}))}))},e.prototype.encodeString=function(e){return(new TextEncoder).encode(e)},e.prototype.viewToLedMatrix=function(e){for(var t=[],n=0;n<5;n++)t[n]=this.byteToBoolArray(e.getUint8(n));return t},e.prototype.byteToBoolArray=function(e){for(var t=[!1,!1,!1,!1,!1],n=0;n<t.length;n++)t[n]=!(1&~e),e>>=1;return t.reverse()},e.prototype.ledMatrixToView=function(e){for(var t=new DataView(new ArrayBuffer(5)),n=0;n<5;n++)t.setUint8(n,this.boolArrayToByte(e[n]));return t},e.prototype.boolArrayToByte=function(e){return e.reduce((function(e,t){return e<<1|(t?1:0)}),0)},e.uuid="e95dd91d-251d-470a-a062-fa1922dfa9a8",e}();!function(e){e.temperature="e95d9250-251d-470a-a062-fa1922dfa9a8",e.temperaturePeriod="e95d1b25-251d-470a-a062-fa1922dfa9a8"}(b||(b={}));var V,L=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("temperaturechanged",b.temperature,this.temperatureChangedHandler.bind(this))];case 1:return e.sent(),[2]}}))}))},i.prototype.readTemperature=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(b.temperature)];case 1:return[2,e.sent().getInt8(0)]}}))}))},i.prototype.getTemperaturePeriod=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(b.temperaturePeriod)];case 1:return[2,e.sent().getUint16(0,!0)]}}))}))},i.prototype.setTemperaturePeriod=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return(t=new DataView(new ArrayBuffer(2))).setUint16(0,e,!0),[4,this.helper.setCharacteristicValue(b.temperaturePeriod,t)];case 1:return[2,n.sent()]}}))}))},i.prototype.temperatureChangedHandler=function(e){var t=e.target.value;this.dispatchEvent("temperaturechanged",t.getInt8(0))},i.uuid="e95d6100-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.accelerometerData="e95dca4b-251d-470a-a062-fa1922dfa9a8",e.accelerometerPeriod="e95dfb24-251d-470a-a062-fa1922dfa9a8"}(V||(V={}));var D,E,S=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("accelerometerdatachanged",V.accelerometerData,this.accelerometerDataChangedHandler.bind(this))];case 1:return e.sent(),[2]}}))}))},i.prototype.readAccelerometerData=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(V.accelerometerData)];case 1:return e=t.sent(),[2,this.dataViewToAccelerometerData(e)]}}))}))},i.prototype.getAccelerometerPeriod=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(V.accelerometerPeriod)];case 1:return[2,e.sent().getUint16(0,!0)]}}))}))},i.prototype.setAccelerometerPeriod=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return(t=new DataView(new ArrayBuffer(2))).setUint16(0,e,!0),[2,this.helper.setCharacteristicValue(V.accelerometerPeriod,t)]}))}))},i.prototype.accelerometerDataChangedHandler=function(e){var t=e.target.value,n=this.dataViewToAccelerometerData(t);this.dispatchEvent("accelerometerdatachanged",n)},i.prototype.dataViewToAccelerometerData=function(e){return{x:e.getInt16(0,!0)/1e3,y:e.getInt16(2,!0)/1e3,z:e.getInt16(4,!0)/1e3}},i.uuid="e95d0753-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.magnetometerData="e95dfb11-251d-470a-a062-fa1922dfa9a8",e.magnetometerPeriod="e95d386c-251d-470a-a062-fa1922dfa9a8",e.magnetometerBearing="e95d9715-251d-470a-a062-fa1922dfa9a8",e.magnetometerCalibration="e95db358-251d-470a-a062-fa1922dfa9a8"}(D||(D={})),function(e){e[e.unknown=0]="unknown",e[e.requested=1]="requested",e[e.completed=2]="completed",e[e.errored=3]="errored"}(E||(E={}));var T,x=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("magnetometerdatachanged",D.magnetometerData,this.magnetometerDataChangedHandler.bind(this))];case 1:return e.sent(),[4,this.helper.handleListener("magnetometerbearingchanged",D.magnetometerBearing,this.magnetometerBearingChangedHandler.bind(this))];case 2:return e.sent(),[4,this.helper.handleListener("magnetometercalibrationchanged",D.magnetometerCalibration,this.magnetometerCalibrationChangedHandler.bind(this))];case 3:return e.sent(),[2]}}))}))},i.prototype.calibrate=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){return[2,this.helper.setCharacteristicValue(D.magnetometerCalibration,new Uint8Array([1]))]}))}))},i.prototype.readMagnetometerData=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(D.magnetometerData)];case 1:return e=t.sent(),[2,this.dataViewToMagnetometerData(e)]}}))}))},i.prototype.readMagnetometerBearing=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(D.magnetometerBearing)];case 1:return 2===(e=t.sent()).byteLength?[2,e.getUint16(0,!0)]:[2,void 0]}}))}))},i.prototype.getMagnetometerPeriod=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.getCharacteristicValue(D.magnetometerPeriod)];case 1:return[2,e.sent().getUint16(0,!0)]}}))}))},i.prototype.setMagnetometerPeriod=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return(t=new DataView(new ArrayBuffer(2))).setUint16(0,e,!0),[2,this.helper.setCharacteristicValue(D.magnetometerPeriod,t)]}))}))},i.prototype.magnetometerDataChangedHandler=function(e){var t=e.target.value,n=this.dataViewToMagnetometerData(t);this.dispatchEvent("magnetometerdatachanged",n)},i.prototype.magnetometerBearingChangedHandler=function(e){var t=e.target.value;2===t.byteLength&&this.dispatchEvent("magnetometerbearingchanged",t.getUint16(0,!0))},i.prototype.magnetometerCalibrationChangedHandler=function(e){var t=e.target.value;1===t.byteLength&&this.dispatchEvent("magnetometercalibrationchanged",t.getUint8(0))},i.prototype.dataViewToMagnetometerData=function(e){return{x:e.getInt16(0,!0),y:e.getInt16(1,!0),z:e.getInt16(2,!0)}},i.uuid="e95df2d8-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.pinData="e95d8d00-251d-470a-a062-fa1922dfa9a8",e.pinAdConfiguration="e95d5899-251d-470a-a062-fa1922dfa9a8",e.pinIoConfiguration="e95db9fe-251d-470a-a062-fa1922dfa9a8",e.pwmControl="e95dd822-251d-470a-a062-fa1922dfa9a8"}(T||(T={}));var A,U,_=!0;!function(e){e[e.Digital=0]="Digital",e[e.Analogue=1]="Analogue"}(A||(A={})),function(e){e[e.Output=0]="Output",e[e.Input=1]="Input"}(U||(U={}));var B,P=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("pindatachanged",T.pinData,this.pinDataChangedHandler.bind(this))];case 1:return e.sent(),[2]}}))}))},i.prototype.readPinData=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(T.pinData)];case 1:return e=t.sent(),[2,this.dataViewToPinData(e)]}}))}))},i.prototype.writePinData=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.pinDataToDataView(e),[2,this.helper.setCharacteristicValue(T.pinData,t)]}))}))},i.prototype.getAdConfiguration=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(T.pinAdConfiguration)];case 1:return e=t.sent(),[2,this.dataViewToConfig(e)]}}))}))},i.prototype.setAdConfiguration=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.configToDataView(e),[2,this.helper.setCharacteristicValue(T.pinAdConfiguration,t)]}))}))},i.prototype.getIoConfiguration=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(T.pinIoConfiguration)];case 1:return e=t.sent(),[2,this.dataViewToConfig(e)]}}))}))},i.prototype.setIoConfiguration=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.configToDataView(e),[2,this.helper.setCharacteristicValue(T.pinIoConfiguration,t)]}))}))},i.prototype.setPwmControl=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){return t=this.pwmControlDataToDataView(e),[2,this.helper.setCharacteristicValue(T.pwmControl,t)]}))}))},i.prototype.pinDataChangedHandler=function(e){var t=e.target.value,n=this.dataViewToPinData(t);this.dispatchEvent("pindatachanged",n)},i.prototype.dataViewToPinData=function(e){for(var t=[],n=0;n<e.byteLength;n+=2)t.push({pin:e.getUint8(n),value:e.getUint8(n+1)});return t},i.prototype.pinDataToDataView=function(e){var t=new DataView(new ArrayBuffer(2*e.length));return e.forEach((function(e,n){t.setUint8(2*n,e.pin),t.setUint8(2*n+1,e.value)})),t},i.prototype.dataViewToConfig=function(e){for(var t=[],n=(e.getUint16(0)<<8)+e.getUint8(2),r=0;r<24;r++)t.push(n>>r);return t},i.prototype.configToDataView=function(e){for(var t=new DataView(new ArrayBuffer(3)),n=0,r=0;r<e.length;r++)n&=1<<e[r];return t.setUint16(0,n>>8,_),t.setUint8(2,255&n),t},i.prototype.pwmControlDataToDataView=function(e){var t=new DataView(new ArrayBuffer(7));return t.setUint8(0,e.pin),t.setUint16(1,e.value,_),t.setUint32(3,e.period,_),t},i.uuid="e95d127b-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.tx="6e400002-b5a3-f393-e0a9-e50e24dcca9e",e.rx="6e400003-b5a3-f393-e0a9-e50e24dcca9e"}(B||(B={}));var M,q=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("receive",B.tx,this.receiveHandler.bind(this))];case 1:return e.sent(),[4,this.helper.handleListener("receiveText",B.tx,this.receiveTextHandler.bind(this))];case 2:return e.sent(),[2]}}))}))},i.prototype.send=function(e){return n(this,void 0,void 0,(function(){return r(this,(function(t){return[2,this.helper.setCharacteristicValue(B.rx,e)]}))}))},i.prototype.sendText=function(e){return n(this,void 0,void 0,(function(){var t,n;return r(this,(function(r){return t=new TextEncoder,n=t.encode(e),[2,this.helper.setCharacteristicValue(B.rx,n)]}))}))},i.prototype.receiveHandler=function(e){var t=e.target.value,n=new Uint8Array(t.buffer);this.dispatchEvent("receive",n)},i.prototype.receiveTextHandler=function(e){var t=e.target.value,n=(new TextDecoder).decode(t.buffer);this.dispatchEvent("receiveText",n)},i.uuid="6e400001-b5a3-f393-e0a9-e50e24dcca9e",i}(m);!function(e){e.microBitRequirements="e95db84c-251d-470a-a062-fa1922dfa9a8",e.microBitEvent="e95d9775-251d-470a-a062-fa1922dfa9a8",e.clientRequirements="e95d23c4-251d-470a-a062-fa1922dfa9a8",e.clientEvent="e95d5404-251d-470a-a062-fa1922dfa9a8"}(M||(M={}));var H,I=function(e){function i(t){var n=e.call(this)||this;return n.helper=new o(t,n),n}return t(i,e),i.create=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return[4,(t=new i(e)).init()];case 1:return n.sent(),[2,t]}}))}))},i.prototype.init=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this.helper.handleListener("microbitevent",M.microBitEvent,this.eventHandler.bind(this))];case 1:return e.sent(),[4,this.helper.handleListener("microbitrequirementschanged",M.microBitRequirements,this.microbitRequirementsChangedHandler.bind(this))];case 2:return e.sent(),[2]}}))}))},i.prototype.getMicrobitRequirements=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(M.microBitRequirements)];case 1:return e=t.sent(),[2,this.viewToMicrobitEvent(e)]}}))}))},i.prototype.setClientRequirements=function(e,t){return n(this,void 0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return(n=new DataView(new ArrayBuffer(4))).setUint16(0,e,!0),n.setUint16(1,t,!0),[4,this.helper.setCharacteristicValue(M.clientRequirements,n)];case 1:return[2,r.sent()]}}))}))},i.prototype.readMicrobitEvent=function(){return n(this,void 0,void 0,(function(){var e;return r(this,(function(t){switch(t.label){case 0:return[4,this.helper.getCharacteristicValue(M.microBitEvent)];case 1:return e=t.sent(),[2,this.viewToMicrobitEvent(e)]}}))}))},i.prototype.writeClientEvent=function(e,t){return n(this,void 0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return(n=new DataView(new ArrayBuffer(4))).setUint16(0,e,!0),n.setUint16(1,t,!0),[4,this.helper.setCharacteristicValue(M.clientEvent,n)];case 1:return[2,r.sent()]}}))}))},i.prototype.microbitRequirementsChangedHandler=function(e){var t=e.target.value,n=this.viewToMicrobitEvent(t);this.dispatchEvent("microbitrequirementschanged",n)},i.prototype.eventHandler=function(e){var t=e.target.value,n=this.viewToMicrobitEvent(t);this.dispatchEvent("microbitevent",n)},i.prototype.viewToMicrobitEvent=function(e){return{type:e.getUint16(0,!0),value:e.getUint16(1,!0)}},i.uuid="e95d93af-251d-470a-a062-fa1922dfa9a8",i}(m);!function(e){e.dfuControl="e95d93b1-251d-470a-a062-fa1922dfa9a8"}(H||(H={}));var R=function(){function e(e){this.helper=new o(e)}return e.create=function(t){return n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,new e(t)]}))}))},e.prototype.requestDfu=function(){return this.helper.setCharacteristicValue(H.dfuControl,new Uint8Array([1]))},e.prototype.requestFlashCode=function(){return this.helper.setCharacteristicValue(H.dfuControl,new Uint8Array([2]))},e.uuid="e95d93b0-251d-470a-a062-fa1922dfa9a8",e}(),O=function(e){function i(t){var n=e.call(this)||this;return n.uartService=t,t.addEventListener("receiveText",(function(e){var t=e.detail;if(t.startsWith("c:")){var r=t.replace("c:","").split(":"),i=r[0],a=r[1];n.emit("receiveCommand",{command:i,value:a?parseInt(a,10):void 0})}})),n}return t(i,e),i.prototype.arg=function(e){return n(this,arguments,void 0,(function(e,t){var n;return void 0===t&&(t="arg"),r(this,(function(r){switch(r.label){case 0:return n="c:".concat(t,":").concat(e,"\n"),[4,this.uartService.sendText(n)];case 1:return r.sent(),[2]}}))}))},i.prototype.narg=function(){return n(this,arguments,void 0,(function(e){var t;return void 0===e&&(e="narg"),r(this,(function(n){switch(n.label){case 0:return t="c:".concat(e,"\n"),[4,this.uartService.sendText(t)];case 1:return n.sent(),[2]}}))}))},i}(m),k=function(){function e(e){this.services=e}return e.prototype.createService=function(e){return n(this,void 0,void 0,(function(){var t;return r(this,(function(n){switch(n.label){case 0:return(t=this.services.find((function(t){return t.uuid===e.uuid})))?[4,e.create(t)]:[2,void 0];case 1:return[2,n.sent()]}}))}))},e}(),j=function(e){return n(void 0,void 0,void 0,(function(){return r(this,(function(t){switch(t.label){case 0:return[4,e.requestDevice({filters:[{namePrefix:"BBC micro:bit"}],optionalServices:[u.uuid,y.uuid,C.uuid,L.uuid,S.uuid,x.uuid,P.uuid,q.uuid,I.uuid,R.uuid]})];case 1:return[2,t.sent()]}}))}))},N=function(e){return n(void 0,void 0,void 0,(function(){var t,n,i,a,o,s,c,h,f,d,l,v,p;return r(this,(function(r){switch(r.label){case 0:return e&&e.gatt?e.gatt.connected?[3,2]:[4,e.gatt.connect()]:[2,{}];case 1:r.sent(),r.label=2;case 2:return[4,e.gatt.getPrimaryServices()];case 3:return t=r.sent(),[4,(n=new k(t)).createService(u)];case 4:return i=r.sent(),[4,n.createService(y)];case 5:return a=r.sent(),[4,n.createService(C)];case 6:return o=r.sent(),[4,n.createService(L)];case 7:return s=r.sent(),[4,n.createService(S)];case 8:return c=r.sent(),[4,n.createService(x)];case 9:return h=r.sent(),[4,n.createService(q)];case 10:return f=r.sent(),[4,n.createService(I)];case 11:return d=r.sent(),[4,n.createService(R)];case 12:return l=r.sent(),[4,n.createService(P)];case 13:return v=r.sent(),p=f?new O(f):void 0,[2,{deviceInformationService:i,buttonService:a,ledService:o,temperatureService:s,accelerometerService:c,magnetometerService:h,uartService:f,eventService:d,dfuControlService:l,ioPinService:v,rpcService:p}]}}))}))};export{N as getServices,j as requestMicrobit}; //# sourceMappingURL=microbit.esm.js.map