Skip to content

Commit

Permalink
Clean up pin arguments and definitions
Browse files Browse the repository at this point in the history
Removing const from pin number members to allow copying, and changing argument names so that 'pin' is the prefix rather than the suffix in all cases.

This should not affect any user code.
  • Loading branch information
dmadison committed Jun 6, 2024
1 parent b56d61f commit 1da6619
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
34 changes: 17 additions & 17 deletions src/SimRacing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static void readFloat(float& value, Stream& client) {

DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detectTime)
:
Pin(sanitizePin(pin)), Inverted(invert), stablePeriod(detectTime), // constants(ish)
pin(sanitizePin(pin)), inverted(invert), stablePeriod(detectTime), // constants(ish)

/* Assume we're connected on first call
*/
Expand All @@ -192,7 +192,7 @@ DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detect
* the device to be read as connected as soon as the board turns on, without
* having to wait an arbitrary amount.
*/
pinState(!Inverted),
pinState(!inverted),

/* Set the last pin change to right now minus the stable period so it's
* read as being already stable. Again, this will make the class return
Expand All @@ -202,7 +202,7 @@ DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detect

{
if (pin != UnusedPin) {
pinMode(Pin, INPUT); // set pin as input, *no* pull-up
pinMode(pin, INPUT); // set pin as input, *no* pull-up
}
}

Expand Down Expand Up @@ -265,30 +265,30 @@ void DeviceConnection::setStablePeriod(unsigned long t) {
}

bool DeviceConnection::readPin() const {
if (Pin == UnusedPin) return HIGH; // if no pin is set, we're always connected
const bool state = digitalRead(Pin);
return Inverted ? !state : state;
if (pin == UnusedPin) return HIGH; // if no pin is set, we're always connected
const bool state = digitalRead(pin);
return inverted ? !state : state;
}

//#########################################################
// AnalogInput #
//#########################################################


AnalogInput::AnalogInput(PinNum p)
: Pin(sanitizePin(p)), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max})
AnalogInput::AnalogInput(PinNum pin)
: pin(sanitizePin(pin)), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max})
{
if (Pin != UnusedPin) {
pinMode(Pin, INPUT);
if (pin != UnusedPin) {
pinMode(pin, INPUT);
}
}

bool AnalogInput::read() {
bool changed = false;

if (Pin != UnusedPin) {
if (pin != UnusedPin) {
const int previous = this->position;
this->position = analogRead(Pin);
this->position = analogRead(pin);

// check if value is different for 'changed' flag
if (previous != this->position) {
Expand Down Expand Up @@ -684,13 +684,13 @@ AnalogShifter::AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum det
/* Two axes, X and Y */
analogAxis{ AnalogInput(pinX), AnalogInput(pinY) },

PinReverse(sanitizePin(pinRev)),
pinReverse(sanitizePin(pinRev)),
detector(detectPin, false) // not inverted
{}

void AnalogShifter::begin() {
if (this->PinReverse != UnusedPin) {
pinMode(PinReverse, INPUT);
if (this->pinReverse != UnusedPin) {
pinMode(pinReverse, INPUT);
}
update(); // set initial gear position
}
Expand Down Expand Up @@ -796,10 +796,10 @@ int AnalogShifter::getPositionRaw(Axis ax) const {
bool AnalogShifter::getReverseButton() const {
// if the reverse pin is not set *or* if the device is not currently
// connected, avoid reading the floating input and just return 'false'
if (PinReverse == UnusedPin || detector.getState() != DeviceConnection::Connected) {
if (pinReverse == UnusedPin || detector.getState() != DeviceConnection::Connected) {
return false;
}
return digitalRead(PinReverse);
return digitalRead(pinReverse);
}

void AnalogShifter::setCalibration(
Expand Down
54 changes: 27 additions & 27 deletions src/SimRacing.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ namespace SimRacing {
*/
bool readPin() const;

const PinNum Pin; ///< The pin number being read from. Can be 'UnusedPin' to disable
const bool Inverted; ///< Whether the input is inverted, so 'LOW' is detected instead of 'HIGH'
PinNum pin; ///< The pin number being read from. Can be 'UnusedPin' to disable
bool inverted; ///< Whether the input is inverted, so 'LOW' is detected instead of 'HIGH'
unsigned long stablePeriod; ///< The amount of time the input must be stable for (ms)

ConnectionState state; ///< The current state of the connection
bool pinState; ///< Buffered state of the input pin, accounting for inversion
unsigned long lastChange; ///< Timestamp of the last pin change, in ms (using millis())
ConnectionState state; ///< The current state of the connection
bool pinState; ///< Buffered state of the input pin, accounting for inversion
unsigned long lastChange; ///< Timestamp of the last pin change, in ms (using millis())
};


Expand All @@ -141,9 +141,9 @@ namespace SimRacing {
/**
* Class constructor
*
* @param p the I/O pin for this input (Arduino numbering)
* @param pin the I/O pin for this input (Arduino numbering)
*/
AnalogInput(PinNum p);
AnalogInput(PinNum pin);

/**
* Updates the current value of the axis by polling the ADC
Expand Down Expand Up @@ -237,9 +237,9 @@ namespace SimRacing {
void setCalibration(Calibration newCal);

private:
const PinNum Pin = UnusedPin; ///< the digital pin number for this input
int position; ///< the axis' position in its range, buffered
Calibration cal; ///< the calibration values for the axis
PinNum pin; ///< the digital pin number for this input
int position; ///< the axis' position in its range, buffered
Calibration cal; ///< the calibration values for the axis
};


Expand Down Expand Up @@ -393,11 +393,11 @@ namespace SimRacing {
/**
* Class constructor
*
* @param gasPin the analog pin for the gas pedal potentiometer
* @param brakePin the analog pin for the brake pedal potentiometer
* @param detectPin the digital pin for device detection (high is detected)
* @param pinGas the analog pin for the gas pedal potentiometer
* @param pinBrake the analog pin for the brake pedal potentiometer
* @param pinDetect the digital pin for device detection (high is detected)
*/
TwoPedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = UnusedPin);
TwoPedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect = UnusedPin);

/**
* Sets the calibration data (min/max) for the pedals
Expand All @@ -421,12 +421,12 @@ namespace SimRacing {
/**
* Class constructor
*
* @param gasPin the analog pin for the gas pedal potentiometer
* @param brakePin the analog pin for the brake pedal potentiometer
* @param clutchPin the analog pin for the clutch pedal potentiometer
* @param detectPin the digital pin for device detection (high is detected)
* @param pinGas the analog pin for the gas pedal potentiometer
* @param pinBrake the analog pin for the brake pedal potentiometer
* @param pinClutch the analog pin for the clutch pedal potentiometer
* @param pinDetect the digital pin for device detection (high is detected)
*/
ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = UnusedPin);
ThreePedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect = UnusedPin);

/**
* Sets the calibration data (min/max) for the pedals
Expand Down Expand Up @@ -552,9 +552,9 @@ namespace SimRacing {
* @param pinX the analog input pin for the X axis
* @param pinY the analog input pin for the Y axis
* @param pinRev the digital input pin for the 'reverse' button
* @param detectPin the digital pin for device detection (high is detected)
* @param pinDetect the digital pin for device detection (high is detected)
*/
AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum detectPin = UnusedPin);
AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum pinDetect = UnusedPin);

/**
* Initializes the hardware pins for reading the gear states.
Expand Down Expand Up @@ -675,7 +675,7 @@ namespace SimRacing {
} calibration;

AnalogInput analogAxis[2]; ///< Axis data for X and Y
const PinNum PinReverse; ///< The pin for the reverse gear button
PinNum pinReverse; ///< The pin for the reverse gear button
DeviceConnection detector; ///< detector instance for checking if the shifter is connected
};

Expand All @@ -691,9 +691,9 @@ namespace SimRacing {
* Class constructor
*
* @param pinAx analog pin number for the handbrake axis
* @param detectPin the digital pin for device detection (high is detected)
* @param pinDetect the digital pin for device detection (high is detected)
*/
Handbrake(PinNum pinAx, PinNum detectPin = UnusedPin);
Handbrake(PinNum pinAx, PinNum pinDetect = UnusedPin);

/**
* Initializes the pin for reading from the handbrake.
Expand Down Expand Up @@ -760,7 +760,7 @@ namespace SimRacing {
class LogitechPedals : public ThreePedals {
public:
/** @copydoc ThreePedals::ThreePedals */
LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = UnusedPin);
LogitechPedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect = UnusedPin);
};

/**
Expand All @@ -775,7 +775,7 @@ namespace SimRacing {
class LogitechDrivingForceGT_Pedals : public TwoPedals {
public:
/** @copydoc TwoPedals::TwoPedals */
LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = UnusedPin);
LogitechDrivingForceGT_Pedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect = UnusedPin);
};

/**
Expand All @@ -787,7 +787,7 @@ namespace SimRacing {
class LogitechShifter : public AnalogShifter {
public:
/** @copydoc AnalogShifter::AnalogShifter */
LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum detectPin = UnusedPin);
LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum pinDetect = UnusedPin);
};


Expand Down

0 comments on commit 1da6619

Please sign in to comment.