@@ -49,7 +49,7 @@ const Comms = {
49
49
/// Evaluate the given expression, return the result as a promise
50
50
eval : ( expr ) => {
51
51
if ( expr === undefined ) throw new Error ( "Comms.eval(undefined) called!" )
52
- if ( typeof UART = == "undefined" ) { // New method
52
+ if ( typeof UART ! == "undefined" ) { // New method
53
53
return UART . eval ( expr ) ;
54
54
} else { // Old method
55
55
return new Promise ( ( resolve , reject ) =>
@@ -76,29 +76,40 @@ const Comms = {
76
76
return Puck . getConnection ( ) ;
77
77
}
78
78
} ,
79
+ supportsPacketUpload : ( ) => Comms . getConnection ( ) . espruinoSendFile && ! Utils . versionLess ( device . version , "2v25" ) ,
79
80
// Faking EventEmitter
80
81
handlers : { } ,
81
82
on : function ( id , callback ) { // calling with callback=undefined will disable
82
83
if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
83
84
var connection = Comms . getConnection ( ) ;
84
85
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
+ }
102
113
}
103
114
} ,
104
115
// ================================================================================
@@ -265,16 +276,15 @@ const Comms = {
265
276
}
266
277
let f = fileContents . shift ( ) ;
267
278
// 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 ( ) ;
269
280
270
281
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
282
if ( uploadPacket ) {
272
283
Comms . getConnection ( ) . espruinoSendFile ( f . name , f . content , {
273
284
fs : Const . FILES_IN_FS ,
274
285
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 ) ;
278
288
} else {
279
289
Comms . uploadCommandList ( f . cmd , currentBytes , maxBytes ) . then ( doUploadFiles ) ;
280
290
}
@@ -621,11 +631,18 @@ ${Const.CONNECTION_DEVICE}.print("\\xFF");
621
631
} ,
622
632
// Read a non-storagefile file
623
633
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)` ) ;
626
635
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
+ }
630
647
} ,
631
648
} ;
0 commit comments