mirror of
https://github.com/mcgurk/Arduino-USB-HID-RetroJoystickAdapter
synced 2025-01-08 12:18:02 -05:00
Update RetroJoystickAdapter_Atari.ino
This commit is contained in:
parent
b662206512
commit
01d9f7e4b6
@ -1,18 +1,19 @@
|
||||
|
||||
//DB9 (8=GND): 1 2 3 4 5 6 7 8 9
|
||||
const uint8_t inputPinsPort1[] = { 5, 6, 7, 8, A2, 0, 0, 0, 4};
|
||||
const uint8_t inputPinsPort2[] = {10, 16, 14, 15, A1, 0, 0, 0, 3};
|
||||
const uint8_t inputPinsPort1[] = { 5, 6, 7, 8, 0, 4, 0, 0, A2};
|
||||
const uint8_t inputPinsPort2[] = {10, 16, 14, 15, 0, 3, 0, 0, A1};
|
||||
//(1 = up, 2 = down, 3 = left, 4 = right, 6 = btn1, 9 = btn2)
|
||||
|
||||
//#define DEBUG
|
||||
#define DEBUG
|
||||
|
||||
inline void translateState(uint8_t *data, uint8_t *state) {
|
||||
state[0] = !bitRead(data[0], 4);
|
||||
inline void translateState(uint8_t data, uint8_t *state) {
|
||||
state[0] = !bitRead(data, 4) | !bitRead(data, 5)<<1;
|
||||
state[1] = 127;
|
||||
state[2] = 127;
|
||||
if (!bitRead(data[0], 0)) state[2] = 0; /* up */
|
||||
if (!bitRead(data[0], 1)) state[2] = 255; /* down */
|
||||
if (!bitRead(data[0], 2)) state[1] = 0; /* left */
|
||||
if (!bitRead(data[0], 3)) state[1] = 255; /* right */
|
||||
if (!bitRead(data, 0)) state[2] = 0; /* up */
|
||||
if (!bitRead(data, 1)) state[2] = 255; /* down */
|
||||
if (!bitRead(data, 2)) state[1] = 0; /* left */
|
||||
if (!bitRead(data, 3)) state[1] = 255; /* right */
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +34,7 @@ inline void translateState(uint8_t *data, uint8_t *state) {
|
||||
#define JOYSTICK_REPORT_ID 0x04
|
||||
#define JOYSTICK2_REPORT_ID 0x05
|
||||
|
||||
#define JOYSTICK_DATA_SIZE 1
|
||||
//#define JOYSTICK_DATA_SIZE 1
|
||||
#define JOYSTICK_STATE_SIZE 3
|
||||
|
||||
|
||||
@ -48,14 +49,14 @@ inline void translateState(uint8_t *data, uint8_t *state) {
|
||||
0x09, 0x04, /* USAGE (Joystick) */ \
|
||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
||||
0x85, REPORT_ID, /* REPORT_ID */ \
|
||||
/* 8 Buttons */ \
|
||||
/* 2 Buttons */ \
|
||||
0x05, 0x09, /* USAGE_PAGE (Button) */ \
|
||||
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ \
|
||||
0x29, 0x01, /* USAGE_MAXIMUM (Button 1) */ \
|
||||
0x29, 0x02, /* USAGE_MAXIMUM (Button 2) */ \
|
||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
||||
0x95, 0x08, /* REPORT_COUNT (8) */ \
|
||||
0x95, 0x08, /* REPORT_COUNT (8) (full byte) */ \
|
||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
||||
/* X and Y Axis */ \
|
||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \
|
||||
@ -85,13 +86,15 @@ class Joystick_ {
|
||||
private:
|
||||
uint8_t joystickId;
|
||||
uint8_t reportId;
|
||||
uint8_t olddata[JOYSTICK_DATA_SIZE];
|
||||
//uint8_t olddata[JOYSTICK_DATA_SIZE];
|
||||
uint8_t olddata;
|
||||
uint8_t state[JOYSTICK_STATE_SIZE];
|
||||
uint8_t flag;
|
||||
|
||||
public:
|
||||
uint8_t type;
|
||||
uint8_t data[JOYSTICK_DATA_SIZE];
|
||||
//uint8_t data[JOYSTICK_DATA_SIZE];
|
||||
uint8_t data;
|
||||
|
||||
Joystick_(uint8_t initJoystickId, uint8_t initReportId) {
|
||||
// Setup HID report structure
|
||||
@ -107,16 +110,20 @@ public:
|
||||
joystickId = initJoystickId;
|
||||
reportId = initReportId;
|
||||
|
||||
data[0] = 0;
|
||||
data[1] = 0;
|
||||
memcpy(olddata, data, JOYSTICK_DATA_SIZE);
|
||||
//data[0] = 0;
|
||||
//data[1] = 0;
|
||||
data = 0;
|
||||
//memcpy(olddata, data, JOYSTICK_DATA_SIZE);
|
||||
olddata = data;
|
||||
translateState(data, state);
|
||||
sendState(1);
|
||||
}
|
||||
|
||||
void updateState() {
|
||||
if (memcmp(olddata, data, JOYSTICK_DATA_SIZE)) {
|
||||
memcpy(olddata, data, JOYSTICK_DATA_SIZE);
|
||||
//if (memcmp(olddata, data, JOYSTICK_DATA_SIZE)) {
|
||||
//memcpy(olddata, data, JOYSTICK_DATA_SIZE);
|
||||
if (olddata != data) {
|
||||
olddata = data;
|
||||
translateState(data, state);
|
||||
flag = 1;
|
||||
}
|
||||
@ -164,21 +171,23 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
|
||||
Joystick[0].data[0] = 0xff;
|
||||
Joystick[1].data[0] = 0xff;
|
||||
Joystick[0].data = 0xff;
|
||||
Joystick[1].data = 0xff;
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
bitWrite(Joystick[0].data[0], i, digitalRead(inputPinsPort1[i])); //AXES1
|
||||
bitWrite(Joystick[1].data[0], i, digitalRead(inputPinsPort2[i])); //AXES2
|
||||
bitWrite(Joystick[0].data, i, digitalRead(inputPinsPort1[i])); //AXES1
|
||||
bitWrite(Joystick[1].data, i, digitalRead(inputPinsPort2[i])); //AXES2
|
||||
}
|
||||
|
||||
bitWrite(Joystick[0].data[0], 4, digitalRead(inputPinsPort1[8]));
|
||||
bitWrite(Joystick[1].data[0], 4, digitalRead(inputPinsPort2[8]));
|
||||
bitWrite(Joystick[0].data, 4, digitalRead(inputPinsPort1[5]));
|
||||
bitWrite(Joystick[0].data, 5, digitalRead(inputPinsPort1[8]));
|
||||
bitWrite(Joystick[1].data, 4, digitalRead(inputPinsPort2[5]));
|
||||
bitWrite(Joystick[1].data, 5, digitalRead(inputPinsPort2[8]));
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print(" data0 j1: 0x"); Serial.print(Joystick[0].data[0], HEX);
|
||||
Serial.print(" data0 j2: 0x"); Serial.print(Joystick[1].data[0], HEX);
|
||||
Serial.print(" data0 j1: 0x"); Serial.print(Joystick[0].data, BIN);
|
||||
Serial.print(" data0 j2: 0x"); Serial.print(Joystick[1].data, BIN);
|
||||
Serial.println();
|
||||
delay(50);
|
||||
Serial.flush();
|
||||
|
Loading…
Reference in New Issue
Block a user