com.pi4j.io.gpio.event.GpioPinListenerAnalog Java Examples

The following examples show how to use com.pi4j.io.gpio.event.GpioPinListenerAnalog. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RaspberryPIConnection.java    From Ardulink-1 with Apache License 2.0 6 votes vote down vote up
private boolean startListenAnalogPin(MessageParsedInfo messageParsedInfo) {
	int pinNum    = (Integer)messageParsedInfo.getParsedValues()[0];
	
	GpioPinListenerAnalog listener = analogListeners.get(pinNum);
	if(listener == null) {
		listener = createAnalogListener(pinNum);
		GpioPinAnalogInput pin = (GpioPinAnalogInput)getPin(pinNum);
		pin.setMode(PinMode.ANALOG_INPUT);
		pin.setPullResistance(PinPullResistance.PULL_DOWN);
		
		analogListeners.put(pinNum, listener);
		pin.addListener(listener);
	}
	
	return true;
}
 
Example #2
Source File: PiLink.java    From Ardulink-2 with Apache License 2.0 5 votes vote down vote up
private GpioPinListenerAnalog analogAdapter() {
	return new GpioPinListenerAnalog() {
		@Override
		public void handleGpioPinAnalogValueChangeEvent(
				GpioPinAnalogValueChangeEvent event) {
			if (event.getEventType() == ANALOG_VALUE_CHANGE) {
				fireStateChanged(analogPinValueChanged(analogPin(event.getPin().getPin().getAddress()), (int) event.getValue()));
			}
		}
	};
}
 
Example #3
Source File: RaspberryPIConnection.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private boolean stopListenAnalogPin(MessageParsedInfo messageParsedInfo) {
	int pinNum    = (Integer)messageParsedInfo.getParsedValues()[0];
	GpioPinListenerAnalog listener = analogListeners.get(pinNum);
	if(listener != null) {
		GpioPinAnalogInput pin = (GpioPinAnalogInput)getPin(pinNum);
		
		analogListeners.remove(pin);
		pin.removeListener(listener);
	}
	
	return true;
}
 
Example #4
Source File: ADS1115GpioExample.java    From wyliodrin-server-nodejs with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws InterruptedException, IOException {
    
    System.out.println("<--Pi4J--> ADS1115 GPIO Example ... started.");

    // number formatters
    final DecimalFormat df = new DecimalFormat("#.##");
    final DecimalFormat pdf = new DecimalFormat("###.#");
    
    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();
    
    // create custom ADS1115 GPIO provider
    final ADS1115GpioProvider gpioProvider = new ADS1115GpioProvider(I2CBus.BUS_1, ADS1115GpioProvider.ADS1115_ADDRESS_0x48);
    
    // provision gpio analog input pins from ADS1115
    GpioPinAnalogInput myInputs[] = {
            gpio.provisionAnalogInputPin(gpioProvider, ADS1115Pin.INPUT_A0, "MyAnalogInput-A0"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1115Pin.INPUT_A1, "MyAnalogInput-A1"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1115Pin.INPUT_A2, "MyAnalogInput-A2"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1115Pin.INPUT_A3, "MyAnalogInput-A3"),
        };
    
    // ATTENTION !!          
    // It is important to set the PGA (Programmable Gain Amplifier) for all analog input pins. 
    // (You can optionally set each input to a different value)    
    // You measured input voltage should never exceed this value!
    //
    // In my testing, I am using a Sharp IR Distance Sensor (GP2Y0A21YK0F) whose voltage never exceeds 3.3 VDC
    // (http://www.adafruit.com/products/164)
    //
    // PGA value PGA_4_096V is a 1:1 scaled input, 
    // so the output values are in direct proportion to the detected voltage on the input pins
    gpioProvider.setProgrammableGainAmplifier(ProgrammableGainAmplifierValue.PGA_4_096V, ADS1115Pin.ALL);
            
    
    // Define a threshold value for each pin for analog value change events to be raised.
    // It is important to set this threshold high enough so that you don't overwhelm your program with change events for insignificant changes
    gpioProvider.setEventThreshold(500, ADS1115Pin.ALL);

    
    // Define the monitoring thread refresh interval (in milliseconds).
    // This governs the rate at which the monitoring thread will read input values from the ADC chip
    // (a value less than 50 ms is not permitted)
    gpioProvider.setMonitorInterval(100);
    
    
    // create analog pin value change listener
    GpioPinListenerAnalog listener = new GpioPinListenerAnalog()
    {
        @Override
        public void handleGpioPinAnalogValueChangeEvent(GpioPinAnalogValueChangeEvent event)
        {
            // RAW value
            double value = event.getValue();

            // percentage
            double percent =  ((value * 100) / ADS1115GpioProvider.ADS1115_RANGE_MAX_VALUE);
            
            // approximate voltage ( *scaled based on PGA setting )
            double voltage = gpioProvider.getProgrammableGainAmplifier(event.getPin()).getVoltage() * (percent/100);

            // display output
            System.out.print("\r (" + event.getPin().getName() +") : VOLTS=" + df.format(voltage) + "  | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + "       ");
        }
    };
    
    myInputs[0].addListener(listener);
    myInputs[1].addListener(listener);
    myInputs[2].addListener(listener);
    myInputs[3].addListener(listener);
    
    // keep program running for 10 minutes 
    for (int count = 0; count < 600; count++) {

        // display output
        //System.out.print("\r ANALOG VALUE (FOR INPUT A0) : VOLTS=" + df.format(voltage) + "  | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + "       ");
        Thread.sleep(1000);
    }
    
    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    gpio.shutdown();
    System.out.print("");
    System.out.print("");
}
 
Example #5
Source File: RaspberryPIConnection.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
private GpioPinListenerAnalog createAnalogListener(int pinNum) {
	return new AnalogListener(pinNum);
}
 
Example #6
Source File: ADS1015GpioExample.java    From wyliodrin-server-nodejs with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws InterruptedException, IOException {
    
    System.out.println("<--Pi4J--> ADS1015 GPIO Example ... started.");

    // number formatters
    final DecimalFormat df = new DecimalFormat("#.##");
    final DecimalFormat pdf = new DecimalFormat("###.#");
    
    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();
    
    // create custom ADS1015 GPIO provider
    final ADS1015GpioProvider gpioProvider = new ADS1015GpioProvider(I2CBus.BUS_1, ADS1015GpioProvider.ADS1015_ADDRESS_0x48);
    
    // provision gpio analog input pins from ADS1015
    GpioPinAnalogInput myInputs[] = {
            gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A0, "MyAnalogInput-A0"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A1, "MyAnalogInput-A1"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A2, "MyAnalogInput-A2"),
            gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A3, "MyAnalogInput-A3"),
        };
    
    // ATTENTION !!          
    // It is important to set the PGA (Programmable Gain Amplifier) for all analog input pins. 
    // (You can optionally set each input to a different value)    
    // You measured input voltage should never exceed this value!
    //
    // In my testing, I am using a Sharp IR Distance Sensor (GP2Y0A21YK0F) whose voltage never exceeds 3.3 VDC
    // (http://www.adafruit.com/products/164)
    //
    // PGA value PGA_4_096V is a 1:1 scaled input, 
    // so the output values are in direct proportion to the detected voltage on the input pins
    gpioProvider.setProgrammableGainAmplifier(ProgrammableGainAmplifierValue.PGA_4_096V, ADS1015Pin.ALL);
            
    
    // Define a threshold value for each pin for analog value change events to be raised.
    // It is important to set this threshold high enough so that you don't overwhelm your program with change events for insignificant changes
    gpioProvider.setEventThreshold(500, ADS1015Pin.ALL);

    
    // Define the monitoring thread refresh interval (in milliseconds).
    // This governs the rate at which the monitoring thread will read input values from the ADC chip
    // (a value less than 50 ms is not permitted)
    gpioProvider.setMonitorInterval(100);
    
    
    // create analog pin value change listener
    GpioPinListenerAnalog listener = new GpioPinListenerAnalog()
    {
        @Override
        public void handleGpioPinAnalogValueChangeEvent(GpioPinAnalogValueChangeEvent event)
        {
            // RAW value
            double value = event.getValue();

            // percentage
            double percent =  ((value * 100) / ADS1015GpioProvider.ADS1015_RANGE_MAX_VALUE);
            
            // approximate voltage ( *scaled based on PGA setting )
            double voltage = gpioProvider.getProgrammableGainAmplifier(event.getPin()).getVoltage() * (percent/100);

            // display output
            System.out.print("\r (" + event.getPin().getName() +") : VOLTS=" + df.format(voltage) + "  | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + "       ");
        }
    };
    
    myInputs[0].addListener(listener);
    myInputs[1].addListener(listener);
    myInputs[2].addListener(listener);
    myInputs[3].addListener(listener);
    
    // keep program running for 10 minutes 
    for (int count = 0; count < 600; count++) {

        // display output
        //System.out.print("\r ANALOG VALUE (FOR INPUT A0) : VOLTS=" + df.format(voltage) + "  | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + "       ");
        Thread.sleep(1000);
    }
    
    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    gpio.shutdown();
    System.out.print("");
    System.out.print("");
}