UNPKG

webmonome

Version:

communicate with devices beyond a translator

11 lines (10 loc) 4.95 kB
'use strict';(function(g,f){"object"===typeof exports&&"undefined"!==typeof module?f(exports):"function"===typeof define&&define.amd?define(["exports"],f):(g=g||self,f(g.WebMonome={}))})(this,function(g){function f(a,b,c){return Math.max(Math.min(a,c),b)}function h(a){let b=0;for(let c=0;c<Math.min(8,a.length);c++)b|=f(a[c],0,1)<<c;return b}function l(a){throw Error("WebMonome: "+a);}function p(a,b=0){console[["log","warn","error"][b]]("WebMonome: "+a)}class q extends EventTarget{constructor(a){super(); this.device=a;this.callbacks={};this.endpointIn=r(a,"in");this.endpointOut=r(a,"out")}async listen(){if(this.isConnected){var a=await this.read(64);"ok"===a.status&&0<a.data.byteLength&&this.processData(a.data,0);await this.listen()}}async read(a){if(this.isConnected)return this.device.transferIn(this.endpointIn,a)}async write(a){a=new Uint8Array(a);let b=await this.device.transferOut(this.endpointOut,a);"ok"!==b.status&&l("write error");b.bytesWritten!==a.byteLength&&l("write is missing bytes"); return b}get isConnected(){return!(!this.device||!this.device.opened)}emit(a,b){a=new CustomEvent(a,{detail:b});this.dispatchEvent(a);return a}}let r=(a,b)=>{const c={in:1,out:2};let d;try{d=a.configuration.interfaces[m[a.vendorId]||0].alternates[0].endpoints.find(({direction:e})=>e===b)}catch(e){p(e,2)}return d?d.endpointNumber:c[b]},t=([a,b,...c])=>[(a&15)<<4|b&15,...(Array.isArray(c)?c:[])],n=(a,b)=>{const c=[];for(var d=0;d<Math.ceil(b/2);d++)c[d]=0;for(d=0;d<Math.min(b,a.length);d++){const e= Math.floor(d/2),k=0===d%2?4:0;"number"!==typeof c[e]&&(c[e]=0);c[e]|=f(a[d],0,15)<<k}return c};class u extends q{constructor(a){super(a)}processData(a,b){switch(a.getUint8(b++)){case 0:this.emit("query",{type:a.getUint8(b++),count:a.getUint8(b++)});break;case 1:let c="";for(let d=0;32>d;d++)c+=String.fromCharCode(a.getUint8(b++));this.emit("getId",c);break;case 3:this.emit("getGridSize",{x:a.getUint8(b++),y:a.getUint8(b++)});break;case 33:this.emit("gridKeyDown",{x:a.getUint8(b++),y:a.getUint8(b++)}); break;case 32:this.emit("gridKeyUp",{x:a.getUint8(b++),y:a.getUint8(b++)})}a.byteLength>b+1&&this.processData(a,b)}query(){return this.writeBuffer(0,0)}getId(){return this.writeBuffer(0,1)}getGridSize(){return this.writeBuffer(0,5)}gridLed(a,b,c){return this.writeBuffer(1,c?1:0,a,b)}gridLedAll(a){return this.writeBuffer(1,a?3:2)}gridLedCol(a,b,c){if(Array.isArray(c))return this.writeBuffer(1,6,a,b,h(c))}gridLedRow(a,b,c){if(Array.isArray(c))return this.writeBuffer(1,5,a,b,h(c))}gridLedMap(a,b,c){if(Array.isArray(c)){var d= [0,0,0,0,0,0,0,0];for(let e=0;e<Math.min(64,c.length);e++){let k=Math.floor(e/8),v=e%8;d[k]|=f(c[e],0,1)<<v}return this.writeBuffer(1,4,a,b,...d)}}gridLedIntensity(a){return this.writeBuffer(1,7,f(a,0,15))}gridLedLevel(a,b,c){return this.writeBuffer(1,8,a,b,f(c,0,15))}gridLedLevelAll(a){return this.writeBuffer(1,9,f(a,0,15))}gridLedLevelCol(a,b,c){if(Array.isArray(c))return this.writeBuffer(1,12,a,b,...n(c,8))}gridLedLevelRow(a,b,c){if(Array.isArray(c))return this.writeBuffer(1,11,a,b,...n(c,8))}gridLedLevelMap(a, b,c){if(Array.isArray(c))return this.writeBuffer(1,10,a,b,...n(c,64))}writeBuffer(){return this.write(t(Array.from(arguments)))}}class w extends q{constructor(a){super(a)}processData(a,b){let c;switch(a.getUint8(b++)){case 0:var d=a.getUint8(b++);[d,c]=[d>>4,d&15];this.emit("gridKeyDown",{x:d,y:c});break;case 16:d=a.getUint8(b++),[d,c]=[d>>4,d&15],this.emit("gridKeyUp",{x:d,y:c})}a.byteLength>b+1&&this.processData(a,b)}query(){}getId(){}getGridSize(){let [,a]=this.device.serialNumber.match(/^m(64|128|256)/); this.emit("getGridSize",{64:{x:8,y:8},128:{x:16,y:8},256:{x:16,y:16}}[a])}gridLed(a,b,c){return this.write([c?32:48,a<<4|b])}gridLedAll(a){return this.write([144|a&1])}gridLedCol(a,b,c){if(Array.isArray(c))return this.write([(8===c.length?80:112)|a&15,h(c)])}gridLedRow(a,b,c){if(Array.isArray(c))return this.write([(8===c.length?64:96)|b&15,h(c)])}gridLedMap(a,b,c){}gridLedIntensity(a){return this.write([160|f(a,0,15)&15])}gridLedLevel(a,b,c){return this.gridLed(a,b,7<c)}gridLedLevelAll(a){return this.gridLedIntensity(a)}gridLedLevelCol(a, b,c){return this.gridLedCol(a,b,c.map(d=>7<d))}gridLedLevelRow(a,b,c){return this.gridLedRow(a,b,c.map(d=>7<d))}gridLedLevelMap(a,b,c){}}let m={1027:0,1155:1},x="navigator"in window&&"usb"in navigator;g.default={async connect(){if(!x)return p("this browser does not support WebUsb.",1);let a;try{a=await navigator.usb.requestDevice({filters:[{vendorId:1027},{vendorId:1155}]});await a.open();null===a.configuration&&await a.selectConfiguration(1);await a.claimInterface(m[a.vendorId]||0);if(/^m(64|128|256)/.test(a.serialNumber)|| /^mk/.test(a.serialNumber))var b="series";else/^[Mm]\d+/.test(a.serialNumber)?b="mext":(l("device type not yet supported"),b=void 0);var c=new {mext:u,series:w}[b](a);c.listen();return c}catch(d){throw d;}}};g.getInterfaceForVendor=a=>m[a]||0;Object.defineProperty(g,"__esModule",{value:!0})})