Sudah bisa DigitalOutput, DigitalInput. Juga sudah menambahkan class NanoPi.
This commit is contained in:
35
src/Device/NanoPi.java
Normal file
35
src/Device/NanoPi.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package Device;
|
||||||
|
|
||||||
|
public class NanoPi {
|
||||||
|
public int GetGPIO(int pin){
|
||||||
|
int GPIONum;
|
||||||
|
if (pin == 9){
|
||||||
|
GPIONum = 363;
|
||||||
|
} else if (pin == 11) {
|
||||||
|
GPIONum = 203;
|
||||||
|
} else if (pin==2) {
|
||||||
|
GPIONum=5;
|
||||||
|
} else if (pin==4) {
|
||||||
|
GPIONum=4;
|
||||||
|
} else if (pin == 8) {
|
||||||
|
GPIONum=11;
|
||||||
|
} else if (pin==10) {
|
||||||
|
GPIONum=12;
|
||||||
|
} else if (pin==12) {
|
||||||
|
GPIONum=13;
|
||||||
|
}else if (pin==14) {
|
||||||
|
GPIONum=14;
|
||||||
|
}else if (pin==16) {
|
||||||
|
GPIONum=16;
|
||||||
|
}else if (pin==18) {
|
||||||
|
GPIONum=15;
|
||||||
|
}else if (pin==20) {
|
||||||
|
GPIONum=199;
|
||||||
|
}else if (pin==22) {
|
||||||
|
GPIONum=198;
|
||||||
|
}else {
|
||||||
|
GPIONum=-1;
|
||||||
|
}
|
||||||
|
return GPIONum;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class DigitalInput {
|
public class DigitalInput {
|
||||||
private int pinnya=0;
|
private final int pinnya;
|
||||||
private boolean currentIsON = false;
|
private boolean currentIsON = false;
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
|
|
||||||
@@ -12,7 +10,10 @@ public class DigitalInput {
|
|||||||
public DigitalInput(int pin_number) {
|
public DigitalInput(int pin_number) {
|
||||||
// if (jGPIO.osname=="") jGPIO.detectOS();
|
// if (jGPIO.osname=="") jGPIO.detectOS();
|
||||||
pinnya = pin_number;
|
pinnya = pin_number;
|
||||||
if (Setup_Pin()) initialized = true;
|
if (Setup_Pin()) {
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(this::Release));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean Setup_Pin() {
|
private boolean Setup_Pin() {
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
public class DigitalOutput {
|
public class DigitalOutput {
|
||||||
int pin;
|
int pin;
|
||||||
|
boolean initState;
|
||||||
DigitalOutput(int pinOut) {
|
boolean initialized;
|
||||||
|
DigitalOutput(int pinOut,boolean initialiaState) {
|
||||||
pin = pinOut;
|
pin = pinOut;
|
||||||
|
initState = initialiaState;
|
||||||
|
initialized = Setup_Pin();
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(this::Release));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean Setup_Pin() {
|
private boolean Setup_Pin() {
|
||||||
@@ -16,22 +19,38 @@ public class DigitalOutput {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// set direction first
|
||||||
// sampe sini, harusnya ada
|
|
||||||
// harus set output dulu
|
|
||||||
if (GeneralCode.SetGpioDirection(pin, true)) {
|
if (GeneralCode.SetGpioDirection(pin, true)) {
|
||||||
// pasang value 1
|
// pasang value 1
|
||||||
if (GeneralCode.SetGpioValue(pin, true)) {
|
if (GeneralCode.SetGpioValue(pin, initState)) {
|
||||||
// ubah jadi input
|
System.out.println("DigitalOutput Setup_Pin success !!");
|
||||||
if (GeneralCode.SetGpioDirection(pin, false)) {
|
return true;
|
||||||
System.out.println("DigitalInput Setup_Pin success !!");
|
|
||||||
return true;
|
|
||||||
} else System.out.println("Gagal SetDirection ke Input untuk DigitalInput pin "+pin);
|
|
||||||
} else System.out.println("Gagal SetValue ke high untuk DigitalInput pin "+pin);
|
} else System.out.println("Gagal SetValue ke high untuk DigitalInput pin "+pin);
|
||||||
} else System.out.println("Gagal SetDirection ke Output untuk DigitalInput pin "+pin);
|
} else System.out.println("Gagal SetDirection ke Output untuk DigitalInput pin "+pin);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetHigh(){
|
||||||
|
if (!initialized) {
|
||||||
|
System.out.println("Pin is not initialized");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GeneralCode.SetGpioValue(pin,true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetLow(){
|
||||||
|
if (!initialized){
|
||||||
|
System.out.println("Pin is not initialized");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GeneralCode.SetGpioValue(pin,false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Release control of Digital Output (not controlling anymore)
|
||||||
|
* return true if success operation
|
||||||
|
*/
|
||||||
|
public void Release() {
|
||||||
|
GeneralCode.UnExportGpio(pin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import java.util.Objects;
|
|||||||
public class GeneralCode {
|
public class GeneralCode {
|
||||||
private static final String GPIO_PATH = "/sys/class/gpio/";
|
private static final String GPIO_PATH = "/sys/class/gpio/";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
check is the OS is Linux or not
|
check is the OS is Linux or not
|
||||||
*/
|
*/
|
||||||
@@ -44,7 +43,7 @@ public class GeneralCode {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param Pin pin that you want to set
|
* @param Pin pin that you want to set
|
||||||
* @param direction true if In, false if Out
|
* @param direction true if out, false if in
|
||||||
* @return True if success
|
* @return True if success
|
||||||
*/
|
*/
|
||||||
public static boolean SetGpioDirection(int Pin, boolean direction){
|
public static boolean SetGpioDirection(int Pin, boolean direction){
|
||||||
@@ -62,7 +61,7 @@ public class GeneralCode {
|
|||||||
* @return true if success
|
* @return true if success
|
||||||
*/
|
*/
|
||||||
public static boolean SetGpioValue(int Pin,boolean Value){
|
public static boolean SetGpioValue(int Pin,boolean Value){
|
||||||
String valNum = "";
|
String valNum;
|
||||||
if (Value){
|
if (Value){
|
||||||
valNum="1";
|
valNum="1";
|
||||||
}else {valNum="0";}
|
}else {valNum="0";}
|
||||||
|
|||||||
@@ -1,22 +1,25 @@
|
|||||||
|
import Device.NanoPi;
|
||||||
|
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
DigitalInput talkButton = new DigitalInput(12);
|
var npi = new NanoPi();
|
||||||
|
DigitalInput talkButton = new DigitalInput(npi.GetGPIO(12));
|
||||||
|
DigitalOutput LED1 = new DigitalOutput(npi.GetGPIO(20),true);
|
||||||
System.out.println("is initialized :"+ talkButton.getIsInitialized());
|
System.out.println("is initialized :"+ talkButton.getIsInitialized());
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(()->{
|
|
||||||
talkButton.Release();
|
|
||||||
System.out.println("program is closed!");
|
|
||||||
}));
|
|
||||||
System.out.println("Test Program GPIO di linux");
|
System.out.println("Test Program GPIO di linux");
|
||||||
|
|
||||||
Timer testTimer = new Timer();
|
Timer MainTimer = new Timer();
|
||||||
testTimer.schedule(new TimerTask() {
|
MainTimer.schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println(talkButton.ReadState());
|
if(talkButton.ReadState() == 1 ){
|
||||||
|
LED1.SetHigh();
|
||||||
|
}else{
|
||||||
|
LED1.SetLow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},0,1000);
|
},0,1000);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user