@@ -52,9 +52,25 @@ As of Espruino 2v25 you can also send data packets:
5252UART.getConnection().espruinoSendFile("test.txt","This is a test of sending data to Espruino").then(_=>console.log("Done"))
5353UART.getConnection().espruinoSendFile("test.txt","This is a test of sending data to Espruino's SD card",{fs:true}).then(_=>console.log("Done"))
5454
55+ UART.debug=3;
56+ var s = ""; for (var i=0;i<256;i++) s+=String.fromCharCode(i);
57+ var ss = ""; for (var i=0;i<256;i++) ss+=s;
58+ UART.getConnection().espruinoSendFile("testbin",ss,{fs:true})
59+
60+
61+ UART.debug=3;
62+ var s = ""; for (var i=0;i<256;i++) s+=String.fromCharCode(i);
63+ UART.getConnection().espruinoSendFile("testbin",s,{fs:true})
64+
65+ UART.debug=3;
66+ var s = ""; for (var i=0;i<256;i++) s+=String.fromCharCode(i)
67+ UART.getConnection().espruinoSendFile("testbin",s)
68+
69+
5570ChangeLog:
5671
5772...
73+ 1.04: For packet uploads, add ability to ste chunk size, report progress or even skip searching for acks
58741.03: Added options for restricting what devices appear
5975 Improve Web Serial Disconnection - didn't work before
60761.02: Added better on/emit/removeListener handling
7288* move 'connection.received' handling into removeListener and add an upper limit (100k?)
7389* add a 'line' event for each line of data that's received
7490* add espruinoEval method using the new packet system
91+ * move XON/XOFF handling into Connection.rxDataHandler
7592
7693*/
7794( function ( root , factory ) {
@@ -157,8 +174,13 @@ To do:
157174 connection . emit ( 'data' , data ) ;
158175 }
159176
160- /* Send a packet of type "RESPONSE/EVAL/EVENT/FILE_SEND/DATA" to Espruino */
161- espruinoSendPacket ( pkType , data ) {
177+ /* Send a packet of type "RESPONSE/EVAL/EVENT/FILE_SEND/DATA" to Espruino
178+ options = {
179+ noACK : bool (don't wait to acknowledgement)
180+ }
181+ */
182+ espruinoSendPacket ( pkType , data , options ) {
183+ options = options || { } ;
162184 if ( "string" != typeof data ) throw new Error ( "'data' must be a String" ) ;
163185 if ( data . length > 0x1FFF ) throw new Error ( "'data' too long" ) ;
164186 const PKTYPES = {
@@ -183,27 +205,59 @@ To do:
183205 tidy ( ) ;
184206 reject ( ) ;
185207 }
186- connection . parsePackets = true ;
187- connection . on ( "ack" , onACK ) ;
188- connection . on ( "nak" , onNAK ) ;
208+ if ( ! options . noACK ) {
209+ connection . parsePackets = true ;
210+ connection . on ( "ack" , onACK ) ;
211+ connection . on ( "nak" , onNAK ) ;
212+ }
189213 let flags = data . length | PKTYPES [ pkType ] ;
190- write ( String . fromCharCode ( /*DLE*/ 16 , /*SOH*/ 1 , ( flags >> 8 ) & 0xFF , flags & 0xFF ) + data ) ;
214+ connection . write ( String . fromCharCode ( /*DLE*/ 16 , /*SOH*/ 1 , ( flags >> 8 ) & 0xFF , flags & 0xFF ) + data , function ( ) {
215+ // write complete
216+ if ( options . noACK ) {
217+ resolve ( ) ; // if not listening for acks, just resolve immediately
218+ }
219+ } ) ;
191220 } ) ;
192221 }
193- /* Send a file to Espruino using 2v25 packets */
222+ /* Send a file to Espruino using 2v25 packets.
223+ options = { // mainly passed to Espruino
224+ fs : true // optional -> write using require("fs") (to SD card)
225+ noACK : bool // (don't wait to acknowledgements)
226+ chunkSize : int // size of chunks to send (default 1024) for safety this depends on how big your device's input buffer is if there isn't flow control
227+ progress : (chunkNo,chunkCount)=>{} // callback to report upload progress
228+ } */
194229 espruinoSendFile ( filename , data , options ) {
195230 if ( "string" != typeof data ) throw new Error ( "'data' must be a String" ) ;
231+ let CHUNK = 1024 ;
196232 options = options || { } ;
197233 options . fn = filename ;
198234 options . s = data . length ;
235+ let packetOptions = { } ;
236+ let progressHandler = ( chunkNo , chunkCount ) => { } ;
237+ if ( options . noACK ) {
238+ delete options . noACK ;
239+ packetOptions . noACK = true ;
240+ }
241+ if ( options . chunkSize ) {
242+ CHUNK = options . chunkSize ;
243+ delete options . chunkSize ;
244+ }
245+ if ( options . progress ) {
246+ progressHandler = options . progress ;
247+ delete options . progress ;
248+ }
199249 let connection = this ;
250+ let packetCount = 0 , packetTotal = Math . ceil ( data . length / CHUNK ) + 1 ;
251+ // always ack the FILE_SEND
252+ progressHandler ( 0 , packetTotal ) ;
200253 return connection . espruinoSendPacket ( "FILE_SEND" , JSON . stringify ( options ) ) . then ( sendData ) ;
254+ // but if noACK don't ack for data
201255 function sendData ( ) {
256+ progressHandler ( ++ packetCount , packetTotal ) ;
202257 if ( data . length == 0 ) return Promise . resolve ( ) ;
203- const CHUNK = 512 ;
204258 let packet = data . substring ( 0 , CHUNK ) ;
205259 data = data . substring ( CHUNK ) ;
206- return connection . espruinoSendPacket ( "DATA" , packet ) . then ( sendData ) ;
260+ return connection . espruinoSendPacket ( "DATA" , packet , packetOptions ) . then ( sendData ) ;
207261 }
208262 }
209263 } ;
@@ -669,7 +723,7 @@ To do:
669723 // ----------------------------------------------------------
670724
671725 var uart = {
672- version : "1.03 " ,
726+ version : "1.04 " ,
673727 /// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
674728 debug : 1 ,
675729 /// Should we use flow control? Default is true
0 commit comments