Skip to content

Commit 06ed53f

Browse files
author
Jason Duncan
committed
Moved to my own derived library that sends a higher resolution for the pedals.
1 parent 7fea583 commit 06ed53f

File tree

3 files changed

+244
-14
lines changed

3 files changed

+244
-14
lines changed

G27PedalsShifter/G27PedalsShifter.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
G27PedalsShifter.cpp
3+
4+
Copyright (c) 2016, Jason Duncan
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
// (stolen from Matthew Heironimus @ https://github.com/MHeironimus/ArduinoJoystickLibrary)
22+
23+
#include "G27PedalsShifter.h"
24+
25+
#if defined(_USING_HID)
26+
27+
#define G_REPORT_ID 0x03
28+
#define G_STATE_SIZE 10
29+
30+
static const uint8_t _hidReportDescriptor[] PROGMEM = {
31+
// Joystick
32+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
33+
0x09, 0x04, // USAGE (Joystick)
34+
0xa1, 0x01, // COLLECTION (Application)
35+
0x85, G_REPORT_ID, // REPORT_ID (3)
36+
37+
// 32 Buttons
38+
0x05, 0x09, // USAGE_PAGE (Button)
39+
0x19, 0x01, // USAGE_MINIMUM (Button 1)
40+
0x29, 0x20, // USAGE_MAXIMUM (Button 32)
41+
0x15, 0x00, // LOGICAL_MINIMUM (0)
42+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
43+
0x75, 0x01, // REPORT_SIZE (1)
44+
0x95, 0x20, // REPORT_COUNT (32)
45+
0x55, 0x00, // UNIT_EXPONENT (0)
46+
0x65, 0x00, // UNIT (None)
47+
0x81, 0x02, // INPUT (Data,Var,Abs)
48+
49+
// X, Y, and Z Axis
50+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
51+
0x15, 0x00, // LOGICAL_MINIMUM (0)
52+
0x26, 0xff, 0x03, // LOGICAL_MAXIMUM (1023)
53+
0x75, 0x10, // REPORT_SIZE (16)
54+
0x09, 0x01, // USAGE (Pointer)
55+
0xA1, 0x00, // COLLECTION (Physical)
56+
0x09, 0x30, // USAGE (x)
57+
0x09, 0x31, // USAGE (y)
58+
0x09, 0x32, // USAGE (z)
59+
0x95, 0x03, // REPORT_COUNT (3)
60+
0x81, 0x02, // INPUT (Data,Var,Abs)
61+
0xc0, // END_COLLECTION
62+
63+
0xc0 // END_COLLECTION
64+
};
65+
66+
G_::G_()
67+
{
68+
// Setup HID report structure
69+
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
70+
HID().AppendDescriptor(&node);
71+
72+
// Initalize State
73+
xAxis = 0;
74+
yAxis = 0;
75+
zAxis = 0;
76+
buttons = 0;
77+
}
78+
79+
void G_::begin(bool initAutoSendState)
80+
{
81+
autoSendState = initAutoSendState;
82+
sendState();
83+
}
84+
85+
void G_::end()
86+
{
87+
}
88+
89+
void G_::setButton(uint8_t button, uint8_t value)
90+
{
91+
if (value == 0)
92+
{
93+
releaseButton(button);
94+
}
95+
else
96+
{
97+
pressButton(button);
98+
}
99+
}
100+
void G_::pressButton(uint8_t button)
101+
{
102+
bitSet(buttons, button);
103+
if (autoSendState) sendState();
104+
}
105+
void G_::releaseButton(uint8_t button)
106+
{
107+
bitClear(buttons, button);
108+
if (autoSendState) sendState();
109+
}
110+
111+
void G_::setXAxis(uint16_t value)
112+
{
113+
xAxis = value;
114+
if (autoSendState) sendState();
115+
}
116+
void G_::setYAxis(uint16_t value)
117+
{
118+
yAxis = value;
119+
if (autoSendState) sendState();
120+
}
121+
void G_::setZAxis(uint16_t value)
122+
{
123+
zAxis = value;
124+
if (autoSendState) sendState();
125+
}
126+
127+
void G_::sendState()
128+
{
129+
uint8_t data[G_STATE_SIZE];
130+
uint32_t tmp = buttons;
131+
132+
// Split 32 bit button-state into 4 bytes
133+
data[0] = tmp & 0xFF;
134+
tmp >>= 8;
135+
data[1] = tmp & 0xFF;
136+
tmp >>= 8;
137+
data[2] = tmp & 0xFF;
138+
tmp >>= 8;
139+
data[3] = tmp & 0xFF;
140+
141+
// axis get 2 bytes each
142+
tmp = xAxis;
143+
data[4] = tmp & 0xFF;
144+
tmp >>=8;
145+
data[5] = tmp & 0xFF;
146+
147+
tmp = yAxis;
148+
data[6] = tmp & 0xFF;
149+
tmp >>=8;
150+
data[7] = tmp & 0xFF;
151+
152+
tmp = zAxis;
153+
data[8] = tmp & 0xFF;
154+
tmp >>=8;
155+
data[9] = tmp & 0xFF;
156+
157+
// HID().SendReport(Report number, array of values in same order as HID descriptor, length)
158+
HID().SendReport(G_REPORT_ID, data, G_STATE_SIZE);
159+
}
160+
161+
G_ G;
162+
163+
#endif

G27PedalsShifter/G27PedalsShifter.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
G27PedalsShifter.h
3+
4+
Copyright (c) 2016, Jason Duncan
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
// (stolen from Matthew Heironimus @ https://github.com/MHeironimus/ArduinoJoystickLibrary)
22+
23+
#ifndef G_h
24+
#define G_h
25+
26+
#include "HID.h"
27+
28+
#if !defined(_USING_HID)
29+
30+
#warning "Using legacy HID core (non pluggable)"
31+
32+
#else
33+
34+
//================================================================================
35+
//================================================================================
36+
// G27 (Gamepad)
37+
38+
class G_
39+
{
40+
private:
41+
bool autoSendState;
42+
uint16_t xAxis;
43+
uint16_t yAxis;
44+
uint16_t zAxis;
45+
uint32_t buttons;
46+
47+
public:
48+
G_();
49+
50+
void begin(bool initAutoSendState = true);
51+
void end();
52+
53+
void setXAxis(uint16_t value);
54+
void setYAxis(uint16_t value);
55+
void setZAxis(uint16_t value);
56+
57+
void setButton(uint8_t button, uint8_t value);
58+
void pressButton(uint8_t button);
59+
void releaseButton(uint8_t button);
60+
61+
void sendState();
62+
};
63+
extern G_ G;
64+
65+
#endif
66+
#endif

G27_Pedals_and_Shifter.ino

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Partially adapted from the work done by isrtv.com forums members pascalh and xxValiumxx:
55
// http://www.isrtv.com/forums/topic/13189-diy-g25-shifter-interface-with-h-pattern-sequential-and-handbrake-modes/
66

7-
#include "Joystick.h"
7+
#include "G27PedalsShifter.h"
88

99
// for debugging, gives serial output rather than working as a joystick
1010
//#define DEBUG true
@@ -93,12 +93,13 @@
9393
#define SHIFTER_YAXIS_246 300 //Gears 2,4,6, R
9494

9595
// MISC.
96-
#define MAX_AXIS 127
96+
#define MAX_AXIS 1023
9797
#define SIGNAL_SETTLE_DELAY 10
9898

9999
// PEDAL CODE
100100
typedef struct pedal {
101-
int pin, min, max, cur, axis;
101+
byte pin;
102+
int min, max, cur, axis;
102103
};
103104

104105
typedef struct pedal Pedal;
@@ -112,16 +113,16 @@ int axisValue(void* in) {
112113

113114
int range = input->max - input->min;
114115
if (range == 0) {
115-
return -MAX_AXIS;
116+
return 0;
116117
}
117118

118119
long step1 = input->cur - input->min;
119120
long step2 = step1 * (MAX_AXIS * 2);
120121
float step3 = step2 / range;
121122
int result = step3 - MAX_AXIS;
122123

123-
if (result < -MAX_AXIS) {
124-
return -MAX_AXIS;
124+
if (result < 0) {
125+
return 0;
125126
}
126127
if (result > MAX_AXIS) {
127128
return MAX_AXIS;
@@ -162,17 +163,17 @@ void describePedal(char* name, char* axisName, void* in) {
162163

163164
void setXAxis(void* in) {
164165
Pedal* input = (Pedal*)in;
165-
Joystick.setXAxis(input->axis);
166+
G.setXAxis(input->axis);
166167
}
167168

168169
void setYAxis(void* in) {
169170
Pedal* input = (Pedal*)in;
170-
Joystick.setYAxis(input->axis);
171+
G.setYAxis(input->axis);
171172
}
172173

173174
void setZAxis(void* in) {
174175
Pedal* input = (Pedal*)in;
175-
Joystick.setZAxis(input->axis);
176+
G.setZAxis(input->axis);
176177
}
177178

178179
void pedalColor(void* inGas, void* inBrake, void* inClutch){
@@ -268,15 +269,15 @@ int getCurrentGear(int shifterPosition[], int btns[]) {
268269
void setButtonStates(int buttons[], int gear) {
269270
// release virtual buttons for all gears
270271
for (byte i = 0; i < 7; ++i) {
271-
Joystick.setButton(i, LOW);
272+
G.setButton(i, LOW);
272273
}
273274

274275
if (gear > 0) {
275-
Joystick.setButton(gear - 1, HIGH);
276+
G.setButton(gear - 1, HIGH);
276277
}
277278

278279
for (byte i = BUTTON_RED_CENTERRIGHT; i <= BUTTON_DPAD_TOP; ++i) {
279-
Joystick.setButton(buttonTable[i], buttons[i]);
280+
G.setButton(buttonTable[i], buttons[i]);
280281
}
281282
}
282283

@@ -337,7 +338,7 @@ void describeButtonStates(int buttons[], int shifterPosition[], int gear) {
337338
void setup() {
338339
Serial.begin(38400);
339340
#if !defined(DEBUG_PEDALS) && !defined(DEBUG_SHIFTER)
340-
Joystick.begin(false);
341+
G.begin(false);
341342
#endif
342343

343344
// lights
@@ -397,7 +398,7 @@ void loop() {
397398
describeButtonStates(buttonStates, shifterPosition, gear);
398399
#else
399400
setButtonStates(buttonStates, gear);
400-
Joystick.sendState();
401+
G.sendState();
401402
#endif
402403

403404
#if defined(DEBUG_PEDALS) || defined(DEBUG_SHIFTER)

0 commit comments

Comments
 (0)