Skip to content

Instantly share code, notes, and snippets.

@assertivist
Last active September 26, 2017 00:01
Show Gist options
  • Save assertivist/430924f1c5f31c93d5c397dc55a460ce to your computer and use it in GitHub Desktop.
Save assertivist/430924f1c5f31c93d5c397dc55a460ce to your computer and use it in GitHub Desktop.
//#define MIDI_CHANNEL 1
int buttons[8][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
const uint16_t keymap[8][16] = {
{KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, ' ', ' ', ' ', ' '},
{KEY_ESC, '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', ' ', ' ', ' ', ' '},
{KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, ' ', ' ', ' ', ' '},
{'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', KEY_BACKSPACE, KEY_PAGE_UP, KEY_INSERT},
{KEY_TAB, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\', KEY_PAGE_DOWN, KEY_DELETE},
{KEY_CAPS_LOCK, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', KEY_ENTER, ' ', KEY_UP, ' '},
{KEY_LEFT_SHIFT, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '/', KEY_RIGHT_SHIFT, ' ', KEY_UP, ' ',},
{KEY_LEFT_CTRL, KEY_LEFT_GUI, KEY_LEFT_ALT, KEY_BACKSPACE, ' ', KEY_RETURN, KEY_HOME, ' ', KEY_END, KEY_RIGHT_ALT, KEY_RIGHT_GUI, KEY_RIGHT_CTRL, ' ', KEY_LEFT, KEY_DOWN, KEY_RIGHT}
};
const int cols = 16;
const int col_pins[16] = {
17, 16, 15, 14, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};
const int rows = 8;
const int row_pins[8] = {
18, 19, 20, 21, 22, 23, 37, 38
};
void select_col_pin(int selected_pin) {
for (int pin = 0; pin < cols; ++pin) {
digitalWrite(col_pins[pin], LOW);
}
digitalWrite(col_pins[selected_pin], HIGH);
}
const int KEEB_LAYER = 0;
//const int MIDI_LAYER = 1;
//int layer = MIDI_LAYER;
void keyscan() {
int current_button = 0;
int prev_button = 0;
for (int col = 0; col < cols; ++col) {
select_col_pin(col);
delay(3);
for (int row = 0; row < rows; ++row) {
if ( digitalRead(row_pins[row]) == HIGH) {
current_button = 1;
}
else {
current_button = 0;
}
prev_button = buttons[row][col];
if (prev_button != current_button) {
buttons[row][col] = current_button;
if (layer == KEEB_LAYER) {
do_key(row, col, current_button);
}
//else if (layer == MIDI_LAYER) {
//do_midi(row, col, current_button);
//}
}
}
}
layer = buttons[7][12];
//while (usbMIDI.read()) {
//}
}
void do_key(int row, int col, int current_button) {
/*if (row == 1 || row == 0) {
if (current_button == 1) {
Keyboard.press(KEY_LEFT_GUI);
if (row == 0) {
Keyboard.press(KEY_LEFT_SHIFT);
}
Keyboard.press(49 + col);
}
else {
Keyboard.release(49 + col);
if (row == 0) {
Keyboard.release(KEY_LEFT_SHIFT);
}
Keyboard.release(KEY_LEFT_GUI);
}
return;
}*/
if (current_button == 1) {
Keyboard.press(keymap[row][col]);
}
else {
Keyboard.release(keymap[row][col]);
}
}
/*
void do_midi(int row, int col, int current_button) {
int note = 21 + (row * 16) + col;
if (current_button == 1) {
usbMIDI.sendNoteOn(note, 99, MIDI_CHANNEL);
}
else {
usbMIDI.sendNoteOff(note, 0, MIDI_CHANNEL);
}
}
*/
const int led = LED_BUILTIN;
void setup() {
// Columns are output
for (int i = 0; i < cols; ++i) {
pinMode(col_pins[i], OUTPUT);
}
// Rows are input
for (int j = 0; j < rows; ++j) {
pinMode(row_pins[j], INPUT_PULLDOWN);
}
pinMode(led, OUTPUT);
Serial.begin(9600);
delay(1000);
}
int switch_layer = 0;
void loop() {
keyscan();
digitalWrite(led, HIGH);
delay(20);
digitalWrite(led, LOW);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment