Instantly share code, notes, and snippets.
Created
June 29, 2021 00:07
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save killa-kyle/d9d996962ba13faad4a29913e93ef0f1 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> | |
#include <Keypad.h> | |
//config variables | |
#define NUM_BTN_COLUMNS (4) | |
#define NUM_BTN_ROWS (4) | |
#define MAX_DEBOUNCE (3) | |
// Global variables | |
char keys[NUM_BTN_ROWS][NUM_BTN_COLUMNS] = { | |
{'1', '2', '3', 'A'}, | |
{'4', '5', '6', 'B'}, | |
{'7', '8', '9', 'C'}, | |
{'*', '0', '#', 'D'}}; | |
static const uint8_t btncolumnpins[NUM_BTN_COLUMNS] = {15, 14, 16, 10}; | |
static const uint8_t btnrowpins[NUM_BTN_ROWS] = {A3, A2, A1, A0}; | |
Keypad buttbx = Keypad(makeKeymap(keys), btnrowpins, btncolumnpins, NUM_BTN_ROWS, NUM_BTN_COLUMNS); | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, | |
16, 0, // button count , hatswitch count | |
false, false, false, // x, y, z axis | |
false, false, false, // no Rx Ry or Rz | |
false, false, // no rudder or throttle | |
false, false, false); // no accelerator, brake, or steering | |
static int8_t debounce_count[NUM_BTN_COLUMNS][NUM_BTN_ROWS]; | |
static void setuppins() | |
{ | |
uint8_t i; | |
Serial.print("Starting Setup..."); | |
// initialize | |
// select lines | |
// button columns | |
for (i = 0; i < NUM_BTN_COLUMNS; i++) | |
{ | |
pinMode(btncolumnpins[i], OUTPUT); | |
// with nothing selected by default | |
digitalWrite(btncolumnpins[i], HIGH); | |
} | |
// button row input lines | |
for (i = 0; i < NUM_BTN_ROWS; i++) | |
{ | |
pinMode(btnrowpins[i], INPUT_PULLUP); | |
} | |
// Initialize the debounce counter array | |
for (uint8_t i = 0; i < NUM_BTN_COLUMNS; i++) | |
{ | |
for (uint8_t j = 0; j < NUM_BTN_ROWS; j++) | |
{ | |
debounce_count[i][j] = 0; | |
} | |
} | |
} | |
static void scan() | |
{ | |
static uint8_t current = 0; | |
uint8_t val; | |
uint8_t i, j; | |
// Select current columns | |
digitalWrite(btncolumnpins[current], LOW); | |
// pause a moment | |
delay(1); | |
// Read the button inputs | |
for (j = 0; j < NUM_BTN_ROWS; j++) | |
{ | |
val = digitalRead(btnrowpins[j]); | |
if (val == LOW) | |
{ | |
// active low: val is low when btn is pressed | |
if (debounce_count[current][j] < MAX_DEBOUNCE) | |
{ | |
debounce_count[current][j]++; | |
if (debounce_count[current][j] == MAX_DEBOUNCE) | |
{ | |
Serial.print("Button "); | |
Serial.println((current * NUM_BTN_ROWS) + j); | |
// button is pressed | |
Joystick.setButton(((current * NUM_BTN_ROWS) + j), 1); | |
} | |
} | |
} | |
else | |
{ | |
// otherwise, button is released | |
if (debounce_count[current][j] > 0) | |
{ | |
debounce_count[current][j]--; | |
if (debounce_count[current][j] == 0) | |
{ | |
// Serial.print("Key Up "); | |
// Serial.println((current * NUM_BTN_ROWS) + j); | |
//button is released | |
Joystick.setButton(((current * NUM_BTN_ROWS) + j), 0); | |
} | |
} | |
} | |
} // for j = 0 to 3; | |
delay(1); | |
digitalWrite(btncolumnpins[current], HIGH); | |
current++; | |
if (current >= NUM_BTN_COLUMNS) | |
{ | |
current = 0; | |
} | |
} | |
void setup() | |
{ | |
// initialize serial communication: | |
Serial.begin(9600); | |
Serial.println("Starting Setup..."); | |
Joystick.begin(); | |
// setup hardware | |
setuppins(); | |
Serial.println("Setup Complete."); | |
} | |
void loop() | |
{ | |
scan(); | |
} | |
// | |
//void handleKeys(){ | |
// | |
// | |
// char key = buttbx.getKey(); | |
// if(key) // Check for a valid key. | |
// { | |
// Serial.print("Key: "); | |
// Serial.print(key); | |
// Serial.println(); | |
// switch (key) | |
// { | |
// case '*': | |
// Serial.println(key); | |
// break; | |
// case '#': | |
// Serial.println(key); | |
// break; | |
// default: | |
// Serial.println(key); | |
// } | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment