{"version":3,"file":"angular-serial.mjs","sources":["../../../projects/angular-serial/src/lib/ngx-serial.ts","../../../projects/angular-serial/src/public-api.ts","../../../projects/angular-serial/src/angular-serial.ts"],"sourcesContent":["import { NONE_TYPE } from \"@angular/compiler\";\n\nexport class NgxSerial {\n\n    private port: any;\n    private options = { baudRate: 9600, dataBits: 8, parity: 'none', bufferSize: 256, flowControl: 'none' }; //Default\n    private writer: any;\n    private readFunction: Function;\n    private controlCharacter: string = \"\\n\";\n    private reader:any;\n    private readableStreamClosed:any;\n    private writableStreamClosed:any;\n    private keepReading:boolean = true;\n  \n    constructor(readFunction: Function, options?: any, controlCharacter?: any) {\n      this.readFunction = readFunction;\n      if (options)\n        this.options = options;\n      if (controlCharacter)\n        this.controlCharacter = controlCharacter;\n  \n    }\n    public async sendData(data: string) {\n      await this.writer.write(data);\n    }\n  \n    private async readLoop() {\n      \n  \n      while (this.port.readable && this.keepReading ) {\n        const textDecoder = new TextDecoderStream();\n        this.readableStreamClosed = this.port.readable.pipeTo(textDecoder.writable);\n        this.reader = textDecoder.readable\n          .pipeThrough(new TransformStream(new LineBreakTransformer(this.controlCharacter)))\n          .getReader();\n  \n        try {\n          while (true) {\n            const { value, done } = await this.reader.read();\n            if (done) {\n              break;\n            }\n            if (value) {\n              this.readFunction(value);\n            }\n          }\n        } catch (error) {\n          console.error(\"Read Loop error. Have the serial device been disconnected ? \");\n        }\n      }\n    }\n    public async close(callback:Function) {\n      this.keepReading = false;\n      this.reader.cancel();\n      await this.readableStreamClosed.catch(() => {});\n      this.writer.close();\n      await this.writableStreamClosed;\n      await this.port.close();\n      callback(null);\n    }\n  \n    public async connect(callback:Function) {\n      this.keepReading = true;\n      if (\"serial\" in navigator) {\n        // The Web Serial API is supported by the browser.\n        let nav: any = navigator;\n        const ports = await nav.serial.getPorts();\n  \n        try {\n          this.port = await nav.serial.requestPort();\n  \n        } catch (error) {\n          console.error(\"Requesting port error: \" + error);\n          return;\n        }\n  \n        try {\n         await this.port.open(this.options);\n  \n  \n        } catch (error) {\n          console.error(\"Opening port error: \" + error);\n          return;\n        }\n  \n        const textEncoder = new TextEncoderStream();\n        this.writableStreamClosed = textEncoder.readable.pipeTo(this.port.writable);\n        this.writer = textEncoder.writable.getWriter();\n  \n        this.readLoop();\n  \n        callback(this.port);\n  \n      } else {\n        console.error(\"This browser does NOT support the Web Serial API\");\n      }\n  \n    }\n  }\n  \n  class LineBreakTransformer {\n    container: any=\"\";\n    private controlCharacter: string;\n  \n    constructor(controlCharacter: string) {\n      this.container = '';\n      this.controlCharacter = controlCharacter\n    }\n  \n    transform(chunk:any, controller:any) {\n      this.container += chunk;\n      const lines = this.container.split(this.controlCharacter);\n      this.container = lines.pop();\n      lines.forEach((line: any) => controller.enqueue(line));\n    }\n  \n    flush(controller:any) {\n      controller.enqueue(this.container);\n    }\n  }","/*\n * Public API Surface of ngx-serial\n */\n\n/*export * from './lib/ngx-serial.service';\nexport * from './lib/ngx-serial.component';\nexport * from './lib/ngx-serial.module';*/\nexport * from './lib/ngx-serial';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":"MAEa,SAAS,CAAA;AAEV,IAAA,IAAI,CAAM;IACV,OAAO,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC;AAChG,IAAA,MAAM,CAAM;AACZ,IAAA,YAAY,CAAW;IACvB,gBAAgB,GAAW,IAAI,CAAC;AAChC,IAAA,MAAM,CAAK;AACX,IAAA,oBAAoB,CAAK;AACzB,IAAA,oBAAoB,CAAK;IACzB,WAAW,GAAW,IAAI,CAAC;AAEnC,IAAA,WAAA,CAAY,YAAsB,EAAE,OAAa,EAAE,gBAAsB,EAAA;AACvE,QAAA,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;AACjC,QAAA,IAAI,OAAO;AACT,YAAA,IAA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