@@ -2443,7 +2443,7 @@ MspHelper.prototype.setRawRx = function (channels) {
2443
2443
2444
2444
/**
2445
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 occured ).
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 = [
@@ -2472,113 +2472,43 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
2472
2472
const dataSize = response . data . readU16 ( ) ;
2473
2473
const dataCompressionType = response . data . readU8 ( ) ;
2474
2474
2475
- try {
2476
- if ( dataCompressionType === 0 ) {
2477
- payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2478
- } else if ( dataCompressionType === 1 ) {
2479
- const compressedCharCount = response . data . readU16 ( ) ;
2480
- const compressedArray = new Uint8Array ( response . data . buffer , response . data . byteOffset + headerSize + 2 , dataSize - 2 ) ;
2481
- const decompressedArray = huffmanDecodeBuf ( compressedArray , compressedCharCount , defaultHuffmanTree , defaultHuffmanLenIndex ) ;
2482
- payloadView = new DataView ( decompressedArray . buffer ) ;
2475
+ if ( chunkAddress === address ) {
2476
+ try {
2477
+ if ( dataCompressionType === 0 ) {
2478
+ payloadView = new DataView (
2479
+ response . data . buffer ,
2480
+ response . data . byteOffset + headerSize ,
2481
+ dataSize
2482
+ ) ;
2483
+ } else if ( dataCompressionType === 1 ) {
2484
+ const compressedCharCount = response . data . readU16 ( ) ;
2485
+ const compressedArray = new Uint8Array (
2486
+ response . data . buffer ,
2487
+ response . data . byteOffset + headerSize + 2 ,
2488
+ dataSize - 2
2489
+ ) ;
2490
+ const decompressedArray = huffmanDecodeBuf (
2491
+ compressedArray ,
2492
+ compressedCharCount ,
2493
+ defaultHuffmanTree ,
2494
+ defaultHuffmanLenIndex
2495
+ ) ;
2496
+ payloadView = new DataView ( decompressedArray . buffer ) ;
2497
+ }
2498
+ } catch ( e ) {
2499
+ console . warn ( 'Decompression or read failed, delivering raw data anyway' ) ;
2500
+ payloadView = new DataView (
2501
+ response . data . buffer ,
2502
+ response . data . byteOffset + headerSize ,
2503
+ dataSize
2504
+ ) ;
2483
2505
}
2484
- } catch ( e ) {
2485
- console . warn ( 'Decompression or read failed, delivering raw data anyway' ) ;
2486
- payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2506
+ } else {
2507
+ console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2487
2508
}
2488
-
2489
- // always deliver the data, even if addresses mismatch or CRC fails
2490
- onDataCallback ( chunkAddress , payloadView ) ;
2491
- } else {
2492
- // if no data at all, deliver null
2493
- onDataCallback ( address , null ) ;
2494
2509
}
2495
- } ) ;
2496
- } ;
2497
2510
2498
2511
// Deliver payloadView if defined, otherwise pass null
2499
- onDataCallback ( address , payloadView ) ;
2500
-
2501
- if ( ! response || response . crcError ) {
2502
- console . log ( `CRC error or missing data at address ${ address } - delivering whatever we got` ) ;
2503
- } else if ( payloadView ) {
2504
- console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
2505
- }
2506
- } , true ) ;
2507
- } ;
2508
-
2509
- MspHelper . prototype . sendServoConfigurations = function ( onCompleteCallback ) {
2510
- let nextFunction = send_next_servo_configuration ;
2511
-
2512
- let servoIndex = 0 ;
2513
-
2514
- if ( FC . SERVO_CONFIG . length == 0 ) {
2515
- onCompleteCallback ( ) ;
2516
- } else {
2517
- nextFunction ( ) ;
2518
- }
2519
-
2520
- function send_next_servo_configuration ( ) {
2521
- const buffer = [ ] ;
2522
-
2523
- // send one at a time, with index
2524
-
2525
- const servoConfiguration = FC . SERVO_CONFIG [ servoIndex ] ;
2526
-
2527
- buffer
2528
- . push8 ( servoIndex )
2529
- . push16 ( servoConfiguration . min )
2530
- . push16 ( servoConfiguration . max )
2531
- . push16 ( servoConfiguration . middle )
2532
- . push8 ( servoConfiguration . rate ) ;
2533
-
2534
- let out = servoConfiguration . indexOfChannelToForward ;
2535
- if ( out == undefined ) {
2536
- out = 255 ; // Cleanflight defines "CHANNEL_FORWARDING_DISABLED" as "(uint8_t)0xFF"
2537
- }
2538
- buffer . push8 ( out ) . push32 ( servoConfiguration . reversedInputSources ) ;
2539
-
2540
- // prepare for next iteration
2541
- servoIndex ++ ;
2542
- if ( servoIndex == FC . SERVO_CONFIG . length ) {
2543
- nextFunction = onCompleteCallback ;
2544
- }
2545
-
2546
- MSP . send_message ( MSPCodes . MSP_SET_SERVO_CONFIGURATION , buffer , false , nextFunction ) ;
2547
- }
2548
- } ;
2549
-
2550
- MspHelper . prototype . sendModeRanges = function ( onCompleteCallback ) {
2551
- let nextFunction = send_next_mode_range ;
2552
-
2553
- let modeRangeIndex = 0 ;
2554
-
2555
- if ( FC . MODE_RANGES . length == 0 ) {
2556
- onCompleteCallback ( ) ;
2557
- } else {
2558
- send_next_mode_range ( ) ;
2559
- }
2560
-
2561
- function send_next_mode_range ( ) {
2562
- const modeRange = FC . MODE_RANGES [ modeRangeIndex ] ;
2563
- const buffer = [ ] ;
2564
-
2565
- buffer
2566
- . push8 ( modeRangeIndex )
2567
- . push8 ( modeRange . id )
2568
- . push8 ( modeRange . auxChannelIndex )
2569
- . push8 ( ( modeRange . range . start - 900 ) / 25 )
2570
- . push8 ( ( modeRange . range . end - 900 ) / 25 ) ;
2571
-
2572
- const modeRangeExtra = FC . MODE_RANGES_EXTRA [ modeRangeIndex ] ;
2573
-
2574
- buffer . push8 ( modeRangeExtra . modeLogic ) . push8 ( modeRangeExtra . linkedTo ) ;
2575
-
2576
- // prepare for next iteration
2577
- modeRangeIndex ++ ;
2578
- if ( modeRangeIndex == FC . MODE_RANGES . length ) {
2579
- nextFunction = onCompleteCallback ;
2580
- }
2581
- MSP . send_message ( MSPCodes . MSP_SET_MODE_RANGE , buffer , false , nextFunction ) ;
2582
2512
}
2583
2513
} ;
2584
2514
0 commit comments