GPIO control.
Bug fix.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package SBC;
|
||||
|
||||
import com.sun.jna.Platform;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.tinylog.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
@@ -8,7 +9,7 @@ import java.nio.file.Path;
|
||||
|
||||
public class GPIO {
|
||||
|
||||
private static final Path gpioPath = Path.of("/sys/class/gpio");
|
||||
public static final Path gpioPath = Path.of("/sys/class/gpio");
|
||||
private static final Path gpioExportPath = Path.of("/sys/class/gpio/export");
|
||||
private static final Path gpioUnexportPath = Path.of("/sys/class/gpio/unexport");
|
||||
|
||||
@@ -31,8 +32,8 @@ public class GPIO {
|
||||
* @param pin GPIO pin number
|
||||
* @return true if the pin is already exported
|
||||
*/
|
||||
public static boolean GpioPinExists(int pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin);
|
||||
public static boolean GpioPinExists(@NotNull RaspberryPi5BPins pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin.gpionumber);
|
||||
return pinPath.toFile().isDirectory();
|
||||
}
|
||||
|
||||
@@ -41,15 +42,15 @@ public class GPIO {
|
||||
* @param pin GPIO pin number
|
||||
* @return true if the pin is successfully exported
|
||||
*/
|
||||
public static boolean ExportPin(int pin){
|
||||
public static boolean ExportPin(@NotNull RaspberryPi5BPins pin){
|
||||
try{
|
||||
if (Files.isWritable(gpioExportPath)){
|
||||
Files.write(gpioExportPath, String.valueOf(pin).getBytes());
|
||||
Logger.info("Pin "+pin+" exported");
|
||||
Files.write(gpioExportPath, String.valueOf(pin.gpionumber).getBytes());
|
||||
Logger.info("Pin "+pin.pin+" exported");
|
||||
return GpioPinExists(pin);
|
||||
} else Logger.error("GPIO export path is not writable");
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to export pin: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to export pin: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -59,14 +60,14 @@ public class GPIO {
|
||||
* @param pin GPIO pin number
|
||||
* @return true if the pin is successfully unexported
|
||||
*/
|
||||
public static boolean UnexportPin(int pin){
|
||||
public static boolean UnexportPin(@NotNull RaspberryPi5BPins pin){
|
||||
if (Files.isWritable(gpioUnexportPath)){
|
||||
try{
|
||||
Files.write(gpioUnexportPath, String.valueOf(pin).getBytes());
|
||||
Logger.info("Pin "+pin+" unexported");
|
||||
Files.write(gpioUnexportPath, String.valueOf(pin.gpionumber).getBytes());
|
||||
Logger.info("Pin "+pin.pin+" unexported");
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to unexport pin: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to unexport pin: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
} else Logger.error("GPIO unexport path is not writable");
|
||||
|
||||
@@ -78,17 +79,17 @@ public class GPIO {
|
||||
* @param pin GPIO pin number
|
||||
* @return "in" if the pin is input, "out" if the pin is output, "unknown" if the direction is unknown
|
||||
*/
|
||||
public static String GetPinDirection(int pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin).resolve("direction");
|
||||
public static String GetPinDirection(@NotNull RaspberryPi5BPins pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin.gpionumber).resolve("direction");
|
||||
if (pinPath.toFile().isFile()){
|
||||
if (Files.isReadable(pinPath)){
|
||||
try{
|
||||
return Files.readString(pinPath).trim();
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to read pin direction: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to read pin direction: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
} else Logger.error("Pin direction file is not readable: "+pin);
|
||||
} else Logger.error("Pin direction file not found: "+pin);
|
||||
} else Logger.error("Pin direction file is not readable: "+pin.pin);
|
||||
} else Logger.error("Pin direction file not found: "+pin.pin);
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
@@ -98,22 +99,22 @@ public class GPIO {
|
||||
* @param direction "in" for input, "out" for output
|
||||
* @return true if the direction is successfully set
|
||||
*/
|
||||
public static boolean SetPinDirection(int pin, String direction){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin).resolve("direction");
|
||||
public static boolean SetPinDirection(@NotNull RaspberryPi5BPins pin, String direction){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin.gpionumber).resolve("direction");
|
||||
if (pinPath.toFile().isFile()){
|
||||
if (Files.isWritable(pinPath)){
|
||||
direction = direction.trim().toLowerCase();
|
||||
if ("in".equals(direction) || "out".equals(direction)){
|
||||
try{
|
||||
Files.write(pinPath, direction.getBytes());
|
||||
Logger.info("Pin "+pin+" direction set to "+direction);
|
||||
Logger.info("Pin "+pin.pin+" direction set to "+direction);
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to set pin direction: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to set pin direction: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
} else Logger.error("Invalid direction: "+direction);
|
||||
} else Logger.error("Pin direction file is not writable: "+pin);
|
||||
} else Logger.error("Pin direction file not found: "+pin);
|
||||
} else Logger.error("Pin direction file is not writable: "+pin.pin);
|
||||
} else Logger.error("Pin direction file not found: "+pin.pin);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -123,19 +124,19 @@ public class GPIO {
|
||||
* @param isON true to set the pin value to 1, false to set the pin value to 0
|
||||
* @return true if the value is successfully set
|
||||
*/
|
||||
public static boolean SetValue(int pin, boolean isON){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin).resolve("value");
|
||||
public static boolean SetValue(@NotNull RaspberryPi5BPins pin, boolean isON){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin.gpionumber).resolve("value");
|
||||
if (pinPath.toFile().isFile()){
|
||||
if (Files.isWritable(pinPath)){
|
||||
try{
|
||||
Files.write(pinPath, isON?"1".getBytes():"0".getBytes());
|
||||
Logger.info("Pin "+pin+" value set to "+(isON?"1":"0"));
|
||||
Logger.info("Pin "+pin.pin+" value set to "+(isON?"1":"0"));
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to set pin value: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to set pin value: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
} else Logger.error("Pin value file is not writable: "+pin);
|
||||
} else Logger.error("Pin value file not found: "+pin);
|
||||
} else Logger.error("Pin value file is not writable: "+pin.pin);
|
||||
} else Logger.error("Pin value file not found: "+pin.pin);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -144,17 +145,17 @@ public class GPIO {
|
||||
* @param pin GPIO pin number
|
||||
* @return "1" if the pin value is 1, "0" if the pin value is 0, "unknown" if the value is unknown
|
||||
*/
|
||||
public static String GetValue(int pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin).resolve("value");
|
||||
public static String GetValue(@NotNull RaspberryPi5BPins pin){
|
||||
Path pinPath = gpioPath.resolve("gpio"+pin.gpionumber).resolve("value");
|
||||
if (pinPath.toFile().isFile()){
|
||||
if (Files.isReadable(pinPath)){
|
||||
try{
|
||||
return Files.readString(pinPath).trim();
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to read pin value: "+pin+", Message: "+e.getMessage());
|
||||
Logger.error("Failed to read pin value: "+pin.pin+", Message: "+e.getMessage());
|
||||
}
|
||||
} else Logger.error("Pin value file is not readable: "+pin);
|
||||
} else Logger.error("Pin value file not found: "+pin);
|
||||
} else Logger.error("Pin value file is not readable: "+pin.pin);
|
||||
} else Logger.error("Pin value file not found: "+pin.pin);
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
42
src/main/java/SBC/RaspberryPi5BPins.java
Normal file
42
src/main/java/SBC/RaspberryPi5BPins.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package SBC;
|
||||
|
||||
public enum RaspberryPi5BPins {
|
||||
Pin03(3,"GPIO2/SDA", 573),
|
||||
Pin05(5,"GPIO3/SCL", 574),
|
||||
Pin07(7,"GPIO4/GPCLK0", 575),
|
||||
Pin08(8,"GPIO14/TXD", 585),
|
||||
Pin10(10,"GPIO15/RXD", 586),
|
||||
Pin11(11,"GPIO17", 588),
|
||||
Pin12(12,"GPIO18/PCMCLK", 589),
|
||||
Pin13(13,"GPIO27", 598),
|
||||
Pin15(15,"GPIO22", 593),
|
||||
Pin16(16,"GPIO23", 594),
|
||||
Pin18(18,"GPIO24", 595),
|
||||
Pin19(19,"GPIO10/MOSI", 581),
|
||||
Pin21(21,"GPIO9/MISO", 580),
|
||||
Pin22(22,"GPIO25", 596),
|
||||
Pin23(23,"GPIO11/SCLK", 582),
|
||||
Pin24(24,"GPIO8/CE0", 579),
|
||||
Pin26(26,"GPIO7/CE1", 578),
|
||||
Pin27(27,"GPIO0/IDSD", 587),
|
||||
Pin28(28,"GPIO1/IDSC", 587),
|
||||
Pin29(29,"GPIO5", 576),
|
||||
Pin31(31,"GPIO6", 577),
|
||||
Pin32(32,"GPIO12/PWM0", 583),
|
||||
Pin33(33,"GPIO13/PWM1", 584),
|
||||
Pin35(35,"GPIO19/PCMFS", 590),
|
||||
Pin36(36,"GPIO16", 587),
|
||||
Pin37(37,"GPIO26", 597),
|
||||
Pin38(38,"GPIO20/PCMDIN", 591),
|
||||
Pin40(40,"GPIO21/PCMDOUT", 592);
|
||||
|
||||
public final int pin;
|
||||
public final String name;
|
||||
public final int gpionumber;
|
||||
|
||||
RaspberryPi5BPins(int pin, String name, int gpionumber){
|
||||
this.pin = pin;
|
||||
this.name = name;
|
||||
this.gpionumber = gpionumber;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user