@@ -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
747748void 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+
846859void AnalogShifter::setCalibration (
847860 GearPosition neutral,
848861 GearPosition g1, GearPosition g2, GearPosition g3, GearPosition g4, GearPosition g5, GearPosition g6,
0 commit comments