forked from espruino/EspruinoWebTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.min.js
9 lines (9 loc) · 6.5 KB
/
uart.min.js
1
2
3
4
5
6
7
8
9
/*
--------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
--------------------------------------------------------------------
Minified version of https://github.com/asterics/EspruinoWebTools/blob/master/uart.js
*/
!function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof module&&module.exports?module.exports=n():e.UART=n()}("undefined"!=typeof self?self:this,(function(){if("undefined"!=typeof navigator){var e=[],n={name:"Web Bluetooth",description:"Bluetooth LE devices",svg:'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17.71 7.71L12 2h-1v7.59L6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 11 14.41V22h1l5.71-5.71-4.3-4.29 4.3-4.29zM13 5.83l1.88 1.88L13 9.59V5.83zm1.88 10.46L13 18.17v-3.76l1.88 1.88z" fill="#ffffff"/></svg>',isSupported:function(){return navigator.platform.indexOf("Win")>=0&&(navigator.userAgent.indexOf("Chrome/54")>=0||navigator.userAgent.indexOf("Chrome/55")>=0||navigator.userAgent.indexOf("Chrome/56")>=0)?"Chrome <56 in Windows has navigator.bluetooth but it's not implemented properly":window&&window.location&&"http:"==window.location.protocol&&"localhost"!=window.location.hostname?"Serving off HTTP (not HTTPS) - Web Bluetooth not enabled":!!navigator.bluetooth||(/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream?"To use Web Bluetooth on iOS you'll need the WebBLE App.\nPlease go to https://itunes.apple.com/us/app/webble/id1193531073 to download it.":"This Web Browser doesn't support Web Bluetooth.\nPlease see https://www.espruino.com/Puck.js+Quick+Start")},connect:function(e,n,t){var i,o,u,f="6e400001-b5a3-f393-e0a9-e50e24dcca9e",l=void 0,d=[],h=!1,g=20;e.close=function(n){e.isOpening=!1,e.isOpen?(e.isOpen=!1,e.emit("close")):n&&n(null),l&&(l.disconnect(),l=void 0,o=void 0,u=void 0)},e.write=function(n,t){n&&d.push({data:n,callback:t,maxLength:n.length}),e.isOpen&&!e.txInProgress&&function n(){if(h)return void setTimeout(n,50);var t;if(!d.length)return void r.writeProgress();var i=d[0];r.writeProgress(i.maxLength-i.data.length,i.maxLength),i.data.length<=g?(t=i.data,i.data=void 0):(t=i.data.substr(0,g),i.data=i.data.substr(g));e.txInProgress=!0,s(2,"Sending "+JSON.stringify(t)),o.writeValue(c(t)).then((function(){s(3,"Sent"),i.data||(d.shift(),i.callback&&i.callback()),e.txInProgress=!1,n()})).catch((function(n){s(1,"SEND ERROR: "+n),d=[],e.close()}))}()};var v=n||{filters:[{namePrefix:"Puck.js"},{namePrefix:"Pixl.js"},{namePrefix:"MDBT42Q"},{namePrefix:"Bangle"},{namePrefix:"RuuviTag"},{namePrefix:"iTracker"},{namePrefix:"Thingy"},{namePrefix:"Espruino"},{services:[f]}],optionalServices:[f]};return navigator.bluetooth.requestDevice(v).then((function(n){return s(1,"Device Name: "+n.name),s(1,"Device ID: "+n.id),n.addEventListener("gattserverdisconnected",(function(){s(1,"Disconnected (gattserverdisconnected)"),e.close()})),n.gatt.connect()})).then((function(e){return s(1,"Connected"),l=e,e.getPrimaryService(f)})).then((function(e){return s(2,"Got service"),(i=e).getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e")})).then((function(n){return u=n,s(2,"RX characteristic:"+JSON.stringify(u)),u.addEventListener("characteristicvaluechanged",(function(n){var t=n.target.value;if(t.byteLength>g&&(s(2,"Received packet of length "+t.byteLength+", increasing chunk size"),g=t.byteLength),r.flowControl)for(var i=0;i<t.byteLength;i++){var o=t.getUint8(i);17==o&&(s(2,"XON received => resume upload"),h=!1),19==o&&(s(2,"XOFF received => pause upload"),h=!0)}var c=a(t.buffer);s(3,"Received "+JSON.stringify(c)),e.emit("data",c)})),u.startNotifications()})).then((function(){return i.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e")})).then((function(e){o=e,s(2,"TX characteristic:"+JSON.stringify(o))})).then((function(){e.txInProgress=!1,e.isOpen=!0,e.isOpening=!1,!1,[],t(e),e.emit("open"),e.write()})).catch((function(n){s(1,"ERROR: "+n),t(null),e.close()})),e}},t={name:"Web Serial",description:"USB connected devices",svg:'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M15 7v4h1v2h-3V5h2l-3-4-3 4h2v8H8v-2.07c.7-.37 1.2-1.08 1.2-1.93 0-1.21-.99-2.2-2.2-2.2-1.21 0-2.2.99-2.2 2.2 0 .85.5 1.56 1.2 1.93V13c0 1.11.89 2 2 2h3v3.05c-.71.37-1.2 1.1-1.2 1.95 0 1.22.99 2.2 2.2 2.2 1.21 0 2.2-.98 2.2-2.2 0-.85-.49-1.58-1.2-1.95V15h3c1.11 0 2-.89 2-2v-2h1V7h-4z" fill="#ffffff"/></svg>',isSupported:function(){return navigator.serial?!window||!window.location||"http:"!=window.location.protocol||"localhost"==window.location.hostname||"Serving off HTTP (not HTTPS) - Web Serial not enabled":"No navigator.serial - Web Serial not enabled"},connect:function(e,n,t){var i;function o(){e.isOpening=!1,e.isOpen&&(s(1,"Disconnected"),e.isOpen=!1,e.emit("close"))}var r=n||{};return navigator.serial.requestPort(r).then((function(e){return s(1,"Connecting to serial port"),i=e,e.open({baudRate:115200})})).then((function(){!function n(){var t=i.readable.getReader();t.read().then((function({value:i,done:r}){if(t.releaseLock(),i){var c=a(i.buffer);s(3,"Received "+JSON.stringify(c)),e.emit("data",c)}r?o():n()})).catch((function(e){s(0,"ERROR: "+e),o()}))}(),s(1,"Serial connected. Receiving data..."),e.txInProgress=!1,e.isOpen=!0,e.isOpening=!1,t(e)})).catch((function(e){s(0,"ERROR: "+e),t(null),o()})),e.close=function(e){i&&(i.close(),i=void 0),o()},e.write=function(n,t){var o=i.writable.getWriter();o.write(c(n)).then((function(){t&&t()})).catch((function(n){s(0,"SEND ERROR: "+n),e.close()})),o.releaseLock()},e}};e.push(n),e.push(t);var i="CONN_TYPE_BT",o="CONN_TYPE_SERIAL",r={debug:0,flowControl:!0,log:function(e,n){e<=this.debug&&console.log("<UART> "+n)},writeProgress:function(e,n){},connectBluetooth:function(e,n){return u(e,i,n)},connectSerial:function(e,n){return u(e,o,n)}};return r}function a(e){return String.fromCharCode.apply(null,new Uint8Array(e))}function c(e){for(var n=new ArrayBuffer(e.length),t=new Uint8Array(n),i=0,o=e.length;i<o;i++)t[i]=e.charCodeAt(i);return n}function s(e,n){r.log&&r.log(e,n)}function u(e,r,a){var c={on:function(e,n){this["on"+e]=n},emit:function(e,n){this["on"+e]&&this["on"+e](n)},isOpen:!1,isOpening:!0,txInProgress:!1};switch(r){case i:f(n)&&(c=n.connect(c,a,e));break;case o:f(t)&&(c=t.connect(c,a,e))}return c}function f(e){var n=e.isSupported();return!0!==n&&s(0,e.name+" not supported, "+n),!0===n}}));