gardnerp
Posts: 2
Joined: Thu May 15, 2014 3:10 pm

Pi4J with I/O expander, pin.getState() starts null

Thu May 15, 2014 5:09 pm

I am using Pi4J and the MCP23017 i/o expander to read state from a rotary switch. The pins from the switch are connected to a pin on the chip which has the pullup resistor activated. The common pin from the switch is connected to ground.

I am polling the state of the the pins attached to the i/o expander, but for some reason, pin.getState() returns null. Once the state of the pin is changed by rotating the switch, the getState() stops returning null and reports as high or low. (Though even then, it seems flakey)

I think I'm missing an initialization step or perhaps I just don't understand how this is supposed to work.

Code: Select all

final GpioController gpio = GpioFactory.getInstance();
final MCP23017GpioProvider gpioProvider3 = new MCP23017GpioProvider(I2CBus.BUS_1, 0x21);
GpioPinDigitalInput pin = gpio.provisionDigitalInputPin(gpioProvider, MCP23017Pin.GPIO_A0, "3.A0", PinPullResistance.PULL_UP);

System.out.println(pin.getState());
result is "null"

Any help would be greatly appreciated.

Thanks in advance,
Peter

User avatar
clicky
Posts: 425
Joined: Thu Oct 25, 2012 7:34 am

Re: Pi4J with I/O expander, pin.getState() starts null

Fri May 16, 2014 3:26 pm

Hi Peter,

I just checked the code behind MCP23017GpioProvider and got entangled in the web of pins/pin providers, etc...

It looks like that you've getting back null because of what GpioStateMonitor (extends Thread) does (or doesn't do if not configured properly). It is in the source code for MCP23017GpioProvider.java.

Anyway, until I go through that logic more thoroughly, you can have a quick look at https://github.com/Pi4J/pi4j/blob/maste ... ample.java and maybe try mimicking the code there and provide listener for that pin (as in the example).

gardnerp
Posts: 2
Joined: Thu May 15, 2014 3:10 pm

Re: Pi4J with I/O expander, pin.getState() starts null

Fri May 16, 2014 7:17 pm

Thank you for looking into this.

The code I use is based on that example code. I have tried to setting up a listener, but my problem is that when the pin states return null, I can't read the initial state of the pins. The initial state is what I need for now. Event listeners will get called when the state changes.

Wouldn't you think that you could simply read the state of the pin after initialization?

User avatar
clicky
Posts: 425
Joined: Thu Oct 25, 2012 7:34 am

Re: Pi4J with I/O expander, pin.getState() starts null

Tue May 20, 2014 2:48 pm

I completely agree with you. I'll have a quick look at it tonight and post back here...

User avatar
clicky
Posts: 425
Joined: Thu Oct 25, 2012 7:34 am

Re: Pi4J with I/O expander, pin.getState() starts null

Tue May 20, 2014 6:27 pm

OK. So far I cannot see how initial state of the pin can be read from this 'device'. I might be wrong (it would be nice to hear from author of the code - Mr Savage), but what I gather from the implementation is as following: in a 'monitor' thread it polls state of interrupt registers (every 50ms) and updates pins that have 'interrupt' flag/bit set and were, previously, 'exported' - used.

So, tell me if you are OK with checking out pi4j source and rebuilding pi4j-gpio-extension subproject. If so, I don't mind making a branch (from master branch or maybe even better develop branch - let me know if you're using 0.0.5 or 1.0-SNAPSHOT version) with a 'fix' - where states of pins are going to be queried as soon as they are 'dispensed' to users or if nothing else provide special method on MCP23017GpioProvider class itself to fetch their states (couldn't make my mind yet!).

If not, I'll think of something else (I have a backup plan)...

Return to “Java”