Arduino Sim Racing Library v1.1.3
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:
61 };
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
@ 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)
Definition: SimRacing.cpp:319
bool isInverted() const
Definition: SimRacing.cpp:311
long getPosition(long rMin=Min, long rMax=Max) const
Definition: SimRacing.cpp:302
int getPositionRaw() const
Definition: SimRacing.cpp:307
static const int Max
Maximum value of the analog to digital (ADC) converter. 10-bit by default.
Definition: SimRacing.h:127
int getMax() const
Definition: SimRacing.h:176
void setCalibration(Calibration newCal)
Definition: SimRacing.cpp:327
virtual bool read()
Definition: SimRacing.cpp:269
int getMin() const
Definition: SimRacing.h:169
void setPosition(int newPos)
Definition: SimRacing.cpp:315
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)
Definition: SimRacing.cpp:786
void serialCalibration(Stream &iface=Serial)
Definition: SimRacing.cpp:867
bool getReverseButton() const
Definition: SimRacing.cpp:777
AnalogShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev=NOT_A_PIN, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:660
int getPositionRaw(Axis ax) const
Definition: SimRacing.cpp:772
virtual bool update()
Definition: SimRacing.cpp:679
bool isConnected() const
Definition: SimRacing.h:629
virtual void begin()
Definition: SimRacing.cpp:674
long getPosition(Axis ax, long rMin=AnalogInput::Min, long rMax=AnalogInput::Max) const
Definition: SimRacing.cpp:767
Used for tracking whether a device is connected to a specific pin and stable.
Definition: SimRacing.h:47
void setStablePeriod(unsigned long t)
Definition: SimRacing.cpp:236
DeviceConnection(uint8_t pin, bool invert=false, unsigned long detectTime=250)
Definition: SimRacing.cpp:164
ConnectionState getState() const
Definition: SimRacing.cpp:228
@ 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
Interface with analog handbrakes that use hall effect sensors.
Definition: SimRacing.h:676
virtual void begin()
Definition: SimRacing.cpp:997
long getPosition(long rMin=0, long rMax=100) const
Definition: SimRacing.cpp:1016
void setCalibration(AnalogInput::Calibration newCal)
Definition: SimRacing.cpp:1024
Handbrake(uint8_t pinAx, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:990
int getPositionRaw() const
Definition: SimRacing.cpp:1020
bool positionChanged() const
Definition: SimRacing.h:724
virtual bool update()
Definition: SimRacing.cpp:1001
bool isConnected() const
Definition: SimRacing.h:733
void serialCalibration(Stream &iface=Serial)
Definition: SimRacing.cpp:1029
Interface with the Logitech Driving Force GT pedals (Gas + Brake)
Definition: SimRacing.h:763
LogitechDrivingForceGT_Pedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:574
Interface with the Logitech pedals (Gas, Brake, and Clutch)
Definition: SimRacing.h:748
LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:565
Interface with the Logitech Driving Force shifter.
Definition: SimRacing.h:775
LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev=NOT_A_PIN, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:980
Base class for all pedals instances.
Definition: SimRacing.h:276
SimRacing::Pedal PedalID
Definition: SimRacing.h:279
bool isConnected() const
Definition: SimRacing.h:358
void serialCalibration(Stream &iface=Serial)
Definition: SimRacing.cpp:410
void setCalibration(PedalID pedal, AnalogInput::Calibration cal)
Definition: SimRacing.cpp:384
Pedals(AnalogInput *dataPtr, uint8_t nPedals, uint8_t detectPin)
Definition: SimRacing.cpp:336
static String getPedalName(PedalID pedal)
Definition: SimRacing.cpp:390
bool hasPedal(PedalID pedal) const
Definition: SimRacing.cpp:380
bool positionChanged() const
Definition: SimRacing.h:339
int getPositionRaw(PedalID pedal) const
Definition: SimRacing.cpp:375
virtual bool update()
Definition: SimRacing.cpp:348
virtual void begin()
Definition: SimRacing.cpp:344
long getPosition(PedalID pedal, long rMin=0, long rMax=100) const
Definition: SimRacing.cpp:370
int getNumPedals() const
Definition: SimRacing.h:332
Abstract class for all peripherals.
Definition: SimRacing.h:237
virtual bool isConnected() const
Definition: SimRacing.h:252
virtual bool update()=0
virtual void begin()
Definition: SimRacing.h:242
Base class for all shifter instances.
Definition: SimRacing.h:445
String getGearString() const
Definition: SimRacing.cpp:648
int8_t getGearMin()
Definition: SimRacing.h:514
const int8_t MaxGear
the highest selectable gear
Definition: SimRacing.h:525
int8_t getGear() const
Definition: SimRacing.h:463
Shifter(int8_t min, int8_t max)
Definition: SimRacing.cpp:585
char getGearChar() const
Definition: SimRacing.cpp:607
int8_t getGearMax()
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
Definition: SimRacing.h:507
Pedal implementation for devices with gas, brake, and clutch.
Definition: SimRacing.h:407
ThreePedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchPin, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:552
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal, AnalogInput::Calibration clutchCal)
Definition: SimRacing.cpp:557
Pedal implementation for devices with only gas and brake.
Definition: SimRacing.h:379
void setCalibration(AnalogInput::Calibration gasCal, AnalogInput::Calibration brakeCal)
Definition: SimRacing.cpp:546
TwoPedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin=NOT_A_PIN)
Definition: SimRacing.cpp:541
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