Skip to content

Commit 9390030

Browse files
committed
Add CreateShieldObject template function
Convenience function for creating objects that are compatible with the Sim Racing Shields (https://github.com/dmadison/Sim-Racing-Shields) without needing to look up or remember the pinout.
1 parent b402d61 commit 9390030

11 files changed

Lines changed: 171 additions & 4 deletions

File tree

examples/Pedals/PedalsJoystick/PedalsJoystick.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const int Pin_Brake = A1;
3636
const int Pin_Clutch = A0;
3737

3838
SimRacing::LogitechPedals pedals(Pin_Gas, Pin_Brake, Pin_Clutch);
39-
//SimRacing::LogitechPedals pedals(PEDAL_SHIELD_V1_PINS);
39+
//SimRacing::LogitechPedals pedals = SimRacing::CreateShieldObject<SimRacing::LogitechPedals, 1>();
4040

4141
Joystick_ Joystick(
4242
JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)

examples/Pedals/PedalsPrint/PedalsPrint.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const int Pin_Brake = A1;
3232
const int Pin_Clutch = A0;
3333

3434
SimRacing::LogitechPedals pedals(Pin_Gas, Pin_Brake, Pin_Clutch);
35-
//SimRacing::LogitechPedals pedals(PEDAL_SHIELD_V1_PINS);
35+
//SimRacing::LogitechPedals pedals = SimRacing::CreateShieldObject<SimRacing::LogitechPedals, 1>();
3636

3737

3838
void setup() {

examples/Shifter/LogitechShifter/LogitechShifter_Joystick/LogitechShifter_Joystick.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const int Pin_ShifterY = A2; // DE-9 pin 8
4949
const int Pin_ShifterRev = 2; // DE-9 pin 2
5050

5151
SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);
52-
//SimRacing::LogitechShifter shifter(SHIFTER_SHIELD_V1_PINS);
52+
//SimRacing::LogitechShifter shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifter, 2>();
5353

5454
const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 };
5555
const int NumGears = sizeof(Gears) / sizeof(Gears[0]);

examples/Shifter/LogitechShifter/LogitechShifter_Print/LogitechShifter_Print.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const int Pin_ShifterY = A2; // DE-9 pin 8
3636
const int Pin_ShifterRev = 2; // DE-9 pin 2
3737

3838
SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);
39-
//SimRacing::LogitechShifter shifter(SHIFTER_SHIELD_V1_PINS);
39+
//SimRacing::LogitechShifter shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifter, 2>();
4040

4141
const unsigned long PrintSpeed = 1500; // ms
4242
unsigned long lastPrint = 0;

examples/Shifter/LogitechShifterG25/LogitechShifterG25_Joystick/LogitechShifterG25_Joystick.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SimRacing::LogitechShifterG25 shifter(
5050
Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData,
5151
Pin_ShifterDetect, Pin_ShifterLED
5252
);
53+
//SimRacing::LogitechShifterG25 shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifterG25, 2>();
5354

5455
// Set this option to 'true' to send the shifter's X/Y position
5556
// as a joystick. This is not needed for most games.

examples/Shifter/LogitechShifterG25/LogitechShifterG25_Print/LogitechShifterG25_Print.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SimRacing::LogitechShifterG25 shifter(
4646
Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData,
4747
Pin_ShifterDetect, Pin_ShifterLED
4848
);
49+
//SimRacing::LogitechShifterG25 shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifterG25, 2>();
4950

5051
// alias so we don't need to type so much
5152
using ShifterButton = SimRacing::LogitechShifterG25::Button;

examples/Shifter/LogitechShifterG27/LogitechShifterG27_Joystick/LogitechShifterG27_Joystick.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SimRacing::LogitechShifterG27 shifter(
5050
Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData,
5151
Pin_ShifterDetect, Pin_ShifterLED
5252
);
53+
//SimRacing::LogitechShifterG27 shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifterG27, 2>();
5354

5455
// Set this option to 'true' to send the shifter's X/Y position
5556
// as a joystick. This is not needed for most games.

examples/Shifter/LogitechShifterG27/LogitechShifterG27_Print/LogitechShifterG27_Print.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SimRacing::LogitechShifterG27 shifter(
4646
Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData,
4747
Pin_ShifterDetect, Pin_ShifterLED
4848
);
49+
//SimRacing::LogitechShifterG27 shifter = SimRacing::CreateShieldObject<SimRacing::LogitechShifterG27, 2>();
4950

5051
// alias so we don't need to type so much
5152
using ShifterButton = SimRacing::LogitechShifterG27::Button;

keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ LogitechShifterG25 KEYWORD1
4444
# Handbrake Classes
4545
Handbrake KEYWORD1
4646

47+
#######################################
48+
# Functions (KEYWORD2)
49+
#######################################
50+
51+
CreateShieldObject KEYWORD2
52+
4753
#######################################
4854
# Peripheral Class Methods and Functions (KEYWORD2)
4955
#######################################

src/SimRacing.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,83 @@
2929

3030
namespace SimRacing {
3131

32+
#if defined(__AVR_ATmega32U4__) || defined(SIM_RACING_DOXYGEN)
33+
34+
template<>
35+
LogitechPedals CreateShieldObject<LogitechPedals, 1>() {
36+
// Power (VCC): DE-9 pin 9, bridged to DE-9 pin 6
37+
// Ground (GND): DE-9 pin 1
38+
39+
const PinNum Pin_Gas = A2; // DE-9 pin 2
40+
const PinNum Pin_Brake = A1; // DE-9 pin 3
41+
const PinNum Pin_Clutch = A0; // DE-9 pin 4
42+
const PinNum Pin_Detect = 10; // DE-9 pin 6, requires 10k Ohm pull-down
43+
44+
return LogitechPedals(Pin_Gas, Pin_Brake, Pin_Clutch, Pin_Detect);
45+
}
46+
47+
template<>
48+
LogitechPedals CreateShieldObject<LogitechPedals, 2>() {
49+
// version 2 of the pedals shield has the same pinout,
50+
// so we can use the v1 function
51+
return CreateShieldObject<LogitechPedals, 1>();
52+
}
53+
54+
template<>
55+
LogitechShifter CreateShieldObject<LogitechShifter, 1>() {
56+
// Power (VCC): DE-9 pin 9, bridged to DE-9 pin 7
57+
// Ground (GND): DE-9 pin 6
58+
// DE-9 pin 3 (CS) needs to be pulled-up to VCC
59+
60+
const PinNum Pin_X_Wiper = A1; // DE-9 pin 4
61+
const PinNum Pin_Y_Wiper = A0; // DE-9 pin 8
62+
const PinNum Pin_DataOut = 14; // DE-9 pin 2
63+
const PinNum Pin_Detect = A2; // DE-9 pin 7, requires 10k Ohm pull-down
64+
65+
return LogitechShifter(Pin_X_Wiper, Pin_Y_Wiper, Pin_DataOut, Pin_Detect);
66+
}
67+
68+
template<>
69+
LogitechShifter CreateShieldObject<LogitechShifter, 2>() {
70+
// version 2 of the shifter shield has the same data pinout for
71+
// the Driving Force shifter, so we can use the v1 function
72+
return CreateShieldObject<LogitechShifter, 1>();
73+
}
74+
75+
/**
76+
* Helper function to create either a LogitechShifterG27 or LogitechShifterG25
77+
* object using the v2 shifter shield pinout, as both use the same pins.
78+
*/
79+
template<class T>
80+
static T CreateShieldShifter() {
81+
// Power (VCC): DE-9 pin 9, bridged to DE-9 pin 1
82+
// Ground (GND): DE-9 pin 6
83+
84+
const PinNum Pin_X_Wiper = A1; // DE-9 pin 4
85+
const PinNum Pin_Y_Wiper = A0; // DE-9 pin 8
86+
const PinNum Pin_DataOut = 14; // DE-9 pin 2
87+
88+
const PinNum Pin_Latch = 10; // DE-9 pin 3, aka chip select, requires 10k Ohm pull-up
89+
const PinNum Pin_Clock = A2; // DE-9 pin 7, should have 470 Ohm resistor to prevent shorts
90+
91+
const PinNum Pin_Detect = 15; // DE-9 pin 1, requires 10k Ohm pull-down
92+
const PinNum Pin_LED = 16; // DE-9 pin 5, requires 100-120 Ohm series resistor
93+
94+
return T(Pin_X_Wiper, Pin_Y_Wiper, Pin_Latch, Pin_Clock, Pin_DataOut, Pin_Detect, Pin_LED);
95+
}
96+
97+
template<>
98+
LogitechShifterG27 CreateShieldObject<LogitechShifterG27, 2>() {
99+
return CreateShieldShifter<LogitechShifterG27>();
100+
}
101+
102+
template<>
103+
LogitechShifterG25 CreateShieldObject<LogitechShifterG25, 2>() {
104+
return CreateShieldShifter<LogitechShifterG25>();
105+
}
106+
#endif // ATmega32U4 for shield functions
107+
108+
32109
/**
33110
* Take a pin number as an input and sanitize it to a known working value
34111
*

0 commit comments

Comments
 (0)