Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Inital commit
  • Loading branch information
amd11037 committed Apr 1, 2015
0 parents commit 7f16143
Show file tree
Hide file tree
Showing 14 changed files with 668 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions .classpath
@@ -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>
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/LampDemo.class
17 changes: 17 additions & 0 deletions .project
@@ -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>
11 changes: 11 additions & 0 deletions .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
9 changes: 9 additions & 0 deletions 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/**".
101 changes: 101 additions & 0 deletions 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();
}
}
}
8 changes: 8 additions & 0 deletions 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.
3 changes: 3 additions & 0 deletions arduino/.gitignore
@@ -0,0 +1,3 @@
/Arduino.class
/CharBuffer.class
/SerialPort.class
179 changes: 179 additions & 0 deletions 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<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);
}

}

0 comments on commit 7f16143

Please sign in to comment.