@@ -2442,8 +2442,8 @@ MspHelper.prototype.setRawRx = function (channels) {
24422442} ;
24432443
24442444/**
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) .
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 .
24472447 */
24482448MspHelper . prototype . dataflashRead = function ( address , blockSize , onDataCallback ) {
24492449 let outData = [
@@ -2465,49 +2465,63 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
24652465
24662466 mspObj . send_message ( MSPCodes . MSP_DATAFLASH_READ , outData , false , function ( response ) {
24672467 let payloadView = null ;
2468+ let bytesCompressed = 0 ;
24682469
24692470 if ( response && response . data ) {
24702471 const headerSize = 7 ;
24712472 const chunkAddress = response . data . readU32 ( ) ;
24722473 const dataSize = response . data . readU16 ( ) ;
24732474 const dataCompressionType = response . data . readU8 ( ) ;
24742475
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' ) ;
2476+ if ( chunkAddress !== address ) {
2477+ console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2478+ }
2479+
2480+ try {
2481+ if ( dataCompressionType === 0 ) {
2482+ // Raw, uncompressed
24962483 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 ;
24972501 }
2498- } else {
2499- console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
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 ;
25002507 }
25012508 }
25022509
2503- // Deliver payloadView if defined, otherwise pass null
2510+ // Deliver payloadView even if CRC failed
25042511 onDataCallback ( address , payloadView ) ;
25052512
2513+ // Logging
25062514 if ( ! response || response . crcError ) {
2507- console . log ( `CRC error or missing data at address ${ address } - delivering whatever we got ` ) ;
2515+ console . log ( `CRC error or missing data at address ${ address } - delivering raw data ( ${ payloadView ? payloadView . byteLength : 0 } bytes) ` ) ;
25082516 } else if ( payloadView ) {
25092517 console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
25102518 }
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+ }
25112525 } , true ) ; // end of send_message
25122526} ; // end of dataflashRead
25132527
0 commit comments