Advertisement
AsmodHacker

Arduino OLED 128x64

Jan 4th, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.48 KB | None | 0 0
  1. //Created by AsmodHacker
  2. //(SPI OLED 128X64) MENU WITH ROTARY ENCODER MENU SELECTION, REQUIRES TWO BUTTONS
  3. //once uploaded, flash with keyboard hex (dfu mode)
  4.  
  5. #include <SPI.h>
  6. #include <Wire.h>
  7. #include <String.h>
  8. #include <Adafruit_GFX.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <EEPROM.h> //access the eeprom read/write
  11.  
  12. ////////////////////////////////////////////////TFT_DISPLAY
  13. #define OLED_MOSI   9
  14. #define OLED_CLK   10
  15. #define OLED_DC    11
  16. #define OLED_CS    12
  17. #define OLED_RESET 13
  18. Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  19. #define NUMFLAKES 10
  20. #define XPOS 0
  21. #define YPOS 1
  22. #define DELTAY 2
  23. #define LOGO16_GLCD_HEIGHT 64
  24. #define LOGO16_GLCD_WIDTH  128
  25. ////////////////////////////////////////////////HID_KEYBOARD
  26.  
  27. //Keyboard report buffer
  28. uint8_t buf[8] = {
  29.   0
  30. };
  31.  
  32. ////////////////////////////////////////////////ROT_ENCODER
  33. #define  ROT_L1_PIN 2
  34. #define  ROT_R1_PIN 4
  35. #define  LED_R_PIN 3
  36. #define  LED_G_PIN 5
  37. #define  LED_B_PIN 6
  38. #define ROT_SW1_PIN 7
  39. // ROT-ENC CONSTANTS
  40. #define  ROT_MAX 1
  41. #define  ROT_MIN -1
  42. #define  ROT_center 0
  43. #define  ROT_LEFT  1
  44. #define  ROT_RIGHT 2
  45. #define  ROT_STAY  3
  46. int ROT_INCREMENTS = 1;
  47. int val1a, val1b;
  48. int dirRot1 = ROT_STAY;
  49. int valRot1 = ROT_center;
  50. int mode = 0;
  51. int oldMode = -1;
  52. int oldRot1 = 0;
  53.  
  54. ///////////////////////////////////////////////MODE_BUTTON/MENU_BUTTON
  55. #define MENUButton 23
  56. #define MODEButton 22
  57.  
  58. int buttonState;             // the current reading from the input pin
  59. int lastButtonState = LOW;   // the previous reading from the input pin
  60. unsigned long lastDebounceTime = 0;
  61. unsigned long debounceDelay = 50;
  62.  
  63. ///////////////////////////////////////////////DISPLAY MENU VARIABLES
  64. bool isconfig = true; // browsing the config
  65. bool ismodifying = false; //modifying values in the config
  66. int currentmenu = 0;
  67. int currentselection = 0;
  68. int menumax = 0;
  69. int screenupdate = 1;
  70.  
  71. ///////////////////////////////////////////////LAG DELAY
  72. int lagdelay = 10;
  73. int max_lagdelay = 1000;
  74. int lagdelay_increment = 1;
  75.  
  76. ///////////////////////////////////////////////KEY CODES/INDEX
  77. 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};
  78. 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",};
  79. int keys_length = 39;
  80. ///////////////////////////////////////////////KEY 1
  81. int selected_key1 = 4;
  82.  
  83. ///////////////////////////////////////////////KEY 2
  84. int selected_key2 = 6;
  85.  
  86. void setup()   {
  87.   display.begin(SSD1306_SWITCHCAPVCC);
  88.   pinMode(MODEButton, INPUT);
  89.   pinMode(MENUButton, INPUT);
  90.   pinMode(ROT_L1_PIN, INPUT);
  91.   pinMode(ROT_R1_PIN, INPUT);
  92.   digitalWrite(ROT_L1_PIN, HIGH);
  93.   digitalWrite(ROT_R1_PIN, HIGH);
  94.   pinMode(ROT_SW1_PIN, INPUT);
  95.   Serial.begin(9600);
  96.   load_Settings();
  97.   delay(10);
  98.   display.invertDisplay(false);
  99.   delay(10);
  100.   display.clearDisplay();
  101.   delay(10);
  102. }
  103. int menu1s_xposition[] = {0x00, 30, 37, 0, 0, 0};
  104.  
  105. const char* menu1Items[] = {"Key Pretender v1.0", "Macro Editor", "Lets Play",};
  106. int menu1Items_xposition[] = {0, 30, 37, 0, 0, 0};
  107. int menu1Items_yposition[] = {0, 17, 27, 0, 0, 0};
  108.  
  109. const char* menu2Items[] = {"Macro Editor", "Edit Key Delay", "Edit Key 1", "Edit Key 2", "Main Menu",};
  110. int menu2Items_xposition[] = {0, 2, 2, 2, 71, 0};
  111. int menu2Items_yposition[] = {0, 18, 28, 38, 48, 0};
  112.  
  113. const char* menu3Items[] = {"Play Mode", "Press Knob for Macro", "Press Menu to Exit",};
  114. int menu3Items_xposition[] = {0, 0, 0, 0, 0, 0};
  115. int menu3Items_yposition[] = {0, 18, 28, 38, 48, 0};
  116.  
  117. int menu_sizes[] = {2, 3, 2};
  118. int menu_size = -1;
  119.  
  120. bool mode_switch;
  121.  
  122. void getButtonMode()
  123. {
  124.   int reading = digitalRead(MODEButton);
  125.   if (reading != lastButtonState) {
  126.     lastDebounceTime = millis();
  127.   }
  128.   if ((millis() - lastDebounceTime) > debounceDelay) {
  129.     if (reading != buttonState) {
  130.       buttonState = !buttonState;
  131.     }
  132.     mode_switch = false;
  133.   } else {
  134.     mode_switch = true;
  135.   }
  136.   lastButtonState = reading;
  137. }
  138.  
  139. void loop() {
  140.   getButtonMode();
  141.   rotEncProcess();
  142.  
  143.   if (digitalRead(MENUButton) == 0)
  144.   {
  145.     update_Settings();
  146.     isconfig = 1;
  147.     currentmenu = 0;
  148.     currentselection = 0;
  149.     delay(200);
  150.     screenupdate = 1;
  151.     //Serial.println("MENU");
  152.   }
  153.  
  154.   if (screenupdate)
  155.   {
  156.     if (currentmenu == 2) //PLAY MENU
  157.     {
  158.       display.setTextSize(1); delay(2);
  159.       display.setTextColor(WHITE); delay(2);
  160.       for (int i = 0; i < 3; i++)
  161.       {
  162.         display.setCursor(menu3Items_xposition[i], menu3Items_yposition[i]); delay(2);
  163.         display.print(menu3Items[i]); delay(2);
  164.       }
  165.     }
  166.  
  167.     if (currentmenu == 0) //MAIN MENU
  168.     {
  169.       update_Settings ();
  170.       display.setTextSize(1); delay(2);
  171.       display.setTextColor(WHITE); delay(2);
  172.       for (int i = 0; i < 3; i++)
  173.       {
  174.         display.setCursor(menu1Items_xposition[i], menu1Items_yposition[i]); delay(2);
  175.         display.print(menu1Items[i]); delay(2);
  176.       }
  177.     }
  178.  
  179.     if (currentmenu == 1) //MACRO MENU
  180.     {
  181.       display.setTextSize(1); delay(2);
  182.       display.setTextColor(WHITE); delay(2);
  183.       for (int i = 0; i < 5; i++)
  184.       {
  185.         if (ismodifying == 1) {
  186.           if (i == 1 && currentselection == 0)
  187.           {
  188.             display.setCursor(menu2Items_xposition[1], menu2Items_yposition[1] + 1); delay(2);
  189.             display.print("Key Delay: " + (String)lagdelay + "(ms)"); delay(2);
  190.  
  191.             if (mode_switch) {
  192.               display.setCursor(0, menu2Items_yposition[4] + 1); delay(2);
  193.               display.print("INC> " + (String)lagdelay_increment); delay(2);
  194.               screenupdate = 1;
  195.             } else {
  196.               display.setCursor(0, menu2Items_yposition[4] + 1); delay(2);
  197.               display.print("INC: " + (String)lagdelay_increment); delay(2);
  198.               screenupdate = 1;
  199.             }
  200.           }
  201.           if (i == 2 && currentselection == 1)
  202.           {
  203.             display.setCursor(0, menu2Items_yposition[2] + 1); delay(2);
  204.             display.print("Key 1> " + (String)keyboard_keys[selected_key1]); delay(2);
  205.           }
  206.           if (i == 3 && currentselection == 2)
  207.           {
  208.             display.setCursor(0, menu2Items_yposition[3] + 1); delay(2);
  209.             display.print("Key 2> " + (String)keyboard_keys[selected_key2]); delay(2);
  210.           }
  211.           if (not(i == 1 && currentselection == 0 || i == 2 && currentselection == 1 || i == 3 && currentselection == 2))
  212.           {
  213.             display.setCursor(menu2Items_xposition[i], menu2Items_yposition[i]); delay(2);
  214.             display.print(menu2Items[i]); delay(2);
  215.           }
  216.         } else {
  217.           display.setCursor(menu2Items_xposition[i], menu2Items_yposition[i]); delay(2);
  218.           display.print(menu2Items[i]); delay(2);
  219.         }
  220.       }
  221.     }
  222.     menu_size = menu_sizes[currentmenu];
  223.     displaySelectItem(currentselection);
  224.     display.display(); delay(1);
  225.     display.clearDisplay();
  226.     screenupdate = 0;
  227.   }
  228. }
  229.  
  230. void rotEncProcess()
  231. {
  232.   val1a = digitalRead( ROT_L1_PIN );
  233.   val1b = digitalRead( ROT_R1_PIN );
  234.   delay(1);
  235.   // Rotary Encoder 1
  236.   if ( val1a == HIGH && val1b == HIGH ) {
  237.     if ( dirRot1 == ROT_LEFT ) {
  238.       valRot1 = 1;
  239.       if (valRot1 > ROT_MAX) {
  240.         valRot1 = ROT_MAX;
  241.       }
  242.     } else if ( dirRot1 == ROT_RIGHT ) {
  243.       valRot1 = -1;
  244.       if (valRot1 < ROT_MIN) {
  245.         valRot1 = ROT_MIN;
  246.       }
  247.     }
  248.     dirRot1 = ROT_STAY;
  249.   } else {
  250.     if ( val1a == LOW ) {
  251.       dirRot1 = ROT_LEFT;
  252.     }
  253.     if ( val1b == HIGH ) {
  254.       dirRot1 = ROT_RIGHT;
  255.     }
  256.   }
  257.  
  258.   if (isconfig == true)
  259.   {
  260.     if (digitalRead(ROT_SW1_PIN) == 1) {
  261.       change_menu();
  262.       delay(100);
  263.     }
  264.     menu_selection();
  265.   } else {
  266.     while (digitalRead(ROT_SW1_PIN) == 1) {
  267.       RUN_MACRO();
  268.     }
  269.   }
  270. }
  271.  
  272.  
  273. void change_menu() { //Encoder [Changing menu index]
  274.   if (currentmenu == 0 && currentselection == 0)
  275.   {
  276.     currentmenu = 1;
  277.     currentselection = 0;
  278.     screenupdate = 1;
  279.     delay(100);
  280.     return;
  281.   }
  282.   if (currentmenu == 0 && currentselection == 1)
  283.   {
  284.     isconfig = false;
  285.     currentmenu = 2;
  286.     currentselection = 0;
  287.     screenupdate = 1;
  288.     delay(100);
  289.     return;
  290.   }
  291.  
  292.   if (currentmenu == 1 && currentselection == 0)
  293.   {
  294.     ismodifying = !ismodifying;
  295.     screenupdate = 1;
  296.     delay(100);
  297.     return;
  298.   }
  299.   if (currentmenu == 1 && currentselection == 1)
  300.   {
  301.     ismodifying = !ismodifying;
  302.     //for key 1 editing
  303.     screenupdate = 1;
  304.     delay(100);
  305.     return;
  306.   }
  307.   if (currentmenu == 1 && currentselection == 2)
  308.   {
  309.     ismodifying = !ismodifying;
  310.     //for key 2 editing
  311.     screenupdate = 1;
  312.     delay(100);
  313.     return;
  314.   }
  315.   if (currentmenu == 1 && currentselection == 3)
  316.   {
  317.     currentmenu = 0;
  318.     currentselection = 0;
  319.     screenupdate = 1;
  320.     delay(100);
  321.     return;
  322.   }
  323. }
  324.  
  325. void menu_selection() {//Encoder [Changing menu item]
  326.   if (valRot1 > ROT_center) {
  327.     if (ismodifying)
  328.     {
  329.       if (currentselection == 0)
  330.       {
  331.         if (mode_switch)
  332.         {
  333.           lagdelay_increment += 1;
  334.           if (lagdelay_increment > 3000)
  335.           {
  336.             lagdelay_increment = 1;
  337.           }
  338.         }
  339.         else {
  340.           lagdelay += lagdelay_increment * 1;
  341.           if (lagdelay > max_lagdelay)
  342.           {
  343.             lagdelay = 0;
  344.           }
  345.         }
  346.       }
  347.       if (currentselection == 1)
  348.       {
  349.         selected_key1 += 1;
  350.         if (selected_key1 > keys_length)
  351.         {
  352.           selected_key1 = keys_length;
  353.         }
  354.       }
  355.       if (currentselection == 2)
  356.       {
  357.         selected_key2 += 1;
  358.         if (selected_key2 > keys_length)
  359.         {
  360.           selected_key2 = 0;
  361.         }
  362.       }
  363.       screenupdate = 1;
  364.     } else {
  365.       currentselection++;
  366.       if (currentselection > menu_sizes[currentmenu]) {
  367.         currentselection = menu_sizes[currentmenu];
  368.       }
  369.       screenupdate = 1;
  370.     }
  371.   }
  372.   if ( valRot1 < ROT_center)  {
  373.     if (ismodifying)
  374.     {
  375.       if (currentselection == 0)
  376.       {
  377.         if (mode_switch)
  378.         {
  379.           lagdelay_increment -= 1;
  380.           if (lagdelay_increment < 0)
  381.           {
  382.             lagdelay_increment = 0;
  383.           }
  384.         }
  385.         else {
  386.           lagdelay -= lagdelay_increment * 1;
  387.           if (lagdelay < 0)
  388.           {
  389.             lagdelay = 0;
  390.           }
  391.         }
  392.       }
  393.       if (currentselection == 1)
  394.       {
  395.         selected_key1 -= 1;
  396.         if (selected_key1 < 0)
  397.         {
  398.           selected_key1 = keys_length;
  399.         }
  400.       }
  401.       if (currentselection == 2)
  402.       {
  403.         selected_key2 -= 1;
  404.         if (selected_key2 < 0)
  405.         {
  406.           selected_key2 = keys_length;
  407.         }
  408.       }
  409.       screenupdate = 1;
  410.     } else {
  411.       currentselection--;
  412.       if (currentselection < 0) {
  413.         currentselection = menu_size;
  414.       }
  415.       screenupdate = 1;
  416.     }
  417.   }
  418.   valRot1 = ROT_center;
  419. }
  420.  
  421. void displaySelectItem(int selecttion) {
  422.   if (currentmenu == 0)
  423.   {
  424.     switch (selecttion) {
  425.       case 0: rect(23, 16, 85, 12, WHITE); return;
  426.       default: rect(23, 26, 85, 12, WHITE); return;
  427.     }
  428.   }
  429.   if (currentmenu == 1)
  430.   {
  431.     switch (selecttion) {
  432.       case 0: rect(0, 16, 126, 12, WHITE); return; //Delay
  433.       case 1: rect(0, 26, 126, 12, WHITE); return; //k1
  434.       case 2: rect(0, 36, 126, 12, WHITE); return; //k2
  435.       default: rect(69, 46, 58, 12, WHITE); return; //Back
  436.     }
  437.   }
  438. }
  439.  
  440. void rect(int x, int y, int sizex, int sizey, uint8_t color) {
  441.   display.drawRect(x, y, sizex, sizey, color % 2);
  442. }
  443.  
  444. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  445. void RUN_MACRO() {
  446.   while (digitalRead(ROT_SW1_PIN)) {
  447.     buf[2] = keyboard_codes[selected_key1];
  448.     Serial.write(buf, 8);
  449.     delay(lagdelay + random(0, 5));
  450.     releaseKey();
  451.     buf[2] = keyboard_codes[selected_key2];
  452.     Serial.write(buf, 8);
  453.     delay(lagdelay + random(0, 5));
  454.     releaseKey();
  455.   }
  456. }
  457.  
  458. void releaseKey() {
  459.   buf[0] = 0; buf[2] = 0;
  460.   Serial.write(buf, 8); // Release key
  461.   delay(1);
  462. }
  463.  
  464. void load_Settings()
  465. {
  466.   lagdelay = EEPROMReadInt(0);
  467.   lagdelay_increment = EEPROMReadInt(2);
  468.   selected_key1 = EEPROMReadInt(4);
  469.   selected_key2 = EEPROMReadInt(6);
  470. }
  471. void update_Settings()
  472. {
  473.   if (lagdelay != EEPROMReadInt(0))
  474.   {
  475.     EEPROMWriteInt(0, lagdelay);
  476.   }
  477.   if (lagdelay_increment != EEPROMReadInt(2))
  478.   {
  479.     EEPROMWriteInt(2, lagdelay_increment);
  480.   }
  481.   if (selected_key1 != EEPROMReadInt(4))
  482.   {
  483.     EEPROMWriteInt(4, selected_key1);
  484.   }
  485.   if (selected_key2 != EEPROMReadInt(6))
  486.   {
  487.     EEPROMWriteInt(6, selected_key2);
  488.   }
  489. }
  490.  
  491. //This function will write a 2 byte integer to the eeprom at the specified address and address + 1
  492. void EEPROMWriteInt(int p_address, int p_value)
  493. {
  494.   byte lowByte = ((p_value >> 0) & 0xFF);
  495.   byte highByte = ((p_value >> 8) & 0xFF);
  496.  
  497.   EEPROM.write(p_address, lowByte);
  498.   EEPROM.write(p_address + 1, highByte);
  499. }
  500.  
  501. //This function will read a 2 byte integer from the eeprom at the specified address and address + 1
  502. unsigned int EEPROMReadInt(int p_address)
  503. {
  504.   byte lowByte = EEPROM.read(p_address);
  505.   byte highByte = EEPROM.read(p_address + 1);
  506.  
  507.   return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement