MCP23008 multiple libraries


#1

So there are 3 published libraries for the MCP23008, two of them seem to be separate efforts to port the adafruit library with pretty much identical results and the 3rd (arguably structured in a nicer fashion) built from the ground up by @rikkas7. The adafruit libraries currently lack support for the interrupt pin and the -RK versions (when I last checked) have neither support for the interrupts or reading all 8 pins at once.

I noticed the Adafruit MCP23017 does have the interrupt features so I cut and pasted those into a local copy, ripped out the port selecting parts and behold that seems to work, its not exactly elegant as the 23017 and 23008 do not appear to be written in the same style despite headers claiming the same author(s).

So I guess the question is do any of those in charge of maintaining those libraries want my sloppy hack as you could literally fit what I know about submitting such things via Github on the head of a pin?

Secondary question, if the interrupt feature gets put into the Adafruit libraries, would it also make its way into the I2C display library that uses copy of those same files?


#2

Seems wise just to post what I did, currently I think there’s now some code missing that handles invalid pin numbers…
… shortly…


#3

The reason the MCP23008-RK library doesn’t support interrupts is that handling it is kind of a problem. While adding the interrupt handler itself is easy, almost certainly the next thing you’ll want to do is query the MCP23008. The problem is that the I2C library is not reentrant, and if the main loop code happens to be making an I2C call when the MCP23008 interrupt happens, bad things will happen.

It’s possible to work around this either by dispatching to the loop, or guarding all of the I2C accesses with some sort of semaphore, but this is a pain to do from a library.

The multiple value read would be easy to add, but the API for MCP23008-RK basically emulates the built-in GPIO calls, and there isn’t a call to read multiple built-in GPIO lines. It would be easy to add, however.


#4

Makes sense, I’m using the interrupt for a fairly specific purpose - waking the Photon up from sleep, so conveniently sidestep all the issues you describe as I don’t need to query it. Using it as a true interrupt against running code however does indeed introduce a few questions. But is that truly a problem the library should be addressing or it something the HAL or Wire library should understand?


#5

@rickkas7,

Thank you for this explanation. I was wondering why there is no support for the Interrupt functionality. Bummer but I understand.

Quick, question. Is there an analog equivalent to the MCP23008? I know you included an i2c ADC in your tutorial but was looking for ADC expansion as well.

Thanks, Chip