├── README.md └── keyboard.ino /README.md: -------------------------------------------------------------------------------- 1 | This repo is really basic firmware for testing a custom mechanical keyboard. Right now it is configured for my 7x4 matrix, but that is defined only by the ROWS and COLS constants at the top and the defined pins. It should theoretically work for any size layout. 2 | 3 | Furthermore, it may require pulldown resistors connecting the rows to ground. Before adding those I was having issues involving pins spontaneously reading high when they shouldn't. It may not be neccesarry, but if you don't use them and have issues, try adding them. 4 | 5 | For development, I used a teensy 3.0, although it should run identially on a 3.1 and should run (possibly with minor modifications) on a 2.0 as well. 6 | -------------------------------------------------------------------------------- /keyboard.ino: -------------------------------------------------------------------------------- 1 | 2 | // Teensy 2.0 has the LED on pin 11 3 | // Teensy++ 2.0 has the LED on pin 6 4 | // Teensy 3.0 has the LED on pin 13 5 | const int ledPin = 13; 6 | 7 | const byte ROWS = 2; // Currently using only a 2x2 matrix test board 8 | const byte COLS = 2; 9 | int LAYERS = 2; 10 | 11 | bool toggleBind = false; 12 | int currLayer = 0; 13 | int prevLayer = 0; 14 | int desiredLayer = 1; 15 | 16 | char layout[][ROWS][COLS] = { 17 | { 18 | 19 | //layer 0 20 | {KEY_A,'^'}, // '^' is defined as fn layer key, when held the the layer goes to the desired layer 21 | {KEY_W,'#'} 22 | },{ 23 | 24 | //layer 1 25 | {KEY_LEFT_ARROW,'^'}, 26 | {KEY_RIGHT_ARROW,'#'} 27 | } 28 | 29 | }; 30 | 31 | byte row[ROWS] = {19,20}; 32 | byte col[COLS] = {2,3}; 33 | 34 | int key[] = {0,0,0,0,0,0}; 35 | char mod[] = {0,0}; 36 | 37 | 38 | 39 | void setup() { 40 | // initialize the digital pin as an output. 41 | pinMode(ledPin, OUTPUT); 42 | for (int c = 0; c < COLS; c++){ 43 | pinMode(col[c], OUTPUT); 44 | } 45 | for (int r = 0; r < ROWS; r++){ 46 | pinMode(row[r], INPUT); 47 | } 48 | } 49 | 50 | // This function will take keypresses passed to it (in the form of a char, for no particular reason) 51 | // and add them to set of six keys that will be passed to the computer when Keyboard.send_now() is called. 52 | 53 | // Basically, this collects the currently pressed keys and stores them until they can be passed to the computer. 54 | void setKey(char keypress){ 55 | 56 | // Look for unused keys in the buffer 57 | int i, j; 58 | for(i = 0; key[i] != 0; i++){} 59 | for(j = 0; mod[j] != 0; j++){} 60 | 61 | // Catch Modifiers 62 | if(keypress == 176){ 63 | mod[j] = KEY_LEFT_CTRL; 64 | } 65 | else if(keypress == 177){ 66 | mod[j] = KEY_LEFT_ALT; 67 | } 68 | else if(keypress == 178){ 69 | mod[j] = KEY_LEFT_SHIFT; 70 | } 71 | else{ 72 | key[i] = keypress; 73 | } 74 | 75 | if(holdKey('^')) // Prevent setting layer key into set_key or set_modifier 76 | return; 77 | 78 | // Hold keypresses in buffer 79 | Keyboard.set_modifier(mod[0]|mod[1]); 80 | Keyboard.set_key1(key[0]); 81 | Keyboard.set_key2(key[1]); 82 | Keyboard.set_key3(key[2]); 83 | Keyboard.set_key4(key[3]); 84 | Keyboard.set_key5(key[4]); 85 | Keyboard.set_key6(key[5]); 86 | 87 | } 88 | 89 | // This method sends the depressed keys and clears the buffer. 90 | void sendKey(){ 91 | 92 | Keyboard.send_now(); 93 | clearBuffer(); 94 | 95 | Keyboard.set_modifier(mod[0]); 96 | Keyboard.set_key1(key[0]); 97 | Keyboard.set_key2(key[1]); 98 | Keyboard.set_key3(key[2]); 99 | Keyboard.set_key4(key[3]); 100 | Keyboard.set_key5(key[4]); 101 | Keyboard.set_key6(key[5]); 102 | } 103 | 104 | // Helper function to clear the buffer 105 | void clearBuffer(){ 106 | 107 | for(int x = 0; x < 6; x++){ key[x] = 0; } 108 | for(int x = 0; x < 2; x++){ mod[x] = 0; } 109 | 110 | } 111 | 112 | // Detects when a key is held down, returns true if held down, false if not 113 | bool holdKey(char keypress){ 114 | 115 | if(key[0] == keypress || 116 | key[1] == keypress || 117 | key[2] == keypress || 118 | key[3] == keypress || 119 | key[4] == keypress || 120 | key[5] == keypress){ 121 | return true; 122 | } 123 | 124 | return false; 125 | } 126 | 127 | // Calling this function will cycle to the next layer 128 | void cycleLayer(){ 129 | 130 | if(currLayer == (LAYERS - 1)) // Reached maximum layer, going back to first layer 131 | currLayer = 0; 132 | 133 | else 134 | currLayer++; // Increments to the next layer 135 | } 136 | 137 | // Toggles between two layers, the curret layer and desired layer 138 | void toggleLayer(char keyHeld, int desLayer){ 139 | 140 | if (holdKey(keyHeld)){ 141 | prevLayer = currLayer; // Saves previous layer 142 | currLayer = desLayer; // Desired layer 143 | } 144 | 145 | else 146 | currLayer = prevLayer; // Returns to previous layer 147 | } 148 | 149 | // Macro sequence 150 | void ctrlAltDel(){ 151 | // Using CTRL+ALT+KEYPAD_0 as example 152 | setKey(KEYPAD_0); 153 | setKey(176); 154 | setKey(177); 155 | 156 | sendKey(); 157 | } 158 | 159 | // Goes to desired layer when keyHeld is pressed, returns to previous layer when released 160 | void holdLayer(char keyHeld, int desLayer){ 161 | 162 | if(holdKey(keyHeld)){ 163 | 164 | if(!toggleBind){ // Saves the previous layer, using boolean to prevent updating prevLayer more than once 165 | prevLayer = currLayer; 166 | toggleBind = 1; 167 | } 168 | 169 | currLayer = desLayer; // Desire layer 170 | } 171 | 172 | else{ 173 | 174 | if(toggleBind){ 175 | toggleBind = !toggleBind; // Resets boolean 176 | } 177 | 178 | currLayer = prevLayer; // Returns to previous layer 179 | } 180 | } 181 | 182 | void loop() { 183 | 184 | for (int c = 0; c < COLS; c++) { 185 | digitalWrite(col[c], HIGH); 186 | for (int r = 0; r < ROWS; r++){ 187 | if (digitalRead(row[r])){ 188 | 189 | // Triggers macro function when '#' is pressed, can be any other char defined in layout[][][] 190 | if(layout[currLayer][r][c] == '#'){ 191 | ctrlAltDel(); // Performs macro function 192 | } 193 | 194 | else 195 | setKey(layout[currLayer][r][c]); 196 | } 197 | } 198 | digitalWrite(col[c], LOW); 199 | } 200 | 201 | holdLayer('^', desiredLayer); // If the fn layer key is held, it changes the layer to the desired layer, when released, returns to previous layer 202 | 203 | // Now that all of the keys have been polled it is time to send them out! 204 | sendKey(); 205 | delay(5); 206 | } 207 | 208 | --------------------------------------------------------------------------------