Created
July 17, 2016 14:57
-
-
Save cdodd/5f744333259e689dbbfb8d8d616914f8 to your computer and use it in GitHub Desktop.
Code for a Teensy based DIY game controller
This file contains hidden or 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 <Bounce.h> | |
// ============================================================================ | |
// Begin configuration items | |
// ============================================================================ | |
// Define the numbers for the hat switch pins | |
#define HAT_UP_PIN 3 | |
#define HAT_DOWN_PIN 1 | |
#define HAT_LEFT_PIN 0 | |
#define HAT_RIGHT_PIN 2 | |
// Define the numbers for the button switch pins | |
#define BUTTON1_PIN 4 | |
#define BUTTON2_PIN 5 | |
#define BUTTON3_PIN 6 | |
#define BUTTON4_PIN 9 | |
#define BUTTON5_PIN 8 | |
#define BUTTON6_PIN 7 | |
#define BUTTON7_PIN 10 | |
#define BUTTON8_PIN 12 | |
#define BUTTON9_PIN 13 | |
#define BUTTON10_PIN 14 | |
// Define the debounce delay values (ms) for the hat and button switches | |
#define HAT_DEBOUNCE_DELAY 5 | |
#define BUTTON_DEBOUNCE_DELAY 5 | |
// ============================================================================ | |
// End of configuration items | |
// ============================================================================ | |
// Define the indices for the hat state array | |
#define HAT_UP_STATE 0 | |
#define HAT_DOWN_STATE 1 | |
#define HAT_LEFT_STATE 2 | |
#define HAT_RIGHT_STATE 3 | |
// Create an array to store the state of the hat switches | |
int hat_state[4] = {0, 0, 0, 0}; | |
// Create debounce objects for the hat switches | |
Bounce hat_up_button = Bounce(HAT_UP_PIN, HAT_DEBOUNCE_DELAY); | |
Bounce hat_down_button = Bounce(HAT_DOWN_PIN, HAT_DEBOUNCE_DELAY); | |
Bounce hat_left_button = Bounce(HAT_LEFT_PIN, HAT_DEBOUNCE_DELAY); | |
Bounce hat_right_button = Bounce(HAT_RIGHT_PIN, HAT_DEBOUNCE_DELAY); | |
// Create debounce objects for the button switches | |
Bounce button1_button = Bounce(BUTTON1_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button2_button = Bounce(BUTTON2_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button3_button = Bounce(BUTTON3_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button4_button = Bounce(BUTTON4_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button5_button = Bounce(BUTTON5_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button6_button = Bounce(BUTTON6_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button7_button = Bounce(BUTTON7_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button8_button = Bounce(BUTTON8_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button9_button = Bounce(BUTTON9_PIN, BUTTON_DEBOUNCE_DELAY); | |
Bounce button10_button = Bounce(BUTTON10_PIN, BUTTON_DEBOUNCE_DELAY); | |
// Create a map to get the hat value from the hat switch states | |
int hat_value_map[4][4] = { | |
{-1, 270, 90, -1}, | |
{0, 315, 45, -1}, | |
{180, 225, 135, -1}, | |
{-1, -1, -1, -1} | |
}; | |
// Create variables for hat_value_map lookups | |
byte x; | |
byte y; | |
// Create a flag to detect when the hat state has changed | |
boolean hat_change = false; | |
void setup() { | |
// Set the hat pins as INPUT_PULLUP | |
pinMode(HAT_UP_PIN, INPUT_PULLUP); | |
pinMode(HAT_DOWN_PIN, INPUT_PULLUP); | |
pinMode(HAT_LEFT_PIN, INPUT_PULLUP); | |
pinMode(HAT_RIGHT_PIN, INPUT_PULLUP); | |
// Set the button pins as INPUT_PULLUP | |
pinMode(BUTTON1_PIN, INPUT_PULLUP); | |
pinMode(BUTTON2_PIN, INPUT_PULLUP); | |
pinMode(BUTTON3_PIN, INPUT_PULLUP); | |
pinMode(BUTTON4_PIN, INPUT_PULLUP); | |
pinMode(BUTTON5_PIN, INPUT_PULLUP); | |
pinMode(BUTTON6_PIN, INPUT_PULLUP); | |
pinMode(BUTTON7_PIN, INPUT_PULLUP); | |
pinMode(BUTTON8_PIN, INPUT_PULLUP); | |
pinMode(BUTTON9_PIN, INPUT_PULLUP); | |
pinMode(BUTTON10_PIN, INPUT_PULLUP); | |
} | |
void loop() { | |
// Update the button objects for the hat switches | |
hat_up_button.update(); | |
hat_down_button.update(); | |
hat_left_button.update(); | |
hat_right_button.update(); | |
// Update the button objects for the button switches | |
button1_button.update(); | |
button2_button.update(); | |
button3_button.update(); | |
button4_button.update(); | |
button5_button.update(); | |
button6_button.update(); | |
button7_button.update(); | |
button8_button.update(); | |
button9_button.update(); | |
button10_button.update(); | |
// Check if any hat switches have been pressed and update the hat state array | |
if (hat_up_button.fallingEdge()) { | |
hat_state[HAT_UP_STATE] = 1; | |
hat_change = true; | |
} | |
if (hat_down_button.fallingEdge()) { | |
hat_state[HAT_DOWN_STATE] = 1; | |
hat_change = true; | |
} | |
if (hat_left_button.fallingEdge()) { | |
hat_state[HAT_LEFT_STATE] = 1; | |
hat_change = true; | |
} | |
if (hat_right_button.fallingEdge()) { | |
hat_state[HAT_RIGHT_STATE] = 1; | |
hat_change = true; | |
} | |
// Check if any hat switches have been released and update the hat state array | |
if (hat_up_button.risingEdge()) { | |
hat_state[HAT_UP_STATE] = 0; | |
hat_change = true; | |
} | |
if (hat_down_button.risingEdge()) { | |
hat_state[HAT_DOWN_STATE] = 0; | |
hat_change = true; | |
} | |
if (hat_left_button.risingEdge()) { | |
hat_state[HAT_LEFT_STATE] = 0; | |
hat_change = true; | |
} | |
if (hat_right_button.risingEdge()) { | |
hat_state[HAT_RIGHT_STATE] = 0; | |
hat_change = true; | |
} | |
if (hat_change) { | |
// Set the hat value based on the hat switch states | |
y = hat_state[HAT_UP_STATE] | (hat_state[HAT_DOWN_STATE] << 1); | |
x = hat_state[HAT_LEFT_STATE] | (hat_state[HAT_RIGHT_STATE] << 1); | |
Joystick.hat(hat_value_map[y][x]); | |
// Reset the hat state change flag | |
hat_change = false; | |
} | |
// Check if any button switches have been pressed | |
if (button1_button.fallingEdge()) { Joystick.button(1, 1); } | |
if (button2_button.fallingEdge()) { Joystick.button(2, 1); } | |
if (button3_button.fallingEdge()) { Joystick.button(3, 1); } | |
if (button4_button.fallingEdge()) { Joystick.button(4, 1); } | |
if (button5_button.fallingEdge()) { Joystick.button(5, 1); } | |
if (button6_button.fallingEdge()) { Joystick.button(6, 1); } | |
if (button7_button.fallingEdge()) { Joystick.button(7, 1); } | |
if (button8_button.fallingEdge()) { Joystick.button(8, 1); } | |
if (button9_button.fallingEdge()) { Joystick.button(9, 1); } | |
if (button10_button.fallingEdge()) { Joystick.button(10, 1); } | |
// Check if any button switches have been released | |
if (button1_button.risingEdge()) { Joystick.button(1, 0); } | |
if (button2_button.risingEdge()) { Joystick.button(2, 0); } | |
if (button3_button.risingEdge()) { Joystick.button(3, 0); } | |
if (button4_button.risingEdge()) { Joystick.button(4, 0); } | |
if (button5_button.risingEdge()) { Joystick.button(5, 0); } | |
if (button6_button.risingEdge()) { Joystick.button(6, 0); } | |
if (button7_button.risingEdge()) { Joystick.button(7, 0); } | |
if (button8_button.risingEdge()) { Joystick.button(8, 0); } | |
if (button9_button.risingEdge()) { Joystick.button(9, 0); } | |
if (button10_button.risingEdge()) { Joystick.button(10, 0); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment