Skip to content

Commit ea86a3b

Browse files
committed
Cache reverse button state for shifter
With the implementation of connection in the base class, caching this value allows us to clear it on disconnect. It also reinforces the idiom that peripheral state is only changed on 'update()'.
1 parent d0cefac commit ea86a3b

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/SimRacing.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ AnalogShifter::AnalogShifter(
741741
/* Two axes, X and Y */
742742
analogAxis{ AnalogInput(pinX), AnalogInput(pinY) },
743743

744-
pinReverse(sanitizePin(pinRev))
744+
pinReverse(sanitizePin(pinRev)),
745+
reverseState(false)
745746
{}
746747

747748
void AnalogShifter::begin() {
@@ -759,6 +760,9 @@ bool AnalogShifter::updateState(bool connected) {
759760
analogAxis[Axis::X].setPosition(calibration.neutralX);
760761
analogAxis[Axis::Y].setPosition(calibration.neutralY);
761762

763+
// set reverse state to unpressed
764+
this->reverseState = false;
765+
762766
// set gear to neutral
763767
this->setGear(0);
764768

@@ -772,6 +776,9 @@ bool AnalogShifter::updateState(bool connected) {
772776
const int x = analogAxis[Axis::X].getPosition();
773777
const int y = analogAxis[Axis::Y].getPosition();
774778

779+
// poll the reverse button and cache in the class
780+
this->reverseState = this->readReverseButton();
781+
775782
// check previous gears for comparison
776783
const Gear previousGear = this->getGear();
777784
const bool prevOdd = ((previousGear != -1) && (previousGear & 1)); // were we previously in an odd gear
@@ -834,15 +841,21 @@ int AnalogShifter::getPositionRaw(Axis ax) const {
834841
return analogAxis[ax].getPositionRaw();
835842
}
836843

837-
bool AnalogShifter::getReverseButton() const {
838-
// if the reverse pin is not set *or* if the device is not currently
839-
// connected, avoid reading the floating input and just return 'false'
844+
bool AnalogShifter::readReverseButton() {
845+
// if the reverse pin is not set, avoid reading the
846+
// floating input and just return 'false'
840847
if (pinReverse == UnusedPin) {
841848
return false;
842849
}
843850
return digitalRead(pinReverse);
844851
}
845852

853+
bool AnalogShifter::getReverseButton() const {
854+
// return the cached reverse state from updateState(bool)
855+
// do NOT poll the button!
856+
return this->reverseState;
857+
}
858+
846859
void AnalogShifter::setCalibration(
847860
GearPosition neutral,
848861
GearPosition g1, GearPosition g2, GearPosition g3, GearPosition g4, GearPosition g5, GearPosition g6,

src/SimRacing.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,17 @@ namespace SimRacing {
721721
virtual bool updateState(bool connected);
722722

723723
private:
724+
/**
725+
* Read the state of the reverse button
726+
*
727+
* This function should *only* be called as part of updateState(bool),
728+
* to update the state of the device.
729+
*
730+
* @returns the state of the reverse button, 'true' if pressed,
731+
* 'false' otherwise
732+
*/
733+
virtual bool readReverseButton();
734+
724735
/**
725736
* Distance from neutral on Y to register a gear as
726737
* being engaged (as a percentage of distance from
@@ -756,6 +767,7 @@ namespace SimRacing {
756767

757768
AnalogInput analogAxis[2]; ///< Axis data for X and Y
758769
PinNum pinReverse; ///< The pin for the reverse gear button
770+
bool reverseState; ///< Buffered value for the state of the reverse gear button
759771
};
760772

761773
/// @} Shifters

0 commit comments

Comments
 (0)