Public feed #3961356

Photoshop Control Pad

public unlisted text v1 · immutable
Raw Edit
·published 2018-01-19 22:44 UTC
rendered paste body
#include <Keyboard.h>
/*just a pin contact issue
 * 
 * 
 */

const int buttons[] = {2,3,4,5,6,7,8,9}; // array of all button pins
//const int dials[] = {0,1}; //analog pins
int dial0 = 0;

//Rotary encoder
#define outputA 15
#define outputB 14
int counter = 0; 
int aState;
int aLastState;  

char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
//  char ctrlKey = KEY_LEFT_CTRL;
char shiftKey = KEY_LEFT_SHIFT;
char altKey = KEY_LEFT_ALT;


void setup() {
 // put your setup code here, to run once:
  Serial.begin(9600);
  Keyboard.begin();

  //Buttons
  for(int i = buttons[0]; i < (sizeof(buttons)/sizeof(buttons[0]))+buttons[0]; ++i) {
    pinMode(i, INPUT);
  }
  //Dials
  dial0= analogRead(0);
  dial0= map(dial0, 0, 1023, 1, 20);

  //Rotary
  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
  // Reads the initial state of the outputA
  aLastState = digitalRead(outputA);   
}

boolean readButton(int pin) {
 // check and debounce buttons
 if (digitalRead(pin) == HIGH) {
 delay(10);
 if (digitalRead(pin) == HIGH) {
 return true;
 }
 }
 return false;
}


void doAction(int pin) {
 // perform tasks
 switch (pin) {
  // ----Modifiers ----
  //Shift
  /*case 2:
    Keyboard.press(shiftKey);
    Serial.print ("input");
    Serial.println(pin);
    delay(10);
    break;
  //Cmd
  case 3:
    Keyboard.press(ctrlKey);
    Serial.print ("input");
    Serial.println(pin);
    delay(10);
    break;*/

  // ----Shortcuts----
  //Undo
  case 4:
    Keyboard.press(ctrlKey);
    Keyboard.print('z');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Redo
  case 5:
    Keyboard.press(ctrlKey);
    Keyboard.print('y');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Brush
  case 6:
    Keyboard.press('b');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Eraser
  case 7:
    Keyboard.press('e');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Lasso
  case 8:
    Keyboard.press('l');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Save
  case 9:
    Keyboard.press(ctrlKey);
    Keyboard.print('s');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  //Rotary Switch -
  case 10:
    Keyboard.print('x');
    Serial.print ("input");
    Serial.println(pin);
    delay(200);
    Keyboard.releaseAll();
    break;
  default:
    Keyboard.releaseAll();
    break;
 }
}


void dialAction(int dial, int newVal, int lastVal) {
  switch (dial) {
    //Opacity
    case 0:
      delay(200);
      if (newVal!=lastVal) {
        int decim = ((newVal*5)/10);
        int unit = ((newVal *5)% 10);
        if (newVal==20) {
          Keyboard.write(48+0);
          Keyboard.write(48+0);
          Serial.println("max dial 1");
        } else {
          decim=constrain(decim,0,9);
          unit=constrain(unit,0,9);
          Serial.println(newVal*2);
          Keyboard.write(48+decim);
          Keyboard.write(48+unit);
        }
      }
      dial0=newVal;
      break;
    default:
      break;
  }
}

void rotaryAction(int dir) {
  if (dir>0) {
    Keyboard.press(']');
  }
  else {
    Keyboard.press('[');
  }
  Keyboard.releaseAll();
}

//------------------MAIN LOOP-------------------------
void loop() {

 // put your main code here, to run repeatedly:
  for(int i = buttons[0]; i < sizeof(buttons)/sizeof(buttons[0])+buttons[0]; ++i) {
  
    if (readButton(i)) {
      doAction(i);
    }
  }
  //Reset modifiers
  Keyboard.releaseAll();
  
  //Opacity
  //delay(500);
  int val0 = analogRead(0);
  val0 = map(val0, 0, 1023, 1, 20);
  //Serial.print ("dial0: ");
  //Serial.println(val0);
  if (val0!=dial0) {
    //Do something
    dialAction(0,val0,dial0);
  }

  //Size
  aState = digitalRead(outputA);
  if (aState != aLastState){ 
    if (digitalRead(outputB) != aState) { 
      //counter ++;
      rotaryAction(1);
    } else {
      //counter --;
      rotaryAction(-1);
    }
  //Serial.print("Position: ");
  //Serial.println(counter);
  } 
  aLastState = aState; 
}