diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..be6c989 Binary files /dev/null and b/.DS_Store differ diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4c210bc --- /dev/null +++ b/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7df38e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/LampDemo.class diff --git a/.project b/.project new file mode 100644 index 0000000..da80612 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ArduinoOSX + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8000cd6 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/Compiler warning.txt b/Compiler warning.txt new file mode 100644 index 0000000..2b178e6 --- /dev/null +++ b/Compiler warning.txt @@ -0,0 +1,9 @@ +If you get a compiler warning on the gnu.io.CommPort import: + +Modify your build path to explicitly allow access to the gnu.io.CommPort class: + +1. Open the Libraries tab of the Java Build Path project property window. +2. Expand the JRE System Library entry. +3. Select "Access rules" and hit the Edit button. +4. Click the Add button in the resulting dialog. +5. For the new access rule, set the resolution to Accessible and the pattern to "gnu/io/**". diff --git a/LampDemo.java b/LampDemo.java new file mode 100644 index 0000000..aeb366f --- /dev/null +++ b/LampDemo.java @@ -0,0 +1,101 @@ +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Socket; +import java.net.UnknownHostException; +import org.json.*; +import arduino.Arduino; + +public class LampDemo +{ + boolean LAMP_ON = false; + public static void main(String[] args) throws UnknownHostException, IOException, JSONException + { + Arduino ard = new Arduino("/dev/tty0"); + ard.connect(); + String params; + String JSONResponse; + BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); + Socket clientSocket = new Socket("localhost", 3333); + DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); + BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + params = inFromUser.readLine(); + outToServer.writeBytes(params + '\n'); + while(ard.isConnected()) + { + while ((JSONResponse = inFromServer.readLine()) != null) { + JSONObject obj = new JSONObject(JSONResponse); + System.out.println(obj); + //do something with this + /* + * //testDigitalWrite(ard); + ld.lampSwitch(ard, ld.LAMP_ON); + */ + } + } + clientSocket.close(); + inFromUser.close(); + inFromServer.close(); + outToServer.close(); + + ard.disconnect(); + } + + public void lampSwitch(Arduino ard, boolean lampON) + { + if (lampON) + ard.digitalWrite(13, true); + else + ard.digitalWrite(13, false); + } + + private static void testAnalogWrite(Arduino ard) + { + for(int n=0; n<255; n++) + { + System.out.println(n); + ard.analogWrite(3, n); + sleep(10); + } + } + + private static void testAnalogRead(Arduino ard) + { + for(int n=0; n<100; n++) + { + System.out.println(ard.analogRead(5)); + sleep(100); + } + } + + private static void testDigitalRead(Arduino ard) + { + ard.pinMode(10, 'I'); + System.out.println(ard.digitalRead(10)); + } + + private static void testDigitalWrite(Arduino ard) + { + ard.pinMode(13, 'O'); + for(int n=0; n<5; n++) + { + ard.digitalWrite(13, true); + sleep(500); + ard.digitalWrite(13, false); + sleep(500); + } + } + + private static void sleep(long delay) + { + try + { + Thread.sleep(delay); + } + catch(InterruptedException e) + { + e.printStackTrace(); + } + } +} diff --git a/Run in 32 bit mode.txt b/Run in 32 bit mode.txt new file mode 100644 index 0000000..8961a20 --- /dev/null +++ b/Run in 32 bit mode.txt @@ -0,0 +1,8 @@ +This project must be run in 32-bit mode in order to be compatible with the RxTx library. + +1. Open the properties for this project. +2. Select Run/Debug Settings (in the left-hand menu list). +3. Click on the main class name and then press the Edit button. +4. Click on the (x)= Arguments tab. +5. In the VM Arguments box enter -d32 +6. Click OK. diff --git a/arduino/.gitignore b/arduino/.gitignore new file mode 100644 index 0000000..2d86e39 --- /dev/null +++ b/arduino/.gitignore @@ -0,0 +1,3 @@ +/Arduino.class +/CharBuffer.class +/SerialPort.class diff --git a/arduino/Arduino.java b/arduino/Arduino.java new file mode 100644 index 0000000..c733315 --- /dev/null +++ b/arduino/Arduino.java @@ -0,0 +1,179 @@ +package arduino; + +import java.util.ArrayList; + +public class Arduino +{ + + private SerialPort _serialPort; + private boolean _isConnected = false; + + public Arduino(String portName) + { + this(new SerialPort(portName)); + } + + public Arduino(SerialPort port) + { + _serialPort = port; + } + + public int analogRead(int pinNumber) + { + String s = "ar" + pinNumber; + _serialPort.writeLn(s); + String result = _serialPort.readLine(); + return Integer.parseInt(result); + } + + public void analogWrite(int pinNumber, int value) + { + String s = "aw" + pinNumber + "," + value; + _serialPort.writeLn(s); + } + + public void connect() + { + _serialPort.setBaud(115200); + _serialPort.setParity(gnu.io.SerialPort.PARITY_NONE); + _serialPort.setDataBits(gnu.io.SerialPort.DATABITS_8); + _serialPort.setStopBits(gnu.io.SerialPort.STOPBITS_1); + _serialPort.setFlowControl(gnu.io.SerialPort.FLOWCONTROL_NONE); + _serialPort.open(); + try + { + for(int n=0; n<20; n++) + { + _serialPort.writeLn("99"); // writeLn flushes automatically + Thread.sleep(250); + if(_serialPort.available() > 0) + { + String response = _serialPort.readLine(); + if(response.equals("11")) + { + Thread.sleep(250); + while(_serialPort.available() > 0) + _serialPort.readChar(); + _isConnected = true; + return; + } + _serialPort.close(); + throw new RuntimeException("Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); + } + Thread.sleep(250); + } + } + catch(InterruptedException e) + { + throw new RuntimeException("Arduino.connect: interrupted while waiting for response from Arduino on port" + _serialPort.getName()); + } + _serialPort.close(); + throw new RuntimeException("Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); + } + + public boolean digitalRead(int pinNumber) + { + String s = "dr" + pinNumber; + _serialPort.writeLn(s); + String result = _serialPort.readLine(); + char cRes = result.charAt(0); + return cRes == '0' ? false : true; + } + + public void digitalWrite(int pinNumber, boolean value) + { + int iValue = value ? 1 : 0; + String s = "dw" + pinNumber + "," + iValue; + _serialPort.writeLn(s); + } + + public void disconnect() + { + _serialPort.close(); + _isConnected = false; + } + + public static String guessPortName() + { + String osName = System.getProperty("os.name").toLowerCase(); + //System.out.println("OS name = \"" + osName + "\""); + ArrayList portNames = SerialPort.listPortNames(); + String portName = null; + if(osName.indexOf("mac") >= 0) + portName = _guessMacPortName(portNames); + else if(osName.indexOf("win") >= 0) + portName = _guessWindowsPortName(portNames); + else if(osName.indexOf("linux") >= 0) + portName = _guessLinuxPortName(portNames); + else + throw new RuntimeException("GuessPortName.guess: operating system " + osName + " not supported"); + return portName; + } + + private static String _guessLinuxPortName(ArrayList portNames) + { + for(String portName : portNames) + if(portName.startsWith("/dev/ttyUSB")) + return portName; + return null; + } + + private static String _guessMacPortName(ArrayList portNames) + { + for(String portName : portNames) + if(portName.startsWith("/dev/tty.usbserial") || portName.startsWith("/dev/tty.usbmodem")) + return portName; + return null; + } + + private static String _guessWindowsPortName(ArrayList portNames) + { + /* + // if the for loop below this comment block does not work right, + // try using the code in this comment block instead + String portName = null; + for(int n=1; n<10; n++) + { + portName = "COM" + n; + SerialPort port = new SerialPort(portName); + try + { + port.open(); + port.close(); + break; + } + catch(Exception exn) + {} + } + return portName;*/ + for(String portName : portNames) + { + SerialPort port = new SerialPort(portName); + try + { + port.open(); + port.close(); + return portName; + } + catch(Exception exn) + {} + } + return null; + } + + public boolean isConnected() + { + return _isConnected; + } + + public void pinMode(int pinNumber, char mode) + { + if(mode == 'o') + mode = 'O'; + else if(mode == 'i') + mode = 'I'; + String s = "pm" + pinNumber + "," + mode; + _serialPort.writeLn(s); + } + +} diff --git a/arduino/CharBuffer.java b/arduino/CharBuffer.java new file mode 100644 index 0000000..3b38b9a --- /dev/null +++ b/arduino/CharBuffer.java @@ -0,0 +1,85 @@ +package arduino; + +public class CharBuffer +{ + + public final static int DEFAULT_SIZE = 1024; + + private char[] _buffer; + private int _size; + private int _head = 0; + private int _tail = 0; + private int _count = 0; + + public CharBuffer() + { + this(DEFAULT_SIZE); + } + + public CharBuffer(int size) + { + _size = size; + _buffer = new char[size]; + } + + public int count() + { + return _count; + } + + public char get() + { + char c = _buffer[_head++]; + if(_head >= _size) + _head = 0; + _count--; + return c; + } + + public void pushBack(char c) + { + if(_count == _size) + throw new RuntimeException("buffer full"); + _head--; + if(_head < 0) + _head = _size - 1; + _buffer[_head] = c; + } + + public void put(char c) + { + if(_count == _size) + throw new RuntimeException("buffer full"); + _buffer[_tail++] = c; + _count++; + if(_tail >= _size) + _tail = 0; + } + + public void put(String s) + { + int count = s.length(); + for(int n=0; n= _size) + head = 0; + } + //sb.append("\")"); + return sb.toString(); + } + +} diff --git a/arduino/SerialPort.java b/arduino/SerialPort.java new file mode 100644 index 0000000..c6fa973 --- /dev/null +++ b/arduino/SerialPort.java @@ -0,0 +1,235 @@ +package arduino; + +import gnu.io.CommPort; +import gnu.io.CommPortIdentifier; +import gnu.io.NoSuchPortException; +import gnu.io.PortInUseException; +import gnu.io.SerialPortEvent; +import gnu.io.SerialPortEventListener; +import gnu.io.UnsupportedCommOperationException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.TooManyListenersException; + +public class SerialPort implements SerialPortEventListener +{ + + public static final int DEFAULT_BUFFER_SIZE = 1024; + + private gnu.io.SerialPort _port; + private String _portName; + private InputStream _in; + private OutputStream _out; + private PrintWriter _pw; + + private int _baud = 115200; + private int _parity = gnu.io.SerialPort.PARITY_NONE; + private int _data = gnu.io.SerialPort.DATABITS_8; + private int _stop = gnu.io.SerialPort.STOPBITS_1; + private int _flow = gnu.io.SerialPort.FLOWCONTROL_NONE; + private boolean _isOpen = false; + + private CharBuffer _buffer = new CharBuffer(DEFAULT_BUFFER_SIZE); + + public SerialPort(String portName) + { + _portName = portName; + } + + public int available() + { + return _buffer.count(); + } + + public void close() + { + if(_isOpen) + { + _port.removeEventListener(); + _port.close(); + _isOpen = false; + } + } + + public String getName() + { + return this._portName; + } + + public boolean isOpen() + { + return _isOpen; + } + + public static ArrayList listPortNames() + { + Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); + ArrayList portNames = new ArrayList(); + while(portEnum.hasMoreElements()) + { + CommPortIdentifier portId = (CommPortIdentifier)portEnum.nextElement(); + String name = portId.getName(); + portNames.add(name); + } + return portNames; + } + + public void open() + { + CommPortIdentifier portIdentifier; + try + { + portIdentifier = CommPortIdentifier.getPortIdentifier(_portName); + if(portIdentifier.isCurrentlyOwned()) + throw new RuntimeException("SerialPort.open: port " + _portName + " is currently in use"); + CommPort commPort = portIdentifier.open("SerialPort.open", 2000); + if(!(commPort instanceof gnu.io.SerialPort)) + throw new RuntimeException("SerialPort.open: port " + _portName + " is not a serial port"); + _port = (gnu.io.SerialPort)commPort; + _port.setSerialPortParams(_baud, _data, _stop, _parity); + _port.setFlowControlMode(_flow); + _in = _port.getInputStream(); + _out = _port.getOutputStream(); + _pw = new PrintWriter(_out); + _port.addEventListener(this); + _port.notifyOnDataAvailable(true); + _isOpen = true; + } + catch(NoSuchPortException exn) + { + throw new RuntimeException("SerialPort.open: there is no port having the name " + _portName); + } + catch(PortInUseException exn) + { + throw new RuntimeException("SerialPort.open: port " + _portName + " is currently in use"); + } + catch(UnsupportedCommOperationException exn) + { + throw new RuntimeException("SerialPort.open: internal error: unsupported settings on port " + _portName); + // this would be an error in the setSerialPortParams method call above + } + catch(IOException e) + { + throw new RuntimeException("SerialPort.open: unable to retrieve input/output streams from serial port " + _portName); + } + catch(TooManyListenersException exn) + { + throw new RuntimeException("SerialPort.open: too many event listeners on port " + _portName); + } + } + + public char readChar() + { + while(true) + { + synchronized(this) + { + if(_buffer.count() > 0) + { + char c = _buffer.get(); + return c; + } + else + { + try + { + this.wait(); + } + catch(InterruptedException e) + { + throw new RuntimeException("SerialPort.readChar: interrupted while waiting for data on port " + _portName); + } + } + } + } + } + + public String readLine() + { + StringBuilder sb = new StringBuilder(); + while(true) + { + char c = readChar(); + if(c == '\r') + { + char c1 = readChar(); + if(c1 != '\n') + _buffer.pushBack(c1); + break; + } + else if(c == '\n') + break; + else + sb.append(c); + } + return sb.toString(); + } + + @Override + public void serialEvent(SerialPortEvent spev) + { + try + { + while(_in.available() > 0) + { + int chr = _in.read(); + synchronized(this) + { + _buffer.put((char)chr); + this.notify(); + } + } + } + catch(IOException exn) + { + throw new RuntimeException("SerialPort.serialEvent: error reading from port " + _portName); + } + } + + public void setBaud(int baudRate) + { + this._baud = baudRate; + } + + public void setDataBits(int dataBits) + { + this._data = dataBits; + } + + public void setFlowControl(int flowControl) + { + this._flow = flowControl; + } + + public void setParity(int parity) + { + this._parity = parity; + } + + public void setStopBits(int stopBits) + { + this._stop = stopBits; + } + + public void write(String string) + { + if(!_isOpen) + throw new RuntimeException("SerialPort.write: port " + _portName + " is not open"); + _pw.print(string); + _pw.flush(); + } + + public void writeLn(String string) + { + if(!_isOpen) + throw new RuntimeException("SerialPort.writeLn: port " + _portName + " is not open"); + _pw.println(string); + _pw.flush(); + } + +} diff --git a/java.policy.applet b/java.policy.applet new file mode 100644 index 0000000..35527af --- /dev/null +++ b/java.policy.applet @@ -0,0 +1,7 @@ +/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/ +/* DO NOT EDIT */ + +grant { + permission java.security.AllPermission; +}; + diff --git a/librxtxSerial.jnilib b/librxtxSerial.jnilib new file mode 100644 index 0000000..3ca1e3b Binary files /dev/null and b/librxtxSerial.jnilib differ