esp-web-tools
Version:
Web tools for ESP devices
2 lines (1 loc) • 2.17 kB
JavaScript
import{c as e,f as t,a as i}from"./index-rScOpSrP.js";const n=(e,t)=>{if(!(e<1024)){if(t){const t=1792===e||2048===e;return{highSpeed:t,shiftBaudIndex:!0,maxBaud:t?12e6:3e6}}switch(e){case 1024:case 1536:return{highSpeed:!1,shiftBaudIndex:!1,maxBaud:3e6};case 1280:case 4096:return{highSpeed:!1,shiftBaudIndex:!0,maxBaud:3e6};default:return{highSpeed:!0,shiftBaudIndex:!0,maxBaud:12e6}}}},s=[0,3,2,4,1,5,6,7],r=(e,t)=>Math.floor((e+Math.floor(t/2))/t),a=(e,t)=>{const i=Math.min(Math.max(e,1),t.maxBaud),n=t.highSpeed?r(96e7,10*i):r(48e6,2*i);let a=n>>3|s[7&n]<<14;return 1===a?a=0:16385===a&&(a=1),t.highSpeed&&(a|=131072),a},h=(e,t=!1)=>{const i={none:0,odd:1,even:2}[e.parity],n=2===e.stopBits?2:0;return e.dataBits|i<<8|n<<11|(t?16384:0)},c=(e,t)=>768|(e?1:0)|(t?2:0);class d{constructor(e){this._device=e,this._index=0,this._packetSize=64}_out(t,i,n){return e(this._device,{requestType:"vendor",recipient:"device",request:t,value:i,index:n})}async open(){const e=t(this._device,255);if(!e)throw new Error("No vendor specific interface found");const s=this._device.configuration.interfaces.length>1,r=this._device.deviceVersionMajor<<8|this._device.deviceVersionMinor<<4|this._device.deviceVersionSubminor,a=n(r,s);if(!a)throw new Error("Unsupported FTDI chip");this._chip=a,this._index=s?e.interfaceNumber+1:0,await this._device.claimInterface(e.interfaceNumber);const h=i(e);return this._packetSize=h.in.packetSize,await this._out(2,0,this._index),h}async setLineConfig(e){this._config=e;const t=a(e.baudRate,this._chip),i=65535&t;let n=t>>>16&65535;this._chip.shiftBaudIndex&&(n=n<<8|this._index),await this._out(3,i,n),await this._out(4,h(e),this._index)}setSignals(e,t){return this._out(1,c(e,t),this._index)}setBreak(e){return this._out(4,h(this._config,e),this._index)}processRead(e){return o(e,this._packetSize)}}const o=(e,t)=>{const i=new Uint8Array(e.byteLength);let n=0;for(let s=0;s<e.byteLength;s+=t){const r=e.subarray(s+2,Math.min(s+t,e.byteLength));i.set(r,n),n+=r.byteLength}return i.subarray(0,n)},u=e=>new d(e);export{u as createFtdiDriver,a as ftdiBaudDivisor,n as ftdiChipFromVersion,h as ftdiDataBits,c as ftdiModemControl,o as ftdiStripStatus};