Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.nio.file.*;
- public class GpioNioTest
- {
- static String GpioChannel = "18";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- /*** Init GPIO port for output ***/
- // Open file handles to GPIO port unexport and export controls
- FileWriter unexportFile = new FileWriter("/sys/class/gpio/unexport");
- FileWriter exportFile = new FileWriter("/sys/class/gpio/export");
- // Reset the port
- File exportFileCheck = new File("/sys/class/gpio/gpio"+ GpioChannel);
- if (exportFileCheck.exists()) {
- unexportFile.write(GpioChannel);
- unexportFile.flush();
- }
- // Set the port for use
- exportFile.write(GpioChannel);
- exportFile.flush();
- // Open file handle to port input/output control
- FileWriter directionFile = new FileWriter("/sys/class/gpio/gpio" + GpioChannel + "/direction");
- // Set port for output
- directionFile.write("out");
- directionFile.flush();
- /*** Send commands to GPIO port ***/
- FileSystem fs = FileSystems.getDefault();
- Path commandChannel = fs.getPath("/sys/class/gpio/gpio" + GpioChannel + "/value");
- byte[] OFF = "0".getBytes();
- byte[] ON = "1".getBytes();
- // Loop forever
- while (true) {
- // Set GPIO port ON
- Files.write(commandChannel, ON);
- // Set GPIO port OFF
- Files.write(commandChannel, OFF);
- }
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement