@@ -49,7 +49,7 @@ const Comms = {
4949 /// Evaluate the given expression, return the result as a promise
5050 eval : ( expr ) => {
5151 if ( expr === undefined ) throw new Error ( "Comms.eval(undefined) called!" )
52- if ( typeof UART = == "undefined" ) { // New method
52+ if ( typeof UART ! == "undefined" ) { // New method
5353 return UART . eval ( expr ) ;
5454 } else { // Old method
5555 return new Promise ( ( resolve , reject ) =>
@@ -76,29 +76,40 @@ const Comms = {
7676 return Puck . getConnection ( ) ;
7777 }
7878 } ,
79+ supportsPacketUpload : ( ) => Comms . getConnection ( ) . espruinoSendFile && ! Utils . versionLess ( device . version , "2v25" ) ,
7980 // Faking EventEmitter
8081 handlers : { } ,
8182 on : function ( id , callback ) { // calling with callback=undefined will disable
8283 if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
8384 var connection = Comms . getConnection ( ) ;
8485 if ( ! connection ) throw new Error ( "No active connection" ) ;
85- /* This is a bit of a mess - the Puck.js lib only supports one callback with `.on`. If you
86- do Puck.getConnection().on('data') then it blows away the default one which is used for
87- .write/.eval and you can't get it back unless you reconnect. So rather than trying to fix the
88- Puck lib we just copy in the default handler here. */
89- if ( callback === undefined ) {
90- connection . on ( "data" , function ( d ) { // the default handler
91- connection . received += d ;
92- connection . hadData = true ;
93- if ( connection . cb ) connection . cb ( d ) ;
94- } ) ;
95- } else {
96- connection . on ( "data" , function ( d ) {
97- connection . received += d ;
98- connection . hadData = true ;
99- if ( connection . cb ) connection . cb ( d ) ;
100- callback ( d ) ;
101- } ) ;
86+ if ( "undefined" !== typeof Puck ) {
87+ /* This is a bit of a mess - the Puck.js lib only supports one callback with `.on`. If you
88+ do Puck.getConnection().on('data') then it blows away the default one which is used for
89+ .write/.eval and you can't get it back unless you reconnect. So rather than trying to fix the
90+ Puck lib we just copy in the default handler here. */
91+ if ( callback === undefined ) {
92+ connection . on ( "data" , function ( d ) { // the default handler
93+ connection . received += d ;
94+ connection . hadData = true ;
95+ if ( connection . cb ) connection . cb ( d ) ;
96+ } ) ;
97+ } else {
98+ connection . on ( "data" , function ( d ) {
99+ connection . received += d ;
100+ connection . hadData = true ;
101+ if ( connection . cb ) connection . cb ( d ) ;
102+ callback ( d ) ;
103+ } ) ;
104+ }
105+ } else { // UART
106+ if ( callback === undefined ) {
107+ if ( Comms . dataCallback ) connection . removeListener ( "data" , Comms . dataCallback ) ;
108+ delete Comms . dataCallback ;
109+ } else {
110+ Comms . dataCallback = callback ;
111+ connection . on ( "data" , Comms . dataCallback ) ;
112+ }
102113 }
103114 } ,
104115// ================================================================================
@@ -265,16 +276,15 @@ const Comms = {
265276 }
266277 let f = fileContents . shift ( ) ;
267278 // 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" ) ;
279+ let uploadPacket = ( ! ! f . canUploadPacket ) && Comms . supportsPacketUpload ( ) ;
269280
270281 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" :"" } )` ) ;
271282 if ( uploadPacket ) {
272283 Comms . getConnection ( ) . espruinoSendFile ( f . name , f . content , {
273284 fs : Const . FILES_IN_FS ,
274285 chunkSize : Const . PACKET_UPLOAD_CHUNKSIZE ,
275- noACK : Const . PACKET_UPLOAD_NOACK ,
276- progress : ( chunkNo , chunkCount ) => { Progress . show ( { percent : chunkNo * 100 / chunkCount } ) ; }
277- } ) . then ( doUploadFiles ) ; // progress?
286+ noACK : Const . PACKET_UPLOAD_NOACK
287+ } ) . then ( doUploadFiles ) ;
278288 } else {
279289 Comms . uploadCommandList ( f . cmd , currentBytes , maxBytes ) . then ( doUploadFiles ) ;
280290 }
@@ -621,11 +631,18 @@ ${Const.CONNECTION_DEVICE}.print("\\xFF");
621631 } ,
622632 // Read a non-storagefile file
623633 writeFile : ( filename , data ) => {
624- console . log ( `<COMMS> writeFile ${ JSON . stringify ( filename ) } ` ) ;
625- var cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
634+ console . log ( `<COMMS> writeFile ${ JSON . stringify ( filename ) } (${ data . length } b)` ) ;
626635 Progress . show ( { title :`Writing ${ JSON . stringify ( filename ) } ` , percent :0 } ) ;
627- return Comms . write ( "\x10" + Comms . getProgressCmd ( ) + "\n" ) . then ( ( ) =>
628- Comms . uploadCommandList ( cmds , 0 , cmds . length )
629- ) ;
636+ if ( Comms . supportsPacketUpload ( ) ) {
637+ return Comms . getConnection ( ) . espruinoSendFile ( filename , data , {
638+ chunkSize : Const . PACKET_UPLOAD_CHUNKSIZE ,
639+ noACK : Const . PACKET_UPLOAD_NOACK
640+ } ) ;
641+ } else {
642+ var cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
643+ return Comms . write ( "\x10" + Comms . getProgressCmd ( ) + "\n" ) . then ( ( ) =>
644+ Comms . uploadCommandList ( cmds , 0 , cmds . length )
645+ ) ;
646+ }
630647 } ,
631648} ;
0 commit comments