processing #7 : proControll

lundi 12 septembre 2011 | programmation, tutorials


Aujourd’hui, je vais aborder une extension librairie qui offre pas mal de possibilités : procontroll.
Pour installer cette librairie, le plus simple est de la dézipper dans

utilisateurs > benoît > documents > processing > librairies

selon vos préférences de Processing :

À quoi sert cette librairie ?
Elle permet de brancher plusieurs périphériques de type joypad, joystick, souris et de récupérer les événements liés.
Premier point : débrancher les périphériques exotiques des ports USB (paradoxalement ma souris Logitech sans fil est incompatible).
Un exemple est livré, celui affiche dans la console les périphériques disponibles :

import procontroll.*;
import java.io.*;

ControllIO controll;

void setup(){
  size(400,400);
  
  controll = ControllIO.getInstance(this);
  controll.printDevices();
  
  for(int i = 0; i < controll.getNumberOfDevices(); i++){
    ControllDevice device = controll.getDevice(i);
    
    println(device.getName()+" has:");
    println(" " + device.getNumberOfSliders() + " sliders");
    println(" " + device.getNumberOfButtons() + " buttons");
    println(" " + device.getNumberOfSticks() + " sticks");
    
    device.printSliders();
    device.printButtons();
    device.printSticks();
  }
}

Si ma souris Logitech est branchée, ça coince :

Failed to enumerate device: Device open failed: -536870203
<<< available proCONTROLL devices: >>>

en la débranchant (sans relancer Processing juste stop -> play), on découvre que je suis sur un Mac ;¬)

0
0

<<< available proCONTROLL devices: >>>

0: Apple Internal Keyboard / Trackpad
1: Apple Internal Keyboard / Trackpad

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Apple Internal Keyboard / Trackpad has:
0 sliders
180 buttons
0 sticks

<<< available Apple Internal Keyboard / Trackpad buttons: >>>

0: Left Control
1: Left Shift
2: Left Alt
3: Left Windows
4: Right Control
5: Right Shift
6: Right Alt
7: Right Windows
8: Unknown
9: Unknown
10: Unknown
11: A
12: B
13: C
14: D
15: E
16: F
17: G
18: H
19: I
20: J
21: K
22: L
23: M
24: N
25: O
26: P
27: Q
28: R
29: S
30: T
31: U
32: V
33: W
34: X
35: Y
36: Z
37: 1
38: 2
39: 3
40: 4
41: 5
42: 6
43: 7
44: 8
45: 9
46: 0
47: Return
48: Escape
49: Back
50: Tab
51:
52: –
53: =
54: [
55: ]
56: \
57: .
58: ;
59: ‘
60: ~
61: ,
62: .
63: /
64: Caps Lock
65: F1
66: F2
67: F3
68: F4
69: F5
70: F6
71: F7
72: F8
73: F9
74: F10
75: F11
76: F12
77: SysRq
78: Scroll Lock
79: Pause
80: Insert
81: Home
82: Pg Up
83: Delete
84: End
85: Pg Down
86: Right
87: Left
88: Down
89: Up
90: Num Lock
91: Num /
92: Unknown
93: Num –
94: Num +
95: Num Enter
96: Num 1
97: Num 2
98: Num 3
99: Num 4
100: Num 5
101: Num 6
102: Num 7
103: Num 8
104: Num 9
105: Num 0
106: Num .
107: \
108: Apps
109: Power
110: Num =
111: F13
112: F14
113: F15
114: Unknown
115: Unknown
116: Unknown
117: Unknown
118: Unknown
119: Unknown
120: Unknown
121: Unknown
122: Unknown
123: Unknown
124: Unknown
125: Unknown
126: Unknown
127: Stop
128: Unknown
129: Unknown
130: Unknown
131: Unknown
132: Unknown
133: Unknown
134: Unknown
135: Unknown
136: Unknown
137: Caps Lock
138: Num Lock
139: Scroll Lock
140: ,
141: Unknown
142: Unknown
143: Unknown
144: Unknown
145: Unknown
146: Unknown
147: Unknown
148: Unknown
149: Unknown
150: Unknown
151: Unknown
152: Unknown
153: Unknown
154: Unknown
155: Unknown
156: Unknown
157: Unknown
158: Unknown
159: Unknown
160: Unknown
161: SysRq
162: Unknown
163: Unknown
164: Pg Up
165: Return
166: Unknown
167: Unknown
168: Unknown
169: Unknown
170: Unknown
171: Unknown
172: Left Control
173: Left Shift
174: Left Alt
175: Left Windows
176: Right Control
177: Right Shift
178: Right Alt
179: Right Windows

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Apple Internal Keyboard / Trackpad has:
2 sliders
3 buttons
1 sticks

<<< available Apple Internal Keyboard / Trackpad sliders: >>>

0: x relative
1: y relative

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad buttons: >>>

0: Left
1: Right
2: Middle

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad sticks: >>>

0: y x

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Et si je branche un joypad (l’affreux de la freebox v6)

0
0
0
0

<<< available proCONTROLL devices: >>>

0: Apple Internal Keyboard / Trackpad
1: Apple Internal Keyboard / Trackpad
2: Apple Internal Keyboard / Trackpad
3: Generic USB Joystick

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Apple Internal Keyboard / Trackpad has:
0 sliders
180 buttons
0 sticks

<<< available Apple Internal Keyboard / Trackpad buttons: >>>

0: Left Control
(…)
179: Right Windows

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Apple Internal Keyboard / Trackpad has:
2 sliders
1 buttons
1 sticks

<<< available Apple Internal Keyboard / Trackpad sliders: >>>

0: x relative
1: y relative

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad buttons: >>>

0: Left

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad sticks: >>>

0: y x

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Apple Internal Keyboard / Trackpad has:
2 sliders
3 buttons
1 sticks

<<< available Apple Internal Keyboard / Trackpad sliders: >>>

0: x relative
1: y relative

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad buttons: >>>

0: Left
1: Right
2: Middle

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Apple Internal Keyboard / Trackpad sticks: >>>

0: y x

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Generic USB Joystick has:
5 sliders
13 buttons
0 sticks

<<< available Generic USB Joystick sliders: >>>

0: x absolute
1: y absolute
2: z absolute
3: z absolute
4: rz absolute

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available Generic USB Joystick buttons: >>>

0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9
10: 10
11: 11
12: cooliehat: pov

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Un autre modèle :

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
USB GAMEPAD 8116 has:
5 sliders
11 buttons
0 sticks

<<< available USB GAMEPAD 8116 sliders: >>>

0: z absolute
1: rz absolute
2: x absolute
3: y absolute
4: slider absolute

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available USB GAMEPAD 8116 buttons: >>>

0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9
10: cooliehat: pov

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

On connait le nom du périphérique
il suffit de régler cette ligne selon votre nom de périphérique comme ci-dessous
à la ligne 26 : device = controll.getDevice(« USB GAMEPAD 8116 »);

import procontroll.*;
import java.io.*;

ControllIO controll;
ControllDevice device;
ControllSlider sliderX; //selon votre sauce
ControllSlider sliderY; //idem
ControllButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; //idem

void setup() {
  size(400, 400);

  controll = ControllIO.getInstance(this);
  for (int i = 0; i < controll.getNumberOfDevices(); i++) {
    ControllDevice device = controll.getDevice(i);

    println(device.getName()+" has:");
    println(" " + device.getNumberOfSliders() + " sliders");
    println(" " + device.getNumberOfButtons() + " buttons");
    println(" " + device.getNumberOfSticks() + " sticks");

    device.printSliders();
    device.printButtons();
    device.printSticks();
  }
  device = controll.getDevice("USB GAMEPAD 8116");
  device.setTolerance(0.05f);
  device.rumble(100,1);
  sliderX = device.getSlider(2);
  sliderY = device.getSlider(3);

  b0 = device.getButton(0);
  b1 = device.getButton(1);
  b2 = device.getButton(2);
  b3 = device.getButton(3);
  b4 = device.getButton(4);
  b5 = device.getButton(5);
  b6 = device.getButton(6);
  b7 = device.getButton(7);
  b8 = device.getButton(8);
  b9 = device.getButton(9);

  fill(0);
  rectMode(CENTER);
  
}

void draw() {
  background(255);

  if (b0.pressed()) {
    fill(255, 0, 0);
    println("b0");
  }
  else if (b1.pressed()) {
    fill(0, 255, 0);
    println("b0");
  }
  else if (b2.pressed()) {
    fill(0, 0,255);
    println("b2");
  }
  else if (b3.pressed()) {
    fill(255,255, 0);
    println("b3");
  }
  else if (b4.pressed()) {
    fill(0,255, 255);
    println("b4");
  }
   else if (b5.pressed()) {
    fill(255, 0, 255);
    println("b5");
  }
  else if (b6.pressed()) {
    fill(255, 255, 255);
    println("b6");
  }
   else if (b7.pressed()) {
    fill(127, 0, 255);
    println("b7");
  }  
  else if (b8.pressed()) {
    fill(255, 0, 255);
    println("b8");
  }
  else if (b9.pressed()) {
    fill(255, 0, 255);
    println("b9");
  }
  else {
    fill(0);
  }

  float x = sliderX.getTotalValue() + width/2;
  float y = sliderY.getTotalValue() + height/2;

  if (x > width + 20 || x < - 20 || y > height + 20 || y < - 20) {
    sliderX.reset();
    sliderY.reset();
  }

  rect(x, y, 20, 20);
}

Je vous laisse décortiquer tout ça :)
Have fun !


tags : , , , ,

3 commentaires

Hernan le 7 Mai 2012 vers 20:01

Muchas gracias desde Argentina, me sirvio mucho :)

je vous remercie beaucoup de l’Argentine, j’ai servi beacoup :)

PD:désolé pour mauvaise traduction

ben le 7 Mai 2012 vers 20:35

de nada Hernan :)

GeorGe le 19 Oct 2012 vers 14:41

Yop !

Tout d’abord merci a toi pour ce code il ma permis de découvrir l’utilisation de ma manette Xbox via processing

Mais cependant une question réside => Y’a t’il possibilité d’utiliser les 2 Joysticks ?
Car pour ma part je ne peut utiliser que le JOY droit :/ puis dans la console processing je ne voie que 5 sliders donc je m’interroge ‘O_o’
Je ne vois pas non plus les gachettes donc je ne sais pas si il y a possibilité de les utiliser

Merci à toi

Comments are closed.