
Instead let's go back to what we know works. Using pure Java Embedded API's we can use the file handle to get to the GPIO pins for access. So, let's roll back and start there again and incrementally move toward trying to match what we should be able to do with NIO.
Here's the code that we will roll back to. You can try this at home to see what type of speed you can get on your oscilloscope. I'll report what I have also.
/* * Example Code */ /* * Java Embedded Raspberry Pi Servo app */ package jerpitest; import java.io.FileWriter; import java.io.File; /** * * @author hinkmond */ public class GPIOMaxRate { static final String GPIO_OUT = "out"; static final String GPIO_ON = "1"; static final String GPIO_OFF = "0"; static String[] GpioChannels = { "24" }; /** * @param args the command line arguments */ @SuppressWarnings("UseSpecificCatch") public static void main(String[] args) { FileWriter[] commandChannels; 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"); // Loop through all ports if more than 1 for (String gpioChannel : GpioChannels) { System.out.println(gpioChannel); // Reset the port, if needed 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(GPIO_OUT); directionFile.flush(); } /*** Send fastest on/off to GPIO port ***/ // Set up a GPIO port as a command channel FileWriter commandChannel = new FileWriter("/sys/class/gpio/gpio" + GpioChannels[0] + "/value"); // Loop forever with fastest on/off to GPIO pin while (true) { commandChannel.write(GPIO_ON); commandChannel.flush(); commandChannel.write(GPIO_OFF); commandChannel.flush(); } } } catch (Exception exception) { exception.printStackTrace(); } } } |
This should be still pretty fast and good enough for most applications. But, we are still searching for the theoretically maximum frequency we can achieve using pure Java Embedded.