Explorar el Código

Update of Application

dev-alex
Alex Volkmann hace 5 años
padre
commit
0ab01b19e6
Se han modificado 1 ficheros con 149 adiciones y 5 borrados
  1. +149
    -5
      src/pdu_jctrl/gpio_java.java

+ 149
- 5
src/pdu_jctrl/gpio_java.java Ver fichero

@@ -1,15 +1,38 @@
package pdu_jctrl;
//package pdu_jctrl;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public class gpio_java
{
public static String rel_map [][] = {
{"rel1","14"},
{"rel2","15"},
{"rel3","18"},
{"rel4","23"},
{"rel5","24"},
{"rel6","25"},
{"rel7","8"},
{"rel8","7"}
};
public static String [] action = {"on", "off", "status"};

public static void main (String [] args)
{
System.out.println("Hello World");
// Zum checken der Anzahl der Argumente
checkargs(args);
// todo: initialisiere alle acht ports (outputs)
init_gpio();
// setze gpio (gemaes der parameter)
gpio_getport(args[0]);
gpio_cmdhandler(args[0], args[1]);
/*
setgpio(true);
try {
@@ -18,16 +41,134 @@ public class gpio_java
catch(InterruptedException e)
{
System.out.println("sleep error InterruptedException ocure");
}
}
setgpio(false);
*/
}
public static void checkargs(String[] argumens)
{
int laenge = argumens.length;
String [] h = {"-h"};
int idx;
boolean parameter1 = false;
boolean parameter2 = false;

if (laenge == 2)
{
//ToDO: Pruefe erstes und zweites Argument
//Erstes Element(rel)
for (idx = 0; idx<8 ; idx++)
{
if (argumens[0].equals(rel_map[idx][0]))
{
parameter1 = true;
break;
}
}
//Zweites Element (action)
for (idx = 0; idx<3 ; idx++)
{
if (argumens[1].equals(action[idx]))
{
parameter2 = true;
break;
}
}
}
else if (Arrays.equals (argumens, h) == true)
{
show_usage();
System.exit(0);
}
else
{
System.out.println("Error. If you need help, type the command -h");
System.exit(0);
}
//ToDo: Return

}
public static void init_gpio()
{
int idx;
String port;
String cmd;
for(idx = 0; idx<8 ; idx++)
{
port = rel_map[idx][1];
cmd = "/usr/bin/gpio -g mode " + port + " out";
launchcmd(cmd);
}
return;
}
public static int gpio_getport(String port)
{
int idx;
for(idx = 0; idx<8 ; idx++)
{
if(port.equals(rel_map[idx][0]))
{
return idx;
}
}
return -1;
}
public static int gpio_cmdhandler(String port, String cmd)
{
int idx_port;
idx_port = gpio_getport(port);

if (cmd.equals(action [0]))
{
//Schalte on
cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 1";
launchcmd(cmd);
}
if (cmd.equals(action [1]))
{
//Schalte off
cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 0";
launchcmd(cmd);
}
/*if (cmd.equals(action [2]))
{
//Stats geben
cmd = "/usr/bin/gpio -g write " + port + status();
launchcmd(cmd);
}*/
return -1;
}
public static void show_usage()
{
System.out.println("You have to enter exactly 2 arguments.");
System.out.println("Example: java gpio_java Argument1 Argument2");
System.out.println("Argument1: rel1, rel2, rel3, ..., rel8");
System.out.println("Argument2: on,off or status");
}
public static void launchcmd(String cmd)
{
// Wenn Simulation True ist kann sie auf dem PC ausfuehren
// wenn die Simulation false is auf dem Raspberry PI
boolean simulation = true;
boolean simulation = false;
if(simulation==true){
System.out.println(cmd);
@@ -57,4 +198,7 @@ public class gpio_java
launchcmd("/usr/bin/gpio -g write 14 0");
}
}
}
}

Cargando…
Cancelar
Guardar