-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoy2usb.ino
240 lines (200 loc) · 7.31 KB
/
joy2usb.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* joy2usb
Author: [email protected]
Copyright (c) 2022, 2023 Michał Stępień
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define UP_BTN 1 //PIND
#define DOWN_BTN 2 //PIND
#define LEFT_BTN 3 //PIND
#define RIGHT_BTN 4 //PIND
#define C64MODE_PIN 7 //PIND
#define FIRE1 0 //PIND
#define FIRE2 5 //PINB
#define FIRE3 6 //PINB
#include <Joystick.h> //https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <math.h>
byte minimal_axis_time;
byte minimal_button_time;
byte pinf = 0;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK,
3, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void debouncing(bool mode = 1) {
if (mode) {
minimal_axis_time = 8;
minimal_button_time = 10;
} else {
minimal_axis_time = 0;
minimal_button_time = 0;
}
}
void setup() {
Serial.begin(9600);
bitSet(DDRE, 6); bitClear(PORTE, 6); //STATUSLED(PE6) as OUTPUT + set to LOW
bitClear(DDRF, 4); bitSet(PORTF, 4); //INPUT_PULLUP FOR DEBOUNCING SWITCH
bitClear(DDRF, 5); bitSet(PORTF, 5); //INPUT_PULLUP FOR FIRE1 CPS MEASURE
bitClear(DDRF, 6); bitSet(PORTF, 6);
bitClear(DDRF, 7); bitSet(PORTF, 7);
DDRD &= ~( bit(UP_BTN) | bit(DOWN_BTN) | bit(LEFT_BTN) | bit(RIGHT_BTN) | bit(FIRE1) | bit(C64MODE_PIN)) ; //PD0-PD5 as INPUT
PORTD |= ( bit(UP_BTN) | bit(DOWN_BTN) | bit(LEFT_BTN) | bit(RIGHT_BTN) | bit(FIRE1)); //FIRE1,UP,DOWN,LEFT,RIGT as INPUT_PULLUP
DDRB &= ~( bit(FIRE2) | bit(FIRE3)); //FIRE2 and FIRE3 as INPUT
//UNUSED PINS
DDRB &= ~( bit(PB1) | bit(PB2) | bit(PB3) | bit(PB4)); //PB1-PB4 as INPUT_PULLUP -- unused_pins
PORTB |= bit(PB1) | bit(PB2) | bit(PB3) | bit(PB4);
bitClear(DDRC, 6); bitSet(PORTC, 6); //INPUT_PULLUP PC6 -- unused pin
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
Joystick.begin(false);
Joystick.setXAxis(0);
Joystick.setYAxis(0);
delay(1500); /*very ugly and dirty hack
without that delay() joystick will not
be centered at the beginning (that is an
issue with Joystick.sendState();
*/
Joystick.sendState();
/*turn off RX and TX LEDS
on permanent basis
turning off these LEDS here at the end of setup()
is an indicator that joy2usb is ready
*/
bitClear(DDRD, 5);
bitClear(DDRB, 0);
bitSet(PORTE, 6); //STATUSLED(PE6) set to HIGH
}
byte joystate_update() {
//UP,DOWN,LEFT,RIGHT,F1,F2,F3
//0, 1, 2, 3, ,4, 5, 6
byte pind = ~PIND;
byte pinb = ~PINB;
bool C64MODE = bitRead(pind, C64MODE_PIN);
pind &= (bit(UP_BTN) | bit(DOWN_BTN) | bit(LEFT_BTN) | bit(RIGHT_BTN) | bit(FIRE1));
pinb &= (bit(FIRE2) | bit(FIRE3));
bitWrite(pinb, FIRE2, (C64MODE ^ bitRead(pinb, FIRE2)));
bitWrite(pinb, FIRE3, (C64MODE ^ bitRead(pinb, FIRE3)));
byte joystate_current = ((pind >> 1) | ((pind & bit(FIRE1)) << 4) | (pinb));
return joystate_current;
}
//NO-OPPOSITE-DIRECTIONS
bool nod(bool OUT_DIR, bool SECOND_DIR) {
//bool value = ((!( OUT_DIR || SECOND_DIR )) || OUT_DIR); //logic where LOW=pressed, HIGH=released
bool value = ((!( OUT_DIR && SECOND_DIR )) && OUT_DIR); //logic where HIGH=pressed, LOW=released
return value;
}
unsigned long prev_button_time[3] = { 0, 0, 0 };
unsigned long prev_axis_time[4] = { 0, 0, 0, 0 };
byte prev_axises;
byte prev_buttons;
void joystate_to_usb(byte joystate) {
byte current_axises = joystate & B00001111;
byte current_buttons = (joystate >> 4);
byte changedAxises = current_axises ^ prev_axises;
byte changedButtons = current_buttons ^ prev_buttons;
if (changedAxises || changedButtons) {
debouncing(bitRead(pinf, 4));
unsigned long current_time = millis(); //ONE common millis() for AXISES AND BUTTONS SECTIONS
//####################################################
if (changedAxises) { //AXISES SECTION
byte JOY_D = 0;
byte JOY_U = 0;
byte JOY_R = 0;
byte JOY_L = 0;
byte axisXchanged = 0;
byte axisYchanged = 0;
for (byte index = 0; index < 4; index++) {
if (bitRead(changedAxises, index)) {
if (current_time - prev_axis_time[index] > minimal_axis_time) {
prev_axis_time[index] = current_time;
prev_axises = current_axises;
bool btn_state = bitRead(current_axises, index);
switch (index) {
case 0: //UP
JOY_U = btn_state;
axisYchanged = 1;
break;
case 1: //DOWN
JOY_D = btn_state;
axisYchanged = 1;
break;
case 2: //LEFT
JOY_L = btn_state;
axisXchanged = 1;
break;
case 3: //RIGHT
JOY_R = btn_state;
axisXchanged = 1;
break;
}
}
}
}
if (axisYchanged) {
Joystick.setYAxis(nod(JOY_D, JOY_U) - nod(JOY_U, JOY_D));
}
if (axisXchanged) {
Joystick.setXAxis(nod(JOY_R, JOY_L) - nod(JOY_L, JOY_R));
}
}
//##############################################
if (changedButtons) { //BUTTONS SECTION
for (byte index = 0; index < 3; index++) {
if (bitRead(changedButtons, index)) {
if (current_time - prev_button_time[index] > minimal_button_time) {
prev_button_time[index] = current_time;
prev_buttons = current_buttons;
Joystick.setButton(index, bitRead(current_buttons, index));
}
}
}
}
}
Joystick.sendState(); //ONE common send.State for AXISES AND BUTTONS SECTIONS
}
byte prev_fire1_state = 0;
int counter = 0;
unsigned long prev_time = 0;
const byte probe_time_SEC = 2;
void measure_freq() {
byte pind = ~PIND;
bool current_fire1_state = bitRead(pind, FIRE1);
bool changed_fire1_state = current_fire1_state ^ prev_fire1_state;
if ( changed_fire1_state ) {
prev_fire1_state = current_fire1_state;
if ( current_fire1_state ) {
counter++;
};
}
unsigned long current_time = millis();
if ( current_time - prev_time > probe_time_SEC * 1000 ) {
Serial.print(current_time);
Serial.print(": CPS: ");
float cps = (float) counter / ( float ) probe_time_SEC;
Serial.println(cps, 1);
prev_time = millis();
counter = 0;
}
}
void loop() {
while (1) {
pinf = PINF;
if ( bitRead(pinf, 5) ) {
joystate_to_usb(joystate_update());
} else {
measure_freq();
}
}
}