/** * Initialise GPIO to echo and trigger pins * * @param triggerGpioNum GPIO connected to the HC-SR04 trigger pin * @param echoGpioNum GPIO connected to the HC-SR04 echo pin * @throws RuntimeIOException if an I/O error occurs */ public HCSR04UsingEvents(int triggerGpioNum, int echoGpioNum) throws RuntimeIOException { lock = new ReentrantLock(); condition = lock.newCondition(); // Define device for trigger pin at HCSR04 trigger = new DigitalOutputDevice(triggerGpioNum, true, false); // Define device for echo pin at HCSR04 echo = new DigitalInputDevice(echoGpioNum, GpioPullUpDown.NONE, GpioEventTrigger.BOTH); echo.addListener(this); // Sleep for 20 ms - let the device settle? SleepUtil.sleepMillis(20); }
/** * Free device GPIOs */ @Override public void close() { Logger.debug("close()"); if (trigger != null) { trigger.close(); } if (echo != null) { echo.close(); } }
private void scanKeys() { for (byte c=0; c<cols.length; c++) { // Begin column pulse output. cols[c].on(); for (byte r=0; r<rows.length; r++) { // keypress is active low so invert to high. values[r][c] = rows[r].isActive(); } // Set pin to high impedance input. Effectively ends column pulse. cols[c].off(); } } }
Logger.debug("Setting interruptGpio ({}) consumer", Integer.valueOf(interrupt_gpio.getGpio())); interrupt_gpio.addListener(this);
interruptGpios[0] = new DigitalInputDevice(interruptGpioA, GpioPullUpDown.NONE, GpioEventTrigger.RISING); interruptGpios[1] = new DigitalInputDevice(interruptGpioB, GpioPullUpDown.NONE, GpioEventTrigger.RISING);
@Override public void valueChanged(DigitalInputEvent event) { Event e = event.getValue() ? highEvent : lowEvent; e.set(); // Notify any listeners super.valueChanged(event); }
if (interrupt_gpio != null && event.getGpio() == interrupt_gpio.getGpio()) { process_event = true; break; intcap[1] = readByte(getIntCapReg(1)); } else if (interruptMode != InterruptMode.DISABLED) { if (interruptGpios[0] != null && event.getGpio() == interruptGpios[0].getGpio()) { intf[0] = readByte(getIntFReg(0)); intcap[0] = readByte(getIntCapReg(0));
public Keypad(int[] rowGpios, int[] colGpios, char[][] keys) { // Validate keys array dimensions matches rowGpios and colGpios if (keys.length != rowGpios.length || keys[0].length != colGpios.length) { throw new IllegalArgumentException("Array dimensions do not match"); } this.keys = keys; rows = new DigitalInputDevice[rowGpios.length]; cols = new DigitalOutputDevice[colGpios.length]; int i=0; for (int gpio : rowGpios) { rows[i++] = new DigitalInputDevice(gpio, GpioPullUpDown.PULL_UP, GpioEventTrigger.NONE); } i=0; for (int gpio : colGpios) { cols[i++] = new DigitalOutputDevice(gpio, false, false); } values = new boolean[rows.length][cols.length]; }
public TCS3200(int outGpio, int s2Gpio, int s3Gpio, int s0Gpio, int s1Gpio, int oeGpio) { out = new DigitalInputDevice(outGpio); s2 = new DigitalOutputDevice(s2Gpio); s3 = new DigitalOutputDevice(s3Gpio); if (s0Gpio != NOT_SET && s1Gpio != NOT_SET) { s0 = new DigitalOutputDevice(s0Gpio); s1 = new DigitalOutputDevice(s1Gpio); } if (oeGpio != NOT_SET) { // Enable device (active low) oe = new DigitalOutputDevice(oeGpio, false, true); } // Disable frequency output //out.setValue(false); setSampleSize(20); // One reading per second setUpdateInterval(1.0); // 2% setFrequency(Frequency.TWO_PERCENT); // Clear setFilter(Filter.CLEAR); out.addListener(this); Thread t = new Thread(this); t.start(); }
@Override public void close() throws RuntimeIOException { Logger.debug("close()"); // Close the interrupt GPIOs for (DigitalInputDevice interrupt_gpio : interruptGpios) { if (interrupt_gpio != null) { interrupt_gpio.close(); } } // Close all open GPIOs before closing the I2C device itself super.close(); }
/** * Initialise GPIO to echo and trigger pins * * @param triggerGpioNum GPIO connected to the HC-SR04 trigger pin * @param echoGpioNum GPIO connected to the HC-SR04 echo pin * @throws RuntimeIOException if an I/O error occurs */ public HCSR04(int triggerGpioNum, int echoGpioNum) throws RuntimeIOException { if (triggerGpioNum != echoGpioNum) { // Define device for trigger pin at HCSR04 trigger = new DigitalOutputDevice(triggerGpioNum, true, false); // Define device for echo pin at HCSR04 echo = new DigitalInputDevice(echoGpioNum, GpioPullUpDown.NONE, GpioEventTrigger.BOTH); } else { triggerAndEcho = new DigitalInputOutputDevice(triggerGpioNum, DeviceMode.DIGITAL_OUTPUT); echo = triggerAndEcho; } // Sleep for 20 ms - let the device settle? SleepUtil.sleepMillis(20); }
@Override public void close() throws IOException { oe.setOn(false); oe.close(); if (s0 != null) { s0.close(); } if (s1 != null) { s1.close(); } s2.close(); s3.close(); out.close(); }