Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Inital commit
- Loading branch information
0 parents
commit 7f16143
Showing
14 changed files
with
668 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path=""/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> | ||
<accessrules> | ||
<accessrule kind="accessible" pattern="gnu/io/**"/> | ||
</accessrules> | ||
</classpathentry> | ||
<classpathentry kind="lib" path="/Library/Java/Extensions/RXTXcomm.jar"/> | ||
<classpathentry kind="lib" path="/Users/x/Downloads/org.json-20120521 (1).jar"/> | ||
<classpathentry kind="output" path=""/> | ||
</classpath> |
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 @@ | ||
/LampDemo.class |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>ArduinoOSX</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,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 |
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,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/**". |
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,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(); | ||
} | ||
} | ||
} |
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,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. |
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,3 @@ | ||
/Arduino.class | ||
/CharBuffer.class | ||
/SerialPort.class |
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,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<String> 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<String> portNames) | ||
{ | ||
for(String portName : portNames) | ||
if(portName.startsWith("/dev/ttyUSB")) | ||
return portName; | ||
return null; | ||
} | ||
|
||
private static String _guessMacPortName(ArrayList<String> 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<String> 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); | ||
} | ||
|
||
} |
Oops, something went wrong.