Before attempting these - ensure that you have the javax.comm property
files and the comm and ambient jar files set up correctly as outlined
previously.
The following piece of code finds the ports - serial and parallel that
are available on your machine and prints out a list of their names.
This comes in handy
when specifying a port for the EZIO.
import javax.comm.CommPortIdentifier; import java.util.Enumeration;
public class FindPorts { public static void main(String[] args) {
Enumeration e = CommPortIdentifier.getPortIdentifiers();
System.out.println("Ports available:\n");
while(e.hasMoreElements()){
CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
System.out.println("Port name: " + cpi.getName()
+ "\t Description: " + cpi.toString());
}
}
}
There are 2 ways to gain sensor readings using the Ambient Interaction Framework (AIF), the first using the AnalogSensor class provided and the second by accessing the EZIO class directly.
The following piece of code will loop continually on a single input line using the AnalogSensor,
the best way to see this working is to connect a touch sensor and see
what values are given for varying amounts of pressure on the sensor. In
order for these examples to work, you will need to connect the EZIO
board to COM port 1 (serial), and connect an analogue sensor to line 1
of the A/D inputs.
import edu.dstc.ambient.EZIO; import edu.dstc.ambient.AnalogSensor; import edu.dstc.ambient.DataValue; import java.io.IOException;
public class AnalogIn{ public static void main(String [] args){ EZIO e; AnalogSensor as;
e = new EZIO("COM1");
try {
as = new AnalogSensor(e, 1, 50);
} catch (IOException e1) {
as = null;
System.err.println("Unable to instantiate AnalogSensor:");
e1.printStackTrace();
}
while(true){
if (as != null) {
DataValue dv = as.getValue();
int dvint = dv.toInt();
}
}
}
}
The next piece of code does the same thing, only this time accessing the EZIO directly.
import edu.dstc.ambient.EZIO; import java.io.IOException;
public class AnalogInFromEZIO{ public static void main(String [] args){ EZIO e;
e = new EZIO("COM1");
while(true){
try {
int value = e.a2d(1);
} catch (IOException e1) {
System.err.println("Unable to read from line.");
e1.printStackTrace();
}
}
}
}
Currently the only way to access digital inputs lines is directly from
the EZIO class. The following is an example of accessing a digital
input. In order for this
example to work you will need to connect the EZIO board to COM port 1
(serial) on your machine and connect a digital sensor (eg. a pressure
mat) to Line 1 of the
DIGITAL IN.
import edu.dstc.ambient.EZIO; import java.io.IOException;
public class DigitalIN { public static void main(String[] args) { EZIO e = new EZIO("COM1"); while(true){ try {
e.readLine(1);
} catch (IOException e1) {
System.err.println("Unable to read line.");
e1.printStackTrace();
}
}
}
}
As with analogue input there are two ways you can do this, through the
DigitalOutput class or directly from the EZIO class. Keeping your
digital sensor connected from the
previous example, connect a red LED to Line 1 of the DIGITAL OUT and a
green LED to Line 2. The code that follows will turn on the red LED
when the sensor
is deactivated and the green when activated. This code can be easily
adapted to respond to analogue input.
import edu.dstc.ambient.EZIO;
import edu.dstc.ambient.DigitalOutput;
import java.io.IOException;
public class DigitalOut {
int SENSOR_ON = 0;
int SENSOR_OFF = 1;
EZIO e;
DigitalOutput RED_LED;
DigitalOutput GREEN_LED;
public DigitalOut(){
e = new EZIO("COM1");
RED_LED = new DigitalOutput(e, 1);
GREEN_LED = new DigitalOutput(e, 2);
RED_LED.turnOff();
GREEN_LED.turnOff();
}
public static void main(String[] args) {
DigitalOut dop = new DigitalOut();
dop.run();
}
private void run() {
while(true){
try {
int valueOfSensor = e.readLine(1);
if(valueOfSensor == SENSOR_ON){
RED_LED.turnOff();
GREEN_LED.turnOn();
}else if(valueOfSensor == SENSOR_OFF){
RED_LED.turnOn();
GREEN_LED.turnOff();
}else{
System.err.println("Sensor returning invalid value.");
}
delay(1000);
} catch (IOException e1) {
System.err.println("Error reading EZIO.");
e1.printStackTrace();
}
}
}
private void delay(int sleep) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e1) {
System.err.println("Unable to sleep thread.");
e1.printStackTrace();
}
}
}
The following piece of code accesses the EZIO class directly.
import edu.dstc.ambient.EZIO; import java.io.IOException;
public class DigitalOutFromEZIO { int RED = 1; int GREEN = 2;
int LED_ON = 1; int LED_OFF = 0;
int SENSOR_ON = 0; int SENSOR_OFF = 1;
EZIO e;
public static void main(String[] args) { DigitalOutFromEZIO dofe = new DigitalOutFromEZIO(); dofe.run(); }
public DigitalOutFromEZIO(){ e = new EZIO("COM1"); try {
e.writeLine(RED, LED_OFF);
e.writeLine(GREEN, LED_OFF);
} catch (IOException e1) {
System.err.println("Error writing to EZIO");
e1.printStackTrace();
}
}
private void run() {
while(true){
try {
int valueOfSensor = e.readLine(1);
if(valueOfSensor == SENSOR_ON){
e.writeLine(RED, LED_OFF);
e.writeLine(GREEN, LED_ON);
}else if(valueOfSensor == SENSOR_OFF){
e.writeLine(RED, LED_ON);
e.writeLine(GREEN, LED_OFF);
}else{
System.err.println("Sensor returning invalid value.");
}
delay(1000);
} catch (IOException e1) {
System.err.println("Error accessing EZIO.");
e1.printStackTrace();
}
}
}
private void delay(int sleep) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e1) {
System.err.println("Unable to sleep thread.");
e1.printStackTrace();
}
}
}
|