-
Notifications
You must be signed in to change notification settings - Fork 0
LegoPort
Before you start with any of the devices (e.g. Motor, Sensor), you have to create yourself a LegoPort instance, pointing to your connected port.
The connected port can be specified as a parameter using these Java Fields:
LegoPort.INPUT_1 - Sensor Port 1
LegoPort.INPUT_2 - Sensor Port 2
LegoPort.INPUT_3 - Sensor Port 3
LegoPort.INPUT_4 - Sensor Port 4
LegoPort.OUTPUT_A - Motor Port A
LegoPort.OUTPUT_B - Motor Port B
LegoPort.OUTPUT_C - Motor Port C
LegoPort.OUTPUT_D - Motor Port D
To do this, we can use the following code:
import ev3dev.hardware.LegoPort
public static void main(String[] args){
try {
LegoPort port = new LegoPort(LegoPort.OUTPUT_A);
catch (InvalidPortException e){
e.printStackTrace();
}
}Yeah! We just created a LegoPort pointing to OUTPUT_A.
Other then, we can get or set different properties from this port!
import ev3dev.hardware.LegoPort;
import java.util.Arrays;
public static void main(String[] args){
try {
LegoPort port = new LegoPort(LegoPort.OUTPUT_A);
System.out.println("Address: " + port.getAddress());
System.out.println("Driver: " + port.getDriverName());
System.out.println("Modes: " + Arrays.deepToString(port.getModes()));
System.out.println("Current Mode: " + port.getMode());
System.out.println("Status: " + port.getStatus());
//Set Mode (Depends on the function, port.getModes())
//port.setMode("");
//Set device, writing the name of a driver
//port.setDevice("");
catch (InvalidPortException e){
e.printStackTrace();
}
}##FAQ
Q: Why a
InvalidPortExceptionis thrown during the creation of the instance?
You have to specific a port that is a Integer Field or a number between 0 and 7.
Q: Why the application throws
IOExceptionwherever I get the addresses or other properties?
There are no connected device or port. So the API couldn't read the properties of the instance.