2

My current code does not debounce the input signals from a hardware switch and as a result I get multiple input signals from a single push of the switch.

Maybe I'm thinking wrong but I thought that sleeping the current thread would keep additional interrupts (produced by bounce on switch) from happening.

package com.na7kr.GPIOController;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.wiringpi.GpioInterrupt;
import com.pi4j.wiringpi.GpioInterruptEvent;
import com.pi4j.wiringpi.GpioInterruptListener;
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioUtil;
import com.na7kr.Utils.Utils;
import com.na7kr.rpi_gpio_controller.Rpi_gpio_controllerApplicationUI;

public enum GPIOController {

INSTANCE(true);
final GpioController gpioController = GpioFactory.getInstance();
private boolean  flagRunning = false;
Rpi_gpio_controllerApplicationUI mApplication = new Rpi_gpio_controllerApplicationUI() ;

// The used GPIO pins
public GpioPinDigitalOutput[] mGPIOPins = new GpioPinDigitalOutput[1];

// ***************************************
// Constuctor
private GPIOController(boolean setLowOnExit) {

    mGPIOPins[0] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_00, "GPIO_Pin_" + 0, PinState.LOW);
    if (setLowOnExit) {
        // Set shutdown behavior for all pins
        mGPIOPins[0].setShutdownOptions(true, PinState.LOW);
    }
}
// ***************************************

// ***************************************
public void GetInput(int trigerpin, int outputpin) throws InterruptedException {
    Utils.Output_WriteDebug(true, " GPIO Trigger ... started on " + trigerpin + " OutPutPin " + outputpin);
    GpioInterrupt.addListener(new GpioInterruptListener() {
        @Override
        public void pinStateChange(GpioInterruptEvent event) {
            Utils.Output_WriteDebug(true, "Raspberry Pi PIN [" + trigerpin + "] is in STATE [" + event.getState() + "] Output =" + outputpin);
            try {

                if (running(outputpin))
                {
                    Thread.sleep(500);
                    flagRunning = false;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    // setup wiring pi
    if (Gpio.wiringPiSetup() == -1) {
        Utils.Output_WriteDebug(true, " ==>> GPIO SETUP FAILED");
    }

    // export all the GPIO pins that we will be using
    GpioUtil.export(trigerpin, GpioUtil.DIRECTION_IN);

    // set the edge state on the pins we will be listening for
    GpioUtil.setEdgeDetection(trigerpin, GpioUtil.EDGE_RISING);

    // configure GPIO x as an INPUT pin; enable it for callbacks
    Gpio.pinMode(trigerpin, Gpio.INPUT);
    Gpio.pullUpDnControl(trigerpin, Gpio.PUD_DOWN );
    GpioInterrupt.enablePinStateChangeCallback(trigerpin);
    flagRunning = false;

}

// ***************************************

// ***************************************
public static void GPIOExit() {
    Utils.Output_WriteDebug(true, " Bye!!" );
    GpioInterrupt.disablePinStateChangeCallback(1);
    GpioInterrupt.disablePinStateChangeCallback(2);
    }
// ***************************************

// ***************************************
private  synchronized boolean running(int outputpin) {
    Utils.Output_WriteDebug(true, "Running function flag= " + flagRunning  );
    if (flagRunning == false)
    {
        Utils.Output_WriteDebug(true, "false");
        flagRunning = true;
        INSTANCE.mGPIOPins[outputpin].toggle();
        mApplication.refreshGPIOPinState();

    }
    Utils.Output_WriteDebug(true, "exit function  flag= " + flagRunning );
    return flagRunning;
}
// ***************************************
  }
  • Just not debouncing – Kevin Roberts Sep 13 '15 at 0:27
  • I'm sorry, but what happens when you run your code? – Bex Sep 15 '15 at 12:19
  • Does not look to helping with the debouncing – Kevin Roberts Sep 15 '15 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.