Advertisement
TheInkAdept

Photoshop Controller

Sep 9th, 2018
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include <Keyboard.h>
  2.  
  3.  
  4. const int buttons[] = {2,3,4,5,6,7,8,9}; // array of all button pins
  5. //const int dials[] = {0,1}; //analog pins
  6. int dial0 = 0;
  7.  
  8. //Rotary encoder
  9. #define outputA 15
  10. #define outputB 14
  11. int counter = 0;
  12. int aState;
  13. int aLastState;  
  14.  
  15. char ctrlKey = KEY_LEFT_GUI;
  16. // use this option for Windows and Linux:
  17. //  char ctrlKey = KEY_LEFT_CTRL;
  18. char shiftKey = KEY_LEFT_SHIFT;
  19. char altKey = KEY_LEFT_ALT;
  20.  
  21.  
  22. void setup() {
  23.  // put your setup code here, to run once:
  24.   Serial.begin(9600);
  25.   Keyboard.begin();
  26.  
  27.   //Buttons
  28.   for(int i = buttons[0]; i < (sizeof(buttons)/sizeof(buttons[0]))+buttons[0]; ++i) {
  29.     pinMode(i, INPUT);
  30.   }
  31.   //Dials
  32.   dial0= analogRead(0);
  33.   dial0= map(dial0, 0, 1023, 1, 20);
  34.  
  35.   //Rotary
  36.   pinMode (outputA,INPUT);
  37.   pinMode (outputB,INPUT);
  38.   // Reads the initial state of the outputA
  39.   aLastState = digitalRead(outputA);  
  40. }
  41.  
  42. boolean readButton(int pin) {
  43.  // check and debounce buttons
  44.  if (digitalRead(pin) == HIGH) {
  45.  delay(10);
  46.  if (digitalRead(pin) == HIGH) {
  47.  return true;
  48.  }
  49.  }
  50.  return false;
  51. }
  52.  
  53.  
  54. void doAction(int pin) {
  55.  // perform tasks
  56.  switch (pin) {
  57.   // ----Modifiers ----
  58.   //Shift
  59.   /*case 2:
  60.     Keyboard.press(shiftKey);
  61.     Serial.print ("input");
  62.     Serial.println(pin);
  63.     delay(10);
  64.     break;
  65.   //Cmd
  66.   case 3:
  67.     Keyboard.press(ctrlKey);
  68.     Serial.print ("input");
  69.     Serial.println(pin);
  70.     delay(10);
  71.     break;*/
  72.  
  73.   // ----Shortcuts----
  74.   //Undo
  75.   case 4:
  76.     Keyboard.press(ctrlKey);
  77.     Keyboard.print('z');
  78.     Serial.print ("input");
  79.     Serial.println(pin);
  80.     delay(200);
  81.     Keyboard.releaseAll();
  82.     break;
  83.   //Redo
  84.   case 5:
  85.     Keyboard.press(ctrlKey);
  86.     Keyboard.print('y');
  87.     Serial.print ("input");
  88.     Serial.println(pin);
  89.     delay(200);
  90.     Keyboard.releaseAll();
  91.     break;
  92.   //Brush
  93.   case 6:
  94.     Keyboard.press('b');
  95.     Serial.print ("input");
  96.     Serial.println(pin);
  97.     delay(200);
  98.     Keyboard.releaseAll();
  99.     break;
  100.   //Eraser
  101.   case 7:
  102.     Keyboard.press('e');
  103.     Serial.print ("input");
  104.     Serial.println(pin);
  105.     delay(200);
  106.     Keyboard.releaseAll();
  107.     break;
  108.   //Lasso
  109.   case 8:
  110.     Keyboard.press('l');
  111.     Serial.print ("input");
  112.     Serial.println(pin);
  113.     delay(200);
  114.     Keyboard.releaseAll();
  115.     break;
  116.   //Save
  117.   case 9:
  118.     Keyboard.press(ctrlKey);
  119.     Keyboard.print('s');
  120.     Serial.print ("input");
  121.     Serial.println(pin);
  122.     delay(200);
  123.     Keyboard.releaseAll();
  124.     break;
  125.   //Rotary Switch -
  126.   case 10:
  127.     Keyboard.print('x');
  128.     Serial.print ("input");
  129.     Serial.println(pin);
  130.     delay(200);
  131.     Keyboard.releaseAll();
  132.     break;
  133.   default:
  134.     Keyboard.releaseAll();
  135.     break;
  136.  }
  137. }
  138.  
  139.  
  140. void dialAction(int dial, int newVal, int lastVal) {
  141.   switch (dial) {
  142.     //Opacity
  143.     case 0:
  144.       delay(200);
  145.       if (newVal!=lastVal) {
  146.         int decim = ((newVal*5)/10);
  147.         int unit = ((newVal *5)% 10);
  148.         if (newVal==20) {
  149.           Keyboard.write(48+0);
  150.           Keyboard.write(48+0);
  151.           Serial.println("max dial 1");
  152.         } else {
  153.           decim=constrain(decim,0,9);
  154.           unit=constrain(unit,0,9);
  155.           Serial.println(newVal*2);
  156.           Keyboard.write(48+decim);
  157.           Keyboard.write(48+unit);
  158.         }
  159.       }
  160.       dial0=newVal;
  161.       break;
  162.     default:
  163.       break;
  164.   }
  165. }
  166.  
  167. void rotaryAction(int dir) {
  168.   if (dir>0) {
  169.     Keyboard.press(']');
  170.   }
  171.   else {
  172.     Keyboard.press('[');
  173.   }
  174.   Keyboard.releaseAll();
  175. }
  176.  
  177. //------------------MAIN LOOP-------------------------
  178. void loop() {
  179.  
  180.  // put your main code here, to run repeatedly:
  181.   for(int i = buttons[0]; i < sizeof(buttons)/sizeof(buttons[0])+buttons[0]; ++i) {
  182.  
  183.     if (readButton(i)) {
  184.       doAction(i);
  185.     }
  186.   }
  187.   //Reset modifiers
  188.   Keyboard.releaseAll();
  189.  
  190.   //Opacity
  191.   //delay(500);
  192.   int val0 = analogRead(0);
  193.   val0 = map(val0, 0, 1023, 1, 20);
  194.   //Serial.print ("dial0: ");
  195.   //Serial.println(val0);
  196.   if (val0!=dial0) {
  197.     //Do something
  198.     dialAction(0,val0,dial0);
  199.   }
  200.  
  201.   //Size
  202.   aState = digitalRead(outputA);
  203.   if (aState != aLastState){
  204.     if (digitalRead(outputB) != aState) {
  205.       //counter ++;
  206.       rotaryAction(1);
  207.     } else {
  208.       //counter --;
  209.       rotaryAction(-1);
  210.     }
  211.   //Serial.print("Position: ");
  212.   //Serial.println(counter);
  213.   }
  214.   aLastState = aState;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement