2929
3030namespace SimRacing {
3131
32+ /* *
33+ * Take a pin number as an input and sanitize it to a known working value
34+ *
35+ * In an ideal world this would check against the available pins on the micro,
36+ * but as far as I know the Arduino API does not have a "valid pin" function.
37+ * Instead, we'll just accept any positive number as a pin and reject any
38+ * negative number as invalid ("Unused").
39+ *
40+ * @param pin the pin number to sanitize
41+ * @returns the pin number, or UnusedPin
42+ */
43+ static constexpr PinNum sanitizePin (PinNum pin) {
44+ return pin < 0 ? UnusedPin : pin;
45+ }
46+
3247
3348/* *
3449* Invert an input value so it's at the same relative position
@@ -161,9 +176,9 @@ static void readFloat(float& value, Stream& client) {
161176// DeviceConnection #
162177// #########################################################
163178
164- DeviceConnection::DeviceConnection (uint8_t pin, bool invert, unsigned long detectTime)
179+ DeviceConnection::DeviceConnection (PinNum pin, bool invert, unsigned long detectTime)
165180 :
166- Pin ( pin), Inverted (invert), stablePeriod(detectTime), // constants(ish)
181+ pin (sanitizePin( pin)), inverted (invert), stablePeriod(detectTime), // constants(ish)
167182
168183 /* Assume we're connected on first call
169184 */
@@ -177,7 +192,7 @@ DeviceConnection::DeviceConnection(uint8_t pin, bool invert, unsigned long detec
177192 * the device to be read as connected as soon as the board turns on, without
178193 * having to wait an arbitrary amount.
179194 */
180- pinState (!Inverted ),
195+ pinState (!inverted ),
181196
182197 /* Set the last pin change to right now minus the stable period so it's
183198 * read as being already stable. Again, this will make the class return
@@ -186,7 +201,9 @@ DeviceConnection::DeviceConnection(uint8_t pin, bool invert, unsigned long detec
186201 lastChange (millis() - detectTime)
187202
188203{
189- pinMode (Pin, INPUT); // set pin as input, *no* pull-up
204+ if (pin != UnusedPin) {
205+ pinMode (pin, INPUT); // set pin as input, *no* pull-up
206+ }
190207}
191208
192209void DeviceConnection::poll () {
@@ -248,30 +265,30 @@ void DeviceConnection::setStablePeriod(unsigned long t) {
248265}
249266
250267bool DeviceConnection::readPin () const {
251- if (Pin == NOT_A_PIN ) return HIGH; // if no pin is set, we're always connected
252- const bool state = digitalRead (Pin );
253- return Inverted ? !state : state;
268+ if (pin == UnusedPin ) return HIGH; // if no pin is set, we're always connected
269+ const bool state = digitalRead (pin );
270+ return inverted ? !state : state;
254271}
255272
256273// #########################################################
257274// AnalogInput #
258275// #########################################################
259276
260277
261- AnalogInput::AnalogInput (uint8_t p )
262- : Pin(p ), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max})
278+ AnalogInput::AnalogInput (PinNum pin )
279+ : pin(sanitizePin(pin) ), position(AnalogInput::Min), cal({AnalogInput::Min, AnalogInput::Max})
263280{
264- if (Pin != NOT_A_PIN ) {
265- pinMode (Pin , INPUT);
281+ if (pin != UnusedPin ) {
282+ pinMode (pin , INPUT);
266283 }
267284}
268285
269286bool AnalogInput::read () {
270287 bool changed = false ;
271288
272- if (Pin != NOT_A_PIN ) {
289+ if (pin != UnusedPin ) {
273290 const int previous = this ->position ;
274- this ->position = analogRead (Pin );
291+ this ->position = analogRead (pin );
275292
276293 // check if value is different for 'changed' flag
277294 if (previous != this ->position ) {
@@ -333,7 +350,7 @@ void AnalogInput::setCalibration(AnalogInput::Calibration newCal) {
333350// Pedals #
334351// #########################################################
335352
336- Pedals::Pedals (AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin)
353+ Pedals::Pedals (AnalogInput* dataPtr, uint8_t nPedals, PinNum detectPin)
337354 :
338355 pedalData (dataPtr),
339356 NumPedals (nPedals),
@@ -538,7 +555,7 @@ void Pedals::serialCalibration(Stream& iface) {
538555}
539556
540557
541- TwoPedals::TwoPedals (uint8_t gasPin, uint8_t brakePin, uint8_t detectPin)
558+ TwoPedals::TwoPedals (PinNum gasPin, PinNum brakePin, PinNum detectPin)
542559 : Pedals(pedalData, NumPedals, detectPin),
543560 pedalData{ AnalogInput (gasPin), AnalogInput (brakePin) }
544561{}
@@ -549,7 +566,7 @@ void TwoPedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Cal
549566}
550567
551568
552- ThreePedals::ThreePedals (uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin)
569+ ThreePedals::ThreePedals (PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin)
553570 : Pedals(pedalData, NumPedals, detectPin),
554571 pedalData{ AnalogInput (gasPin), AnalogInput (brakePin), AnalogInput (clutchPin) }
555572{}
@@ -562,7 +579,7 @@ void ThreePedals::setCalibration(AnalogInput::Calibration gasCal, AnalogInput::C
562579
563580
564581
565- LogitechPedals::LogitechPedals (uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin)
582+ LogitechPedals::LogitechPedals (PinNum gasPin, PinNum brakePin, PinNum clutchPin, PinNum detectPin)
566583 : ThreePedals(gasPin, brakePin, clutchPin, detectPin)
567584{
568585 // taken from calibrating my own pedals. the springs are pretty stiff so while
@@ -571,7 +588,7 @@ LogitechPedals::LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchP
571588 this ->setCalibration ({ 904 , 48 }, { 944 , 286 }, { 881 , 59 });
572589}
573590
574- LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals (uint8_t gasPin, uint8_t brakePin, uint8_t detectPin)
591+ LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals (PinNum gasPin, PinNum brakePin, PinNum detectPin)
575592 : TwoPedals(gasPin, brakePin, detectPin)
576593{
577594 this ->setCalibration ({ 646 , 0 }, { 473 , 1023 }); // taken from calibrating my own pedals
@@ -657,22 +674,24 @@ const float AnalogShifter::CalEngagementPoint = 0.70;
657674const float AnalogShifter::CalReleasePoint = 0.50 ;
658675const float AnalogShifter::CalEdgeOffset = 0.60 ;
659676
660- AnalogShifter::AnalogShifter (uint8_t pinX, uint8_t pinY, uint8_t pinRev, uint8_t detectPin)
677+ AnalogShifter::AnalogShifter (PinNum pinX, PinNum pinY, PinNum pinRev, PinNum detectPin)
661678 :
662679 /* In initializing the Shifter, the lowest gear is going to be '-1' if a pin
663680 * exists for reverse, otherwise it's going to be '0' (neutral).
664681 */
665- Shifter (pinRev != NOT_A_PIN ? -1 : 0 , 6 ),
682+ Shifter (sanitizePin( pinRev) != UnusedPin ? -1 : 0 , 6 ),
666683
667684 /* Two axes, X and Y */
668685 analogAxis{ AnalogInput (pinX), AnalogInput (pinY) },
669686
670- PinReverse ( pinRev),
687+ pinReverse (sanitizePin( pinRev) ),
671688 detector (detectPin, false ) // not inverted
672689{}
673690
674691void AnalogShifter::begin () {
675- pinMode (PinReverse, INPUT);
692+ if (this ->pinReverse != UnusedPin) {
693+ pinMode (pinReverse, INPUT);
694+ }
676695 update (); // set initial gear position
677696}
678697
@@ -777,10 +796,10 @@ int AnalogShifter::getPositionRaw(Axis ax) const {
777796bool AnalogShifter::getReverseButton () const {
778797 // if the reverse pin is not set *or* if the device is not currently
779798 // connected, avoid reading the floating input and just return 'false'
780- if (PinReverse == NOT_A_PIN || detector.getState () != DeviceConnection::Connected) {
799+ if (pinReverse == UnusedPin || detector.getState () != DeviceConnection::Connected) {
781800 return false ;
782801 }
783- return digitalRead (PinReverse );
802+ return digitalRead (pinReverse );
784803}
785804
786805void AnalogShifter::setCalibration (
@@ -977,7 +996,7 @@ void AnalogShifter::serialCalibration(Stream& iface) {
977996 iface.println (F (" \n\n Calibration complete! :)\n " ));
978997}
979998
980- LogitechShifter::LogitechShifter (uint8_t pinX, uint8_t pinY, uint8_t pinRev, uint8_t detectPin)
999+ LogitechShifter::LogitechShifter (PinNum pinX, PinNum pinY, PinNum pinRev, PinNum detectPin)
9811000 : AnalogShifter(pinX, pinY, pinRev, detectPin)
9821001{
9831002 this ->setCalibration ({ 490 , 440 }, { 253 , 799 }, { 262 , 86 }, { 460 , 826 }, { 470 , 76 }, { 664 , 841 }, { 677 , 77 });
@@ -987,7 +1006,7 @@ LogitechShifter::LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev, uin
9871006// Handbrake #
9881007// #########################################################
9891008
990- Handbrake::Handbrake (uint8_t pinAx, uint8_t detectPin)
1009+ Handbrake::Handbrake (PinNum pinAx, PinNum detectPin)
9911010 :
9921011 analogAxis (pinAx),
9931012 detector (detectPin),
0 commit comments