Skip to content

Commit 422a07c

Browse files
committed
Remove detection from Peripheral constructor
Although I think the detector being a part of the Peripheral base class is wise, I'm not a fan of passing the detector pointer through the constructor. If a derived class passes its own detector and a later derived class wants to change that detector for whatever reason, it's blocked. Changing to a protected function makes more sense. This also in effect removes arbitrary detection support for the generic base classes (Shifter, Pedals, Handbrake, etc.). I have no evidence that that feature was ever used by an end-user, and with the way the library is structured that is better implemented as a custom derived class for their specific device. It can always be added back in later if necessary (via a second constructor taking a pin and declaring an object on the heap, most likely).
1 parent 7b3fa7a commit 422a07c

File tree

2 files changed

+53
-67
lines changed

2 files changed

+53
-67
lines changed

src/SimRacing.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,6 @@ void AnalogInput::setCalibration(AnalogInput::Calibration newCal) {
430430
// Peripheral #
431431
//#########################################################
432432

433-
Peripheral::Peripheral(DeviceConnection* detector)
434-
: detector(detector)
435-
{}
436-
437433
bool Peripheral::update() {
438434
// if the detector exists, poll for state
439435
if (this->detector) {
@@ -457,6 +453,10 @@ bool Peripheral::isConnected() const {
457453
return true;
458454
}
459455

456+
void Peripheral::setDetectPtr(DeviceConnection* d) {
457+
this->detector = d;
458+
}
459+
460460
void Peripheral::setStablePeriod(unsigned long t) {
461461
// if detector exists, set the stable period
462462
if (this->detector) {
@@ -468,9 +468,8 @@ void Peripheral::setStablePeriod(unsigned long t) {
468468
// Pedals #
469469
//#########################################################
470470

471-
Pedals::Pedals(AnalogInput* dataPtr, uint8_t nPedals, DeviceConnection* detector)
471+
Pedals::Pedals(AnalogInput* dataPtr, uint8_t nPedals)
472472
:
473-
Peripheral(detector),
474473
pedalData(dataPtr),
475474
NumPedals(nPedals),
476475
changed(false)
@@ -676,8 +675,8 @@ void Pedals::serialCalibration(Stream& iface) {
676675
}
677676

678677

679-
TwoPedals::TwoPedals(PinNum gasPin, PinNum brakePin, DeviceConnection* detector)
680-
: Pedals(pedalData, NumPedals, detector),
678+
TwoPedals::TwoPedals(PinNum gasPin, PinNum brakePin)
679+
: Pedals(pedalData, NumPedals),
681680
pedalData{ AnalogInput(gasPin), AnalogInput(brakePin) }
682681
{}
683682

@@ -687,8 +686,8 @@ void TwoPedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Cal
687686
}
688687

689688

690-
ThreePedals::ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, DeviceConnection* detector)
691-
: Pedals(pedalData, NumPedals, detector),
689+
ThreePedals::ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin)
690+
: Pedals(pedalData, NumPedals),
692691
pedalData{ AnalogInput(gasPin), AnalogInput(brakePin), AnalogInput(clutchPin) }
693692
{}
694693

@@ -701,9 +700,11 @@ void ThreePedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::C
701700

702701
LogitechPedals::LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin)
703702
:
704-
ThreePedals(gasPin, brakePin, clutchPin, &this->detectObj),
703+
ThreePedals(gasPin, brakePin, clutchPin),
705704
detectObj(detectPin, false) // active high
706705
{
706+
this->setDetectPtr(&this->detectObj);
707+
707708
// taken from calibrating my own pedals. the springs are pretty stiff so while
708709
// this covers the whole travel range, users may want to back it down for casual
709710
// use (esp. for the brake travel)
@@ -712,9 +713,10 @@ LogitechPedals::LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin,
712713

713714
LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin)
714715
:
715-
TwoPedals(gasPin, brakePin, &this->detectObj),
716+
TwoPedals(gasPin, brakePin),
716717
detectObj(detectPin, false) // active high
717718
{
719+
this->setDetectPtr(&this->detectObj);
718720
this->setCalibration({ 646, 0 }, { 473, 1023 }); // taken from calibrating my own pedals
719721
}
720722

@@ -723,9 +725,8 @@ LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(PinNum gasPin, PinN
723725
// Shifter #
724726
//#########################################################
725727

726-
Shifter::Shifter(Gear min, Gear max, DeviceConnection* detector)
728+
Shifter::Shifter(Gear min, Gear max)
727729
:
728-
Peripheral(detector),
729730
MinGear(min), MaxGear(max)
730731
{
731732
this->currentGear = this->previousGear = 0; // neutral
@@ -814,10 +815,9 @@ const float AnalogShifter::CalEdgeOffset = 0.60;
814815

815816
AnalogShifter::AnalogShifter(
816817
Gear gearMin, Gear gearMax,
817-
PinNum pinX, PinNum pinY, PinNum pinRev,
818-
DeviceConnection* detector
818+
PinNum pinX, PinNum pinY, PinNum pinRev
819819
) :
820-
Shifter(gearMin, gearMax, detector),
820+
Shifter(gearMin, gearMax),
821821

822822
/* Two axes, X and Y */
823823
analogAxis{ AnalogInput(pinX), AnalogInput(pinY) },
@@ -1132,12 +1132,15 @@ void AnalogShifter::serialCalibration(Stream& iface) {
11321132
}
11331133

11341134
LogitechShifter::LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum detectPin)
1135-
: AnalogShifter(
1135+
:
1136+
AnalogShifter(
11361137
-1, 6, // includes reverse and gears 1-6
1137-
pinX, pinY, pinRev, &this->detectObj),
1138+
pinX, pinY, pinRev
1139+
),
11381140

1139-
detectObj(detectPin, false) // active high
1141+
detectObj(detectPin, false) // active high
11401142
{
1143+
this->setDetectPtr(&this->detectObj);
11411144
this->setCalibration({ 490, 440 }, { 253, 799 }, { 262, 86 }, { 460, 826 }, { 470, 76 }, { 664, 841 }, { 677, 77 });
11421145
}
11431146

@@ -1654,11 +1657,9 @@ void LogitechShifterG25::serialCalibrationSequential(Stream& iface) {
16541657
// Handbrake #
16551658
//#########################################################
16561659

1657-
Handbrake::Handbrake(PinNum pinAx, PinNum detectPin, boolean detectActiveLow)
1660+
Handbrake::Handbrake(PinNum pinAx)
16581661
:
1659-
Peripheral(&this->detectObj),
16601662
analogAxis(pinAx),
1661-
detectObj(detectPin, detectActiveLow),
16621663
changed(false)
16631664
{}
16641665

src/SimRacing.h

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,6 @@ namespace SimRacing {
250250
*/
251251
class Peripheral {
252252
public:
253-
/**
254-
* Class Constructor
255-
*
256-
* @param detector pointer to a device connection instance, to use for
257-
* determining if the peripheral is connected
258-
*/
259-
Peripheral(DeviceConnection* detector = nullptr);
260-
261253
/**
262254
* Class destructor
263255
*/
@@ -301,6 +293,20 @@ namespace SimRacing {
301293
*/
302294
virtual bool updateState(bool connected) = 0;
303295

296+
/**
297+
* Sets the pointer to the detector object
298+
*
299+
* The detector object is used to check if the peripheral is connected
300+
* to the microcontroller. The object is polled on every update.
301+
*
302+
* Although the detector instance is accessed via the Peripheral class,
303+
* it is the responsibility of the dervied class to store the
304+
* DeviceConnection object and manage its lifetime.
305+
*
306+
* @param d pointer to the detector object
307+
*/
308+
void setDetectPtr(DeviceConnection* d);
309+
304310
private:
305311
DeviceConnection* detector; ///< Pointer to a device connection instance
306312
};
@@ -337,12 +343,9 @@ namespace SimRacing {
337343
* @param dataPtr pointer to the analog input data managed by the class,
338344
* stored elsewhere
339345
* @param nPedals the number of pedals stored in said data pointer
340-
* @param detector pointer to a device connection instance, to use for
341-
* determining if the peripheral is connected
342346
*/
343347
Pedals(
344-
AnalogInput* dataPtr, uint8_t nPedals,
345-
DeviceConnection* detector = nullptr
348+
AnalogInput* dataPtr, uint8_t nPedals
346349
);
347350

348351
/** @copydoc Peripheral::begin() */
@@ -438,12 +441,9 @@ namespace SimRacing {
438441
*
439442
* @param pinGas the analog pin for the gas pedal potentiometer
440443
* @param pinBrake the analog pin for the brake pedal potentiometer
441-
* @param detector pointer to a device connection instance, to use for
442-
* determining if the peripheral is connected
443444
*/
444445
TwoPedals(
445-
PinNum pinGas, PinNum pinBrake,
446-
DeviceConnection* detector = nullptr
446+
PinNum pinGas, PinNum pinBrake
447447
);
448448

449449
/**
@@ -471,12 +471,9 @@ namespace SimRacing {
471471
* @param pinGas the analog pin for the gas pedal potentiometer
472472
* @param pinBrake the analog pin for the brake pedal potentiometer
473473
* @param pinClutch the analog pin for the clutch pedal potentiometer
474-
* @param detector pointer to a device connection instance, to use for
475-
* determining if the peripheral is connected
476474
*/
477475
ThreePedals(
478-
PinNum pinGas, PinNum pinBrake, PinNum pinClutch,
479-
DeviceConnection* detector = nullptr
476+
PinNum pinGas, PinNum pinBrake, PinNum pinClutch
480477
);
481478

482479
/**
@@ -515,12 +512,10 @@ namespace SimRacing {
515512
/**
516513
* Class constructor
517514
*
518-
* @param min the lowest gear possible
519-
* @param max the highest gear possible
520-
* @param detector pointer to a device connection instance, to use for
521-
* determining if the peripheral is connected
515+
* @param min the lowest gear possible
516+
* @param max the highest gear possible
522517
*/
523-
Shifter(Gear min, Gear max, DeviceConnection* detector = nullptr);
518+
Shifter(Gear min, Gear max);
524519

525520
/**
526521
* Returns the currently selected gear.
@@ -621,13 +616,11 @@ namespace SimRacing {
621616
/**
622617
* Class constructor
623618
*
624-
* @param gearMin the lowest gear possible
625-
* @param gearMax the highest gear possible
626-
* @param pinX the analog input pin for the X axis
627-
* @param pinY the analog input pin for the Y axis
628-
* @param pinRev the digital input pin for the 'reverse' button
629-
* @param detector pointer to a device connection instance, to use for
630-
* determining if the peripheral is connected
619+
* @param gearMin the lowest gear possible
620+
* @param gearMax the highest gear possible
621+
* @param pinX the analog input pin for the X axis
622+
* @param pinY the analog input pin for the Y axis
623+
* @param pinRev the digital input pin for the 'reverse' button
631624
*
632625
* @note With the way the class is designed, the lowest possible gear is
633626
* -1 (reverse), and the highest possible gear is 6. Setting the
@@ -638,8 +631,7 @@ namespace SimRacing {
638631
AnalogShifter(
639632
Gear gearMin, Gear gearMax,
640633
PinNum pinX, PinNum pinY,
641-
PinNum pinRev = UnusedPin,
642-
DeviceConnection* detector = nullptr
634+
PinNum pinRev = UnusedPin
643635
);
644636

645637
/**
@@ -781,15 +773,9 @@ namespace SimRacing {
781773
/**
782774
* Class constructor
783775
*
784-
* @param pinAx analog pin number for the handbrake axis
785-
* @param pinDetect the digital pin for device detection
786-
* @param detectActiveLow whether the device is detected on a high signal (false,
787-
* default) or a low signal (true)
788-
*/
789-
Handbrake(
790-
PinNum pinAx,
791-
PinNum pinDetect = UnusedPin, boolean detectActiveLow = false
792-
);
776+
* @param pinAx analog pin number for the handbrake axis
777+
*/
778+
Handbrake(PinNum pinAx);
793779

794780
/**
795781
* Initializes the pin for reading from the handbrake.
@@ -836,7 +822,6 @@ namespace SimRacing {
836822

837823
private:
838824
AnalogInput analogAxis; ///< axis data for the handbrake's position
839-
DeviceConnection detectObj; ///< detector instance for checking if the handbrake is connected
840825
bool changed; ///< whether the handbrake position has changed since the previous update
841826
};
842827

0 commit comments

Comments
 (0)