Created
January 29, 2017 13:33
-
-
Save thnikk/855253949d22fb15459b16796c2e7852 to your computer and use it in GitHub Desktop.
basic code with serial remapper commented out
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
/************************************************* | |
Basic/Basic+ Keypad | |
Written for Pro Micro/Leonardo. Adds 1000hz | |
polling rate and button remapper. Can be | |
upgraded to LED fairly easily by wiring | |
positive ends of the LEDs to pin 5 and 6. | |
Key Numbering | |
--------- | |
- | 1 | 2 | | |
--------- | |
^ Side button is key 3 | |
*Pinout is 2, 3, 4 for buttons 1, 2, and 3 | |
For more info, please visit: | |
----> http://thnikk.moe/ | |
----> https://www.etsy.com/shop/thnikk | |
----> twitter.com/thnikk | |
----> https://github.com/thnikk | |
Written by thnikk. | |
*************************************************/ | |
#include <EEPROM.h> | |
#include <Bounce2.h> | |
#include <Keyboard.h> | |
// Version number (increment to update EEPROM values) | |
bool version = 0; | |
char initMapping[] = {"zx"}; | |
// How many keys (0 indexed) | |
const byte numkeys = 2; | |
// Array for buttons (for use in for loop.) | |
const byte button[] = { 2, 3, 4 }; | |
// Makes button press/release action happen only once | |
bool pressed[2]; | |
// Array for storing bounce values | |
bool bounce[3]; | |
// Mapping multidimensional array | |
char mapping[2][3]; | |
unsigned long previousMillis = 0; | |
byte set = 0; | |
// Bounce declaration | |
Bounce b1d = Bounce(); | |
Bounce b2d = Bounce(); | |
Bounce b3d = Bounce(); | |
void setup() { | |
// Serial.begin(9600); | |
// Load + Initialize EEPROM | |
loadEEPROM(); | |
// Set input pullup resistors | |
for (int x = 0; x <= numkeys; x++) { | |
pinMode(button[x], INPUT_PULLUP); | |
} | |
// Bounce initializtion | |
b1d.attach(button[0]); | |
b1d.interval(8); | |
b2d.attach(button[1]); | |
b2d.interval(8); | |
b3d.attach(button[2]); | |
b3d.interval(8); | |
} | |
void loadEEPROM() { | |
// VInitialize EEPROM | |
if (EEPROM.read(0) != version) { | |
// Single values | |
EEPROM.write(0, version); | |
for (int x = 0; x < numkeys; x++) { | |
for (int y= 0; y < 3; y++) { | |
if (y == 0) EEPROM.write(40+(x*3)+y, int(initMapping[x])); | |
if (y > 0) EEPROM.write(40+(x*3)+y, 0); | |
} | |
} | |
} | |
// Load values from EEPROM | |
for (int x = 0; x < numkeys; x++) { | |
// Load button mapping | |
for (int y= 0; y < 3; y++) { | |
mapping[x][y] = char(EEPROM.read(40+(x*3)+y)); | |
} | |
} | |
} | |
void loop() { | |
// Run to get latest bounce values | |
bounceSetup(); // Moved here for program-wide access to latest debounced button values | |
/*if ((millis() - previousMillis) > 1000) { | |
if (Serial && set == 0) remapSerial(); // if(Serial) checks to see if a serial connection has been established (Leonardo specific) | |
if (!Serial) set = 0; | |
previousMillis = millis(); | |
}*/ | |
keyboard(); | |
} | |
// Allows keys to be remapped through the serial monitor | |
/*void remapSerial() { | |
// Flavor text | |
Serial.println("Welcome to the serial remapper!"); | |
Serial.println("Shift: +, Control: ^, Alt: !, Win: #"); | |
// Print current EEPROM values | |
Serial.print("Current values are: "); | |
for (int x = 0; x < numkeys; x++) { | |
for (int y = 0; y < 3; y++) { | |
Serial.print(mapping[x][y]); | |
} | |
if (x < (numkeys - 1)) Serial.print(", "); | |
} | |
Serial.println(); | |
// End of print | |
// Take serial inputs | |
Serial.println("Please input modifiers first and then a printable character."); | |
for (int x = 0; x < numkeys; x++) { | |
// Says what key is being changed | |
Serial.print("Key "); | |
Serial.print(x+1); | |
Serial.print(": "); | |
// Waits for input and converts received string into array for storing values | |
while(!Serial.available()){} | |
String serialInput = Serial.readString(); | |
// Says what was received and saves to eeprom and array | |
for (int y= 0; y < 3; y++) { | |
serialInput[y] = inputInterpreter(serialInput[y]); // Filter through modifier converter | |
EEPROM.write(40+(x*3)+y, int(serialInput[y])); | |
mapping[x][y] = serialInput[y]; | |
} | |
// New line | |
Serial.println(); | |
} | |
// Required to keep remapper from constantly running and holding the program up | |
Serial.println("Values saved! Please reopen the serial monitor to remap again."); | |
set++; | |
} | |
// Converts characters to modifiers (in theory) | |
char inputInterpreter(char input) { | |
switch(input) { | |
case '+': | |
Serial.print("shift, "); | |
return char(129); | |
case '^': | |
Serial.print("ctrl, "); | |
return char(128); | |
case '!': | |
Serial.print("alt, "); | |
return char(130); | |
case '#': | |
Serial.print("win, "); | |
return char(131); | |
default: | |
Serial.print(input); | |
return input; | |
break; | |
} | |
}*/ | |
// Sets up bounce inputs and sets values for the bounce array | |
void bounceSetup() { | |
b1d.update(); | |
b2d.update(); | |
b3d.update(); | |
bounce[0] = b1d.read(); | |
bounce[1] = b2d.read(); | |
bounce[2] = b3d.read(); | |
} | |
// Does keyboard stuff | |
void keyboard(){ | |
if(!bounce[2]) Keyboard.press(177); | |
if(bounce[2]) Keyboard.release(177); | |
for (int a = 0; a < numkeys; a++) { | |
// Cycles through key and modifiers set | |
if (!pressed[a]) { | |
for (int b = 0; b < 3; b++) if (!bounce[a]) { | |
Keyboard.press(mapping[a][b]); | |
pressed[a] = 1; | |
} | |
} | |
if (pressed[a]) { | |
for (int b = 0; b < 3; b++) if (bounce[a]) { | |
Keyboard.release(mapping[a][b]); | |
pressed[a] = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment