Skip to content

Commit

Permalink
Input: Refactor input type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 committed Nov 22, 2021
1 parent c3dae9b commit 74cc8fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 66 deletions.
83 changes: 28 additions & 55 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
if (response == NULL)
return 0;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
//Prep N64 response
n64_buttonmap *state = (n64_buttonmap *)response;
Expand Down Expand Up @@ -481,7 +481,7 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
}
}
#if (MAX_MICE >= 1)
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
//Prep N64 response
n64_buttonmap *state = (n64_buttonmap *)response;
Expand Down Expand Up @@ -517,7 +517,7 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
#endif

#if (MAX_KB >= 1)
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
//Prep N64 response
n64_randnet_kb *state = (n64_randnet_kb *)response;
Expand Down Expand Up @@ -560,7 +560,7 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
#endif

#if (ENABLE_HARDWIRED_CONTROLLER >=1)
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
n64_buttonmap *state = (n64_buttonmap *)response;
state->dButtons = 0;
Expand Down Expand Up @@ -599,13 +599,13 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
void input_apply_rumble(int id, uint8_t strength)
{
JoystickController *joy;
if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
joy = (JoystickController *)input_devices[id].driver;
joy->setRumble(strength, strength, 20);
}
#if (ENABLE_HARDWIRED_CONTROLLER >=1)
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
(strength > 0) ? digitalWrite(HW_RUMBLE, HIGH) : digitalWrite(HW_RUMBLE, LOW);
}
Expand All @@ -618,15 +618,15 @@ bool input_is_connected(int id)
if (_check_id(id) == 0)
return false;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
JoystickController *joy = (JoystickController *)input_devices[id].driver;
if (*joy == true)
connected = true;
}

#if (MAX_MICE >=1)
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
USBHIDInput *mouse = (USBHIDInput *)input_devices[id].driver;
if (*mouse == true)
Expand All @@ -635,7 +635,7 @@ bool input_is_connected(int id)
#endif

#if (MAX_KB >=1)
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
USBHIDInput *kb = (USBHIDInput *)input_devices[id].driver;
if (*kb == true)
Expand All @@ -644,7 +644,7 @@ bool input_is_connected(int id)
#endif

#if (ENABLE_HARDWIRED_CONTROLLER >=1)
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
if (digitalRead(HW_EN) == 0)
connected = true;
Expand All @@ -654,38 +654,11 @@ bool input_is_connected(int id)
return connected;
}

bool input_is_mouse(int id)
bool input_is(int id, input_type_t type)
{
if (_check_id(id) == 0)
return false;
if (input_devices[id].type == USB_MOUSE)
return true;
return false;
}

bool input_is_kb(int id)
{
if (_check_id(id) == 0)
return false;
if (input_devices[id].type == USB_KB)
return true;
return false;
}

bool input_is_gamecontroller(int id)
{
if (_check_id(id) == 0)
return false;
if (input_devices[id].type == USB_GAMECONTROLLER)
return true;
return false;
}

bool input_is_hw_gamecontroller(int id)
{
if (_check_id(id) == 0)
return false;
if (input_devices[id].type == HW_GAMECONTROLLER)
if (input_devices[id].type == type)
return true;
return false;
}
Expand All @@ -695,22 +668,22 @@ uint16_t input_get_id_product(int id)
if (_check_id(id) == 0 || input_is_connected(id) == 0)
return 0;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
JoystickController *joy = (JoystickController *)input_devices[id].driver;
return joy->idProduct();
}
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
USBHIDInput *mouse = (USBHIDInput *)input_devices[id].driver;
return mouse->idProduct();
}
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
USBHIDInput *kb = (USBHIDInput *)input_devices[id].driver;
return kb->idProduct();
}
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
return 0xBEEF;
}
Expand All @@ -723,22 +696,22 @@ uint16_t input_get_id_vendor(int id)
if (_check_id(id) == 0 || input_is_connected(id) == 0)
return 0;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
JoystickController *joy = (JoystickController *)input_devices[id].driver;
return joy->idVendor();
}
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
USBHIDInput *mouse = (USBHIDInput *)input_devices[id].driver;
return mouse->idVendor();
}
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
USBHIDInput *kb = (USBHIDInput *)input_devices[id].driver;
return kb->idVendor();
}
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
return 0xDEAD;
}
Expand All @@ -752,22 +725,22 @@ const char *input_get_manufacturer_string(int id)
if (_check_id(id) == 0 || input_is_connected(id) == false)
return NC;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
JoystickController *joy = (JoystickController *)input_devices[id].driver;
return (const char *)joy->manufacturer();
}
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
USBHIDInput *mouse = (USBHIDInput *)input_devices[id].driver;
return (const char *)mouse->manufacturer();
}
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
USBHIDInput *kb = (USBHIDInput *)input_devices[id].driver;
return (const char *)kb->manufacturer();
}
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
return "USB64";
}
Expand All @@ -781,22 +754,22 @@ const char *input_get_product_string(int id)
if (_check_id(id) == 0 || input_is_connected(id) == 0)
return NC;

if (input_is_gamecontroller(id))
if (input_is(id, USB_GAMECONTROLLER))
{
JoystickController *joy = (JoystickController *)input_devices[id].driver;
return (const char *)joy->product();
}
else if (input_is_mouse(id))
else if (input_is(id, USB_MOUSE))
{
USBHIDInput *mouse = (USBHIDInput *)input_devices[id].driver;
return (const char *)mouse->product();
}
else if (input_is_kb(id))
else if (input_is(id, USB_KB))
{
USBHIDInput *kb = (USBHIDInput *)input_devices[id].driver;
return (const char *)kb->product();
}
else if (input_is_hw_gamecontroller(id))
else if (input_is(id, HW_GAMECONTROLLER))
{
return "HARDWIRED";
}
Expand Down
11 changes: 4 additions & 7 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,25 @@ static const randnet_map_t randnet_map[] = {
{KEY_RIGHT, 0x0405}, //Right Cursor
};

enum
typedef enum
{
USB_MOUSE,
USB_KB,
USB_GAMECONTROLLER,
HW_GAMECONTROLLER,
I2C_GAMECONTROLLER
};
} input_type_t;

typedef struct
{
void *driver;
int type;
input_type_t type;
} input;

void input_init();
void input_update_input_devices();
bool input_is_connected(int id);
bool input_is_mouse(int id);
bool input_is_kb(int id);
bool input_is_gamecontroller(int id);
bool input_is_hw_gamecontroller(int id);
bool input_is(int id, input_type_t type);
uint16_t input_get_id_product(int id);
uint16_t input_get_id_vendor(int id);
const char *input_get_manufacturer_string(int id);
Expand Down
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void loop()
}
n64_in_dev[c].interrupt_attached = true;
}
if (input_is_gamecontroller(c))
if (input_is(c, USB_GAMECONTROLLER))
{
n64_buttonmap *new_state = (n64_buttonmap *)n64_response[c];
input_get_state(c, new_state, &n64_combo);
Expand Down Expand Up @@ -191,7 +191,7 @@ void loop()
}
}
#if (MAX_MICE >= 1)
else if (input_is_mouse(c))
else if (input_is(c, USB_MOUSE))
{
n64_buttonmap *new_state = (n64_buttonmap *)n64_response[c];
input_get_state(c, new_state, &n64_combo);
Expand All @@ -207,7 +207,7 @@ void loop()
}
#endif
#if (MAX_KB >= 1)
else if (input_is_kb(c))
else if (input_is(c, USB_KB))
{
n64_randnet_kb *new_state = (n64_randnet_kb *)n64_response[c];
//Maintain the old led state
Expand All @@ -233,7 +233,7 @@ void loop()

//Get a copy of the latest n64 button presses to handle the below combos
uint16_t n64_buttons = 0;
if (input_is_gamecontroller(c))
if (input_is(c, USB_GAMECONTROLLER))
{
n64_buttonmap *new_state = (n64_buttonmap *)n64_response[c];
n64_buttons = new_state->dButtons;
Expand Down

0 comments on commit 74cc8fd

Please sign in to comment.