@@ -2442,8 +2442,8 @@ MspHelper.prototype.setRawRx = function (channels) {
2442
2442
} ;
2443
2443
2444
2444
/**
2445
- * Send a request to read a block of data from the dataflash at the given address and pass that address and a DataView
2446
- * of the returned data to the given callback. If an error or CRC fail occurs, the raw data is delivered instead of null .
2445
+ * Send a request to read a block of data from the dataflash at the given address and pass that address and a dataview
2446
+ * of the returned data to the given callback ( or null for the data if an error occurred) .
2447
2447
*/
2448
2448
MspHelper . prototype . dataflashRead = function ( address , blockSize , onDataCallback ) {
2449
2449
let outData = [
@@ -2465,63 +2465,49 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
2465
2465
2466
2466
mspObj . send_message ( MSPCodes . MSP_DATAFLASH_READ , outData , false , function ( response ) {
2467
2467
let payloadView = null ;
2468
- let bytesCompressed = 0 ;
2469
2468
2470
2469
if ( response && response . data ) {
2471
2470
const headerSize = 7 ;
2472
2471
const chunkAddress = response . data . readU32 ( ) ;
2473
2472
const dataSize = response . data . readU16 ( ) ;
2474
2473
const dataCompressionType = response . data . readU8 ( ) ;
2475
2474
2476
- if ( chunkAddress !== address ) {
2477
- console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2478
- }
2479
-
2480
- try {
2481
- if ( dataCompressionType === 0 ) {
2482
- // Raw, uncompressed
2475
+ if ( chunkAddress === address ) {
2476
+ try {
2477
+ if ( dataCompressionType === 0 ) {
2478
+ payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2479
+ } else if ( dataCompressionType === 1 ) {
2480
+ const compressedCharCount = response . data . readU16 ( ) ;
2481
+ const compressedArray = new Uint8Array (
2482
+ response . data . buffer ,
2483
+ response . data . byteOffset + headerSize + 2 ,
2484
+ dataSize - 2
2485
+ ) ;
2486
+ const decompressedArray = huffmanDecodeBuf (
2487
+ compressedArray ,
2488
+ compressedCharCount ,
2489
+ defaultHuffmanTree ,
2490
+ defaultHuffmanLenIndex
2491
+ ) ;
2492
+ payloadView = new DataView ( decompressedArray . buffer ) ;
2493
+ }
2494
+ } catch ( e ) {
2495
+ console . warn ( 'Decompression or read failed, delivering raw data anyway' ) ;
2483
2496
payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2484
- bytesCompressed = dataSize ;
2485
- } else if ( dataCompressionType === 1 ) {
2486
- // Compressed
2487
- const compressedCharCount = response . data . readU16 ( ) ;
2488
- const compressedArray = new Uint8Array (
2489
- response . data . buffer ,
2490
- response . data . byteOffset + headerSize + 2 ,
2491
- dataSize - 2
2492
- ) ;
2493
- const decompressedArray = huffmanDecodeBuf (
2494
- compressedArray ,
2495
- compressedCharCount ,
2496
- defaultHuffmanTree ,
2497
- defaultHuffmanLenIndex
2498
- ) ;
2499
- payloadView = new DataView ( decompressedArray . buffer ) ;
2500
- bytesCompressed = compressedArray . length ;
2501
2497
}
2502
- } catch ( e ) {
2503
- console . warn ( 'Decompression failed, delivering raw data anyway:' , e ) ;
2504
- // fallback to raw block
2505
- payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2506
- bytesCompressed = dataSize ;
2498
+ } else {
2499
+ console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2507
2500
}
2508
2501
}
2509
2502
2510
- // Deliver payloadView even if CRC failed
2503
+ // Deliver payloadView if defined, otherwise pass null
2511
2504
onDataCallback ( address , payloadView ) ;
2512
2505
2513
- // Logging
2514
2506
if ( ! response || response . crcError ) {
2515
- console . log ( `CRC error or missing data at address ${ address } - delivering raw data ( ${ payloadView ? payloadView . byteLength : 0 } bytes) ` ) ;
2507
+ console . log ( `CRC error or missing data at address ${ address } - delivering whatever we got ` ) ;
2516
2508
} else if ( payloadView ) {
2517
2509
console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
2518
2510
}
2519
-
2520
- // Track total compressed bytes globally, if needed
2521
- if ( typeof bytesCompressed === "number" ) {
2522
- if ( typeof totalBytesCompressed === "undefined" || totalBytesCompressed == null ) totalBytesCompressed = 0 ;
2523
- totalBytesCompressed += bytesCompressed ;
2524
- }
2525
2511
} , true ) ; // end of send_message
2526
2512
} ; // end of dataflashRead
2527
2513
0 commit comments