Skip to content

Commit 97b7a34

Browse files
committed
Check analog range calibration for change
Instead of detecting simply if the ADC value changed, this now checks if the changed value is outside of the calibrated range (in the same direction it was previously). If so, the user-facing classes aren't going to care and can safely ignore the update. This was written specifically to solve an issue with the pedals, which don't need to send constant updates when they're at their resting position.
1 parent b0bc51a commit 97b7a34

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/SimRacing.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,33 @@ AnalogInput::AnalogInput(uint8_t p)
268268

269269
bool AnalogInput::read() {
270270
bool changed = false;
271+
271272
if (Pin != NOT_A_PIN) {
272-
const int current = this->position;
273+
const int previous = this->position;
273274
this->position = analogRead(Pin);
274-
if (current != this->position) changed = true;
275+
276+
// check if value is different for 'changed' flag
277+
if (previous != this->position) {
278+
279+
const int rMin = isInverted() ? getMax() : getMin();
280+
const int rMax = isInverted() ? getMin() : getMax();
281+
282+
if (
283+
// if the previous value was under the minimum range
284+
// and the current value is as well, no change
285+
!(previous < rMin && this->position < rMin) &&
286+
287+
// if the previous value was over the maximum range
288+
// and the current value is as well, no change
289+
!(previous > rMax && this->position > rMax)
290+
)
291+
{
292+
// otherwise, the current value is either within the
293+
// range limits *or* it has changed from one extreme
294+
// to the other. Either way, mark it changed!
295+
changed = true;
296+
}
297+
}
275298
}
276299
return changed;
277300
}

0 commit comments

Comments
 (0)