Skip to content

Commit ff1b1cf

Browse files
committed
Added support for Rudder, Throttle, Accelerator, Brake, and Steering.
1 parent 1f6559f commit ff1b1cf

File tree

5 files changed

+653
-69
lines changed

5 files changed

+653
-69
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Program used to test the driving simulator functions on
2+
// the USB Joystick object on the Arduino Leonardo or
3+
// Arduino Micro.
4+
//
5+
// Matthew Heironimus
6+
// 2016-05-29 Original version.
7+
//------------------------------------------------------------
8+
9+
#include "Joystick.h"
10+
11+
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 4,
12+
false, false, false, false, false, false,
13+
false, false, true, true, true);
14+
15+
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
16+
//const bool testAutoSendMode = true;
17+
const bool testAutoSendMode = false;
18+
19+
const unsigned long gcCycleDelta = 1000;
20+
const unsigned long gcButtonDelta = 500;
21+
const unsigned long gcAnalogDelta = 25;
22+
unsigned long gNextTime = 0;
23+
unsigned int gCurrentStep = 0;
24+
25+
void testSingleButtonPush(unsigned int button)
26+
{
27+
if (button > 0)
28+
{
29+
Joystick.releaseButton(button - 1);
30+
}
31+
if (button < 4)
32+
{
33+
Joystick.pressButton(button);
34+
}
35+
}
36+
37+
void testMultiButtonPush(unsigned int currentStep)
38+
{
39+
for (int button = 0; button < 4; button++)
40+
{
41+
if ((currentStep == 0) || (currentStep == 2))
42+
{
43+
if ((button % 2) == 0)
44+
{
45+
Joystick.pressButton(button);
46+
} else if (currentStep != 2)
47+
{
48+
Joystick.releaseButton(button);
49+
}
50+
} // if ((currentStep == 0) || (currentStep == 2))
51+
if ((currentStep == 1) || (currentStep == 2))
52+
{
53+
if ((button % 2) != 0)
54+
{
55+
Joystick.pressButton(button);
56+
} else if (currentStep != 2)
57+
{
58+
Joystick.releaseButton(button);
59+
}
60+
} // if ((currentStep == 1) || (currentStep == 2))
61+
if (currentStep == 3)
62+
{
63+
Joystick.releaseButton(button);
64+
} // if (currentStep == 3)
65+
} // for (int button = 0; button < 32; button++)
66+
}
67+
68+
void testAcceleratorBrake(int value)
69+
{
70+
Joystick.setAccelerator(value);
71+
Joystick.setBrake(260 - value);
72+
}
73+
74+
void testSteering(int value)
75+
{
76+
if (value < 300) {
77+
Joystick.setSteering(value);
78+
} else {
79+
Joystick.setSteering(600 - value);
80+
}
81+
}
82+
83+
void setup() {
84+
85+
Joystick.setAcceleratorRange(0, 260);
86+
Joystick.setBrakeRange(0, 260);
87+
Joystick.setSteeringRange(0, 300);
88+
89+
if (testAutoSendMode)
90+
{
91+
Joystick.begin();
92+
}
93+
else
94+
{
95+
Joystick.begin(false);
96+
}
97+
98+
pinMode(A0, INPUT_PULLUP);
99+
pinMode(13, OUTPUT);
100+
}
101+
102+
void loop() {
103+
104+
// System Disabled
105+
if (digitalRead(A0) != 0)
106+
{
107+
// Turn indicator light off.
108+
digitalWrite(13, 0);
109+
return;
110+
}
111+
112+
// Turn indicator light on.
113+
digitalWrite(13, 1);
114+
115+
if (millis() >= gNextTime)
116+
{
117+
118+
if (gCurrentStep < 4)
119+
{
120+
gNextTime = millis() + gcButtonDelta;
121+
testSingleButtonPush(gCurrentStep);
122+
}
123+
else if (gCurrentStep < 9)
124+
{
125+
gNextTime = millis() + gcButtonDelta;
126+
testMultiButtonPush(gCurrentStep - 5);
127+
}
128+
else if (gCurrentStep < (9 + 260))
129+
{
130+
gNextTime = millis() + gcAnalogDelta;
131+
testAcceleratorBrake(gCurrentStep - 9);
132+
}
133+
else if (gCurrentStep < (9 + 260 + 600))
134+
{
135+
gNextTime = millis() + gcAnalogDelta;
136+
testSteering(gCurrentStep - (9 + 260));
137+
}
138+
139+
if (testAutoSendMode == false)
140+
{
141+
Joystick.sendState();
142+
}
143+
144+
gCurrentStep++;
145+
if (gCurrentStep >= (9 + 260 + 600))
146+
{
147+
gNextTime = millis() + gcCycleDelta;
148+
gCurrentStep = 0;
149+
}
150+
}
151+
}
152+
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Program used to test the USB Joystick library when used as
2+
// a Flight Controller on the Arduino Leonardo or Arduino
3+
// Micro.
4+
//
5+
// Matthew Heironimus
6+
// 2016-05-29 - Original Version
7+
//------------------------------------------------------------
8+
9+
#include "Joystick.h"
10+
11+
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 32,
12+
true, true, false, false, false, false,
13+
true, true, false, false, false);
14+
15+
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
16+
//const bool testAutoSendMode = true;
17+
const bool testAutoSendMode = false;
18+
19+
const unsigned long gcCycleDelta = 1000;
20+
const unsigned long gcAnalogDelta = 25;
21+
const unsigned long gcButtonDelta = 500;
22+
unsigned long gNextTime = 0;
23+
unsigned int gCurrentStep = 0;
24+
25+
void testSingleButtonPush(unsigned int button)
26+
{
27+
if (button > 0)
28+
{
29+
Joystick.releaseButton(button - 1);
30+
}
31+
if (button < 32)
32+
{
33+
Joystick.pressButton(button);
34+
}
35+
}
36+
37+
void testMultiButtonPush(unsigned int currentStep)
38+
{
39+
for (int button = 0; button < 32; button++)
40+
{
41+
if ((currentStep == 0) || (currentStep == 2))
42+
{
43+
if ((button % 2) == 0)
44+
{
45+
Joystick.pressButton(button);
46+
} else if (currentStep != 2)
47+
{
48+
Joystick.releaseButton(button);
49+
}
50+
} // if ((currentStep == 0) || (currentStep == 2))
51+
if ((currentStep == 1) || (currentStep == 2))
52+
{
53+
if ((button % 2) != 0)
54+
{
55+
Joystick.pressButton(button);
56+
} else if (currentStep != 2)
57+
{
58+
Joystick.releaseButton(button);
59+
}
60+
} // if ((currentStep == 1) || (currentStep == 2))
61+
if (currentStep == 3)
62+
{
63+
Joystick.releaseButton(button);
64+
} // if (currentStep == 3)
65+
} // for (int button = 0; button < 32; button++)
66+
}
67+
68+
void testXYAxis(unsigned int currentStep)
69+
{
70+
int xAxis;
71+
int yAxis;
72+
73+
if (currentStep < 256)
74+
{
75+
xAxis = currentStep - 127;
76+
yAxis = -127;
77+
Joystick.setXAxis(xAxis);
78+
Joystick.setYAxis(yAxis);
79+
}
80+
else if (currentStep < 512)
81+
{
82+
yAxis = currentStep - 256 - 127;
83+
Joystick.setYAxis(yAxis);
84+
}
85+
else if (currentStep < 768)
86+
{
87+
xAxis = 128 - (currentStep - 512);
88+
Joystick.setXAxis(xAxis);
89+
}
90+
else if (currentStep < 1024)
91+
{
92+
yAxis = 128 - (currentStep - 768);
93+
Joystick.setYAxis(yAxis);
94+
}
95+
else if (currentStep < 1024 + 128)
96+
{
97+
xAxis = currentStep - 1024 - 127;
98+
Joystick.setXAxis(xAxis);
99+
Joystick.setYAxis(xAxis);
100+
}
101+
}
102+
103+
void testThrottleRudder(unsigned int value)
104+
{
105+
Joystick.setThrottle(value);
106+
Joystick.setRudder(255 - value);
107+
}
108+
109+
void setup() {
110+
111+
Joystick.setXAxisRange(-127, 127);
112+
Joystick.setYAxisRange(-127, 127);
113+
Joystick.setZAxisRange(-127, 127);
114+
Joystick.setThrottleRange(0, 255);
115+
Joystick.setRudderRange(0, 255);
116+
117+
if (testAutoSendMode)
118+
{
119+
Joystick.begin();
120+
}
121+
else
122+
{
123+
Joystick.begin(false);
124+
}
125+
126+
pinMode(A0, INPUT_PULLUP);
127+
pinMode(13, OUTPUT);
128+
}
129+
130+
void loop() {
131+
132+
// System Disabled
133+
if (digitalRead(A0) != 0)
134+
{
135+
// Turn indicator light off.
136+
digitalWrite(13, 0);
137+
return;
138+
}
139+
140+
// Turn indicator light on.
141+
digitalWrite(13, 1);
142+
143+
if (millis() >= gNextTime)
144+
{
145+
146+
if (gCurrentStep < 33)
147+
{
148+
gNextTime = millis() + gcButtonDelta;
149+
testSingleButtonPush(gCurrentStep);
150+
}
151+
else if (gCurrentStep < 37)
152+
{
153+
gNextTime = millis() + gcButtonDelta;
154+
testMultiButtonPush(gCurrentStep - 33);
155+
}
156+
else if (gCurrentStep < (37 + 256))
157+
{
158+
gNextTime = millis() + gcAnalogDelta;
159+
testThrottleRudder(gCurrentStep - 37);
160+
}
161+
else if (gCurrentStep < (37 + 256 + 1024 + 128))
162+
{
163+
gNextTime = millis() + gcAnalogDelta;
164+
testXYAxis(gCurrentStep - (37 + 256));
165+
}
166+
167+
if (testAutoSendMode == false)
168+
{
169+
Joystick.sendState();
170+
}
171+
172+
gCurrentStep++;
173+
if (gCurrentStep >= (37 + 256 + 1024 + 128))
174+
{
175+
gNextTime = millis() + gcCycleDelta;
176+
gCurrentStep = 0;
177+
}
178+
}
179+
}
180+

0 commit comments

Comments
 (0)