Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- public class GpioTest
- {
- 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 ***/
- FileWriter commandChannel = new FileWriter("/sys/class/gpio/gpio" + GpioChannel + "/value");
- // Loop forever
- while (true) {
- // Set GPIO port ON
- commandChannel.write("1");
- commandChannel.flush();
- // Set GPIO port OFF
- commandChannel.write("0");
- commandChannel.flush();
- }
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement