Arduino Sim Racing Library v1.1.4
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 enum Axis : uint8_t {
38 X = 0,
39 Y = 1,
40 };
41
42
48 public:
62
71 DeviceConnection(uint8_t pin, bool invert = false, unsigned long detectTime = 250);
72
78 void poll();
79
86
93 bool isConnected() const;
94
101 void setStablePeriod(unsigned long t);
102
103 private:
109 bool readPin() const;
110
111 const uint8_t Pin;
112 const bool Inverted;
113 unsigned long stablePeriod;
114
115 ConnectionState state;
116 bool pinState;
117 unsigned long lastChange;
118 };
119
120
125 public:
126 static const int Min = 0;
127 static const int Max = 1023;
128
134 AnalogInput(uint8_t p);
135
141 virtual bool read();
142
155 long getPosition(long rMin = Min, long rMax = Max) const;
156
162 int getPositionRaw() const;
163
169 int getMin() const { return this->cal.min; }
170
176 int getMax() const { return this->cal.max; }
177
183 bool isInverted() const;
184
193 void setPosition(int newPos);
194
202 void setInverted(bool invert = true);
203
215 struct Calibration {
216 int min;
217 int max;
218 };
219
225 void setCalibration(Calibration newCal);
226
227 private:
228 const uint8_t Pin = NOT_A_PIN;
229 int position;
230 Calibration cal;
231 };
232
233
238 public:
242 virtual void begin() {};
243
249 virtual bool update() = 0;
250
252 virtual bool isConnected() const { return true; }
253 };
254
255
265 enum Pedal {
266 Gas = 0,
267 Accelerator = Gas,
268 Throttle = Gas,
269 Brake = 1,
270 Clutch = 2,
271 };
272
276 class Pedals : public Peripheral {
277 public:
280
288 Pedals(AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin);
289
291 virtual void begin();
292
294 virtual bool update();
295
308 long getPosition(PedalID pedal, long rMin = 0, long rMax = 100) const;
309
317 int getPositionRaw(PedalID pedal) const;
318
325 bool hasPedal(PedalID pedal) const;
326
332 int getNumPedals() const { return this->NumPedals; }
333
339 bool positionChanged() const { return changed; }
340
348
355 void serialCalibration(Stream& iface = Serial);
356
358 bool isConnected() const { return detector.isConnected(); }
359
366 static String getPedalName(PedalID pedal);
367
368 private:
369 AnalogInput* pedalData;
370 const int NumPedals;
371 DeviceConnection detector;
372 bool changed;
373 };
374
375
379 class TwoPedals : public Pedals {
380 public:
388 TwoPedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin = NOT_A_PIN);
389
397
398 private:
399 static const uint8_t NumPedals = 2;
400 AnalogInput pedalData[NumPedals];
401 };
402
403
407 class ThreePedals : public Pedals {
408 public:
417 ThreePedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin = NOT_A_PIN);
418
427
428 private:
429 static const uint8_t NumPedals = 3;
430 AnalogInput pedalData[NumPedals];
431 };
432
434
435
445 class Shifter : public Peripheral {
446 public:
453 Shifter(int8_t min, int8_t max);
454
463 int8_t getGear() const { return currentGear; }
464
473 static char getGearChar(int gear);
474
481 char getGearChar() const;
482
492 static String getGearString(int gear);
493
500 String getGearString() const;
501
507 bool gearChanged() const { return changed; }
508
514 int8_t getGearMin() { return MinGear; }
515
521 int8_t getGearMax() { return MaxGear; }
522
523 protected:
524 const int8_t MinGear;
525 const int8_t MaxGear;
526
527 int8_t currentGear;
528 bool changed;
529 };
530
531
535 class AnalogShifter : public Shifter {
536 public:
545 AnalogShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev = NOT_A_PIN, uint8_t detectPin = NOT_A_PIN);
546
552 virtual void begin();
553
559 virtual bool update();
560
564 long getPosition(Axis ax, long rMin = AnalogInput::Min, long rMax = AnalogInput::Max) const;
565
569 int getPositionRaw(Axis ax) const;
570
578 bool getReverseButton() const;
579
584 int x;
585 int y;
586 };
587
615 void setCalibration(
616 GearPosition neutral,
618 float engagePoint = CalEngagementPoint, float releasePoint = CalReleasePoint, float edgeOffset = CalEdgeOffset);
619
626 void serialCalibration(Stream& iface = Serial);
627
629 bool isConnected() const { return detector.isConnected(); }
630
631 private:
637 static const float CalEngagementPoint;
638
644 static const float CalReleasePoint;
645
651 static const float CalEdgeOffset;
652
653 /*** Internal calibration struct */
654 struct Calibration {
655 int neutralX;
656 int neutralY;
657 int oddTrigger;
658 int oddRelease;
659 int evenTrigger;
660 int evenRelease;
661 int leftEdge;
662 int rightEdge;
663 } calibration;
664
665 AnalogInput analogAxis[2];
666 const uint8_t PinReverse;
667 DeviceConnection detector;
668 };
669
671
672
676 class Handbrake : public Peripheral {
677 public:
684 Handbrake(uint8_t pinAx, uint8_t detectPin = NOT_A_PIN);
685
689 virtual void begin();
690
696 virtual bool update();
697
709 long getPosition(long rMin = 0, long rMax = 100) const;
710
717 int getPositionRaw() const;
718
724 bool positionChanged() const { return changed; }
725
728
730 void serialCalibration(Stream& iface = Serial);
731
733 bool isConnected() const { return detector.isConnected(); }
734
735 private:
736 AnalogInput analogAxis;
737 DeviceConnection detector;
738 bool changed;
739 };
740
741
749 public:
751 LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin = NOT_A_PIN);
752 };
753
764 public:
766 LogitechDrivingForceGT_Pedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin = NOT_A_PIN);
767 };
768
776 public:
778 LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev = NOT_A_PIN, uint8_t detectPin = NOT_A_PIN);
779 };
780
781
782#if defined(__AVR_ATmega32U4__) || defined(SIM_RACING_DOXYGEN)
799 #define SHIFTER_SHIELD_V1_PINS A1, A0, 14, A2
800
817 #define PEDAL_SHIELD_V1_PINS A2, A1, A0, 10
818#endif
819
820} // end SimRacing namespace
821
822#endif
Axis
Enumeration for analog axis names, mapped to integers.
Definition SimRacing.h:37
@ X
Cartesian X axis.
Definition SimRacing.h:38
@ Y
Cartesian Y axis.
Definition SimRacing.h:39
Handle I/O for analog (ADC) inputs.
Definition SimRacing.h:124
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:127
int getMax() const
Retrieves the calibrated maximum position.
Definition SimRacing.h:176
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:169
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:126
Interface with shifters using two potentiometers for gear position.
Definition SimRacing.h:535
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.
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:629
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:47
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:56
@ Unplug
Device was just removed (connection ends)
Definition SimRacing.h:60
@ PlugIn
Device was just plugged in (connection starts), unstable.
Definition SimRacing.h:58
@ Disconnected
No connection present.
Definition SimRacing.h:57
@ Connected
Connection present and stable.
Definition SimRacing.h:59
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:676
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:724
virtual bool update()
Polls the handbrake to update its position.
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:733
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:763
Interface with the Logitech pedals (Gas, Brake, and Clutch)
Definition SimRacing.h:748
Interface with the Logitech Driving Force shifter.
Definition SimRacing.h:775
Base class for all pedals instances.
Definition SimRacing.h:276
SimRacing::Pedal PedalID
Scoped alias for SimRacing::Pedal.
Definition SimRacing.h:279
bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:358
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:339
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:332
Abstract class for all peripherals.
Definition SimRacing.h:237
virtual bool isConnected() const
Check if the device is physically connected to the board.
Definition SimRacing.h:252
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:242
Base class for all shifter instances.
Definition SimRacing.h:445
String getGearString() const
Returns a String that represents the current gear.
int8_t getGearMin()
Retrieves the minimum possible gear index.
Definition SimRacing.h:514
const int8_t MaxGear
the highest selectable gear
Definition SimRacing.h:525
int8_t getGear() const
Returns the currently selected gear.
Definition SimRacing.h:463
char getGearChar() const
Returns a character that represents the current gear.
int8_t getGearMax()
Retrieves the maximum possible gear index.
Definition SimRacing.h:521
bool changed
whether the gear has changed since the previous update
Definition SimRacing.h:528
int8_t currentGear
index of the current gear
Definition SimRacing.h:527
const int8_t MinGear
the lowest selectable gear
Definition SimRacing.h:524
bool gearChanged() const
Checks whether the current gear has changed since the last update.
Definition SimRacing.h:507
Pedal implementation for devices with gas, brake, and clutch.
Definition SimRacing.h:407
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal, AnalogInput::Calibration clutchCal)
Sets the calibration data (min/max) for the pedals.
Pedal implementation for devices with only gas and brake.
Definition SimRacing.h:379
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal)
Sets the calibration data (min/max) for the pedals.
Pedal
Pedal ID names.
Definition SimRacing.h:265
Simple struct containing min/max values for axis calibration.
Definition SimRacing.h:215
int max
Maximum value of the analog axis.
Definition SimRacing.h:217
int min
Minimum value of the analog axis.
Definition SimRacing.h:216
Simple struct to store X/Y coordinates for the calibration function.
Definition SimRacing.h:583
int y
Y coordinate of the gear position from the ADC.
Definition SimRacing.h:585
int x
X coordinate of the gear position from the ADC.
Definition SimRacing.h:584