Advertisement
savageautomate

Raspberry Pi - Java GPIO Frequency Test (java.io)

Jan 9th, 2013
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3.  
  4.  
  5. public class GpioTest
  6. {
  7.     static String GpioChannel = "18";
  8.    
  9.     /**
  10.      * @param args the command line arguments
  11.      */
  12.     public static void main(String[] args) {
  13.         try {
  14.            
  15.             /*** Init GPIO port for output ***/
  16.            
  17.             // Open file handles to GPIO port unexport and export controls
  18.             FileWriter unexportFile = new FileWriter("/sys/class/gpio/unexport");
  19.             FileWriter exportFile = new FileWriter("/sys/class/gpio/export");
  20.  
  21.             // Reset the port
  22.             File exportFileCheck = new File("/sys/class/gpio/gpio"+ GpioChannel);
  23.             if (exportFileCheck.exists()) {
  24.                 unexportFile.write(GpioChannel);
  25.                 unexportFile.flush();
  26.             }
  27.        
  28.             // Set the port for use
  29.             exportFile.write(GpioChannel);  
  30.             exportFile.flush();
  31.  
  32.             // Open file handle to port input/output control
  33.             FileWriter directionFile = new FileWriter("/sys/class/gpio/gpio" + GpioChannel + "/direction");
  34.        
  35.             // Set port for output
  36.             directionFile.write("out");
  37.             directionFile.flush();
  38.                
  39.            
  40.             /*** Send commands to GPIO port ***/
  41.             FileWriter commandChannel = new FileWriter("/sys/class/gpio/gpio" + GpioChannel + "/value");
  42.            
  43.             // Loop forever
  44.             while (true) {
  45.                
  46.                     // Set GPIO port ON
  47.                     commandChannel.write("1");
  48.                     commandChannel.flush();
  49.                
  50.                     // Set GPIO port OFF
  51.                     commandChannel.write("0");
  52.                     commandChannel.flush();
  53.             }
  54.  
  55.         } catch (Exception exception) {
  56.             exception.printStackTrace();
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement