xreef / PCF8574_micropython_library

MicroPython library for pcf8574 an i2c digital expander for Arduino, esp32, SMT32 and ESP8266. Can read write digital values with only 2 wire.
https://www.mischianti.org/category/my-libraries/pcf8574/
Other
5 stars 0 forks source link

Mixing in and out pins not possible #2

Open niko084 opened 2 months ago

niko084 commented 2 months ago

I am really glad that I found your code to be used on an ESP32.

During implementation of my application I found out that mixing in and out pins not possible at the moment. Let's say

Doing the following

makes P1 fall to low after the read.

Looking into you code I found the line in the function 'read_buffer'

self._i2c.writeto(self._address, bytearray([self.read_mode]))

As self.read_mode only contains the Pin.IN-related configuration, it sets the Pin.OUTs to 0 respective low.

Maybe I am getting it wrong, but the i2c.writeto operation is basically not needed here. Commenting out this one and two similar lines (227, 261 and 304) solved the issue for me. Alternatively extending the line by applying a bitwise or with self.write_byte_buffered, or maybe something similar may help as well. I am not too deep into it here, and as I said, the line might not be needed at all

xreef commented 2 months ago

Hi, Try to use the basic function to obtain your results, and give me the feedback. Bye Renzo

niko084 commented 2 months ago

Hi,

I did so:

from PCF8574 import PCF8574
from machine import Pin, I2C

pcf8574Instance = PCF8574(32, sda=21, scl=22)

pcf8574Instance.Pin(0, Pin.IN)
pcf8574Instance.Pin(1, Pin.OUT, 1)

pcf8574Instance.begin()

#Pin1 is high
print ("read_mode is :" + bin(pcf8574Instance.read_mode))  #0b1
pcf8574Instance._i2c.writeto(pcf8574Instance._address, bytearray([pcf8574Instance.read_mode]))

#Pin1 is low now - Not expected

#Reset scenario
pcf8574Instance.digital_write(1, 1)

#Updated statement taking write_byte_buffered into consideration
print ("write_byte_buffered is :" + bin(pcf8574Instance.write_byte_buffered))  #0b10
print ("write_mode is :" + bin(pcf8574Instance.write_mode))  #0b10
print ("read_mode is :" + bin(pcf8574Instance.read_mode))  #0b1
pcf8574Instance._i2c.writeto(pcf8574Instance._address, bytearray([pcf8574Instance.read_mode | (pcf8574Instance.write_mode & pcf8574Instance.write_byte_buffered)]))

#Works as expected, no change at Pin1
xreef commented 1 month ago

Hi Niko, I think you are right, sorry for the late response but i have time problem. I try to update the code as soon as possible. Thanks again Renzo