Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
added ev3dev-lang. motor class implemented only to get address, only...
Browse files Browse the repository at this point in the history
  • Loading branch information
mob41 committed Mar 30, 2016
1 parent 3ce7ac1 commit fa83c21
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "ev3dev-lang"]
path = ev3dev-lang
url = https://github.com/ev3dev/ev3dev-lang.git
1 change: 1 addition & 0 deletions ev3dev-lang
Submodule ev3dev-lang added at e5e1b7
49 changes: 46 additions & 3 deletions src/main/java/ev3dev/EV3.java
Expand Up @@ -14,10 +14,10 @@ public class EV3 {
public static final String SYSTEM_CLASS_PATH = "/sys/class/";

/***
* Reads the variable of the class specified.
* Reads the property of the class specified.
* @param class_name The class name
* @param variable
* @return The variable of the class.
* @param variable The property name of the class.
* @return The value of the property
* @throws FileNotFoundException If the specified class isn't exist.
* @throws IOException If the API couldn't read the class's variable
* @throws AccessControlException If you are trying to write to a read-only variable, read from a write-only variable
Expand Down Expand Up @@ -50,6 +50,43 @@ public static String read(String class_name, String variable) throws FileNotFoun
return sb.toString();
}

/***
* Reads the property of the class & subclass specified.
* @param class_name The class name.
* @param subclass The Sub-class name.
* @param variable The property name of the class
* @return The value of the property
* @throws FileNotFoundException If the specified class isn't exist.
* @throws IOException If the API couldn't read the class's variable
* @throws AccessControlException If you are trying to write to a read-only variable, read from a write-only variable
*/
public static String read(String class_name, String subclass, String variable) throws FileNotFoundException, IOException, AccessControlException{
return read(class_name, subclass + "/" + variable);
}

/***
* Writes the property of the class & subclass specified.
* @param class_name The class name.
* @param subclass The Sub-class name.
* @param variable The property name of the class
* @param new_value The new value of the property
* @throws FileNotFoundException If the specified class isn't exist.
* @throws IOException If the API couldn't read the class's variable
* @throws AccessControlException If you are trying to write to a read-only variable, read from a write-only variable
*/
public static void write(String class_name, String subclass, String variable, String new_value) throws FileNotFoundException, AccessControlException{
write(class_name, subclass + "/" + variable, new_value);
}

/***
* Writes the property of the class specified.
* @param class_name The class name.
* @param variable The property name of the class
* @param new_value The new value of the property
* @throws FileNotFoundException If the specified class isn't exist.
* @throws IOException If the API couldn't read the class's variable
* @throws AccessControlException If you are trying to write to a read-only variable, read from a write-only variable
*/
public static void write(String class_name, String variable, String new_value) throws FileNotFoundException, AccessControlException{
PrintWriter out = new PrintWriter(SYSTEM_CLASS_PATH + class_name + "/" + variable);
class_name = class_name.toLowerCase();
Expand All @@ -59,4 +96,10 @@ public static void write(String class_name, String variable, String new_value) t
out.flush();
out.close();
}

public static int getNumbersOfSubClass(String class_name){
File file = new File(SYSTEM_CLASS_PATH + class_name);
File[] files = file.listFiles();
return files == null ? -1 : files.length;
}
}
21 changes: 21 additions & 0 deletions src/main/java/ev3dev/TestStart.java
@@ -0,0 +1,21 @@
package ev3dev;

import ev3dev.exception.InvalidPortException;
import ev3dev.hardware.LegoPort;

public class TestStart {

public static void main(String[] args) throws InvalidPortException {
LegoPort port;
for (int i = 0; i <= 7; i++){
port = new LegoPort(i);
System.out.println(
"\nAddress: " + port.getAddress() +
"\nDriver Nm: " + port.getDriverName() +
"\nMode: " + port.getMode() +
"\nStatus: " + port.getStatus()
);
}
}

}
32 changes: 32 additions & 0 deletions src/main/java/ev3dev/exception/InvalidPortException.java
@@ -0,0 +1,32 @@
package ev3dev.exception;

/***
* This exception is thrown if:<br>
* <b>LegoPort</b> port is lower than 0 or higher than 7<br>
* <b>TachoMotor</b> port number does not found.
* @author Anthony
*
*/
public class InvalidPortException extends Exception {

/**
*
*/
private static final long serialVersionUID = 1L;

public InvalidPortException(){
super();
}

public InvalidPortException(String message){
super(message);
}

public InvalidPortException(String message, Throwable cause){
super(message, cause);
}

public InvalidPortException(Throwable cause){
super(cause);
}
}
13 changes: 12 additions & 1 deletion src/main/java/ev3dev/hardware/Device.java
@@ -1,8 +1,19 @@
package ev3dev.hardware;

public abstract class Device {

private LegoPort port;

public Device(){
//TODO the available devices should be enumerated until a suitable device is found
}

public Device(LegoPort port){

this.port = port;
}

public boolean isConnected(){
//TODO I don't understand around the Device class... :(
return true;
}
}
86 changes: 75 additions & 11 deletions src/main/java/ev3dev/hardware/LegoPort.java
@@ -1,36 +1,100 @@
package ev3dev.hardware;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.AccessControlException;

import ev3dev.EV3;
import ev3dev.exception.InvalidPortException;

public class LegoPort {

private static String SYSTEM_CLASS_PATH = "/sys/class/lego-port";

private int port = 0;

private String address;
public static final int PORT_1 = 0;

public static final int PORT_2 = 1;

public static final int PORT_3 = 2;

private String driver_name;
public static final int PORT_4 = 3;

private String[] modes;
public static final int PORT_A = 4;

private String mode;
public static final int PORT_B = 5;

private String set_device;
public static final int PORT_C = 6;

private String status;
public static final int PORT_D = 7;

public LegoPort(int port){
public LegoPort(int port) throws InvalidPortException{
if (port < 0){
throw new InvalidPortException("Port is lower than 0, Port: " + port);
} else if (port > 7){
throw new InvalidPortException("Port is higher than 7, Port: " + port);
}
this.port = port;
}

public final String getAddress(){
public String getAddress(){
String address = null;
try { //TODO I shouldn't do like this. Do Error handling.
address = EV3.read("lego-port", "address");
address = EV3.read("lego-port", "port" + port, "address");
} catch (Exception e){
e.printStackTrace();
}
return address;
}

public String getDriverName(){
String drivername = null;
try {
drivername = EV3.read("lego-port", "port" + port, "driver_name");
} catch (Exception e){
e.printStackTrace();
}
return drivername;
}

public String[] getModes(){
String[] modes = null;
//TODO This
return modes;
}

public String getMode(){
String mode = null;
try {
mode = EV3.read("lego-port", "port" + port, "mode");
} catch (Exception e){
e.printStackTrace();
}
return mode;
}

public void setMode(String mode){
try {
EV3.write("lego-port", "port" + port, "mode", mode);
} catch (Exception e){
e.printStackTrace();
}
}

public void setDevice(String driver){
try {
EV3.write("lego-port", "port" + port, "set_device", driver);
} catch (Exception e){
e.printStackTrace();
}
}

public String getStatus(){
String status = null;
try {
status = EV3.read("lego-port", "port" + port, "status");
} catch (Exception e){
e.printStackTrace();
}
return status;
}
}
57 changes: 57 additions & 0 deletions src/main/java/ev3dev/hardware/Motor.java
@@ -0,0 +1,57 @@
package ev3dev.hardware;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.AccessControlException;

import ev3dev.EV3;
import ev3dev.exception.InvalidPortException;

public class Motor {

private static final String SYSTEM_CLASS_NAME = "tacho-motor";
private LegoPort port;
private String address;

/***
* Creates a new motor object.
* @param port Specify a LegoPort
* @throws InvalidPortException If the LegoPort isn't a OUTPUT, invalid or a tacho-motor.
*/
public Motor(LegoPort port) throws InvalidPortException{
this.port = port;
address = port.getAddress();

//Verify is the LegoPort connecting a motor / is a output
if (!address.contains("out")){
throw new InvalidPortException("The specified port (" + port.getAddress() + ") isn't a output.");
} else if (port.getStatus() != "tacho-motor"){
throw new InvalidPortException("The specified port (" + port.getAddress() + ") isn't a motor (" + port.getStatus() + ")");
}

}

/***
* Get the address of this motor.
* @return LegoPort address described in String
* @throws IOException If the motor doesn't exist or IO ERROR
*/
public String getAddress() throws IOException{
return EV3.read(SYSTEM_CLASS_NAME, "tacho-motor", "");
}

private static int getMotorNumber(String address){
int motors = EV3.getNumbersOfSubClass("motor");
if (motors == -1){
return -1;
}
for (int i = 0; i < motors; i++){
try {
if (EV3.read("tacho-motor", "motor" + i, "address").equals(address)){
return i;
}
} catch (IOException ignore){}
}
return -1;
}
}

0 comments on commit fa83c21

Please sign in to comment.