@@ -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. 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) .
24472447 */
24482448MspHelper . prototype . dataflashRead = function ( address , blockSize , onDataCallback ) {
24492449 let outData = [
@@ -2465,63 +2465,49 @@ 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 ;
24692468
24702469 if ( response && response . data ) {
24712470 const headerSize = 7 ;
24722471 const chunkAddress = response . data . readU32 ( ) ;
24732472 const dataSize = response . data . readU16 ( ) ;
24742473 const dataCompressionType = response . data . readU8 ( ) ;
24752474
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' ) ;
24832496 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 ;
25012497 }
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 } ` ) ;
25072500 }
25082501 }
25092502
2510- // Deliver payloadView even if CRC failed
2503+ // Deliver payloadView if defined, otherwise pass null
25112504 onDataCallback ( address , payloadView ) ;
25122505
2513- // Logging
25142506 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 ` ) ;
25162508 } else if ( payloadView ) {
25172509 console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
25182510 }
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- }
25252511 } , true ) ; // end of send_message
25262512} ; // end of dataflashRead
25272513
0 commit comments