@@ -3,26 +3,6 @@ console.log("================================================")
33console . log ( "Type 'Comms.debug()' to enable Comms debug info" )
44console . log ( "================================================" )
55
6- /*
7-
8- Puck = {
9- /// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
10- debug : UART.debug,
11- /// Used internally to write log information - you can replace this with your own function
12- log : function(level, s) { if (level <= this.debug) console.log("<UART> "+s)},
13- /// Called with the current send progress or undefined when done - you can replace this with your own function
14- writeProgress : function() {},
15- connect : UART.connect,
16- write : UART.write,
17- eval : UART.eval,
18- isConnected : () => UART.isConnected() && UART.getConnection().isOpen,
19- getConnection : UART.getConnection,
20- close : UART.close,
21- RECEIVED_NOT_IN_DATA_HANDLER : true, // hack for Comms.on
22- };
23- // FIXME: disconnected event?
24- */
25-
266/// Add progress handler so we get nice upload progress shown
277{
288 let COMMS = ( typeof UART != "undefined" ) ?UART :Puck ;
@@ -100,26 +80,22 @@ const Comms = {
10080 handlers : { } ,
10181 on : function ( id , callback ) { // calling with callback=undefined will disable
10282 if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
103- var connection = Puck . getConnection ( ) ;
83+ var connection = Comms . getConnection ( ) ;
10484 if ( ! connection ) throw new Error ( "No active connection" ) ;
10585 /* This is a bit of a mess - the Puck.js lib only supports one callback with `.on`. If you
10686 do Puck.getConnection().on('data') then it blows away the default one which is used for
10787 .write/.eval and you can't get it back unless you reconnect. So rather than trying to fix the
10888 Puck lib we just copy in the default handler here. */
10989 if ( callback === undefined ) {
11090 connection . on ( "data" , function ( d ) { // the default handler
111- if ( ! Puck . RECEIVED_NOT_IN_DATA_HANDLER ) {
112- connection . received += d ;
113- connection . hadData = true ;
114- }
91+ connection . received += d ;
92+ connection . hadData = true ;
11593 if ( connection . cb ) connection . cb ( d ) ;
11694 } ) ;
11795 } else {
11896 connection . on ( "data" , function ( d ) {
119- if ( ! Puck . RECEIVED_NOT_IN_DATA_HANDLER ) {
120- connection . received += d ;
121- connection . hadData = true ;
122- }
97+ connection . received += d ;
98+ connection . hadData = true ;
12399 if ( connection . cb ) connection . cb ( d ) ;
124100 callback ( d ) ;
125101 } ) ;
@@ -277,7 +253,7 @@ const Comms = {
277253
278254 // Upload each file one at a time
279255 function doUploadFiles ( ) {
280- // No files left - print 'reboot' message
256+ // No files left - print 'reboot' message
281257 if ( fileContents . length == 0 ) {
282258 ( options . noFinish ? Promise . resolve ( ) : Comms . showUploadFinished ( ) ) . then ( ( ) => {
283259 Progress . hide ( { sticky :true } ) ;
@@ -288,10 +264,21 @@ const Comms = {
288264 return ;
289265 }
290266 let f = fileContents . shift ( ) ;
291- console . log ( `<COMMS> Upload ${ f . name } => ${ JSON . stringify ( f . content ) } ` ) ;
292- Comms . uploadCommandList ( f . cmd , currentBytes , maxBytes ) . then ( ( ) => doUploadFiles ( ) ) ;
267+ // Only upload as a packet if it makes sense for the file, connection supports it, as does device firmware
268+ let uploadPacket = ( ! ! f . canUploadPacket ) && Comms . getConnection ( ) . espruinoSendFile && ! Utils . versionLess ( device . version , "2v25" ) ;
269+
270+ console . log ( `<COMMS> Upload ${ f . name } => ${ JSON . stringify ( f . content . length > 50 ? f . content . substr ( 0 , 50 ) + "..." : f . content ) } (${ f . content . length } b${ uploadPacket ?", binary" :"" } )` ) ;
271+ if ( uploadPacket ) {
272+ Comms . getConnection ( ) . espruinoSendFile ( f . name , f . content , {
273+ fs :Const . FILES_IN_FS ,
274+ progress :( chunkNo , chunkCount ) => { Progress . show ( { percent : chunkNo * 100 / chunkCount } ) ; }
275+ } ) . then ( doUploadFiles ) ; // progress?
276+ } else {
277+ Comms . uploadCommandList ( f . cmd , currentBytes , maxBytes ) . then ( doUploadFiles ) ;
278+ }
293279 currentBytes += f . cmd . length ;
294280 }
281+
295282 // Start the upload
296283 function doUpload ( ) {
297284 Comms . showMessage ( `Uploading\n${ app . id } ...` ) .
0 commit comments