-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobocam_control.pde
223 lines (168 loc) · 4.19 KB
/
robocam_control.pde
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* ///////////////////////////////////////////
* ROBOCAM_CONTROL
* Feb 02-13-2013
* Cameron Browning
* Use WASD to Focus / Zoom
* Use Arrow Keys to Pan / Tilt
* A VC-C4 Camera
*
*////////////////////////////////////////////
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int[] header;
int[] command;
int[] footer;
boolean isReady;
boolean newLine;
String header_str = "FF 30 30 00";
String hostControl_str = "90 30";
String footer_str = "EF";
String initialize_str = "58 30";
String panLeft = "53 32";
String panRight = "53 31";
String tiltUp = "53 33";
String tiltDown = "53 34";
String panTiltStop = "53 30";
String zoomIn = "A2 32";
String zoomOut = "A2 31";
String focusNear = "A1 32";
String focusFar = "A1 33";
String focusManual = "A1 31";
String zoomStop = "A2 30";
String lastHexString = "";
void setup()
{
newLine = true;
size(200, 200);
//println(Serial.list());
String portName;
int portNumber = -1;
// if you're lucky:
// portName = Serial.list()[0];
// otherwise:
portName = "/dev/tty.usbserial";
String [] serialPorts = Serial.list();
for (int i=0;i<serialPorts.length;i++) {
//print(serialPorts[i]);
if (serialPorts[i].equals(portName)) {
// println("*");
portNumber = i;
}
else {
// println();
}
}
if(portNumber == -1){
println(portName + " not found, defaulting to: " + serialPorts[0]+".");
portNumber = 0;
} else {
println(portName + " found. ");
}
portName = Serial.list()[portNumber];
myPort = new Serial(this, portName, 9600);
isReady = true;
sendCommand(buildSerialString(hostControl_str));
sendCommand(buildSerialString(initialize_str));
/*
// this works
int [] controlstring = {0xFF, 0x30, 0x30, 0x00, 0x90, 0x30, 0xEF};
for(int i=0;i<controlstring.length;i++){
println(controlstring[i]);
myPort.write(controlstring[i]);
}
*/
}
void draw() {
if ( myPort.available() > 0) { // If data is available,
if (newLine) print("received:\t");
newLine = false;
int inByte = myPort.read();
print (hex(inByte, 2));
if (hex(inByte, 2).equals("EF")) {
println(".");
newLine = true;
}
}
else {
}
}
void stop() {
/* this is not calling :/ */
println("exiting application, stopping the serial port.");
myPort.stop();
exit();
}
int [] buildSerialString(String commandString) {
int [] returnArray;
if (!commandString.equals(lastHexString)) {
String wholeString = header_str + " " + commandString + " " + footer_str;
String[] list = split(wholeString, " ");
returnArray = new int[list.length];
for (int i=0;i<list.length;i++) {
returnArray[i] = int(unhex(list[i]));
}
lastHexString = commandString;
}
else {
returnArray = new int[0];
}
return returnArray;
}
void sendCommand(int [] serialString) {
// if(isReady){
for (int i=0;i<serialString.length;i++) {
myPort.write(serialString[i]);
}
isReady = false;
// }
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
sendCommand(buildSerialString(tiltUp));
}
else if (keyCode == DOWN) {
sendCommand(buildSerialString(tiltDown));
}
else if (keyCode== LEFT) {
sendCommand(buildSerialString(panLeft));
}
else if (keyCode == RIGHT) {
sendCommand(buildSerialString(panRight));
}
}
else {
if (key=='a' || key == 'A') {
sendCommand(buildSerialString(focusNear));
}
else if (key == 'd' || key == 'D') {
sendCommand(buildSerialString(focusFar));
}
else if (key == 'w' || key == 'W') {
sendCommand(buildSerialString(zoomIn));
}
else if (key == 's' || key == 'S') {
sendCommand(buildSerialString(zoomOut));
} else {
// any other key
stop();
}
// do nothing
}
}
void keyReleased() {
if (key==CODED) {
// arrow key released
sendCommand(buildSerialString(panTiltStop));
}
else {
// it's a zoom or focus
if (key=='w' || key == 'W' || key == 's' || key == 'S') {
sendCommand(buildSerialString(zoomStop));
}
else {
sendCommand(buildSerialString(focusManual));
}
}
}