All Downloads are FREE. Search and download functionalities are using the official Maven repository.

-example.1.1.source-code.PCF8574GpioExample Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
/*
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  PCF8574GpioExample.java
 *
 * This file is part of the Pi4J project. More information about
 * this project can be found here:  http://www.pi4j.com/
 * **********************************************************************
 * %%
 * Copyright (C) 2012 - 2016 Pi4J
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
import java.io.IOException;

import com.pi4j.gpio.extension.pcf.PCF8574GpioProvider;
import com.pi4j.gpio.extension.pcf.PCF8574Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;

/**
 * 

* This example code demonstrates how to setup a custom GpioProvider * for GPIO pin state control and monitoring. *

* *

* This example implements the PCF8574 GPIO expansion board. * More information about the board can be found here: * * http://www.ti.com/lit/ds/symlink/pcf8574.pdf *

* *

* The PCF8574 is connected via I2C connection to the Raspberry Pi and provides * 16 GPIO pins that can be used for either digital input or digital output pins. *

* * @author Robert Savage */ public class PCF8574GpioExample { public static void main(String args[]) throws InterruptedException, UnsupportedBusNumberException, IOException { System.out.println("<--Pi4J--> PCF8574 GPIO Example ... started."); // create gpio controller final GpioController gpio = GpioFactory.getInstance(); // create custom MCP23017 GPIO provider final PCF8574GpioProvider provider = new PCF8574GpioProvider(I2CBus.BUS_1, PCF8574GpioProvider.PCF8574A_0x3F); // provision gpio input pins from MCP23017 GpioPinDigitalInput myInputs[] = { gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_00), gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_01), gpio.provisionDigitalInputPin(provider, PCF8574Pin.GPIO_02) }; // create and register gpio pin listener gpio.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { // display pin state on console System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState()); } }, myInputs); // provision gpio output pins and make sure they are all LOW at startup GpioPinDigitalOutput myOutputs[] = { gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_04, PinState.LOW), gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_05, PinState.LOW), gpio.provisionDigitalOutputPin(provider, PCF8574Pin.GPIO_06, PinState.LOW) }; // on program shutdown, set the pins back to their default state: HIGH gpio.setShutdownOptions(true, PinState.HIGH, myOutputs); // keep program running for 20 seconds for (int count = 0; count < 10; count++) { gpio.setState(true, myOutputs); Thread.sleep(1000); gpio.setState(false, myOutputs); 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.println("Exiting PCF8574GpioExample"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy