-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm2joystick.ino
130 lines (96 loc) · 2.25 KB
/
ppm2joystick.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
/*
*
* A very trivial PPM to USB Joystick converter, for teensy controllers
* Time between falling edges is measured and converted to joystick axis.
* Additionnally, the ranges are auto scaled.
*
*/
// Maximum number of channels
#define NB_PEAKS 15
#define PPM_PIN 2
unsigned long int before;
typedef struct {
int val;
int min;
int max;
} PEAK;
PEAK peaks[NB_PEAKS];
//specifing arrays and variables to store values
void readPin() {
static int i = 0;
unsigned int now = micros(); //store time value a when pin value is falling
unsigned diff = now-before; //calculating time inbetween two peaks
before = now; //
if (diff > 6000) {
i=0;
return;
}
if (i++>NB_PEAKS)
i = 0;
peaks[i].val=diff;
}
unsigned int valToPos(int i) {
PEAK * pPeak = &peaks[i];
int range = pPeak->max-pPeak->min;
if (range == 0)
return 0;
return ((pPeak->val-pPeak->min)*1024)/range;
}
bool valToButton(int i) {
int v = valToPos(i);
if (v > 512)
return true;
else
return false;
}
void getPeaks(){
for (int i=0;i<NB_PEAKS;i++) {
PEAK * pPeak = &peaks[i];
if (pPeak->min>pPeak->val && pPeak->val != 0)
pPeak->min = pPeak->val;
if (pPeak->max < pPeak->val)
pPeak->max = pPeak->val;
#ifdef DEBUG
Serial.print(x[i]);
Serial.print("(");
Serial.print(xMin[i]);
Serial.print(",");
Serial.print(xMax[i]);
Serial.print(")->");
Serial.print(valToPos(i));
Serial.print("\t");
#endif
}
#ifdef DEBUG
Serial.println("");
#endif // DEBUG
Joystick.X(valToPos(1));
Joystick.Y(valToPos(2));
Joystick.Z(valToPos(3));
Joystick.Zrotate(valToPos(4));
Joystick.sliderLeft(valToPos(5));
Joystick.sliderRight(valToPos(6));
Joystick.button(1, valToButton(7));
Joystick.button(2, valToButton(8));
Joystick.button(3, valToButton(9));
}
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif // DEBUG
pinMode(PPM_PIN, INPUT_PULLUP);
Joystick.useManualSend(true);
for (int i=0; i<NB_PEAKS;i++) {
PEAK * pPeak = &peaks[i];
pPeak->min=1000;
pPeak->max=2000;
}
attachInterrupt(digitalPinToInterrupt(2), readPin , FALLING);
}
void loop()
{
getPeaks();
Joystick.send_now();
delay(20); // do not print too fast!
}