-
Notifications
You must be signed in to change notification settings - Fork 1
/
OutputDevice.ino
185 lines (149 loc) · 4.07 KB
/
OutputDevice.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// This file contains methods used to interact with the joystick / mouse objects.
#define SERIAL_PORT Serial3
//#define ALT_AXIS
#define OUTPUT_MIN 0
#define OUTPUT_MAX 1023 //Per Teensy specs
#define OUTPUT_MID 512 //Per Teensy specs
// Should restart Teensy 3, will also disconnect USB during restart
// From http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337e/Cihcbadd.html
// Search for "0xE000ED0C"
// Original question http://forum.pjrc.com/threads/24304-_reboot_Teensyduino%28%29-vs-_restart_Teensyduino%28%29?p=35981#post35981
#define RESTART_ADDR 0xE000ED0C
#define READ_RESTART() (*(volatile uint32_t *)RESTART_ADDR)
#define WRITE_RESTART(val) ((*(volatile uint32_t *)RESTART_ADDR) = (val))
int outputX = 0;
int outputY = 0;
bool buttonState[7];
#ifdef SERIAL_PORT
uint8_t serialData[4];
#endif
bool initOutputDevice()
{
#ifdef SERIAL_PORT
SERIAL_PORT.begin(115200);
#endif
#ifdef JOYSTICK_INTERFACE
Joystick.useManualSend(true);
Joystick.X(OUTPUT_MID);
Joystick.Y(OUTPUT_MID);
Joystick.Z(OUTPUT_MID);
Joystick.sliderLeft(OUTPUT_MID);
Joystick.sliderRight(OUTPUT_MID);
Joystick.Zrotate(OUTPUT_MID);
Joystick.hat(-1);
Joystick.button(1, 0);
Joystick.button(2, 0);
Joystick.button(3, 0);
Joystick.button(4, 0);
Joystick.button(5, 0);
Joystick.button(6, 0);
Joystick.send_now();
#endif
#ifdef MOUSE_INTERFACE
if (Mouse.isPressed(MOUSE_LEFT)) Mouse.release(MOUSE_LEFT);
if (Mouse.isPressed(MOUSE_RIGHT)) Mouse.release(MOUSE_RIGHT);
if (Mouse.isPressed(MOUSE_MIDDLE)) Mouse.release(MOUSE_MIDDLE);
Mouse.screenSize(OUTPUT_MAX, OUTPUT_MAX);
#endif
return true;
}
void setButtonState(uint8_t button, bool val)
{
if (button >= 7 || button < 1)
{
return;
}
buttonState[button] = val;
if (useMouse)
{
#ifdef MOUSE_INTERFACE
if (val)
{
switch (button)
{
case 1: if (!Mouse.isPressed(MOUSE_LEFT)) Mouse.press(MOUSE_LEFT);
case 2: if (!Mouse.isPressed(MOUSE_RIGHT)) Mouse.press(MOUSE_RIGHT);
case 3: if (!Mouse.isPressed(MOUSE_MIDDLE)) Mouse.press(MOUSE_MIDDLE);
}
}
else
{
switch (button)
{
case 1: if (Mouse.isPressed(MOUSE_LEFT)) Mouse.release(MOUSE_LEFT);
case 2: if (Mouse.isPressed(MOUSE_RIGHT)) Mouse.release(MOUSE_RIGHT);
case 3: if (Mouse.isPressed(MOUSE_MIDDLE)) Mouse.release(MOUSE_MIDDLE);
}
}
#endif
}
else
{
#ifdef JOYSTICK_INTERFACE
Joystick.button(button, val);
#endif
}
}
void sendPosition()
{
#ifdef SERIAL_PORT
int8_t c;
while ((c = SERIAL_PORT.read()) >= 0)
{
if (c == 'r') WRITE_RESTART(0x5FA0004);
}
#endif
if (calibrating || !active)
{
outputX = OUTPUT_MID;
outputY = OUTPUT_MID;
}
else {
//Compute screen position based on orientation angles
outputX = (((heading - centerHeading) / (-rangeHeading * omegaBlobs)) * OUTPUT_MAX) + OUTPUT_MID;
outputY = (((pitch - centerPitch) / (rangePitch * omegaBlobs)) * OUTPUT_MAX) + OUTPUT_MID;
//Determine if we are offscreen
if (outputX >= OUTPUT_MAX || outputX < OUTPUT_MIN || outputY >= OUTPUT_MAX || outputY < OUTPUT_MIN) {
outputX = OUTPUT_MAX;
outputY = OUTPUT_MAX;
}
}
#ifdef SERIAL_PORT
//Send to Serial
serialData[0] = outputX & 0x7F;
serialData[1] = outputY & 0x7F;
serialData[2] = ((outputX & 0x0380) >> 7) | ((outputY & 0x0380) >> 3);
serialData[3] =
(buttonState[1] ? 0x01 : 0x00)
| (buttonState[2] ? 0x02 : 0x00)
| (buttonState[3] ? 0x04 : 0x00)
| (buttonState[4] ? 0x08 : 0x00)
| (buttonState[5] ? 0x10 : 0x00)
| (buttonState[6] ? 0x20 : 0x00)
| (active ? 0x40 : 0x00)
| 0x80;
SERIAL_PORT.write(serialData, sizeof(serialData));
#endif
if (useMouse)
{
#ifdef MOUSE_INTERFACE
if (active)
{
Mouse.moveTo(outputX, outputY);
}
#endif
}
else
{
#ifdef JOYSTICK_INTERFACE
#ifdef ALT_AXIS
Joystick.sliderRight(outputX);
Joystick.sliderLeft(outputY);
#else
Joystick.X(outputX);
Joystick.Y(outputY);
#endif
Joystick.send_now();
#endif
}
}