Created
November 4, 2019 00:25
-
-
Save jgkong/bca0d64e7e4af1cacc2a1e44fad0eae3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Joystick.h> | |
#define COLS 4 | |
#define ROWS 4 | |
#define PINS COLS+ROWS | |
#define BTNS COLS*ROWS | |
#define BTNCOUNT 12 | |
// Pins 1-7 of the keypad connected to the Arduino respectively: | |
int timeout; // timeout variable used in loop | |
int rowPins[ROWS] = {2, 3, 8, 9}; | |
int colPins[COLS] = {4, 5, 6, 7}; | |
enum _JOY_NUM { | |
JOY_UP, | |
JOY_RIGHT, | |
JOY_DOWN, | |
JOY_LEFT, | |
JOY_L2, | |
JOY_R2, | |
JOY_LTHUMB, | |
JOY_RTHUMB, | |
JOY_L, | |
JOY_X, | |
JOY_START, | |
JOY_A, | |
JOY_Y, | |
JOY_R, | |
JOY_B, | |
JOY_SELECT | |
}; | |
#define PAD_A 1 | |
#define PAD_B 2 | |
#define PAD_X 3 | |
#define PAD_Y 0 | |
#define PAD_L 4 | |
#define PAD_R 5 | |
#define PAD_SELECT 8 | |
#define PAD_START 9 | |
#define PAD_LTHUMB 6 | |
#define PAD_RTHUMB 7 | |
#define PAD_L2 10 | |
#define PAD_R2 11 | |
#define PAD_HAT_START 100 | |
#define PAD_UP 100 | |
#define PAD_RIGHT 101 | |
#define PAD_DOWN 102 | |
#define PAD_LEFT 103 | |
#define PAD_HAT_END 103 | |
// ppsspp | |
//int BTNMAP[BTNS] = {2, 8, 0, 7, 6, 3, 9, 1, PAD_DOWN, PAD_RIGHT, PAD_UP, PAD_LEFT, 8, 9, 10, 11}; | |
int BTNMAP[BTNS]; | |
int PADMAP[5]; | |
int prev[BTNS]; | |
int val; | |
int num; | |
Joystick_ Joystick( | |
JOYSTICK_DEFAULT_REPORT_ID, // hidReportId | |
JOYSTICK_TYPE_GAMEPAD, // joystickType - JOYSTICK_TYPE_JOYSTICK, JOYSTICK_TYPE_GAMEPAD, JOYSTICK_TYPE_MULTI_AXIS | |
BTNS, // buttonCount | |
1, // hatSwitchCount | |
false, // includeXAxis | |
false, // includeYAxis | |
false, // includeZAxis | |
false, // includeRxAxis | |
false, // includeRyAxis | |
false, // includeRzAxis | |
false, // includeRudder | |
false, // includeThrottle | |
false, // includeAccelerator | |
false, // includeBrake | |
false // includeSteering | |
); | |
void setup() | |
{ | |
BTNMAP[JOY_LEFT] = PAD_LEFT; | |
BTNMAP[JOY_UP] = PAD_UP; | |
BTNMAP[JOY_RIGHT] = PAD_RIGHT; | |
BTNMAP[JOY_DOWN] = PAD_DOWN; | |
BTNMAP[JOY_A] = PAD_A; | |
BTNMAP[JOY_B] = PAD_B; | |
BTNMAP[JOY_X] = PAD_X; | |
BTNMAP[JOY_Y] = PAD_Y; | |
BTNMAP[JOY_L] = PAD_L; | |
BTNMAP[JOY_R] = PAD_R; | |
BTNMAP[JOY_SELECT] = PAD_SELECT; | |
BTNMAP[JOY_START] = PAD_START; | |
BTNMAP[JOY_LTHUMB] = PAD_LTHUMB; | |
BTNMAP[JOY_RTHUMB] = PAD_RTHUMB; | |
BTNMAP[JOY_L2] = PAD_L2; | |
BTNMAP[JOY_R2] = PAD_R2; | |
for (int i = 0; i < BTNS; i++) { | |
prev[i] = 0; | |
if (BTNMAP[i] >= PAD_HAT_START) { | |
PADMAP[BTNMAP[i] - PAD_HAT_START] = i; | |
} | |
} | |
PADMAP[4] = PADMAP[0]; | |
resetPins(); | |
Joystick.begin(); | |
} | |
void loop() | |
{ | |
checkJoystickStatus(); // read which buttons are pressed | |
delay(2); | |
} | |
void resetPins() { | |
for (int i = 0; i < ROWS; i++) { | |
pinMode(rowPins[i], INPUT); // Set all keypad pins as inputs | |
digitalWrite(rowPins[i], HIGH); // pull all keypad pins high | |
} | |
for (int i = 0; i < COLS; i++) { | |
pinMode(colPins[i], INPUT); // Set all keypad pins as inputs | |
digitalWrite(colPins[i], HIGH); // pull all keypad pins high | |
} | |
} | |
void sendChopung() { | |
Joystick.setHatSwitch(0, 90); | |
delay(5); | |
Joystick.setHatSwitch(0, -1); | |
delay(5); | |
Joystick.setHatSwitch(0, 180); | |
delay(3); | |
Joystick.setHatSwitch(0, 135); | |
Joystick.setButton(PAD_Y, 1); | |
delay(5); | |
Joystick.setHatSwitch(0, -1); | |
Joystick.setButton(PAD_Y, 0); | |
} | |
void sendChange(int n, int v) { | |
if (n == JOY_L2 && v) { | |
sendChopung(); | |
} | |
if (BTNMAP[n] < PAD_HAT_START) { // Button | |
Joystick.setButton(BTNMAP[n], v); | |
} else { // Joystick | |
int radius = -1; | |
for (int i = 0; i < 4; i++) { | |
if (prev[PADMAP[i]]) { | |
radius = i * 90; | |
if (prev[PADMAP[i + 1]]) { | |
radius += 45; | |
} | |
if (i > 0 || !prev[PADMAP[3]]) { | |
break; | |
} | |
} | |
} | |
Joystick.setHatSwitch(0, radius); | |
} | |
} | |
/* checkJoystickStatus(): This function returns an int that represents | |
the status of the 12-button keypad. Only the 12 LSb's of the return | |
value hold any significange. Each bit represents the status of a single | |
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ..., | |
'#' is bit 11. | |
This function doesn't work for multitouch. | |
*/ | |
void checkJoystickStatus() | |
{ | |
int keypadStatus = 0; // this will be what's returned | |
/* initialize all pins, inputs w/ pull-ups */ | |
resetPins(); | |
for (int row = 0; row < ROWS; row++) { // initial for loop to check all 4 rows | |
pinMode(rowPins[row], OUTPUT); // set the row pin as an output | |
digitalWrite(rowPins[row], LOW); // pull the row pins low | |
for (int col = 0; col < COLS; col++) { // embedded for loop to check all 3 columns of each row | |
val = 1 - digitalRead(colPins[col]); | |
num = row * ROWS + col; | |
if (prev[num] != val) { | |
prev[num] = val; | |
sendChange(num, val); | |
} | |
} | |
pinMode(rowPins[row], INPUT); // reset the row pin as an input | |
digitalWrite(rowPins[row], HIGH); // pull the row pin high | |
} | |
return keypadStatus; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment