diff --git a/pi4j-gpio-extension/src/main/java/com/pi4j/gpio/extension/mcp/MCP23017GpioProvider.java b/pi4j-gpio-extension/src/main/java/com/pi4j/gpio/extension/mcp/MCP23017GpioProvider.java index b1d05c8d8..4956c3a3c 100644 --- a/pi4j-gpio-extension/src/main/java/com/pi4j/gpio/extension/mcp/MCP23017GpioProvider.java +++ b/pi4j-gpio-extension/src/main/java/com/pi4j/gpio/extension/mcp/MCP23017GpioProvider.java @@ -61,6 +61,7 @@ public class MCP23017GpioProvider extends GpioProviderBase implements GpioProvid public static final String NAME = "com.pi4j.gpio.extension.mcp.MCP23017GpioProvider"; public static final String DESCRIPTION = "MCP23017 GPIO Provider"; public static final int DEFAULT_ADDRESS = 0x20; + public static final int DEFAULT_POLLING_TIME = 50; private static final int REGISTER_IODIR_A = 0x00; private static final int REGISTER_IODIR_B = 0x01; @@ -89,6 +90,8 @@ public class MCP23017GpioProvider extends GpioProviderBase implements GpioProvid private int currentPullupA = 0; private int currentPullupB = 0; + private int pollingTime = DEFAULT_POLLING_TIME; + private boolean i2cBusOwner = false; private final I2CBus bus; private final I2CDevice device; @@ -96,11 +99,19 @@ public class MCP23017GpioProvider extends GpioProviderBase implements GpioProvid public MCP23017GpioProvider(int busNumber, int address) throws IOException { // create I2C communications bus instance - this(I2CFactory.getInstance(busNumber), address); - i2cBusOwner = true; + this(busNumber, address, DEFAULT_POLLING_TIME); + } + + public MCP23017GpioProvider(int busNumber, int address, int pollingTime) throws IOException { + // create I2C communications bus instance + this(I2CFactory.getInstance(busNumber), address, pollingTime); } public MCP23017GpioProvider(I2CBus bus, int address) throws IOException { + this(bus, address, DEFAULT_POLLING_TIME); + } + + public MCP23017GpioProvider(I2CBus bus, int address, int pollingTime) throws IOException { // set reference to I2C communications bus instance this.bus = bus; @@ -135,6 +146,11 @@ public MCP23017GpioProvider(I2CBus bus, int address) throws IOException { // set all default pin pull up resistors device.write(REGISTER_GPPU_A, (byte) currentPullupA); device.write(REGISTER_GPPU_B, (byte) currentPullupB); + + // set pollingtime + this.pollingTime = pollingTime; + + i2cBusOwner = true; } @Override @@ -408,6 +424,9 @@ public void shutdown() { } } + public void setPollingTime(int pollingTime) { + this.pollingTime = pollingTime; + } /** * This class/thread is used to to actively monitor for GPIO interrupts @@ -481,7 +500,7 @@ public void run() { // ... lets take a short breather ... Thread.currentThread(); - Thread.sleep(50); + Thread.sleep(pollingTime); } catch (Exception ex) { ex.printStackTrace(); }