sparkfun / SparkFun_SX1509_Arduino_Library

Arduino library for the SX1509 16-I/O GPIO expander.
44 stars 38 forks source link

pinMode() breaks subtly when used with ESP32 #20

Open novirium opened 1 year ago

novirium commented 1 year ago

As implemented, pinMode() uses an additional pin mode constant to set the pin up with the LED driver:

#define ANALOG_OUTPUT 0x3

This collides with the constant used for OUTPUT in the ESP32 Arduino core:

#define OUTPUT            0x03

The result is that when setting the pin mode to OUTPUT, the pin is inadvertently set up in the ANALOG_OUTPUT LED driver mode, rather than the plain output GPIO mode. This isn't immediately obvious, as writing high or low to the pin will still work, but other things will break (I found it because once set as a LED output it disables the input buffer for the pin, and so switching the pin back to an input no longer works).

The AVR core uses 0x00 to 0x02, and the ESP32 one uses 0x01 to 0x12 for pin modes, with analog being 0xC0. As far as I can tell, almost any other constant would work - though maybe pick something up the other end to reduce chance of hitting something else? 0xE0?