Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Created by AsmodHacker
- //(SPI OLED 128X64) MENU WITH ROTARY ENCODER MENU SELECTION, REQUIRES TWO BUTTONS
- //once uploaded, flash with keyboard hex (dfu mode)
- #include <SPI.h>
- #include <Wire.h>
- #include <String.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <EEPROM.h> //access the eeprom read/write
- ////////////////////////////////////////////////TFT_DISPLAY
- #define OLED_MOSI 9
- #define OLED_CLK 10
- #define OLED_DC 11
- #define OLED_CS 12
- #define OLED_RESET 13
- Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- #define NUMFLAKES 10
- #define XPOS 0
- #define YPOS 1
- #define DELTAY 2
- #define LOGO16_GLCD_HEIGHT 64
- #define LOGO16_GLCD_WIDTH 128
- ////////////////////////////////////////////////HID_KEYBOARD
- //Keyboard report buffer
- uint8_t buf[8] = {
- 0
- };
- ////////////////////////////////////////////////ROT_ENCODER
- #define ROT_L1_PIN 2
- #define ROT_R1_PIN 4
- #define LED_R_PIN 3
- #define LED_G_PIN 5
- #define LED_B_PIN 6
- #define ROT_SW1_PIN 7
- // ROT-ENC CONSTANTS
- #define ROT_MAX 1
- #define ROT_MIN -1
- #define ROT_center 0
- #define ROT_LEFT 1
- #define ROT_RIGHT 2
- #define ROT_STAY 3
- int ROT_INCREMENTS = 1;
- int val1a, val1b;
- int dirRot1 = ROT_STAY;
- int valRot1 = ROT_center;
- int mode = 0;
- int oldMode = -1;
- int oldRot1 = 0;
- ///////////////////////////////////////////////MODE_BUTTON/MENU_BUTTON
- #define MENUButton 23
- #define MODEButton 22
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- ///////////////////////////////////////////////DISPLAY MENU VARIABLES
- bool isconfig = true; // browsing the config
- bool ismodifying = false; //modifying values in the config
- int currentmenu = 0;
- int currentselection = 0;
- int menumax = 0;
- int screenupdate = 1;
- ///////////////////////////////////////////////LAG DELAY
- int lagdelay = 10;
- int max_lagdelay = 1000;
- int lagdelay_increment = 1;
- ///////////////////////////////////////////////KEY CODES/INDEX
- const int keyboard_codes[] = {(1 << 1), (1 << 0), 44, 40, 57, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39};
- const char* keyboard_keys[] = {"MOD_SHIFT_LEFT", "MOD_CONTROL_LEFT", "SPACE", "ENTER", "CAPSLOCK", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",};
- int keys_length = 39;
- ///////////////////////////////////////////////KEY 1
- int selected_key1 = 4;
- ///////////////////////////////////////////////KEY 2
- int selected_key2 = 6;
- void setup() {
- display.begin(SSD1306_SWITCHCAPVCC);
- pinMode(MODEButton, INPUT);
- pinMode(MENUButton, INPUT);
- pinMode(ROT_L1_PIN, INPUT);
- pinMode(ROT_R1_PIN, INPUT);
- digitalWrite(ROT_L1_PIN, HIGH);
- digitalWrite(ROT_R1_PIN, HIGH);
- pinMode(ROT_SW1_PIN, INPUT);
- Serial.begin(9600);
- load_Settings();
- delay(10);
- display.invertDisplay(false);
- delay(10);
- display.clearDisplay();
- delay(10);
- }
- int menu1s_xposition[] = {0x00, 30, 37, 0, 0, 0};
- const char* menu1Items[] = {"Key Pretender v1.0", "Macro Editor", "Lets Play",};
- int menu1Items_xposition[] = {0, 30, 37, 0, 0, 0};
- int menu1Items_yposition[] = {0, 17, 27, 0, 0, 0};
- const char* menu2Items[] = {"Macro Editor", "Edit Key Delay", "Edit Key 1", "Edit Key 2", "Main Menu",};
- int menu2Items_xposition[] = {0, 2, 2, 2, 71, 0};
- int menu2Items_yposition[] = {0, 18, 28, 38, 48, 0};
- const char* menu3Items[] = {"Play Mode", "Press Knob for Macro", "Press Menu to Exit",};
- int menu3Items_xposition[] = {0, 0, 0, 0, 0, 0};
- int menu3Items_yposition[] = {0, 18, 28, 38, 48, 0};
- int menu_sizes[] = {2, 3, 2};
- int menu_size = -1;
- bool mode_switch;
- void getButtonMode()
- {
- int reading = digitalRead(MODEButton);
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading != buttonState) {
- buttonState = !buttonState;
- }
- mode_switch = false;
- } else {
- mode_switch = true;
- }
- lastButtonState = reading;
- }
- void loop() {
- getButtonMode();
- rotEncProcess();
- if (digitalRead(MENUButton) == 0)
- {
- update_Settings();
- isconfig = 1;
- currentmenu = 0;
- currentselection = 0;
- delay(200);
- screenupdate = 1;
- //Serial.println("MENU");
- }
- if (screenupdate)
- {
- if (currentmenu == 2) //PLAY MENU
- {
- display.setTextSize(1); delay(2);
- display.setTextColor(WHITE); delay(2);
- for (int i = 0; i < 3; i++)
- {
- display.setCursor(menu3Items_xposition[i], menu3Items_yposition[i]); delay(2);
- display.print(menu3Items[i]); delay(2);
- }
- }
- if (currentmenu == 0) //MAIN MENU
- {
- update_Settings ();
- display.setTextSize(1); delay(2);
- display.setTextColor(WHITE); delay(2);
- for (int i = 0; i < 3; i++)
- {
- display.setCursor(menu1Items_xposition[i], menu1Items_yposition[i]); delay(2);
- display.print(menu1Items[i]); delay(2);
- }
- }
- if (currentmenu == 1) //MACRO MENU
- {
- display.setTextSize(1); delay(2);
- display.setTextColor(WHITE); delay(2);
- for (int i = 0; i < 5; i++)
- {
- if (ismodifying == 1) {
- if (i == 1 && currentselection == 0)
- {
- display.setCursor(menu2Items_xposition[1], menu2Items_yposition[1] + 1); delay(2);
- display.print("Key Delay: " + (String)lagdelay + "(ms)"); delay(2);
- if (mode_switch) {
- display.setCursor(0, menu2Items_yposition[4] + 1); delay(2);
- display.print("INC> " + (String)lagdelay_increment); delay(2);
- screenupdate = 1;
- } else {
- display.setCursor(0, menu2Items_yposition[4] + 1); delay(2);
- display.print("INC: " + (String)lagdelay_increment); delay(2);
- screenupdate = 1;
- }
- }
- if (i == 2 && currentselection == 1)
- {
- display.setCursor(0, menu2Items_yposition[2] + 1); delay(2);
- display.print("Key 1> " + (String)keyboard_keys[selected_key1]); delay(2);
- }
- if (i == 3 && currentselection == 2)
- {
- display.setCursor(0, menu2Items_yposition[3] + 1); delay(2);
- display.print("Key 2> " + (String)keyboard_keys[selected_key2]); delay(2);
- }
- if (not(i == 1 && currentselection == 0 || i == 2 && currentselection == 1 || i == 3 && currentselection == 2))
- {
- display.setCursor(menu2Items_xposition[i], menu2Items_yposition[i]); delay(2);
- display.print(menu2Items[i]); delay(2);
- }
- } else {
- display.setCursor(menu2Items_xposition[i], menu2Items_yposition[i]); delay(2);
- display.print(menu2Items[i]); delay(2);
- }
- }
- }
- menu_size = menu_sizes[currentmenu];
- displaySelectItem(currentselection);
- display.display(); delay(1);
- display.clearDisplay();
- screenupdate = 0;
- }
- }
- void rotEncProcess()
- {
- val1a = digitalRead( ROT_L1_PIN );
- val1b = digitalRead( ROT_R1_PIN );
- delay(1);
- // Rotary Encoder 1
- if ( val1a == HIGH && val1b == HIGH ) {
- if ( dirRot1 == ROT_LEFT ) {
- valRot1 = 1;
- if (valRot1 > ROT_MAX) {
- valRot1 = ROT_MAX;
- }
- } else if ( dirRot1 == ROT_RIGHT ) {
- valRot1 = -1;
- if (valRot1 < ROT_MIN) {
- valRot1 = ROT_MIN;
- }
- }
- dirRot1 = ROT_STAY;
- } else {
- if ( val1a == LOW ) {
- dirRot1 = ROT_LEFT;
- }
- if ( val1b == HIGH ) {
- dirRot1 = ROT_RIGHT;
- }
- }
- if (isconfig == true)
- {
- if (digitalRead(ROT_SW1_PIN) == 1) {
- change_menu();
- delay(100);
- }
- menu_selection();
- } else {
- while (digitalRead(ROT_SW1_PIN) == 1) {
- RUN_MACRO();
- }
- }
- }
- void change_menu() { //Encoder [Changing menu index]
- if (currentmenu == 0 && currentselection == 0)
- {
- currentmenu = 1;
- currentselection = 0;
- screenupdate = 1;
- delay(100);
- return;
- }
- if (currentmenu == 0 && currentselection == 1)
- {
- isconfig = false;
- currentmenu = 2;
- currentselection = 0;
- screenupdate = 1;
- delay(100);
- return;
- }
- if (currentmenu == 1 && currentselection == 0)
- {
- ismodifying = !ismodifying;
- screenupdate = 1;
- delay(100);
- return;
- }
- if (currentmenu == 1 && currentselection == 1)
- {
- ismodifying = !ismodifying;
- //for key 1 editing
- screenupdate = 1;
- delay(100);
- return;
- }
- if (currentmenu == 1 && currentselection == 2)
- {
- ismodifying = !ismodifying;
- //for key 2 editing
- screenupdate = 1;
- delay(100);
- return;
- }
- if (currentmenu == 1 && currentselection == 3)
- {
- currentmenu = 0;
- currentselection = 0;
- screenupdate = 1;
- delay(100);
- return;
- }
- }
- void menu_selection() {//Encoder [Changing menu item]
- if (valRot1 > ROT_center) {
- if (ismodifying)
- {
- if (currentselection == 0)
- {
- if (mode_switch)
- {
- lagdelay_increment += 1;
- if (lagdelay_increment > 3000)
- {
- lagdelay_increment = 1;
- }
- }
- else {
- lagdelay += lagdelay_increment * 1;
- if (lagdelay > max_lagdelay)
- {
- lagdelay = 0;
- }
- }
- }
- if (currentselection == 1)
- {
- selected_key1 += 1;
- if (selected_key1 > keys_length)
- {
- selected_key1 = keys_length;
- }
- }
- if (currentselection == 2)
- {
- selected_key2 += 1;
- if (selected_key2 > keys_length)
- {
- selected_key2 = 0;
- }
- }
- screenupdate = 1;
- } else {
- currentselection++;
- if (currentselection > menu_sizes[currentmenu]) {
- currentselection = menu_sizes[currentmenu];
- }
- screenupdate = 1;
- }
- }
- if ( valRot1 < ROT_center) {
- if (ismodifying)
- {
- if (currentselection == 0)
- {
- if (mode_switch)
- {
- lagdelay_increment -= 1;
- if (lagdelay_increment < 0)
- {
- lagdelay_increment = 0;
- }
- }
- else {
- lagdelay -= lagdelay_increment * 1;
- if (lagdelay < 0)
- {
- lagdelay = 0;
- }
- }
- }
- if (currentselection == 1)
- {
- selected_key1 -= 1;
- if (selected_key1 < 0)
- {
- selected_key1 = keys_length;
- }
- }
- if (currentselection == 2)
- {
- selected_key2 -= 1;
- if (selected_key2 < 0)
- {
- selected_key2 = keys_length;
- }
- }
- screenupdate = 1;
- } else {
- currentselection--;
- if (currentselection < 0) {
- currentselection = menu_size;
- }
- screenupdate = 1;
- }
- }
- valRot1 = ROT_center;
- }
- void displaySelectItem(int selecttion) {
- if (currentmenu == 0)
- {
- switch (selecttion) {
- case 0: rect(23, 16, 85, 12, WHITE); return;
- default: rect(23, 26, 85, 12, WHITE); return;
- }
- }
- if (currentmenu == 1)
- {
- switch (selecttion) {
- case 0: rect(0, 16, 126, 12, WHITE); return; //Delay
- case 1: rect(0, 26, 126, 12, WHITE); return; //k1
- case 2: rect(0, 36, 126, 12, WHITE); return; //k2
- default: rect(69, 46, 58, 12, WHITE); return; //Back
- }
- }
- }
- void rect(int x, int y, int sizex, int sizey, uint8_t color) {
- display.drawRect(x, y, sizex, sizey, color % 2);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void RUN_MACRO() {
- while (digitalRead(ROT_SW1_PIN)) {
- buf[2] = keyboard_codes[selected_key1];
- Serial.write(buf, 8);
- delay(lagdelay + random(0, 5));
- releaseKey();
- buf[2] = keyboard_codes[selected_key2];
- Serial.write(buf, 8);
- delay(lagdelay + random(0, 5));
- releaseKey();
- }
- }
- void releaseKey() {
- buf[0] = 0; buf[2] = 0;
- Serial.write(buf, 8); // Release key
- delay(1);
- }
- void load_Settings()
- {
- lagdelay = EEPROMReadInt(0);
- lagdelay_increment = EEPROMReadInt(2);
- selected_key1 = EEPROMReadInt(4);
- selected_key2 = EEPROMReadInt(6);
- }
- void update_Settings()
- {
- if (lagdelay != EEPROMReadInt(0))
- {
- EEPROMWriteInt(0, lagdelay);
- }
- if (lagdelay_increment != EEPROMReadInt(2))
- {
- EEPROMWriteInt(2, lagdelay_increment);
- }
- if (selected_key1 != EEPROMReadInt(4))
- {
- EEPROMWriteInt(4, selected_key1);
- }
- if (selected_key2 != EEPROMReadInt(6))
- {
- EEPROMWriteInt(6, selected_key2);
- }
- }
- //This function will write a 2 byte integer to the eeprom at the specified address and address + 1
- void EEPROMWriteInt(int p_address, int p_value)
- {
- byte lowByte = ((p_value >> 0) & 0xFF);
- byte highByte = ((p_value >> 8) & 0xFF);
- EEPROM.write(p_address, lowByte);
- EEPROM.write(p_address + 1, highByte);
- }
- //This function will read a 2 byte integer from the eeprom at the specified address and address + 1
- unsigned int EEPROMReadInt(int p_address)
- {
- byte lowByte = EEPROM.read(p_address);
- byte highByte = EEPROM.read(p_address + 1);
- return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement