Skip to content

Commit a11f9ec

Browse files
committed
Remove direct gear access in Shifter base
Changes out the direct access to 'currentGear' and the 'changed' flag with a buffer function that handles that for us. Much cleaner and safer. Not sure what I was thinking the first time around...
1 parent 5c4bac3 commit a11f9ec

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/SimRacing.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,19 @@ LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(PinNum gasPin, PinN
607607

608608
Shifter::Shifter(Gear min, Gear max)
609609
: MinGear(min), MaxGear(max)
610-
{}
610+
{
611+
this->currentGear = this->previousGear = 0; // neutral
612+
}
613+
614+
void Shifter::setGear(Gear gear) {
615+
// if gear is out of range, set it to neutral
616+
if (gear < MinGear || gear > MaxGear) {
617+
gear = 0;
618+
}
619+
620+
this->previousGear = this->currentGear;
621+
this->currentGear = gear;
622+
}
611623

612624
char Shifter::getGearChar(int gear) {
613625
char c = '?';
@@ -715,24 +727,22 @@ bool AnalogShifter::update() {
715727
// on an unplug event, we want to reset our position back to
716728
// neutral and then immediately return
717729
case(DeviceConnection::Unplug):
718-
{
719-
const Gear previousGear = this->getGear();
720730

731+
// set axis values to calibrated neutral
721732
analogAxis[Axis::X].setPosition(calibration.neutralX);
722733
analogAxis[Axis::Y].setPosition(calibration.neutralY);
723734

724-
if (previousGear != 0) changed = true;
725-
currentGear = 0;
726-
return changed;
735+
// set gear to neutral
736+
this->setGear(0);
737+
738+
return this->gearChanged();
727739
break;
728-
}
729740

730-
// if the device is either disconnected or just plugged in and unstable, set gear
731-
// 'changed' to false and then immediately return false to save on processing
741+
// if the device is either disconnected or just plugged in and unstable,
742+
// immediately return false to save on processing
732743
case(DeviceConnection::PlugIn):
733744
case(DeviceConnection::Disconnected):
734-
changed = false;
735-
return changed;
745+
return false;
736746
break;
737747
}
738748

@@ -783,10 +793,10 @@ bool AnalogShifter::update() {
783793
}
784794
}
785795

786-
changed = (newGear != previousGear) ? 1 : 0;
787-
currentGear = newGear;
796+
// finally, store the newly calculated gear
797+
this->setGear(newGear);
788798

789-
return changed;
799+
return this->gearChanged();
790800
}
791801

792802
long AnalogShifter::getPosition(Axis ax, long min, long max) const {

src/SimRacing.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ namespace SimRacing {
535535
*
536536
* @return 'true' if gear has changed, 'false' otherwise
537537
*/
538-
bool gearChanged() const { return changed; }
538+
bool gearChanged() const {
539+
return this->currentGear != this->previousGear;
540+
}
539541

540542
/**
541543
* Retrieves the minimum possible gear index.
@@ -552,11 +554,23 @@ namespace SimRacing {
552554
Gear getGearMax() { return MaxGear; }
553555

554556
protected:
557+
/**
558+
* Changes the currently set gear, internally
559+
*
560+
* This function sanitizes the newly selected gear with MinGear / MaxGear,
561+
* and handles caching the previous value for checking if the gear has
562+
* changed.
563+
*
564+
* @param gear the new gear value to set
565+
*/
566+
void setGear(Gear gear);
567+
568+
private:
555569
const Gear MinGear; ///< the lowest selectable gear
556570
const Gear MaxGear; ///< the highest selectable gear
557571

558572
Gear currentGear; ///< index of the current gear
559-
bool changed; ///< whether the gear has changed since the previous update
573+
Gear previousGear; ///< index of the last selected gear
560574
};
561575

562576

0 commit comments

Comments
 (0)