HID Project lib and dpad

This commit is contained in:
Tropicananass 2020-04-19 16:37:55 +02:00
parent 28d9217385
commit 5680e44847
3 changed files with 66 additions and 21 deletions

View File

@ -9,7 +9,7 @@
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env] [env]
lib_deps = Joystick lib_deps = HID-Project
[env:sparkfun_promicro16] [env:sparkfun_promicro16]
platform = atmelavr platform = atmelavr

BIN
res/pro_micro_pinout.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

View File

@ -1,7 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <Joystick.h> #include <HID-Project.h>
#define NUM_KEYS 12
uint8_t const pin_lr{2}; uint8_t const pin_lr{2};
uint8_t const pin_rr{A3}; uint8_t const pin_rr{A3};
@ -16,32 +14,79 @@ uint8_t const pin_a{14};
uint8_t const pin_select{9}; uint8_t const pin_select{9};
uint8_t const pin_start{10}; uint8_t const pin_start{10};
uint8_t const pin_array[] = {pin_lr, pin_rr, pin_left , pin_up , pin_right , pin_down , pin_y, pin_x, pin_b, pin_a, pin_select, pin_start}; uint8_t const ButtonNum{8};
// uin8__t const key_array[] = {0, 1, 2, 3, 4, 5, }; uint8_t const ButtonPin[ButtonNum] = {pin_lr, pin_rr, pin_y, pin_x,
// int const key_array[] = {'r' , 'l' , GAMEPAD_DPAD_LEFT, GAMEPAD_DPAD_UP, GAMEPAD_DPAD_RIGHT, GAMEPAD_DPAD_DOWN, 'y' , 'x' , 'b' , 'a' , KEY_RETURN, ' ' }; pin_b, pin_a, pin_select, pin_start};
bool states [] = {LOW , LOW , LOW , LOW , LOW , LOW , LOW , LOW , LOW , LOW , LOW , LOW }; uint8_t const ButtonKey[ButtonNum] = {11, 12, 13, 14, 15, 16, 17, 18};
bool buttonPreviousState[ButtonNum];
Joystick_ Joystick; enum DPad_e { DPadUp = 0, DPadLeft = 1, DPadRight = 2, DPadDown = 3 };
uint8_t const DPadNum{DPadDown + 1};
uint8_t const DPadPin[DPadNum] = {pin_up, pin_left, pin_right, pin_down};
bool dPadState[DPadNum];
void setup() { void setup() {
Joystick.begin(); for (size_t i = 0; i < ButtonNum; ++i) {
for (size_t i = 0; i < NUM_KEYS; ++i) { pinMode(ButtonPin[i], INPUT_PULLUP);
pinMode(pin_array[i], INPUT_PULLUP); buttonPreviousState[i] = digitalRead(ButtonPin[i]);
states[i] = digitalRead(pin_array[i]);
} }
for (size_t i = 0; i < DPadNum; ++i) {
pinMode(DPadPin[i], INPUT_PULLUP);
dPadState[i] = digitalRead(DPadPin[i]);
}
Gamepad.begin();
} }
void loop() { void loop() {
bool current_state; for (size_t i = 0; i < ButtonNum; ++i) {
for (size_t i = 0; i < NUM_KEYS; ++i) { bool buttonCurrentState = digitalRead(ButtonPin[i]);
current_state = digitalRead(pin_array[i]); if (buttonCurrentState != buttonPreviousState[i]) {
if (current_state != states[i]) { if (buttonCurrentState == LOW) {
if (current_state == LOW) { Gamepad.press(ButtonKey[i]);
Joystick.pressButton(i);
} else { } else {
Joystick.releaseButton(i); Gamepad.release(ButtonKey[i]);
} }
states[i] = current_state;
} }
buttonPreviousState[i] = buttonCurrentState;
} }
for (size_t i = 0; i < DPadNum; ++i) {
bool dPadCurrentState = digitalRead(DPadPin[i]);
dPadState[i] = dPadCurrentState;
}
if (dPadState[DPadUp] != dPadState[DPadDown]) {
if (dPadState[DPadUp] == LOW) {
if (dPadState[DPadLeft] == dPadState[DPadRight]) {
Gamepad.dPad1(GAMEPAD_DPAD_UP);
} else if (dPadState[DPadLeft] == LOW) {
Gamepad.dPad1(GAMEPAD_DPAD_UP_LEFT);
} else {
Gamepad.dPad1(GAMEPAD_DPAD_UP_RIGHT);
}
} else {
if (dPadState[DPadDown] == LOW) {
if (dPadState[DPadLeft] == dPadState[DPadRight]) {
Gamepad.dPad1(GAMEPAD_DPAD_DOWN);
} else if (dPadState[DPadLeft] == LOW) {
Gamepad.dPad1(GAMEPAD_DPAD_DOWN_LEFT);
} else {
Gamepad.dPad1(GAMEPAD_DPAD_DOWN_RIGHT);
}
}
}
} else if (dPadState[DPadLeft] != dPadState[DPadRight]) {
if (dPadState[DPadLeft] == LOW) {
Gamepad.dPad1(GAMEPAD_DPAD_LEFT);
} else {
Gamepad.dPad1(GAMEPAD_DPAD_RIGHT);
}
} else {
Gamepad.dPad1(GAMEPAD_DPAD_CENTERED);
}
Gamepad.write();
} }