@@ -3,6 +3,17 @@ const fs = require('fs').promises;
33const net = require ( 'net' ) ;
44const cli = require ( '../utils/arduino-exec' ) ;
55const tmpFiles = require ( '../utils/files' ) ;
6+ const json = require ( '../utils/json' ) ;
7+
8+ const exists = async ( path ) => {
9+ try {
10+ await fs . access ( path ) ;
11+ return true ;
12+ } catch ( err ) {
13+ Math . random ( err ) ;
14+ return false ;
15+ }
16+ } ;
617
718const program = {
819
@@ -35,11 +46,58 @@ const program = {
3546 const response = program . getError ( res ) ;
3647 if ( ! response . error && ! noHex ) {
3748 const ref = socket . sketchPath . split ( '/' ) . pop ( ) ;
38- try {
39- const hex = await fs . readFile ( `${ socket . sketchPath } /output/${ ref } .ino.hex` , 'base64' ) ;
40- response . hex = hex ;
41- } catch ( err ) {
42- console . error ( err ) ;
49+ if ( fqbn . indexOf ( 'esp' ) !== 0 ) {
50+ try {
51+ const hex = await fs . readFile ( `${ socket . sketchPath } /output/${ ref } .ino.hex` , 'base64' ) ;
52+ response . hex = hex ;
53+ } catch ( err ) {
54+ console . error ( err ) ;
55+ }
56+ } else if ( fqbn . indexOf ( 'esp8266' ) === 0 ) {
57+ try {
58+ const bin = await fs . readFile ( `${ socket . sketchPath } /output/${ ref } .ino.bin` , 'base64' ) ;
59+ response . files = [ { data : bin , address : 0 } ] ;
60+ } catch ( err ) {
61+ console . error ( err ) ;
62+ }
63+ } else if ( fqbn . indexOf ( 'esp32' ) === 0 ) {
64+ const boards = await json . loadLargeData ( 'boards' ) ;
65+ const baseFqbn = fqbn . split ( ':' ) . slice ( 0 , 4 ) . join ( ':' ) ;
66+ const board = boards . find ( ( b ) => b . fqbn === baseFqbn ) ;
67+ if ( board ) {
68+ const flashFreq = `${
69+ ( fqbn . split ( ':' ) . find ( ( part ) => part . includes ( 'FlashFreq=' ) ) || '' ) . replace ( 'FlashFreq=' , '' )
70+ || ( board . config_options . find ( ( o ) => o . option === 'FlashFreq' )
71+ || { values : [ { selected : true , value : '' } ] } ) . values . find ( ( v ) => v . selected ) . value
72+ || board . properties . build . flash_freq
73+ } m`. replace ( 'mm' , 'm' ) ;
74+ const flashMode = ( fqbn . split ( ':' ) . find ( ( part ) => part . includes ( 'FlashMode=' ) ) || '' ) . replace ( 'FlashMode=' , '' )
75+ || ( board . config_options . find ( ( o ) => o . option === 'FlashMode' )
76+ || { values : [ { selected : true , value : '' } ] } ) . values . find ( ( v ) => v . selected ) . value
77+ || board . properties . build . boot ;
78+ const espToolsPath = '/mnt/duino-data/.arduino15/packages/esp32/hardware/esp32/1.0.6/tools' ;
79+ let bootPath = `${ espToolsPath } /sdk/bin/bootloader_${ flashMode } _${ flashFreq } .bin` ;
80+ // account for the new path for the bin in the next arduino-esp32 release. (2021-07-18)
81+ if ( ! ( await exists ( bootPath ) ) ) {
82+ bootPath = `${ espToolsPath } /sdk/${ board . properties . build . mcu } /bin/bootloader_${ flashMode } _${ flashFreq } .bin` ;
83+ }
84+ try {
85+ const appBin = await fs . readFile ( `${ socket . sketchPath } /output/${ ref } .ino.bin` , 'base64' ) ;
86+ const partBin = await fs . readFile ( `${ socket . sketchPath } /output/${ ref } .ino.partitions.bin` , 'base64' ) ;
87+ const app0Bin = await fs . readFile ( `${ espToolsPath } /partitions/boot_app0.bin` , 'base64' ) ;
88+ const bootBin = await fs . readFile ( bootPath , 'base64' ) ;
89+ response . files = [
90+ { data : app0Bin , address : 0xe000 } ,
91+ { data : bootBin , address : Number ( board . properties . build . bootloader_addr ) || 0x1000 } ,
92+ { data : appBin , address : 0x10000 } ,
93+ { data : partBin , address : 0x8000 } ,
94+ ] ;
95+ response . flash_freq = flashFreq ;
96+ response . flash_mode = flashMode ;
97+ } catch ( err ) {
98+ console . error ( err ) ;
99+ }
100+ }
43101 }
44102 }
45103 response . log = libRes + res ;
0 commit comments