First attempt
This commit is contained in:
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
### IntelliJ IDEA ###
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
8
.idea/artifacts/JavaInRaspi_jar.xml
generated
Normal file
8
.idea/artifacts/JavaInRaspi_jar.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<component name="ArtifactManager">
|
||||||
|
<artifact type="jar" build-on-make="true" name="JavaInRaspi:jar">
|
||||||
|
<output-path>$PROJECT_DIR$/out/artifacts/JavaInRaspi_jar</output-path>
|
||||||
|
<root id="archive" name="JavaInRaspi.jar">
|
||||||
|
<element id="module-output" name="JavaInRaspi" />
|
||||||
|
</root>
|
||||||
|
</artifact>
|
||||||
|
</component>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="liberica-21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/JavaInRaspi.iml" filepath="$PROJECT_DIR$/JavaInRaspi.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
JavaInRaspi.iml
Normal file
11
JavaInRaspi.iml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
81
src/DigitalInput.java
Normal file
81
src/DigitalInput.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class DigitalInput {
|
||||||
|
private int pinnya=0;
|
||||||
|
private boolean currentIsON = false;
|
||||||
|
private boolean initialized = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buat di JAVA coding
|
||||||
|
* @param pin_number : pin
|
||||||
|
*/
|
||||||
|
public DigitalInput(int pin_number) {
|
||||||
|
// if (jGPIO.osname=="") jGPIO.detectOS();
|
||||||
|
pinnya = pin_number;
|
||||||
|
if (Setup_Pin()) initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean Setup_Pin() {
|
||||||
|
if (!GeneralCode.GpioIsExported(pinnya)) {
|
||||||
|
// belum ada, coba export dulu
|
||||||
|
if (!GeneralCode.ExportGpio(pinnya)) {
|
||||||
|
// gagal export
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sampe sini, harusnya ada
|
||||||
|
// harus set output dulu
|
||||||
|
if (GeneralCode.SetGpioDirection(pinnya, true)) {
|
||||||
|
// pasang value 1
|
||||||
|
if (GeneralCode.SetGpioValue(pinnya, true)) {
|
||||||
|
// ubah jadi input
|
||||||
|
if (GeneralCode.SetGpioDirection(pinnya, false)) {
|
||||||
|
System.out.println("DigitalInput Setup_Pin success !!");
|
||||||
|
return true;
|
||||||
|
} else System.out.println("Gagal SetDirection ke Input untuk DigitalInput pin "+pinnya);
|
||||||
|
} else System.out.println("Gagal SetValue ke high untuk DigitalInput pin "+pinnya);
|
||||||
|
} else System.out.println("Gagal SetDirection ke Output untuk DigitalInput pin "+pinnya);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean getIsInitialized(){
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read State of GPIO
|
||||||
|
* @return 0 = Low, 1 = high, -1 = failed reading
|
||||||
|
*/
|
||||||
|
public int ReadState() {
|
||||||
|
int value = GeneralCode.GetGpioValue(pinnya);
|
||||||
|
if (value==0) {
|
||||||
|
currentIsON = false;
|
||||||
|
} else if (value==1) {
|
||||||
|
currentIsON = true;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get last ReadState value
|
||||||
|
* return true if High 1
|
||||||
|
* return false if Low 0
|
||||||
|
*/
|
||||||
|
public boolean getIsON(){
|
||||||
|
return currentIsON;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Release control of Digital Output (not controlling anymore)
|
||||||
|
* return true if success operation
|
||||||
|
*/
|
||||||
|
public void Release() {
|
||||||
|
GeneralCode.UnExportGpio(pinnya);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
35
src/DigitalOutput.java
Normal file
35
src/DigitalOutput.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
public class DigitalOutput {
|
||||||
|
int pin;
|
||||||
|
|
||||||
|
DigitalOutput(int pinOut) {
|
||||||
|
pin = pinOut;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean Setup_Pin() {
|
||||||
|
if (!GeneralCode.GpioIsExported(pin)) {
|
||||||
|
// belum ada, coba export dulu
|
||||||
|
if (!GeneralCode.ExportGpio(pin)) {
|
||||||
|
// gagal export
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sampe sini, harusnya ada
|
||||||
|
// harus set output dulu
|
||||||
|
if (GeneralCode.SetGpioDirection(pin, true)) {
|
||||||
|
// pasang value 1
|
||||||
|
if (GeneralCode.SetGpioValue(pin, true)) {
|
||||||
|
// ubah jadi input
|
||||||
|
if (GeneralCode.SetGpioDirection(pin, false)) {
|
||||||
|
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 SetDirection ke Output untuk DigitalInput pin "+pin);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
104
src/GeneralCode.java
Normal file
104
src/GeneralCode.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class GeneralCode {
|
||||||
|
private static final String GPIO_PATH = "/sys/class/gpio/";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
check is the OS is Linux or not
|
||||||
|
*/
|
||||||
|
public static boolean OsIsLinux(){
|
||||||
|
boolean result = false;
|
||||||
|
String OsResult = System.getProperty("os.name").toLowerCase();
|
||||||
|
if (OsResult.contains("linux")) {
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checking if pin is already exported in /sys/class/gpio
|
||||||
|
* @param Pin --> pin that you want to check
|
||||||
|
* @return true if pin exported
|
||||||
|
*/
|
||||||
|
public static boolean GpioIsExported(int Pin){
|
||||||
|
String fileName = "/sys/class/gpio/gpio"+Pin;
|
||||||
|
File file = new File(fileName);
|
||||||
|
return file.exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export file, is a must before set In or Out.
|
||||||
|
* @param Pin that you want to set
|
||||||
|
* @return true if success
|
||||||
|
*/
|
||||||
|
public static boolean ExportGpio(int Pin){
|
||||||
|
return writeToFile(GPIO_PATH + "export", String.valueOf(Pin));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean UnExportGpio(int Pin){
|
||||||
|
return writeToFile(GPIO_PATH+"unexport",String.valueOf(Pin));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param Pin pin that you want to set
|
||||||
|
* @param direction true if In, false if Out
|
||||||
|
* @return True if success
|
||||||
|
*/
|
||||||
|
public static boolean SetGpioDirection(int Pin, boolean direction){
|
||||||
|
String val;
|
||||||
|
if(direction){
|
||||||
|
val = "out";
|
||||||
|
}else {val="in";}
|
||||||
|
return writeToFile(GPIO_PATH+"gpio"+Pin+"/direction",val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set value to file,
|
||||||
|
* @param Pin pin that you want to set value
|
||||||
|
* @param Value if true-> HIGH false-> LOW
|
||||||
|
* @return true if success
|
||||||
|
*/
|
||||||
|
public static boolean SetGpioValue(int Pin,boolean Value){
|
||||||
|
String valNum = "";
|
||||||
|
if (Value){
|
||||||
|
valNum="1";
|
||||||
|
}else {valNum="0";}
|
||||||
|
return writeToFile(GPIO_PATH + "gpio" + Pin + "/value", valNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value from file
|
||||||
|
* @param Pin that you want to get value
|
||||||
|
* @return 1/0
|
||||||
|
*/
|
||||||
|
public static int GetGpioValue(int Pin){
|
||||||
|
String filePath = GPIO_PATH+"gpio"+Pin+"/value";
|
||||||
|
return Integer.parseInt(Objects.requireNonNull(readFromFile(filePath)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to write to a file
|
||||||
|
private static boolean writeToFile(String filePath, String value) {
|
||||||
|
System.out.println("writing value : "+filePath+" " + value);
|
||||||
|
boolean result = false;
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||||
|
writer.write(value);
|
||||||
|
result = true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error writing to " + filePath + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to read from a file (if needed)
|
||||||
|
private static String readFromFile(String filePath) {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
return reader.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error reading from " + filePath + ": " + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/META-INF/MANIFEST.MF
Normal file
3
src/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: Main
|
||||||
|
|
||||||
25
src/Main.java
Normal file
25
src/Main.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
DigitalInput talkButton = new DigitalInput(12);
|
||||||
|
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");
|
||||||
|
|
||||||
|
Timer testTimer = new Timer();
|
||||||
|
testTimer.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println(talkButton.ReadState());
|
||||||
|
}
|
||||||
|
},0,1000);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user