Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from GeVanCo/make-i2c-polling-dynamic
Browse files Browse the repository at this point in the history
Make I2c polling on the MCP23017 IO expander dynamic
  • Loading branch information
GeVanCo committed Jan 24, 2016
2 parents 5004ebc + 6773da5 commit 49f3474
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,18 +90,28 @@ 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;
private GpioStateMonitor monitor = null;

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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 49f3474

Please sign in to comment.