/
Arduino.java
253 lines (231 loc) · 7.66 KB
/
Arduino.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Arduino.java - Arduino/firmata library for Processing
* Copyright (C) 2006-08 David A. Mellis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* Processing code to communicate with the Arduino Firmata 2 firmware.
* http://firmata.org/
*
* $Id$
*/
package cc.arduino;
import processing.core.PApplet;
import processing.serial.Serial;
import org.firmata.Firmata;
/**
* Together with the Firmata 2 firmware (an Arduino sketch uploaded to the
* Arduino board), this class allows you to control the Arduino board from
* Processing: reading from and writing to the digital pins and reading the
* analog inputs.
*/
public class Arduino {
/**
* Constant to set a pin to input mode (in a call to pinMode()).
*/
public static final int INPUT = 0;
/**
* Constant to set a pin to output mode (in a call to pinMode()).
*/
public static final int OUTPUT = 1;
/**
* Constant to set a pin to analog mode (in a call to pinMode()).
*/
public static final int ANALOG = 2;
/**
* Constant to set a pin to PWM mode (in a call to pinMode()).
*/
public static final int PWM = 3;
/**
* Constant to set a pin to servo mode (in a call to pinMode()).
*/
public static final int SERVO = 4;
/**
* Constant to set a pin to shiftIn/shiftOut mode (in a call to pinMode()).
*/
public static final int SHIFT = 5;
/**
* Constant to set a pin to I2C mode (in a call to pinMode()).
*/
public static final int I2C = 6;
/**
* Constant to set a pin to input mode and enable the pull-up resistor (in a call to pinMode()).
*/
public static final int INPUT_PULLUP = 11;
/**
* Constant to write a high value (+5 volts) to a pin (in a call to
* digitalWrite()).
*/
public static final int LOW = 0;
/**
* Constant to write a low value (0 volts) to a pin (in a call to
* digitalWrite()).
*/
public static final int HIGH = 1;
PApplet parent;
Serial serial;
SerialProxy serialProxy;
Firmata firmata;
// We need a class descended from PApplet so that we can override the
// serialEvent() method to capture serial data. We can't use the Arduino
// class itself, because PApplet defines a list() method that couldn't be
// overridden by the static list() method we use to return the available
// serial ports. This class needs to be public so that the Serial class
// can access its serialEvent() method.
public class SerialProxy extends PApplet {
public SerialProxy() {
}
public void serialEvent(Serial which) {
try {
// Notify the Arduino class that there's serial data for it to process.
while (which.available() > 0)
firmata.processInput(which.read());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error inside Arduino.serialEvent()");
}
}
}
public class FirmataWriter implements Firmata.Writer {
public void write(int val) {
serial.write(val);
// System.out.print("<" + val + " ");
}
}
public void dispose() {
this.serial.dispose();
}
/**
* Get a list of the available Arduino boards; currently all serial devices
* (i.e. the same as Serial.list()). In theory, this should figure out
* what's an Arduino board and what's not.
*/
public static String[] list() {
return Serial.list();
}
/**
* Create a proxy to an Arduino board running the Firmata 2 firmware at the
* default baud rate of 57600.
*
* @param parent the Processing sketch creating this Arduino board
* (i.e. "this").
* @param iname the name of the serial device associated with the Arduino
* board (e.g. one the elements of the array returned by Arduino.list())
*/
public Arduino(PApplet parent, String iname) {
this(parent, iname, 57600);
}
/**
* Create a proxy to an Arduino board running the Firmata 2 firmware.
*
* @param parent the Processing sketch creating this Arduino board
* (i.e. "this").
* @param iname the name of the serial device associated with the Arduino
* board (e.g. one the elements of the array returned by Arduino.list())
* @param irate the baud rate to use to communicate with the Arduino board
* (the firmata library defaults to 57600, and the examples use this rate,
* but other firmwares may override it)
*/
public Arduino(PApplet parent, String iname, int irate) {
this.parent = parent;
this.firmata = new Firmata(new FirmataWriter());
this.serialProxy = new SerialProxy();
this.serial = new Serial(serialProxy, iname, irate);
parent.registerMethod("dispose", this);
try {
Thread.sleep(3000); // let bootloader timeout
} catch (InterruptedException e) {}
firmata.init();
}
/**
* Returns the last known value read from the digital pin: HIGH or LOW.
*
* @param pin the digital pin whose value should be returned (from 2 to 13,
* since pins 0 and 1 are used for serial communication)
*/
public int digitalRead(int pin) {
return firmata.digitalRead(pin);
}
/**
* Returns the last known value read from the analog pin: 0 (0 volts) to
* 1023 (5 volts).
*
* @param pin the analog pin whose value should be returned (from 0 to 5)
*/
public int analogRead(int pin) {
return firmata.analogRead(pin);
}
/**
* Set a digital pin to input or output mode.
*
* @param pin the pin whose mode to set (from 2 to 13)
* @param mode either Arduino.INPUT or Arduino.OUTPUT
*/
public void pinMode(int pin, int mode) {
try {
firmata.pinMode(pin, mode);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error inside Arduino.pinMode()");
}
}
/**
* Write to a digital pin (the pin must have been put into output mode with
* pinMode()).
*
* @param pin the pin to write to (from 2 to 13)
* @param value the value to write: Arduino.LOW (0 volts) or Arduino.HIGH
* (5 volts)
*/
public void digitalWrite(int pin, int value) {
try {
firmata.digitalWrite(pin, value);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error inside Arduino.digitalWrite()");
}
}
/**
* Write an analog value (PWM-wave) to a digital pin.
*
* @param pin the pin to write to (must be 9, 10, or 11, as those are they
* only ones which support hardware pwm)
* @param value the value: 0 being the lowest (always off), and 255 the highest
* (always on)
*/
public void analogWrite(int pin, int value) {
try {
firmata.analogWrite(pin, value);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error inside Arduino.analogWrite()");
}
}
/**
* Write a value to a servo pin.
*
* @param pin the pin the servo is attached to
* @param value the value: 0 being the lowest angle, and 180 the highest angle
*/
public void servoWrite(int pin, int value) {
try {
firmata.servoWrite(pin, value);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error inside Arduino.servoWrite()");
}
}
}