From 4a7a2eb23bfab83618f0f3e20aed174cff4ce27c Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 6 Jun 2024 14:41:30 -0400 Subject: [PATCH 1/4] Change all pin types to use PinNum alias --- src/SimRacing.cpp | 20 ++++++++++---------- src/SimRacing.h | 32 +++++++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/SimRacing.cpp b/src/SimRacing.cpp index fd2da96..3a71cd7 100644 --- a/src/SimRacing.cpp +++ b/src/SimRacing.cpp @@ -161,7 +161,7 @@ static void readFloat(float& value, Stream& client) { // DeviceConnection # //######################################################### -DeviceConnection::DeviceConnection(uint8_t pin, bool invert, unsigned long detectTime) +DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detectTime) : Pin(pin), Inverted(invert), stablePeriod(detectTime), // constants(ish) @@ -258,7 +258,7 @@ bool DeviceConnection::readPin() const { //######################################################### -AnalogInput::AnalogInput(uint8_t p) +AnalogInput::AnalogInput(PinNum p) : Pin(p), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max}) { if (Pin != NOT_A_PIN) { @@ -333,7 +333,7 @@ void AnalogInput::setCalibration(AnalogInput::Calibration newCal) { // Pedals # //######################################################### -Pedals::Pedals(AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin) +Pedals::Pedals(AnalogInput* dataPtr, uint8_t nPedals, PinNum detectPin) : pedalData(dataPtr), NumPedals(nPedals), @@ -538,7 +538,7 @@ void Pedals::serialCalibration(Stream& iface) { } -TwoPedals::TwoPedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin) +TwoPedals::TwoPedals(PinNum gasPin, PinNum brakePin, PinNum detectPin) : Pedals(pedalData, NumPedals, detectPin), pedalData{ AnalogInput(gasPin), AnalogInput(brakePin) } {} @@ -549,7 +549,7 @@ void TwoPedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Cal } -ThreePedals::ThreePedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin) +ThreePedals::ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin) : Pedals(pedalData, NumPedals, detectPin), pedalData{ AnalogInput(gasPin), AnalogInput(brakePin), AnalogInput(clutchPin) } {} @@ -562,7 +562,7 @@ void ThreePedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::C -LogitechPedals::LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin) +LogitechPedals::LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin) : ThreePedals(gasPin, brakePin, clutchPin, detectPin) { // taken from calibrating my own pedals. the springs are pretty stiff so while @@ -571,7 +571,7 @@ LogitechPedals::LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchP this->setCalibration({ 904, 48 }, { 944, 286 }, { 881, 59 }); } -LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin) +LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin) : TwoPedals(gasPin, brakePin, detectPin) { this->setCalibration({ 646, 0 }, { 473, 1023 }); // taken from calibrating my own pedals @@ -657,7 +657,7 @@ const float AnalogShifter::CalEngagementPoint = 0.70; const float AnalogShifter::CalReleasePoint = 0.50; const float AnalogShifter::CalEdgeOffset = 0.60; -AnalogShifter::AnalogShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev, uint8_t detectPin) +AnalogShifter::AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum detectPin) : /* In initializing the Shifter, the lowest gear is going to be '-1' if a pin * exists for reverse, otherwise it's going to be '0' (neutral). @@ -977,7 +977,7 @@ void AnalogShifter::serialCalibration(Stream& iface) { iface.println(F("\n\nCalibration complete! :)\n")); } -LogitechShifter::LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev, uint8_t detectPin) +LogitechShifter::LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum detectPin) : AnalogShifter(pinX, pinY, pinRev, detectPin) { this->setCalibration({ 490, 440 }, { 253, 799 }, { 262, 86 }, { 460, 826 }, { 470, 76 }, { 664, 841 }, { 677, 77 }); @@ -987,7 +987,7 @@ LogitechShifter::LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev, uin // Handbrake # //######################################################### -Handbrake::Handbrake(uint8_t pinAx, uint8_t detectPin) +Handbrake::Handbrake(PinNum pinAx, PinNum detectPin) : analogAxis(pinAx), detector(detectPin), diff --git a/src/SimRacing.h b/src/SimRacing.h index 52459b2..5d946cf 100644 --- a/src/SimRacing.h +++ b/src/SimRacing.h @@ -31,6 +31,12 @@ */ namespace SimRacing { + /** + * Type alias for pin numbers, using Arduino numbering + */ + using PinNum = uint8_t; + + /** * Enumeration for analog axis names, mapped to integers */ @@ -68,7 +74,7 @@ namespace SimRacing { * @param detectTime the amount of time, in ms, the input must be stable for * before it's interpreted as 'detected' */ - DeviceConnection(uint8_t pin, bool invert = false, unsigned long detectTime = 250); + DeviceConnection(PinNum pin, bool invert = false, unsigned long detectTime = 250); /** * Checks if the pin detects a connection. This polls the input and checks @@ -108,7 +114,7 @@ namespace SimRacing { */ bool readPin() const; - const uint8_t Pin; ///< The pin number being read from. Can be 'NOT_A_PIN' to disable + const PinNum Pin; ///< The pin number being read from. Can be 'NOT_A_PIN' to disable const 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) @@ -131,7 +137,7 @@ namespace SimRacing { * * @param p the I/O pin for this input (Arduino numbering) */ - AnalogInput(uint8_t p); + AnalogInput(PinNum p); /** * Updates the current value of the axis by polling the ADC @@ -225,7 +231,7 @@ namespace SimRacing { void setCalibration(Calibration newCal); private: - const uint8_t Pin = NOT_A_PIN; ///< the digital pin number for this input + const PinNum Pin = NOT_A_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 }; @@ -285,7 +291,7 @@ namespace SimRacing { * @param nPedals the number of pedals stored in said data pointer * @param detectPin the digital pin for device detection (high is detected) */ - Pedals(AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin); + Pedals(AnalogInput* dataPtr, uint8_t nPedals, PinNum detectPin); /** @copydoc Peripheral::begin() */ virtual void begin(); @@ -385,7 +391,7 @@ namespace SimRacing { * @param brakePin the analog pin for the brake pedal potentiometer * @param detectPin the digital pin for device detection (high is detected) */ - TwoPedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin = NOT_A_PIN); + TwoPedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = NOT_A_PIN); /** * Sets the calibration data (min/max) for the pedals @@ -414,7 +420,7 @@ namespace SimRacing { * @param clutchPin the analog pin for the clutch pedal potentiometer * @param detectPin the digital pin for device detection (high is detected) */ - ThreePedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin = NOT_A_PIN); + ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = NOT_A_PIN); /** * Sets the calibration data (min/max) for the pedals @@ -542,7 +548,7 @@ namespace SimRacing { * @param pinRev the digital input pin for the 'reverse' button * @param detectPin the digital pin for device detection (high is detected) */ - AnalogShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev = NOT_A_PIN, uint8_t detectPin = NOT_A_PIN); + AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = NOT_A_PIN, PinNum detectPin = NOT_A_PIN); /** * Initializes the hardware pins for reading the gear states. @@ -663,7 +669,7 @@ namespace SimRacing { } calibration; AnalogInput analogAxis[2]; ///< Axis data for X and Y - const uint8_t PinReverse; ///< The pin for the reverse gear button + const PinNum PinReverse; ///< The pin for the reverse gear button DeviceConnection detector; ///< detector instance for checking if the shifter is connected }; @@ -681,7 +687,7 @@ namespace SimRacing { * @param pinAx analog pin number for the handbrake axis * @param detectPin the digital pin for device detection (high is detected) */ - Handbrake(uint8_t pinAx, uint8_t detectPin = NOT_A_PIN); + Handbrake(PinNum pinAx, PinNum detectPin = NOT_A_PIN); /** * Initializes the pin for reading from the handbrake. @@ -748,7 +754,7 @@ namespace SimRacing { class LogitechPedals : public ThreePedals { public: /** @copydoc ThreePedals::ThreePedals */ - LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin = NOT_A_PIN); + LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = NOT_A_PIN); }; /** @@ -763,7 +769,7 @@ namespace SimRacing { class LogitechDrivingForceGT_Pedals : public TwoPedals { public: /** @copydoc TwoPedals::TwoPedals */ - LogitechDrivingForceGT_Pedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin = NOT_A_PIN); + LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = NOT_A_PIN); }; /** @@ -775,7 +781,7 @@ namespace SimRacing { class LogitechShifter : public AnalogShifter { public: /** @copydoc AnalogShifter::AnalogShifter */ - LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev = NOT_A_PIN, uint8_t detectPin = NOT_A_PIN); + LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = NOT_A_PIN, PinNum detectPin = NOT_A_PIN); }; From 93a4a6d22cbb1deb50f77901f224b957e96d9de9 Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 6 Jun 2024 14:46:49 -0400 Subject: [PATCH 2/4] Replace all NOT_A_PIN references Using a value we control rather than the platform-specific define (which isn't really meant for this purpose anyways). --- src/SimRacing.cpp | 10 +++++----- src/SimRacing.h | 28 +++++++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/SimRacing.cpp b/src/SimRacing.cpp index 3a71cd7..cefa0ef 100644 --- a/src/SimRacing.cpp +++ b/src/SimRacing.cpp @@ -248,7 +248,7 @@ void DeviceConnection::setStablePeriod(unsigned long t) { } bool DeviceConnection::readPin() const { - if (Pin == NOT_A_PIN) return HIGH; // if no pin is set, we're always connected + if (Pin == UnusedPin) return HIGH; // if no pin is set, we're always connected const bool state = digitalRead(Pin); return Inverted ? !state : state; } @@ -261,7 +261,7 @@ bool DeviceConnection::readPin() const { AnalogInput::AnalogInput(PinNum p) : Pin(p), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max}) { - if (Pin != NOT_A_PIN) { + if (Pin != UnusedPin) { pinMode(Pin, INPUT); } } @@ -269,7 +269,7 @@ AnalogInput::AnalogInput(PinNum p) bool AnalogInput::read() { bool changed = false; - if (Pin != NOT_A_PIN) { + if (Pin != UnusedPin) { const int previous = this->position; this->position = analogRead(Pin); @@ -662,7 +662,7 @@ AnalogShifter::AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum det /* In initializing the Shifter, the lowest gear is going to be '-1' if a pin * exists for reverse, otherwise it's going to be '0' (neutral). */ - Shifter(pinRev != NOT_A_PIN ? -1 : 0, 6), + Shifter(pinRev != UnusedPin ? -1 : 0, 6), /* Two axes, X and Y */ analogAxis{ AnalogInput(pinX), AnalogInput(pinY) }, @@ -777,7 +777,7 @@ 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 == NOT_A_PIN || detector.getState() != DeviceConnection::Connected) { + if (PinReverse == UnusedPin || detector.getState() != DeviceConnection::Connected) { return false; } return digitalRead(PinReverse); diff --git a/src/SimRacing.h b/src/SimRacing.h index 5d946cf..55728a6 100644 --- a/src/SimRacing.h +++ b/src/SimRacing.h @@ -34,7 +34,13 @@ namespace SimRacing { /** * Type alias for pin numbers, using Arduino numbering */ - using PinNum = uint8_t; + using PinNum = int16_t; + + /** + * Dummy pin number signaling that a pin is unused + * and can be safely ignored + */ + const PinNum UnusedPin = -1; /** @@ -69,7 +75,7 @@ namespace SimRacing { /** * Class constructor * - * @param pin the pin number being read. Can be 'NOT_A_PIN' to disable. + * @param pin the pin number being read. Can be 'UnusedPin' to disable. * @param invert whether the input is inverted, so 'LOW' is detected instead of 'HIGH' * @param detectTime the amount of time, in ms, the input must be stable for * before it's interpreted as 'detected' @@ -114,7 +120,7 @@ namespace SimRacing { */ bool readPin() const; - const PinNum Pin; ///< The pin number being read from. Can be 'NOT_A_PIN' to disable + 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' unsigned long stablePeriod; ///< The amount of time the input must be stable for (ms) @@ -231,7 +237,7 @@ namespace SimRacing { void setCalibration(Calibration newCal); private: - const PinNum Pin = NOT_A_PIN; ///< the digital pin number for this input + 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 }; @@ -391,7 +397,7 @@ namespace SimRacing { * @param brakePin the analog pin for the brake pedal potentiometer * @param detectPin the digital pin for device detection (high is detected) */ - TwoPedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = NOT_A_PIN); + TwoPedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = UnusedPin); /** * Sets the calibration data (min/max) for the pedals @@ -420,7 +426,7 @@ namespace SimRacing { * @param clutchPin the analog pin for the clutch pedal potentiometer * @param detectPin the digital pin for device detection (high is detected) */ - ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = NOT_A_PIN); + ThreePedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = UnusedPin); /** * Sets the calibration data (min/max) for the pedals @@ -548,7 +554,7 @@ namespace SimRacing { * @param pinRev the digital input pin for the 'reverse' button * @param detectPin the digital pin for device detection (high is detected) */ - AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = NOT_A_PIN, PinNum detectPin = NOT_A_PIN); + AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum detectPin = UnusedPin); /** * Initializes the hardware pins for reading the gear states. @@ -687,7 +693,7 @@ namespace SimRacing { * @param pinAx analog pin number for the handbrake axis * @param detectPin the digital pin for device detection (high is detected) */ - Handbrake(PinNum pinAx, PinNum detectPin = NOT_A_PIN); + Handbrake(PinNum pinAx, PinNum detectPin = UnusedPin); /** * Initializes the pin for reading from the handbrake. @@ -754,7 +760,7 @@ namespace SimRacing { class LogitechPedals : public ThreePedals { public: /** @copydoc ThreePedals::ThreePedals */ - LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = NOT_A_PIN); + LogitechPedals(PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin = UnusedPin); }; /** @@ -769,7 +775,7 @@ namespace SimRacing { class LogitechDrivingForceGT_Pedals : public TwoPedals { public: /** @copydoc TwoPedals::TwoPedals */ - LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = NOT_A_PIN); + LogitechDrivingForceGT_Pedals(PinNum gasPin, PinNum brakePin, PinNum detectPin = UnusedPin); }; /** @@ -781,7 +787,7 @@ namespace SimRacing { class LogitechShifter : public AnalogShifter { public: /** @copydoc AnalogShifter::AnalogShifter */ - LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = NOT_A_PIN, PinNum detectPin = NOT_A_PIN); + LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum detectPin = UnusedPin); }; From b56d61fe980aec49d17aad39dd48871f811255b3 Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 6 Jun 2024 14:57:24 -0400 Subject: [PATCH 3/4] Implement pin number sanitization Treating all negative numbers as invalid pins, sanitized to UnusedPin on assignment. --- src/SimRacing.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/SimRacing.cpp b/src/SimRacing.cpp index cefa0ef..9ae8672 100644 --- a/src/SimRacing.cpp +++ b/src/SimRacing.cpp @@ -29,6 +29,21 @@ namespace SimRacing { +/** +* Take a pin number as an input and sanitize it to a known working value +* +* In an ideal world this would check against the available pins on the micro, +* but as far as I know the Arduino API does not have a "valid pin" function. +* Instead, we'll just accept any positive number as a pin and reject any +* negative number as invalid ("Unused"). +* +* @param pin the pin number to sanitize +* @returns the pin number, or UnusedPin +*/ +static constexpr PinNum sanitizePin(PinNum pin) { + return pin < 0 ? UnusedPin : pin; +} + /** * Invert an input value so it's at the same relative position @@ -163,7 +178,7 @@ static void readFloat(float& value, Stream& client) { DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detectTime) : - Pin(pin), Inverted(invert), stablePeriod(detectTime), // constants(ish) + Pin(sanitizePin(pin)), Inverted(invert), stablePeriod(detectTime), // constants(ish) /* Assume we're connected on first call */ @@ -186,7 +201,9 @@ DeviceConnection::DeviceConnection(PinNum pin, bool invert, unsigned long detect lastChange(millis() - detectTime) { - pinMode(Pin, INPUT); // set pin as input, *no* pull-up + if (pin != UnusedPin) { + pinMode(Pin, INPUT); // set pin as input, *no* pull-up + } } void DeviceConnection::poll() { @@ -259,7 +276,7 @@ bool DeviceConnection::readPin() const { AnalogInput::AnalogInput(PinNum p) - : Pin(p), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max}) + : Pin(sanitizePin(p)), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max}) { if (Pin != UnusedPin) { pinMode(Pin, INPUT); @@ -662,17 +679,19 @@ AnalogShifter::AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev, PinNum det /* In initializing the Shifter, the lowest gear is going to be '-1' if a pin * exists for reverse, otherwise it's going to be '0' (neutral). */ - Shifter(pinRev != UnusedPin ? -1 : 0, 6), + Shifter(sanitizePin(pinRev) != UnusedPin ? -1 : 0, 6), /* Two axes, X and Y */ analogAxis{ AnalogInput(pinX), AnalogInput(pinY) }, - PinReverse(pinRev), + PinReverse(sanitizePin(pinRev)), detector(detectPin, false) // not inverted {} void AnalogShifter::begin() { - pinMode(PinReverse, INPUT); + if (this->PinReverse != UnusedPin) { + pinMode(PinReverse, INPUT); + } update(); // set initial gear position } From 1da66198c2bf9dbfc9e120b6cac583fb5836731f Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 6 Jun 2024 15:12:33 -0400 Subject: [PATCH 4/4] Clean up pin arguments and definitions 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. --- src/SimRacing.cpp | 34 ++++++++++++++--------------- src/SimRacing.h | 54 +++++++++++++++++++++++------------------------ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/SimRacing.cpp b/src/SimRacing.cpp index 9ae8672..e3472c4 100644 --- a/src/SimRacing.cpp +++ b/src/SimRacing.cpp @@ -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 */ @@ -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 @@ -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 } } @@ -265,9 +265,9 @@ 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; } //######################################################### @@ -275,20 +275,20 @@ bool DeviceConnection::readPin() const { //######################################################### -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) { @@ -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 } @@ -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( diff --git a/src/SimRacing.h b/src/SimRacing.h index 55728a6..b1cea20 100644 --- a/src/SimRacing.h +++ b/src/SimRacing.h @@ -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()) }; @@ -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 @@ -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 }; @@ -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 @@ -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 @@ -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. @@ -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 }; @@ -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. @@ -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); }; /** @@ -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); }; /** @@ -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); };