21
21
function init ( ) {
22
22
}
23
23
24
+ function defaultOptions ( options ) {
25
+ options . serialDevice = options . serialDevice || "Serial2" ;
26
+ options . serialRx = options . serialRx || "A3" ;
27
+ options . serialTx = options . serialTx || "A2" ;
28
+ }
29
+
24
30
/* options = {
25
31
binary : ArrayBuffer,
26
32
cbStatus,
27
- cbDone */
33
+ cbDone,
34
+ serialDevice, // Serial2
35
+ serialRx, // A3
36
+ serialTx, // A2
37
+ chBoot, // A14
38
+ chPD, // A13
39
+ */
28
40
function flashDevice ( options ) {
29
41
if ( ! options . binary ) throw new Error ( "Needs binary" ) ;
42
+ defaultOptions ( options ) ;
30
43
31
- Espruino . Core . Serial . startListening ( function ( buffer ) {
44
+ var prevReader = Espruino . Core . Serial . startListening ( function ( buffer ) {
32
45
var bufView = new Uint8Array ( buffer ) ;
33
46
for ( var i = 0 ; i < bufView . length ; i ++ )
34
47
uartLine += String . fromCharCode ( bufView [ i ] ) ;
37
50
var a = l . shift ( ) ;
38
51
console . log ( ">>>" , a ) ;
39
52
try {
40
- if ( packetHandler ) packetHandler ( JSON . parse ( a ) ) ;
53
+ if ( packetHandler ) packetHandler ( Espruino . Core . Utils . parseJSONish ( a ) ) ;
41
54
} catch ( e ) { console . log ( "Unable to decode" ) ; }
42
55
}
43
56
uartLine = l [ 0 ] ;
49
62
// ignore keyPress from terminal during flashing
50
63
} ) ;
51
64
function finish ( ) {
65
+ Espruino . Core . Serial . startListening ( prevReader ) ;
52
66
Espruino . Core . Serial . setSlowWrite ( hadSlowWrite ) ;
53
67
Espruino . Core . Serial . setBinary ( false ) ;
54
68
Espruino . Core . Terminal . setInputDataHandler ( oldHandler ) ;
66
80
} ) . catch ( function ( error ) {
67
81
console . log ( "Error!" , error ) ;
68
82
finish ( ) ;
69
- if ( options . cbDone ) options . cbDone ( ) ;
83
+ if ( options . cbDone ) options . cbDone ( error ) ;
70
84
} ) ;
71
85
}
72
86
73
87
74
- function wr ( cmd , data ) {
88
+ function wr ( options , cmd , data ) {
75
89
//console.log("Write",cmd,data.length,data);
76
90
if ( typeof data !== "string" )
77
91
data = String . fromCharCode . apply ( null , data ) ;
@@ -83,12 +97,12 @@ function wr(cmd,data) {
83
97
pk = pk . replace ( / \xDB / g, "\xDB\xDD" ) . replace ( / \xC0 / g, "\xDB\xDC" ) ;
84
98
pk = "\xC0" + pk + "\xC0" ;
85
99
//console.log("OUT: ",pk.length,pk.split("").map(x=>x.charCodeAt().toString(16).padStart(2,'0')).join(" "));
86
- Espruino . Core . Serial . write ( `\x10n="${ Espruino . Core . Utils . btoa ( pk ) } ";Serial2 .write(atob(n))\n` , false ) ;
100
+ Espruino . Core . Serial . write ( `\x10n="${ Espruino . Core . Utils . btoa ( pk ) } ";${ options . serialDevice } .write(atob(n))\n` , false ) ;
87
101
}
88
102
89
- function sendCmd ( cmd , data ) {
103
+ function sendCmd ( options , cmd , data ) {
90
104
return new Promise ( ( resolve , reject ) => {
91
- wr ( cmd , data ) ;
105
+ wr ( options , cmd , data ) ;
92
106
packetHandler = function ( d ) {
93
107
//console.log(d);
94
108
packetHandler = undefined ;
@@ -104,9 +118,9 @@ function setupEspruino(options) {
104
118
Espruino . Core . Serial . write ( '\x03\x10reset()\n' , false , function ( ) {
105
119
setTimeout ( function ( ) {
106
120
console . log ( "Start Wifi, add handler" ) ;
107
- Espruino . Core . Serial . write ( `\x10Serial2. setup(74880 , { rx: A3 , tx : A2 });
108
- \x10var packetHandler, packetData="";
109
- \x10Serial2 .on('data', function(d) {
121
+ Espruino . Core . Serial . write ( `\x10 ${ options . serialDevice } . setup(115200 , { rx: ${ options . serialRx } , tx : ${ options . serialTx } });
122
+ \x10var packetHandler, packetData="", OUT=eval(process.env.CONSOLE) ;
123
+ \x10 ${ options . serialDevice } .on('data', function(d) {
110
124
packetData+=d;
111
125
if (packetData[0]!="\\xc0") {
112
126
var i = packetData.indexOf("\\xc0");
@@ -117,7 +131,7 @@ function setupEspruino(options) {
117
131
if (e<0) break;
118
132
var packet = packetData.substr(1,e-1);
119
133
var len = packet.charCodeAt(2)|(packet.charCodeAt(3)<<8);
120
- USB .println(JSON.stringify({
134
+ OUT .println(JSON.stringify({
121
135
dir : packet.charCodeAt(0),
122
136
cmd : packet.charCodeAt(1),
123
137
len: len,
@@ -126,9 +140,9 @@ function setupEspruino(options) {
126
140
packetData=packetData.substr(e+1);
127
141
}
128
142
});
129
- \x10digitalWrite(A14 , 0); // make sure WiFi starts off
130
- \x10digitalWrite(A13 , 0); // into of boot mode
131
- \x10digitalWrite(A14 , 1); // turn on wifi
143
+ \x10 ${ options . chPD ? `digitalWrite( ${ options . chPD } , 0);` : `` /* make sure WiFi starts off*/ }
144
+ \x10 ${ options . chBoot ? `digitalWrite( ${ options . chBoot } , 0);` : `` /* into of boot mode */ }
145
+ \x10 ${ options . chPD ? `digitalWrite( ${ options . chPD } , 1);` : `` /* turn on wifi */ }
132
146
` , false , function ( ) {
133
147
console . log ( "Handler added" ) ;
134
148
resolve ( ) ;
@@ -168,7 +182,7 @@ function cmdSync(options) {
168
182
} , 500 ) ;
169
183
}
170
184
}
171
- var tries = 5 ;
185
+ var tries = 20 ;
172
186
interval = setInterval ( function ( ) {
173
187
if ( tries -- <= 0 ) {
174
188
clearInterval ( interval ) ;
@@ -178,7 +192,7 @@ function cmdSync(options) {
178
192
var d = new Uint8Array ( 36 ) ;
179
193
d . fill ( 0x55 ) ;
180
194
d . set ( [ 0x07 , 0x07 , 0x12 , 0x20 ] ) ;
181
- wr ( 8 /* SYNC */ , d ) ;
195
+ wr ( options , 8 /* SYNC */ , d ) ;
182
196
} , 500 ) ;
183
197
} ) ;
184
198
}
@@ -196,7 +210,7 @@ function cmdFlash(options) {
196
210
0 // flash offset
197
211
] ) ;
198
212
var idx = 0 ;
199
- return sendCmd ( 2 /* FLASH_BEGIN */ , new Uint8Array ( d . buffer ) ) . then ( function flash ( ) {
213
+ return sendCmd ( options , 2 /* FLASH_BEGIN */ , new Uint8Array ( d . buffer ) ) . then ( function flash ( ) {
200
214
console . log ( "Block " + idx ) ;
201
215
if ( options . cbStatus ) options . cbStatus ( `Writing Block ${ idx } / ${ blockCount } ` , idx / blockCount ) ;
202
216
if ( idx >= blockCount ) return true ;
@@ -209,27 +223,35 @@ function cmdFlash(options) {
209
223
] ) ;
210
224
d . set ( new Uint8Array ( binary . buffer , BLOCK_SIZE * idx , BLOCK_SIZE ) , 16 ) ;
211
225
idx ++ ;
212
- return sendCmd ( 3 /* FLASH_DATA */ , d ) . then ( flash ) ;
226
+ return sendCmd ( options , 3 /* FLASH_DATA */ , d ) . then ( flash ) ;
213
227
} ) ;
214
228
}
215
229
216
- function getFirmwareVersion ( callback ) {
230
+ function getFirmwareVersion ( options , callback ) {
231
+ defaultOptions ( options ) ;
217
232
Espruino . Core . Serial . write ( '\x03\x10reset()\n' , false , function ( ) {
218
233
setTimeout ( function ( ) {
219
- Espruino . Core . Serial . write ( `\x10digitalWrite(A14, 0);/*WiFi off*/digitalWrite(A13, 1);/*no boot*/digitalWrite(A14, 1);/*WiFi On*/\n` , false , function ( ) {
234
+ var cmd = "\x10\n" ;
235
+ if ( options . chPD && options . chBoot )
236
+ cmd = `\x10digitalWrite(${ options . chPD } , 0);/*WiFi off*/digitalWrite(${ options . chBoot } , 1);/*no boot*/digitalWrite(${ options . chPD } , 1);/*WiFi On*/\n` ;
237
+ else if ( options . chPD )
238
+ cmd = `\x10digitalWrite(${ options . chPD } , 1);/*WiFi On*/\n` ;
239
+
240
+ Espruino . Core . Serial . write ( cmd , false , function ( ) {
220
241
setTimeout ( function ( ) {
221
242
var result = "" ;
222
- Espruino . Core . Serial . startListening ( function ( buffer ) {
243
+ var prevReader = Espruino . Core . Serial . startListening ( function ( buffer ) {
223
244
var bufView = new Uint8Array ( buffer ) ;
224
245
for ( var i = 0 ; i < bufView . length ; i ++ )
225
246
result += String . fromCharCode ( bufView [ i ] ) ;
226
247
} ) ;
227
- Espruino . Core . Serial . write ( `\x10Serial2. pipe(USB);Serial2. setup(115200, { rx: A3 , tx : A2 });Serial2 .print("AT+GMR\\r\\n");\n` , false , function ( ) {
248
+ Espruino . Core . Serial . write ( `\x10 ${ options . serialDevice } . pipe(eval(process.env.CONSOLE)); ${ options . serialDevice } . setup(115200, { rx: ${ options . serialRx } , tx : ${ options . serialTx } });${ options . serialDevice } .print("AT+GMR\\r\\n");\n` , false , function ( ) {
228
249
setTimeout ( function ( ) {
229
250
Espruino . Core . Serial . write ( '\x03\x10reset()\n' , false , function ( ) {
251
+ Espruino . Core . Serial . startListening ( prevReader ) ;
230
252
callback ( result . trim ( ) ) ;
231
253
} ) ;
232
- } , 500 ) ;
254
+ } , 1500 ) ;
233
255
} ) ;
234
256
} , 500 ) ;
235
257
} ) ;
0 commit comments