Advertisement
Guest User

macrokey.ino

a guest
Mar 6th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.47 KB | None | 0 0
  1. /*
  2.     Toby's Low Cost Macro Keyboard
  3. */
  4.  
  5. #ifndef USER_USB_RAM
  6. #error "Require USB RAM. Go Tools > USB Setting and pick the 2nd option in the dropdown list"
  7. #endif
  8.  
  9. #include "src/userUsbHidMediaKeyboard/USBHIDMediaKeyboard.h"
  10.  
  11. ////////////// HARDWARE CONFIG //////////
  12. //Mode LED
  13. #define LED_A 34
  14. #define LED_B 33
  15.  
  16. //Mode switch
  17. #define SWITCH 14
  18.  
  19. //Button (Mechnical, left to right)
  20. #define BTN_1 15
  21. #define BTN_2 16
  22. #define BTN_3 17
  23. #define BTN_4 11
  24.  
  25. ///////////////// RUNTIME ///////////////
  26. //Current Mode
  27. bool modeA = true;
  28.  
  29. //Previous button states
  30. bool bt1ActiveState = false;
  31. bool bt2ActiveState = false;
  32. bool bt3ActiveState = false;
  33. bool bt4ActiveState = false;
  34.  
  35. //Current button states
  36. bool bt1Active = false;
  37. bool bt2Active = false;
  38. bool bt3Active = false;
  39. bool bt4Active = false;
  40.  
  41. ////////////// Special Hotkeys //////////////////
  42. //When Button 2 is hold in mode A, activate volume Mode to
  43. //allow button 3 and 4 to change volume instead of prev / next song
  44. bool volMode = false;
  45.  
  46. void setup() {
  47.   USBInit();
  48.   pinMode(SWITCH, INPUT);
  49.   pinMode(LED_A, OUTPUT);
  50.   pinMode(LED_B, OUTPUT);
  51.  
  52.   if (!digitalRead(SWITCH)){
  53.     digitalWrite(LED_A, HIGH);
  54.     digitalWrite(LED_B, LOW);
  55.   }else{
  56.     digitalWrite(LED_A, LOW);
  57.     digitalWrite(LED_B, HIGH);
  58.   }
  59. }
  60.  
  61. void loop() {
  62.   //Read the mode of the keyboard
  63.   modeA = !digitalRead(SWITCH);
  64.  
  65.   //Read the button states, default PULL HIGH (aka LOW Activate)
  66.   bt1Active = !digitalRead(BTN_1);
  67.   bt2Active = !digitalRead(BTN_2);
  68.   bt3Active = !digitalRead(BTN_3);
  69.   bt4Active = !digitalRead(BTN_4);
  70.  
  71.   if (modeA == HIGH){
  72.     //Mode A
  73.     //Media Controller
  74.  
  75.     //Button 1: Play / Pause
  76.     if (bt1ActiveState != bt1Active){
  77.       bt1ActiveState = bt1Active;
  78.       if (bt1Active){
  79.         Consumer_press(MEDIA_PLAY_PAUSE);
  80.       }else{
  81.         Consumer_release(MEDIA_PLAY_PAUSE);
  82.       }
  83.     }
  84.  
  85.     //Button 2: Toggle Playlist / Volumn Control Mode
  86.     if (bt2ActiveState != bt2Active){
  87.       bt2ActiveState = bt2Active;
  88.       if (bt2Active){
  89.         volMode = true;
  90.       }else{
  91.         volMode = false;
  92.       }
  93.     }
  94.  
  95.     //Previous Song / Vol Down
  96.     if (bt3ActiveState != bt3Active){
  97.       bt3ActiveState = bt3Active;
  98.       if (volMode){
  99.          //Button 2 is held
  100.         if (bt3Active){
  101.           Consumer_press(MEDIA_VOLUME_DOWN);
  102.         }else{
  103.           Consumer_release(MEDIA_VOLUME_DOWN);
  104.         }
  105.       }else{
  106.          //Default mode
  107.         if (bt3Active){
  108.           Consumer_press(MEDIA_PREV);
  109.         }else{
  110.           Consumer_release(MEDIA_PREV);
  111.         }
  112.       }
  113.      
  114.     }
  115.  
  116.     //Button 4: Next Song / Vol Up
  117.     if (bt4ActiveState != bt4Active){
  118.       bt4ActiveState = bt4Active;
  119.        if (volMode){
  120.           //Button 2 is held
  121.           if (bt4Active){
  122.             Consumer_press(MEDIA_VOLUME_UP);
  123.           }else{
  124.             Consumer_release(MEDIA_VOLUME_UP);
  125.           }
  126.        }else{
  127.           //Default mode
  128.           if (bt4Active){
  129.             Consumer_press(MEDIA_NEXT);
  130.           }else{
  131.              Consumer_release(MEDIA_NEXT);
  132.           }
  133.        }
  134.     }
  135.  
  136.     //Set the status LED
  137.     digitalWrite(LED_A, HIGH);
  138.     digitalWrite(LED_B, LOW);
  139.   }else{
  140.     //Mode B
  141.     //For Adobe Premiere Pro CS6 hotkey
  142.    
  143.     //Button 1: Cut clip at cursor location
  144.     if (bt1ActiveState != bt1Active){
  145.       bt1ActiveState = bt1Active;
  146.       if (bt1Active){
  147.         //Ctrl + K = Cut
  148.         Keyboard_press(KEY_LEFT_CTRL);
  149.         delay(100);
  150.         Keyboard_write('k');
  151.         delay(100);
  152.         Keyboard_release(KEY_LEFT_CTRL);
  153.       }
  154.     }
  155.  
  156.     //Button 2: Snap to next marker and cut
  157.     if (bt2ActiveState != bt2Active){
  158.       bt2ActiveState = bt2Active;
  159.       if (bt2Active){
  160.         //Shift + M = Next Marker
  161.         Keyboard_press(KEY_LEFT_SHIFT);
  162.         delay(100);
  163.         Keyboard_write('m');
  164.         delay(100);
  165.         Keyboard_release(KEY_LEFT_SHIFT);
  166.         delay(100);
  167.         //Ctrl + K = Cut
  168.         Keyboard_press(KEY_LEFT_CTRL);
  169.         delay(100);
  170.         Keyboard_write('k');
  171.         delay(100);
  172.         Keyboard_release(KEY_LEFT_CTRL);
  173.       }
  174.     }
  175.  
  176.     //Button 3: Cut and ripple delete (Require selecting the target clip before click)
  177.     if (bt3ActiveState != bt3Active){
  178.       bt3ActiveState = bt3Active;
  179.       if (bt3Active){
  180.         //Ctrl + K = Cut
  181.         Keyboard_press(KEY_LEFT_CTRL);
  182.         delay(100);
  183.         Keyboard_write('k');
  184.         delay(100);
  185.         Keyboard_release(KEY_LEFT_CTRL);
  186.  
  187.         //UP = Move to front of the clip
  188.         delay(100);
  189.         Keyboard_press(KEY_UP_ARROW);
  190.         delay(100);
  191.         Keyboard_release(KEY_UP_ARROW);
  192.  
  193.         //Ripple Delete
  194.         delay(100);
  195.         Keyboard_press(KEY_LEFT_SHIFT);
  196.         delay(100);
  197.         Keyboard_press(KEY_DELETE);
  198.         delay(100);
  199.         Keyboard_release(KEY_DELETE);
  200.         Keyboard_release(KEY_LEFT_SHIFT);
  201.       }
  202.     }
  203.  
  204.     //Button 4: Ripple Delete (Require selecting clip)
  205.     if (bt4ActiveState != bt4Active){
  206.       bt4ActiveState = bt4Active;
  207.       if (bt4Active){
  208.         //Shift + DEL = Ripple delete
  209.         Keyboard_press(KEY_LEFT_SHIFT);
  210.         delay(100);
  211.         Keyboard_press(KEY_DELETE);
  212.         delay(100);
  213.         Keyboard_release(KEY_DELETE);
  214.         Keyboard_release(KEY_LEFT_SHIFT);
  215.       }
  216.        
  217.     }
  218.    
  219.     //Set the status LED
  220.     digitalWrite(LED_A, LOW);
  221.     digitalWrite(LED_B, HIGH);
  222.   }
  223.  
  224.   delay(50);  //naive debouncing
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement