Search in sources :

Example 1 with UnsupportedBusNumberException

use of com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException in project openhab1-addons by openhab.

the class MCP23017Binding method bindGpioPin.

private void bindGpioPin(MCP23017BindingProvider provider, String itemName) {
    try {
        int address = provider.getBusAddress(itemName);
        MCP23017GpioProvider mcp = mcpProviders.get(address);
        if (mcp == null) {
            try {
                mcp = new MCP23017GpioProvider(I2CBus.BUS_1, address);
                mcp.setPollingTime(pollingInterval);
            } catch (UnsupportedBusNumberException ex) {
                throw new IllegalArgumentException("Tried to access not available I2C bus");
            }
            mcpProviders.put(address, mcp);
        }
        Pin pin = provider.getPin(itemName);
        PinMode mode = provider.getPinMode(itemName);
        if (mode.equals(PinMode.DIGITAL_OUTPUT)) {
            GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(mcp, pin, itemName, provider.getDefaultState(itemName));
            gpioPins.put(itemName, output);
            logger.debug("Provisioned digital output for {}", itemName);
        } else if (mode.equals(PinMode.DIGITAL_INPUT)) {
            GpioPinDigitalInput input = gpio.provisionDigitalInputPin(mcp, pin, itemName, PinPullResistance.OFF);
            input.setDebounce(20);
            input.addListener(this);
            gpioPins.put(itemName, input);
            logger.debug("Provisioned digital input for {}", itemName);
        } else {
            throw new IllegalArgumentException("Invalid Pin Mode in config " + mode.name());
        }
    } catch (IOException exception) {
        logger.error("I/O error {}", exception.getMessage());
    }
}
Also used : GpioPinDigitalOutput(com.pi4j.io.gpio.GpioPinDigitalOutput) GpioPinDigitalInput(com.pi4j.io.gpio.GpioPinDigitalInput) GpioPin(com.pi4j.io.gpio.GpioPin) Pin(com.pi4j.io.gpio.Pin) MCP23017GpioProvider(com.pi4j.gpio.extension.mcp.MCP23017GpioProvider) PinMode(com.pi4j.io.gpio.PinMode) IOException(java.io.IOException) UnsupportedBusNumberException(com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException)

Example 2 with UnsupportedBusNumberException

use of com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException in project openhab1-addons by openhab.

the class MCP3424Binding method bindGpioPin.

private void bindGpioPin(MCP3424BindingProvider provider, String itemName) {
    try {
        int address = provider.getBusAddress(itemName);
        MCP3424GpioProvider mcp = mcpProviders.get(address);
        if (mcp == null) {
            try {
                int gain = provider.getGain(itemName);
                int resolution = provider.getResolution(itemName);
                mcp = new MCP3424GpioProvider(I2CBus.BUS_1, address, resolution, gain);
                mcp.setMonitorInterval(pollingInterval);
            } catch (UnsupportedBusNumberException ex) {
                throw new IllegalArgumentException("Tried to access not available I2C bus");
            }
            mcpProviders.put(address, mcp);
        }
        mcp.setMonitorEnabled(false);
        Pin pin = provider.getPin(itemName);
        GpioPinAnalogInput input = gpio.provisionAnalogInputPin(mcp, pin, itemName);
        input.addListener(this);
        input.setTag(provider.getItem(itemName));
        gpioPins.put(itemName, input);
        mcp.setEventThreshold(0, input);
        mcp.setMonitorEnabled(true);
        logger.debug("Provisioned analog input for {}", itemName);
    } catch (IOException exception) {
        logger.error("I/O error {}", exception.getMessage());
    }
}
Also used : MCP3424GpioProvider(com.pi4j.gpio.extension.mcp.MCP3424GpioProvider) GpioPin(com.pi4j.io.gpio.GpioPin) Pin(com.pi4j.io.gpio.Pin) IOException(java.io.IOException) UnsupportedBusNumberException(com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException) GpioPinAnalogInput(com.pi4j.io.gpio.GpioPinAnalogInput)

Aggregations

GpioPin (com.pi4j.io.gpio.GpioPin)2 Pin (com.pi4j.io.gpio.Pin)2 UnsupportedBusNumberException (com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException)2 IOException (java.io.IOException)2 MCP23017GpioProvider (com.pi4j.gpio.extension.mcp.MCP23017GpioProvider)1 MCP3424GpioProvider (com.pi4j.gpio.extension.mcp.MCP3424GpioProvider)1 GpioPinAnalogInput (com.pi4j.io.gpio.GpioPinAnalogInput)1 GpioPinDigitalInput (com.pi4j.io.gpio.GpioPinDigitalInput)1 GpioPinDigitalOutput (com.pi4j.io.gpio.GpioPinDigitalOutput)1 PinMode (com.pi4j.io.gpio.PinMode)1