Skip to content

Commit e0af45e

Browse files
Fix parsing of Rx16/64IoSampleResponses
The old code didn't calculate the offsets correctly causing isDigitalEnabled() and isDigitalOn() to only at the first sample data (instead of looking at the mask or subsequent samples). This introduces a getSampleStart() method to allow sharing some code between getAnalog() and isDigitalOn().
1 parent 164aa67 commit e0af45e

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

XBee.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ bool RxIoSampleBaseResponse::isAnalogEnabled(uint8_t pin) {
357357

358358
bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) {
359359
if (pin < 8) {
360-
return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
360+
return ((getFrameData()[getSampleOffset() + 2] >> pin) & 1) == 1;
361361
} else {
362-
return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
362+
return (getFrameData()[getSampleOffset() + 1] & 1) == 1;
363363
}
364364
}
365365

@@ -454,50 +454,50 @@ bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) {
454454
// return (this.getProcessedPacketBytes()[startIndex] << 8) + this.getProcessedPacketBytes()[startIndex + 1];
455455
// }
456456

457-
// THIS IS WRONG
458-
uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) {
459-
460-
// analog starts 3 bytes after sample size, if no dio enabled
461-
uint8_t start = 3;
457+
uint8_t RxIoSampleBaseResponse::getSampleStart(uint8_t sample) {
458+
uint8_t spacing = 0;
462459

463460
if (containsDigital()) {
464461
// make room for digital i/o sample (2 bytes per sample)
465-
start+=2*(sample + 1);
462+
spacing += 2;
466463
}
467464

468-
uint8_t spacing = 0;
469-
470-
// spacing between samples depends on how many are enabled. add one for each analog that's enabled
465+
// spacing between samples depends on how many are enabled. add
466+
// 2 bytes for each analog that's enabled
471467
for (int i = 0; i <= 5; i++) {
472468
if (isAnalogEnabled(i)) {
473469
// each analog is two bytes
474470
spacing+=2;
475471
}
476472
}
477473

478-
// std::cout << "spacing is " << static_cast<unsigned int>(spacing) << std::endl;
474+
// Skip 3-byte header and "sample" full samples
475+
return getSampleOffset() + 3 + sample * spacing;
476+
}
479477

480-
// start depends on how many pins before this pin are enabled
478+
uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) {
479+
uint8_t start = getSampleStart(sample);
480+
481+
if (containsDigital()) {
482+
// Skip digital sample info
483+
start += 2;
484+
}
485+
486+
// Skip any analog samples before this pin
481487
for (int i = 0; i < pin; i++) {
482488
if (isAnalogEnabled(i)) {
483489
start+=2;
484490
}
485491
}
486492

487-
start+= sample * spacing;
488-
489-
// std::cout << "start for analog pin ["<< static_cast<unsigned int>(pin) << "]/sample " << static_cast<unsigned int>(sample) << " is " << static_cast<unsigned int>(start) << std::endl;
490-
491-
// std::cout << "returning index " << static_cast<unsigned int>(getSampleOffset() + start) << " and index " << static_cast<unsigned int>(getSampleOffset() + start + 1) << ", val is " << static_cast<unsigned int>(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast<unsigned int>(getFrameData()[getSampleOffset() + start + 1]) << std::endl;
492-
493-
return (uint16_t)((getFrameData()[getSampleOffset() + start] << 8) + getFrameData()[getSampleOffset() + start + 1]);
493+
return (uint16_t)((getFrameData()[start] << 8) + getFrameData()[start + 1]);
494494
}
495495

496496
bool RxIoSampleBaseResponse::isDigitalOn(uint8_t pin, uint8_t sample) {
497497
if (pin < 8) {
498-
return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
498+
return ((getFrameData()[getSampleStart(sample) + 1] >> pin) & 1) == 1;
499499
} else {
500-
return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
500+
return (getFrameData()[getSampleStart(sample)] & 1) == 1;
501501
}
502502
}
503503

XBee.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ class RxIoSampleBaseResponse : public RxResponse {
550550
*/
551551
bool isDigitalOn(uint8_t pin, uint8_t sample);
552552
uint8_t getSampleOffset();
553+
554+
/**
555+
* Gets the offset of the start of the given sample.
556+
*/
557+
uint8_t getSampleStart(uint8_t sample);
553558
private:
554559
};
555560

0 commit comments

Comments
 (0)