@@ -3,26 +3,6 @@ console.log("================================================")
3
3
console . log ( "Type 'Comms.debug()' to enable Comms debug info" )
4
4
console . log ( "================================================" )
5
5
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
-
26
6
/// Add progress handler so we get nice upload progress shown
27
7
{
28
8
let COMMS = ( typeof UART != "undefined" ) ?UART :Puck ;
@@ -100,26 +80,22 @@ const Comms = {
100
80
handlers : { } ,
101
81
on : function ( id , callback ) { // calling with callback=undefined will disable
102
82
if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
103
- var connection = Puck . getConnection ( ) ;
83
+ var connection = Comms . getConnection ( ) ;
104
84
if ( ! connection ) throw new Error ( "No active connection" ) ;
105
85
/* This is a bit of a mess - the Puck.js lib only supports one callback with `.on`. If you
106
86
do Puck.getConnection().on('data') then it blows away the default one which is used for
107
87
.write/.eval and you can't get it back unless you reconnect. So rather than trying to fix the
108
88
Puck lib we just copy in the default handler here. */
109
89
if ( callback === undefined ) {
110
90
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 ;
115
93
if ( connection . cb ) connection . cb ( d ) ;
116
94
} ) ;
117
95
} else {
118
96
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 ;
123
99
if ( connection . cb ) connection . cb ( d ) ;
124
100
callback ( d ) ;
125
101
} ) ;
@@ -277,7 +253,7 @@ const Comms = {
277
253
278
254
// Upload each file one at a time
279
255
function doUploadFiles ( ) {
280
- // No files left - print 'reboot' message
256
+ // No files left - print 'reboot' message
281
257
if ( fileContents . length == 0 ) {
282
258
( options . noFinish ? Promise . resolve ( ) : Comms . showUploadFinished ( ) ) . then ( ( ) => {
283
259
Progress . hide ( { sticky :true } ) ;
@@ -288,10 +264,21 @@ const Comms = {
288
264
return ;
289
265
}
290
266
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
+ }
293
279
currentBytes += f . cmd . length ;
294
280
}
281
+
295
282
// Start the upload
296
283
function doUpload ( ) {
297
284
Comms . showMessage ( `Uploading\n${ app . id } ...` ) .
0 commit comments