This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
forked from damellis/A4S
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce294c7
commit 8093c46
Showing
14 changed files
with
637 additions
and
37 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
/** | ||
* 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 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()"); | ||
} | ||
} | ||
} |
Oops, something went wrong.