-
Notifications
You must be signed in to change notification settings - Fork 2
/
tiusb.min.js
1 lines (1 loc) · 12.3 KB
/
tiusb.min.js
1
const SCREEN_SIZE=153600,BIT_5_FACTOR=255/31,BIT_6_FACTOR=255/63,TI_Event={TI_DEVICE_CONNECTED:0,TI_DEVICE_DISCONNECTED:1,TI_DEVICE_INFO:2,TI_DEVICE_SEND:3,TI_DEVICE_GET:4,TI_DEVICE_DIR:5,TI_DEVICE_FILESIZE:6,TI_DEVICE_SCREEN:7,TI_DEVICE_INSTALLOS:8,TI_DEVICE_ECHOTEST:9,TI_DEVICE_ECHOUPDATE:10};class TICalculatorCCode{constructor(){this.buffer8888=new Uint8Array(2*SCREEN_SIZE),this.t}init(e,t,n,a,r){this.connectCallback=e,this.disconnectCallback=t,this.connectingCallback=n,this.noDeviceSelectionCallback=a,this.connectionFailedCallback=r;const o=this;return new Promise((e,t)=>{const n=()=>{Module.resolveInitialization=(()=>{o.t&&clearTimeout(o.t),Module.print=function(){return function(e){arguments.length>1&&(e=Array.prototype.slice.call(arguments).join(" ")),0===e.indexOf("DEBUG:")?console.debug(e):0===e.indexOf("ERROR:")?console.error(e):console.log(e)}},Module.printErr=function(e){arguments.length>1&&(e=Array.prototype.slice.call(arguments).join(" ")),console.error(e)};const t=(e,t,n)=>Module[`_${e}`]?Module.cwrap(e,t,n):void console.error(`Couldn't find ${e} in the module!!!`);o._getDevices=t("getDevices","",[""]),o.deviceAdded=t("deviceAdded","",["number"]),o.deviceRemoved=t("deviceRemoved","",["number"]),o.readCallback=t("readCallback","",["number","number","number"]),o._getFile=t("webapi_getFile","number",["number"]),o._getDirectory=t("webapi_getDirList","number",["number","number","number"]),o._getScreen=t("webapi_getScreen","number",["number","number"]),o._getDeviceInfo=t("webapi_getDeviceInfo","number",["number"]),o.sendFile=t("webapi_sendFile","number",["number","number","number"]),o._installOS=t("webapi_installOS","number",["number","number","number"]),o._getFileSize=t("webapi_getFileSize","number",["number","number"]),o.shutdown=t("webapi_shutdown","number",[""]),o.getLastResult=t("webapi_getResult","number",[""]),o.getInstallOSProgress=t("webapi_getInstallOSProgress","number",[""]),o.cancelOperation=t("webapi_cancelOperation","",[""]),o.cancelDirListing=t("webapi_cancelDirListing","",[""]),o._setLogLevels=t("webapi_setLogLevels","number",["number","number"]),o._echoTest=t("webapi_echo","number",["number","number","number","number","number"]),o.buffer8888=new Uint8Array(2*SCREEN_SIZE),e()}),o.t=setTimeout(()=>{t("WASM Load timed out")},2e3)};"undefined"!=typeof Module?n():(console.log("waiting for connectivity module to load"),setTimeout(()=>{n()},3e3))})}async getDevices(){this._getDevices()}async getFileSize(e){TICalculatorCCode.instance.sizeptr=this.allocateMemory(4),TICalculatorCCode.instance.nameptr=this.allocateAndSet(intArrayFromString(e),"i8",ALLOC_NORMAL);let t=await this.getFileSizeInternal(),n=getValue(TICalculatorCCode.instance.sizeptr,"i32");return this.freeMem(TICalculatorCCode.instance.sizeptr),this.freeMem(TICalculatorCCode.instance.nameptr),t<0?t:n}getFileSizeInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._getFileSize(TICalculatorCCode.instance.nameptr,TICalculatorCCode.instance.sizeptr)})}async putFile(e,t){TICalculatorCCode.instance.bufferptr=this.allocateAndSet(e,"i8",ALLOC_NORMAL),TICalculatorCCode.instance.nameptr=this.allocateAndSet(intArrayFromString(t),"i8",ALLOC_NORMAL),TICalculatorCCode.instance.bufferlength=e.length;let n=await this.putFileInternal();return this.freeMem(TICalculatorCCode.instance.bufferptr),this.freeMem(TICalculatorCCode.instance.nameptr),n}putFileInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this.sendFile(TICalculatorCCode.instance.bufferptr,TICalculatorCCode.instance.bufferlength,TICalculatorCCode.instance.nameptr)})}async getFile(e){TICalculatorCCode.instance.nameptr=this.allocateAndSet(intArrayFromString(e),"i8",ALLOC_NORMAL);let t=await this.getFileInternal();return this.freeMem(TICalculatorCCode.instance.nameptr),t}getFileInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._getFile(TICalculatorCCode.instance.nameptr)})}getFileContents(){const e=FS.root.contents["getfile.tns"],t=e.usedBytes;if(null!=e&&t>0){const n=new Uint8Array(t);for(let a=0;a<t;a++)n[a]=e.contents[a];return n}return null}async getDirectory(){TICalculatorCCode.instance.bufferptr=this.allocateMemory(102400),TICalculatorCCode.instance.bufferlength=102400,TICalculatorCCode.instance.nameptr=this.allocateAndSet(intArrayFromString("/"),"i8",ALLOC_NORMAL);let e=await this.getDirectoryInternal();return this.freeMem(TICalculatorCCode.instance.nameptr),this.freeMem(TICalculatorCCode.instance.bufferptr),e}getDirectoryInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._getDirectory(TICalculatorCCode.instance.bufferptr,TICalculatorCCode.instance.bufferlength,TICalculatorCCode.instance.nameptr)})}async getScreen(){TICalculatorCCode.instance.bufferptr=this.allocateMemory(SCREEN_SIZE);let e=await this.getScreenInternal();for(let t=0,n=0;t<SCREEN_SIZE;t+=2,n+=4){const a=e[t]+(e[t+1]<<8);this.buffer8888[n]=255&Math.round((a>>11)*BIT_5_FACTOR),this.buffer8888[n+1]=255&Math.round((a>>5&63)*BIT_6_FACTOR),this.buffer8888[n+2]=255&Math.round((31&a)*BIT_5_FACTOR),this.buffer8888[n+3]=255}return this.freeMem(TICalculatorCCode.instance.bufferptr),this.buffer8888}getScreenInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._getScreen(TICalculatorCCode.instance.bufferptr,SCREEN_SIZE)})}async getDeviceInfo(){TICalculatorCCode.instance.bufferptr=this.allocateMemory(40),TICalculatorCCode.instance.bufferlength=40;const e=await this.getDeviceInfoInternal(),t={};return t.deviceCode=e[0],t.major=e[1],t.minor=e[2],t.build=e[3]|e[4]<<8,this.freeMem(TICalculatorCCode.instance.bufferptr),t}getDeviceInfoInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._getDeviceInfo(TICalculatorCCode.instance.bufferptr)})}async installOS(e){TICalculatorCCode.instance.bufferptr=this.allocateAndSet(e,"i8",ALLOC_NORMAL),TICalculatorCCode.instance.bufferlength=e.length,TICalculatorCCode.instance.installSpaceDeficitPtr=this.allocateMemory(4);let t=await this.installOSInternal();return this.freeMem(TICalculatorCCode.instance.bufferptr),this.freeMem(TICalculatorCCode.instance.installSpaceDeficitPtr),t}installOSInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._installOS(TICalculatorCCode.instance.bufferptr,TICalculatorCCode.instance.bufferlength,TICalculatorCCode.instance.installSpaceDeficitPtr)})}async echoTest(e,t,n,a,r){TICalculatorCCode.instance.bufferptr=this.allocateAndSet(e,"i8",ALLOC_NORMAL),TICalculatorCCode.instance.min=t,TICalculatorCCode.instance.max=n,TICalculatorCCode.instance.step=a,TICalculatorCCode.instance.iterations=r;let o=await this.echoTestInternal();return this.freeMem(TICalculatorCCode.instance.bufferptr),o}echoTestInternal(){return new Promise(e=>{TICalculatorCCode.instance.resolve=e,this._echoTest(TICalculatorCCode.instance.bufferptr,TICalculatorCCode.instance.min,TICalculatorCCode.instance.max,TICalculatorCCode.instance.step,TICalculatorCCode.instance.iterations)})}getContentsOfLogFile(e){return FS.root.contents[e].contents}setLogLevels(e,t){this._setLogLevels(e,t)}getUint8ArrayFromCbuffer(e,t){return new Uint8Array(Module.HEAPU8.subarray(e,t))}getJSStringFromCString(e){return Module.UTF8ToString(e)}allocateMemory(e){return Module._malloc(e)}setArrayInModule(e,t){return Module.HEAPU8.set(e,t),t}allocateAndSet(e,t,n){const a=this.allocateMemory(e.length);return Module.HEAPU8.set(e,a),a}setStringInModule(e){var t=this.allocateMemory(e.length+1);return Module.stringToUTF8(e,t,e.length+1),t}freeMem(e){Module._free(e)}setContent(e){let t=document.getElementById("content");""===t.value?t.value=e:t.value+="\n"+e}eventCallback(e){let t,n,a;switch(e){case TI_Event.TI_DEVICE_CONNECTED:this.connectCallback();break;case TI_Event.TI_DEVICE_DISCONNECTED:this.disconnectCallback();break;case TI_Event.TI_DEVICE_INFO:a=TICalculatorCCode.instance.bufferptr;const r=TICalculatorCCode.instance.bufferptr;TICalculatorCCode.instance.resolve(this.getUint8ArrayFromCbuffer(a,a+r));break;case TI_Event.TI_DEVICE_SEND:case TI_Event.TI_DEVICE_GET:TICalculatorCCode.instance.resolve(this.getLastResult());break;case TI_Event.TI_DEVICE_DIR:t=this.getJSStringFromCString(TICalculatorCCode.instance.bufferptr),TICalculatorCCode.instance.resolve(t);break;case TI_Event.TI_DEVICE_FILESIZE:TICalculatorCCode.instance.resolve(this.getLastResult());break;case TI_Event.TI_DEVICE_INSTALLOS:n=getValue(TICalculatorCCode.instance.installSpaceDeficitPtr,"i32"),TICalculatorCCode.instance.resolve({statusResult:this.getLastResult(),spaceDeficit:n});break;case TI_Event.TI_DEVICE_SCREEN:a=TICalculatorCCode.instance.bufferptr,TICalculatorCCode.instance.resolve(this.getUint8ArrayFromCbuffer(a,a+SCREEN_SIZE));break;case TI_Event.TI_DEVICE_ECHOTEST:TICalculatorCCode.instance.resolve(this.getLastResult());break;case TI_Event.TI_DEVICE_ECHOUPDATE:console.log("echo update\n")}}}TICalculatorCCode.getInstance=(()=>(TICalculatorCCode.instance||(TICalculatorCCode.instance=new TICalculatorCCode),TICalculatorCCode.instance));class TIWebConnApi{constructor(){}init(e,t,n,a,r){return TICalculatorCCode.getInstance().init(e,t,n,a,r)}getFileSize(e){return TICalculatorCCode.getInstance().getFileSize(e)}putFile(e,t){return TICalculatorCCode.getInstance().putFile(e,t)}getFile(e){return TICalculatorCCode.getInstance().getFile(e)}getFileContents(){return TICalculatorCCode.getInstance().getFileContents()}getDirectory(){return TICalculatorCCode.getInstance().getDirectory()}getScreen(){return TICalculatorCCode.getInstance().getScreen()}getDeviceInfo(){return TICalculatorCCode.getInstance().getDeviceInfo()}installOS(e){return TICalculatorCCode.getInstance().installOS(e)}getContentsOfLogFile(e){return TICalculatorCCode.getInstance().getContentsOfLogFile(e)}setLogLevels(e,t){return TICalculatorCCode.getInstance().setLogLevels(e,t)}getDevices(){return TICalculatorCCode.getInstance().getDevices()}getInstallOSProgress(){return TICalculatorCCode.getInstance().getInstallOSProgress()}cancelOperation(){return TICalculatorCCode.getInstance().cancelOperation()}cancelDirListing(){return TICalculatorCCode.getInstance().cancelDirListing()}echoTest(e,t,n,a,r){return TICalculatorCCode.getInstance().echoTest(e,t,n,a,r)}}function TI_DeviceManager(){TI_DeviceManager.instance=this,this.device}TI_DeviceManager.getInstance=(()=>(TI_DeviceManager.instance||(TI_DeviceManager.instance=new TI_DeviceManager),TI_DeviceManager.instance)),TI_DeviceManager.prototype.addDevice=(e=>{const t={webUSBDevice:e,open:!1,vendorId:e.vendorId,productId:e.productId,name:"CX-II (not from device)"};return TI_DeviceManager.instance.tiDevice=t,0}),TI_DeviceManager.prototype.removeDevice=(e=>TI_DeviceManager.instance.tiDevice.webUSBDevice===e.device?(console.log("--- removing device "),TI_DeviceManager.instance.tiDevice=null,!0):(console.log("NOT removing device "),!1)),TI_DeviceManager.prototype.getDevice=(()=>TI_DeviceManager.instance.tiDevice);const VID=1105,PID=57378,filter={vendorId:VID,productId:PID};function TI_USB(){TI_USB.instance=this}TI_USB.getInstance=(()=>(TI_USB.instance||(TI_USB.instance=new TI_USB),TI_USB.instance)),TI_USB.prototype.openDevice=(e=>new Promise((t,n)=>{(async()=>{try{await e.webUSBDevice.open();const a=e.webUSBDevice.configurations[0].configurationValue;await e.webUSBDevice.selectConfiguration(a);for(let t=0;t<e.webUSBDevice.configurations.length;t+=1)for(let n=0;n<e.webUSBDevice.configurations[t].interfaces[0].alternates[0].endpoints.length;n+=1){const t=e.webUSBDevice.configurations[0].interfaces[0].alternates[0].endpoints[n];"out"===t.direction&&(e.outputEndpoint=t.endpointNumber),"in"===t.direction&&(e.inputEndpoint=t.endpointNumber)}e.name=e.webUSBDevice.productName,e.open=!0,await e.webUSBDevice.claimInterface(0),console.log("opened device + "+JSON.stringify(e)),t(e)}catch(e){n(e)}})()})),TI_USB.prototype.getDevices=(()=>new Promise((e,t)=>{(async()=>{console.log("Calling requestDevice");try{var n=await navigator.usb.requestDevice({filters:[filter]});e(n)}catch(e){t(e)}})()})),TI_USB.prototype.write=((e,t)=>new Promise((n,a)=>{!async function(){try{const r=e.outputEndpoint;let o="ok"===(await e.webUSBDevice.transferOut(r,t)).status?0:1;0===o?n(o):a(o)}catch(e){a(e)}}()})),TI_USB.prototype.read=(e=>{return new Promise((t,n)=>{!async function(){try{const a=e.inputEndpoint;let r=await e.webUSBDevice.transferIn(a,4096);"ok"===r.status?t(new Uint8Array(r.data.buffer)):n(r.status)}catch(e){n(e)}}()})}),TI_USB.prototype.addOnDeviceAddedListener=function(e){navigator.usb.onconnect=async function(t){e(t)}},TI_USB.prototype.addOnDeviceRemovedListener=function(e){navigator.usb.ondisconnect=function(t){e(t)}};