@@ -23,7 +23,7 @@ const alertDiv = document.getElementById("alertDiv");
2323// This is a frontend example of Esptool-JS using local bundle file
2424// To optimize use a CDN hosted version like
2525// https://unpkg.com/esptool-js/bundle.js
26- import { ESPLoader , FlashOptions , LoaderOptions , WebSerialTransport , SerialOptions } from "../../../lib" ;
26+ import { ESPLoader , FlashOptions , LoaderOptions , WebSerialTransport , SerialOptions , ITrace } from "../../../lib" ;
2727
2828declare let Terminal ; // Terminal is imported in HTML script
2929declare let CryptoJS ; // CryptoJS is imported in HTML script
@@ -79,10 +79,35 @@ const espLoaderTerminal = {
7979 } ,
8080} ;
8181
82+ class TraceObject implements ITrace {
83+ traceBuffer : string ;
84+ private lastTraceTime = Date . now ( ) ;
85+
86+ trace ( message : string ) {
87+ const delta = Date . now ( ) - this . lastTraceTime ;
88+ const prefix = `TRACE ${ delta . toFixed ( 3 ) } ` ;
89+ const traceMessage = `${ prefix } ${ message } ` ;
90+ console . log ( traceMessage ) ;
91+ this . traceBuffer += traceMessage + "\n" ;
92+ }
93+
94+ async returnTrace ( ) {
95+ try {
96+ await navigator . clipboard . writeText ( this . traceBuffer ) ;
97+ console . log ( "Text copied to clipboard!" ) ;
98+ } catch ( err ) {
99+ console . error ( "Failed to copy text:" , err ) ;
100+ }
101+ return this . traceBuffer ;
102+ }
103+ }
104+
105+ const traceObj = new TraceObject ( ) ;
106+
82107connectButton . onclick = async ( ) => {
83108 if ( device === null ) {
84109 device = await navigator . serial . requestPort ( { } ) ;
85- transport = new WebSerialTransport ( device , true ) ;
110+ transport = new WebSerialTransport ( device , traceObj ) ;
86111 }
87112
88113 const serialOptions = { baudRate : parseInt ( baudrates . value ) } as SerialOptions ;
@@ -118,8 +143,8 @@ connectButton.onclick = async () => {
118143} ;
119144
120145traceButton . onclick = async ( ) => {
121- if ( transport ) {
122- transport . returnTrace ( ) ;
146+ if ( traceObj ) {
147+ traceObj . returnTrace ( ) ;
123148 }
124149} ;
125150
@@ -233,7 +258,7 @@ let isConsoleClosed = false;
233258consoleStartButton . onclick = async ( ) => {
234259 if ( device === null ) {
235260 device = await navigator . serial . requestPort ( { } ) ;
236- transport = new WebSerialTransport ( device , true ) ;
261+ transport = new WebSerialTransport ( device , traceObj ) ;
237262 }
238263 lblConsoleFor . style . display = "block" ;
239264 lblConsoleBaudrate . style . display = "none" ;
@@ -248,7 +273,7 @@ consoleStartButton.onclick = async () => {
248273 isConsoleClosed = false ;
249274
250275 while ( true && ! isConsoleClosed ) {
251- const val = await transport . rawRead ( ) ;
276+ const val = await transport . read ( ) ;
252277 if ( typeof val !== "undefined" ) {
253278 term . write ( val ) ;
254279 } else {
0 commit comments