forked from gbraad/arduino-RCUSB4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCUSB4.ino
127 lines (111 loc) · 2.61 KB
/
RCUSB4.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
/*
* kekse23.de RCUSB
* Copyright (c) 2019, Nicholas Regitz
*
* Diese Datei ist Lizensiert unter der Creative Commons 4.0 CC BY-NC-SA
* https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
*/
#include <Joystick.h>
#include "AVRPort23.h"
#define CHAN1 D,0
#define _INT1 0
#define CHAN2 D,1
#define _INT2 1
#define CHAN3 D,3
#define _INT3 3
#define CHAN4 D,2
#define _INT4 2
#define RXLED B,0
#define TXLED D,5
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
0, 0, // Button Count, Hat Switch Count
true, true, false, // X, Y, Z
true, true, false, // Rx, Ry, Rz
false, false, // Rudder, Throttle
false, false, false); // Accelerator, Brake, Steering
void setup()
{
portMode(CHAN1, INPUT, HIGH);
portMode(CHAN2, INPUT, HIGH);
portMode(CHAN3, INPUT, HIGH);
portMode(CHAN4, INPUT, HIGH);
portMode(RXLED, OUTPUT, LOW);
portMode(TXLED, OUTPUT, LOW);
Joystick.begin();
Joystick.setXAxisRange(2250, 750);
Joystick.setYAxisRange(2250, 750);
Joystick.setRxAxisRange(2250, 750);
Joystick.setRyAxisRange(2250, 750);
attachInterrupt(_INT1, isr1, CHANGE);
attachInterrupt(_INT2, isr2, CHANGE);
attachInterrupt(_INT3, isr3, CHANGE);
attachInterrupt(_INT4, isr4, CHANGE);
}
volatile unsigned long Time[4];
volatile unsigned int Value[4];
volatile bool ValChanged[4];
unsigned int NewValue[4];
void loop()
{
if (ValChanged[0])
{
NewValue[0] = (NewValue[0]+Value[0])/2;
Joystick.setXAxis(NewValue[0]);
ValChanged[0] = false;
}
if (ValChanged[1])
{
NewValue[1] = (NewValue[1]+Value[1])/2;
Joystick.setYAxis(NewValue[1]);
ValChanged[1] = false;
}
if (ValChanged[2])
{
NewValue[2] = (NewValue[2]+Value[2])/2;
Joystick.setRxAxis(NewValue[2]);
ValChanged[2] = false;
}
if (ValChanged[3])
{
NewValue[3] = (NewValue[3]+Value[3])/2;
Joystick.setRyAxis(NewValue[3]);
ValChanged[3] = false;
}
delay(8);
}
void isr1()
{
if (portRead(CHAN1)) Time[0] = micros();
else if (micros() > Time[0])
{
Value[0] = (Value[0]+(micros()-Time[0]))/2;
ValChanged[0] = true;
}
}
void isr2()
{
if (portRead(CHAN2)) Time[1] = micros();
else if (micros() > Time[1])
{
Value[1] = (Value[1]+(micros()-Time[1]))/2;
ValChanged[1] = true;
}
}
void isr3()
{
if (portRead(CHAN3)) Time[2] = micros();
else if (micros() > Time[2])
{
Value[2] = (Value[2]+(micros()-Time[2]))/2;
ValChanged[2] = true;
}
}
void isr4()
{
if (portRead(CHAN4)) Time[3] = micros();
else if (micros() > Time[3])
{
Value[3] = (Value[3]+(micros()-Time[3]))/2;
ValChanged[3] = true;
}
}