Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
SimRacing.h
Go to the documentation of this file.
1/*
2 * Project Sim Racing Library for Arduino
3 * @author David Madison
4 * @link github.com/dmadison/Sim-Racing-Arduino
5 * @license LGPLv3 - Copyright (c) 2022 David Madison
6 *
7 * This file is part of the Sim Racing Library for Arduino.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#ifndef SIM_RACING_LIB_H
24#define SIM_RACING_LIB_H
25
26#include <Arduino.h>
27
33namespace SimRacing {
37 using PinNum = int16_t;
38
43 const PinNum UnusedPin = -1;
44
45
49 enum Axis : uint8_t {
50 X = 0,
51 Y = 1,
52 };
53
54
60 public:
74
83 DeviceConnection(PinNum pin, bool invert = false, unsigned long detectTime = 250);
84
90 void poll();
91
98
105 bool isConnected() const;
106
113 void setStablePeriod(unsigned long t);
114
115 private:
121 bool readPin() const;
122
123 PinNum pin;
124 bool inverted;
125 unsigned long stablePeriod;
126
127 ConnectionState state;
128 bool pinState;
129 unsigned long lastChange;
130 };
131
132
137 public:
138 static const int Min = 0;
139 static const int Max = 1023;
140
146 AnalogInput(PinNum pin);
147
153 virtual bool read();
154
167 long getPosition(long rMin = Min, long rMax = Max) const;
168
174 int getPositionRaw() const;
175
181 int getMin() const { return this->cal.min; }
182
188 int getMax() const { return this->cal.max; }
189
195 bool isInverted() const;
196
205 void setPosition(int newPos);
206
214 void setInverted(bool invert = true);
215
227 struct Calibration {
228 int min;
229 int max;
230 };
231
237 void setCalibration(Calibration newCal);
238
239 private:
240 PinNum pin;
241 int position;
242 Calibration cal;
243 };
244
245
250 public:
254 virtual void begin() {};
255
261 virtual bool update() = 0;
262
264 virtual bool isConnected() const { return true; }
265 };
266
267
277 enum Pedal {
278 Gas = 0,
279 Accelerator = Gas,
280 Throttle = Gas,
281 Brake = 1,
282 Clutch = 2,
283 };
284
288 class Pedals : public Peripheral {
289 public:
292
300 Pedals(AnalogInput* dataPtr, uint8_t nPedals, PinNum detectPin);
301
303 virtual void begin();
304
306 virtual bool update();
307
320 long getPosition(PedalID pedal, long rMin = 0, long rMax = 100) const;
321
329 int getPositionRaw(PedalID pedal) const;
330
337 bool hasPedal(PedalID pedal) const;
338
344 int getNumPedals() const { return this->NumPedals; }
345
351 bool positionChanged() const { return changed; }
352
360
367 void serialCalibration(Stream& iface = Serial);
368
370 bool isConnected() const { return detector.isConnected(); }
371
378 static String getPedalName(PedalID pedal);
379
380 private:
381 AnalogInput* pedalData;
382 const int NumPedals;
383 DeviceConnection detector;
384 bool changed;
385 };
386
387
391 class TwoPedals : public Pedals {
392 public:
400 TwoPedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect = UnusedPin);
401
409
410 private:
411 static const uint8_t NumPedals = 2;
412 AnalogInput pedalData[NumPedals];
413 };
414
415
419 class ThreePedals : public Pedals {
420 public:
429 ThreePedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect = UnusedPin);
430
439
440 private:
441 static const uint8_t NumPedals = 3;
442 AnalogInput pedalData[NumPedals];
443 };
444
446
447
457 class Shifter : public Peripheral {
458 public:
465 Shifter(int8_t min, int8_t max);
466
475 int8_t getGear() const { return currentGear; }
476
485 static char getGearChar(int gear);
486
493 char getGearChar() const;
494
504 static String getGearString(int gear);
505
512 String getGearString() const;
513
519 bool gearChanged() const { return changed; }
520
526 int8_t getGearMin() { return MinGear; }
527
533 int8_t getGearMax() { return MaxGear; }
534
535 protected:
536 const int8_t MinGear;
537 const int8_t MaxGear;
538
539 int8_t currentGear;
540 bool changed;
541 };
542
543
547 class AnalogShifter : public Shifter {
548 public:
557 AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum pinDetect = UnusedPin);
558
564 virtual void begin();
565
571 virtual bool update();
572
576 long getPosition(Axis ax, long rMin = AnalogInput::Min, long rMax = AnalogInput::Max) const;
577
581 int getPositionRaw(Axis ax) const;
582
590 bool getReverseButton() const;
591
596 int x;
597 int y;
598 };
599
627 void setCalibration(
628 GearPosition neutral,
630 float engagePoint = CalEngagementPoint, float releasePoint = CalReleasePoint, float edgeOffset = CalEdgeOffset);
631
638 void serialCalibration(Stream& iface = Serial);
639
641 bool isConnected() const { return detector.isConnected(); }
642
643 private:
649 static const float CalEngagementPoint;
650
656 static const float CalReleasePoint;
657
663 static const float CalEdgeOffset;
664
665 /*** Internal calibration struct */
666 struct Calibration {
667 int neutralX;
668 int neutralY;
669 int oddTrigger;
670 int oddRelease;
671 int evenTrigger;
672 int evenRelease;
673 int leftEdge;
674 int rightEdge;
675 } calibration;
676
677 AnalogInput analogAxis[2];
678 PinNum pinReverse;
679 DeviceConnection detector;
680 };
681
683
684
688 class Handbrake : public Peripheral {
689 public:
696 Handbrake(PinNum pinAx, PinNum pinDetect = UnusedPin);
697
701 virtual void begin();
702
708 virtual bool update();
709
721 long getPosition(long rMin = 0, long rMax = 100) const;
722
729 int getPositionRaw() const;
730
736 bool positionChanged() const { return changed; }
737
740
742 void serialCalibration(Stream& iface = Serial);
743
745 bool isConnected() const { return detector.isConnected(); }
746
747 private:
748 AnalogInput analogAxis;
749 DeviceConnection detector;
750 bool changed;
751 };
752
753
761 public:
763 LogitechPedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect = UnusedPin);
764 };
765
776 public:
778 LogitechDrivingForceGT_Pedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect = UnusedPin);
779 };
780
788 public:
790 LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev = UnusedPin, PinNum pinDetect = UnusedPin);
791 };
792
793
794#if defined(__AVR_ATmega32U4__) || defined(SIM_RACING_DOXYGEN)
811 #define SHIFTER_SHIELD_V1_PINS A1, A0, 14, A2
812
829 #define PEDAL_SHIELD_V1_PINS A2, A1, A0, 10
830#endif
831
832} // end SimRacing namespace
833
834#endif
Axis
Enumeration for analog axis names, mapped to integers.
Definition SimRacing.h:49
@ X
Cartesian X axis.
Definition SimRacing.h:50
@ Y
Cartesian Y axis.
Definition SimRacing.h:51
const PinNum UnusedPin
Dummy pin number signaling that a pin is unused and can be safely ignored.
Definition SimRacing.h:43
int16_t PinNum
Type alias for pin numbers, using Arduino numbering.
Definition SimRacing.h:37
Handle I/O for analog (ADC) inputs.
Definition SimRacing.h:136
void setInverted(bool invert=true)
Set the 'inverted' state of the axis.
bool isInverted() const
Check whether the axis is inverted or not.
long getPosition(long rMin=Min, long rMax=Max) const
Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibratio...
int getPositionRaw() const
Retrieves the buffered position for the analog axis.
static const int Max
Maximum value of the analog to digital (ADC) converter. 10-bit by default.
Definition SimRacing.h:139
int getMax() const
Retrieves the calibrated maximum position.
Definition SimRacing.h:188
void setCalibration(Calibration newCal)
Calibrate the axis' min/max values for rescaling.
virtual bool read()
Updates the current value of the axis by polling the ADC.
int getMin() const
Retrieves the calibrated minimum position.
Definition SimRacing.h:181
void setPosition(int newPos)
Override the current position with a custom value.
static const int Min
Minimum value of the analog to digital (ADC) converter.
Definition SimRacing.h:138
AnalogInput(PinNum pin)
Class constructor.
Interface with shifters using two potentiometers for gear position.
Definition SimRacing.h:547
void setCalibration(GearPosition neutral, GearPosition g1, GearPosition g2, GearPosition g3, GearPosition g4, GearPosition g5, GearPosition g6, float engagePoint=CalEngagementPoint, float releasePoint=CalReleasePoint, float edgeOffset=CalEdgeOffset)
Calibrate the gear shifter for more accurate shifting.
void serialCalibration(Stream &iface=Serial)
Runs an interactive calibration tool using the serial interface.
bool getReverseButton() const
Checks the current state of the "reverse" button at the bottom of the shift column.
int getPositionRaw(Axis ax) const
Retrieves the buffered position for the analog axis.
virtual bool update()
Polls the hardware to update the current gear state.
AnalogShifter(PinNum pinX, PinNum pinY, PinNum pinRev=UnusedPin, PinNum pinDetect=UnusedPin)
Class constructor.
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:641
virtual void begin()
Initializes the hardware pins for reading the gear states.
long getPosition(Axis ax, long rMin=AnalogInput::Min, long rMax=AnalogInput::Max) const
Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibratio...
Used for tracking whether a device is connected to a specific pin and stable.
Definition SimRacing.h:59
void poll()
Checks if the pin detects a connection.
void setStablePeriod(unsigned long t)
Allows the user to change the stable period of the detector.
ConnectionState getState() const
Retrieves the current ConnectionState from the instance.
ConnectionState
The state of the connection, whether it is connected, disconnected, and everywhere in-between.
Definition SimRacing.h:68
@ Unplug
Device was just removed (connection ends)
Definition SimRacing.h:72
@ PlugIn
Device was just plugged in (connection starts), unstable.
Definition SimRacing.h:70
@ Disconnected
No connection present.
Definition SimRacing.h:69
@ Connected
Connection present and stable.
Definition SimRacing.h:71
DeviceConnection(PinNum pin, bool invert=false, unsigned long detectTime=250)
Class constructor.
bool isConnected() const
Check if the device is physically connected to the board.
Interface with analog handbrakes that use hall effect sensors.
Definition SimRacing.h:688
virtual void begin()
Initializes the pin for reading from the handbrake.
long getPosition(long rMin=0, long rMax=100) const
Retrieves the buffered position for the handbrake axis, rescaled to a nominal range using the calibra...
void setCalibration(AnalogInput::Calibration newCal)
Calibrate the axis' min/max values for rescaling.
int getPositionRaw() const
Retrieves the buffered position for the handbrake, ignoring the calibration data.
bool positionChanged() const
Checks whether the handbrake's position has changed since the last update.
Definition SimRacing.h:736
virtual bool update()
Polls the handbrake to update its position.
Handbrake(PinNum pinAx, PinNum pinDetect=UnusedPin)
Class constructor.
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:745
void serialCalibration(Stream &iface=Serial)
Runs an interactive calibration tool using the serial interface.
Interface with the Logitech Driving Force GT pedals (Gas + Brake)
Definition SimRacing.h:775
LogitechDrivingForceGT_Pedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect=UnusedPin)
Class constructor.
Interface with the Logitech pedals (Gas, Brake, and Clutch)
Definition SimRacing.h:760
LogitechPedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect=UnusedPin)
Class constructor.
Interface with the Logitech Driving Force shifter.
Definition SimRacing.h:787
LogitechShifter(PinNum pinX, PinNum pinY, PinNum pinRev=UnusedPin, PinNum pinDetect=UnusedPin)
Class constructor.
Base class for all pedals instances.
Definition SimRacing.h:288
SimRacing::Pedal PedalID
Scoped alias for SimRacing::Pedal.
Definition SimRacing.h:291
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:370
Pedals(AnalogInput *dataPtr, uint8_t nPedals, PinNum detectPin)
Class constructor.
void serialCalibration(Stream &iface=Serial)
Runs an interactive calibration tool using the serial interface.
void setCalibration(PedalID pedal, AnalogInput::Calibration cal)
Calibrate a pedal's min/max values for rescaling.
static String getPedalName(PedalID pedal)
Utility function to get the string name for each pedal.
bool hasPedal(PedalID pedal) const
Checks if a given pedal is present in the class.
bool positionChanged() const
Checks whether the current pedal positions have changed since the last update.
Definition SimRacing.h:351
int getPositionRaw(PedalID pedal) const
Retrieves the buffered position for the pedal, ignoring the calibration data.
virtual bool update()
Perform a poll of the hardware to refresh the class state.
virtual void begin()
Initialize the hardware (if necessary)
long getPosition(PedalID pedal, long rMin=0, long rMax=100) const
Retrieves the buffered position for the pedal, rescaled to a nominal range using the calibration valu...
int getNumPedals() const
Retrieves the number of pedals handled by the class.
Definition SimRacing.h:344
Abstract class for all peripherals.
Definition SimRacing.h:249
virtual bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:264
virtual bool update()=0
Perform a poll of the hardware to refresh the class state.
virtual void begin()
Initialize the hardware (if necessary)
Definition SimRacing.h:254
Base class for all shifter instances.
Definition SimRacing.h:457
String getGearString() const
Returns a String that represents the current gear.
int8_t getGearMin()
Retrieves the minimum possible gear index.
Definition SimRacing.h:526
const int8_t MaxGear
the highest selectable gear
Definition SimRacing.h:537
int8_t getGear() const
Returns the currently selected gear.
Definition SimRacing.h:475
Shifter(int8_t min, int8_t max)
Class constructor.
char getGearChar() const
Returns a character that represents the current gear.
int8_t getGearMax()
Retrieves the maximum possible gear index.
Definition SimRacing.h:533
bool changed
whether the gear has changed since the previous update
Definition SimRacing.h:540
int8_t currentGear
index of the current gear
Definition SimRacing.h:539
const int8_t MinGear
the lowest selectable gear
Definition SimRacing.h:536
bool gearChanged() const
Checks whether the current gear has changed since the last update.
Definition SimRacing.h:519
Pedal implementation for devices with gas, brake, and clutch.
Definition SimRacing.h:419
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal, AnalogInput::Calibration clutchCal)
Sets the calibration data (min/max) for the pedals.
ThreePedals(PinNum pinGas, PinNum pinBrake, PinNum pinClutch, PinNum pinDetect=UnusedPin)
Class constructor.
Pedal implementation for devices with only gas and brake.
Definition SimRacing.h:391
TwoPedals(PinNum pinGas, PinNum pinBrake, PinNum pinDetect=UnusedPin)
Class constructor.
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal)
Sets the calibration data (min/max) for the pedals.
Pedal
Pedal ID names.
Definition SimRacing.h:277
Simple struct containing min/max values for axis calibration.
Definition SimRacing.h:227
int max
Maximum value of the analog axis.
Definition SimRacing.h:229
int min
Minimum value of the analog axis.
Definition SimRacing.h:228
Simple struct to store X/Y coordinates for the calibration function.
Definition SimRacing.h:595
int y
Y coordinate of the gear position from the ADC.
Definition SimRacing.h:597
int x
X coordinate of the gear position from the ADC.
Definition SimRacing.h:596