diff --git a/CHANGELOG.txt b/CHANGELOG.txt index d21f2a8..79c6a76 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -9,7 +9,12 @@ Change Log ------------------ - Second Release -0.0.3 (27/10/2020) +0.0.3 (28/10/2020) ------------------ - third Release - fix bugs + +0.0.4 (30/10/2020) +------------------ +- fourth Release +- fix bugs diff --git a/README.md b/README.md index 383ad02..58308e3 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ import pcf8574_io # you can use up to 8 PCF8574 boards 0x20 and 0x21 are the I2C addresses # true will set all the pins HIGH +5v false will set them to LOW 0v -p1 = pcf8574_io.PCF(0x20, True) -p2 = pcf8574_io.PCF(0x21, False) +p1 = pcf8574_io +p2 = pcf8574_io # p0 to p7 are the pins name # INPUT or OUTPUT is the mode diff --git a/pcf8574_io/PCF85.py b/pcf8574_io/PCF85.py index cfe94f5..ec91a9c 100644 --- a/pcf8574_io/PCF85.py +++ b/pcf8574_io/PCF85.py @@ -1,139 +1,76 @@ from smbus2 import SMBus - -pinModeFlag = 0x00 -PCFAddress = 0x20 -smBusNum = 1 -writeData = 0x00 +import math def setup(PCFAdd, smBus, status): - global pinModeFlag, writeData, PCFAddress, smBusNum - pinModeFlag = 0x00 - PCFAddress = PCFAdd - smBusNum = smBus - writeData = 0x00 if status: - with SMBus(smBusNum) as bus: - b = bus.write_byte(PCFAddress, 0xFF) + with SMBus(smBus) as bus: + bus.write_byte(PCFAdd, 0xFF) elif not status: - with SMBus(smBusNum) as bus: - b = bus.write_byte(PCFAddress, 0x00) - - -# mode INPUT 0,OUTPUT 1 -def pin_mode(pinName, mode): - global pinModeFlag - if "p0" in pinName.strip(): - set_mode(pinName, mode, 0, 0x01) - elif "p1" in pinName.strip(): - set_mode(pinName, mode, 1, 0x02) - elif "p2" in pinName.strip(): - set_mode(pinName, mode, 2, 0x04) - elif "p3" in pinName.strip(): - set_mode(pinName, mode, 3, 0x08) - elif "p4" in pinName.strip(): - set_mode(pinName, mode, 4, 0x10) - elif "p5" in pinName.strip(): - set_mode(pinName, mode, 5, 0x20) - elif "p6" in pinName.strip(): - set_mode(pinName, mode, 6, 0x40) - elif "p7" in pinName.strip(): - set_mode(pinName, mode, 7, 0x80) + with SMBus(smBus) as bus: + bus.write_byte(PCFAdd, 0x00) + + +def pin_mode(pinName, mode, flg): + return set_mode(pinNameToNum(pinName), mode, flg, int(math.pow(2, pinNameToNum(pinName)))) + + +def set_mode(pnNum, mode, rValue, flg): + if "INPUT" in mode.strip() and isKthBitSet(flg, pnNum + 1): + flg = flg - rValue + return flg + elif "OUTPUT" in mode.strip() and not isKthBitSet(flg, pnNum + 1): + flg = flg + rValue + return flg else: - print("Wrong Pin Name!") + return flg -def isKthBitSet(n, k): - if n & (1 << (k - 1)): +def digitalRead(pinName, smbs, addr): + with SMBus(smbs) as bus: + b = bus.read_byte(addr) + if isKthBitSet(b, pinNameToNum(pinName)): return True else: return False -def read_data(pinNum): - with SMBus(smBusNum) as bus: - b = bus.read_byte(PCFAddress) - if isKthBitSet(b, pinNum): - return True +def pinNameToNum(pinName): + pn = pinName.strip().replace("p", "").strip() + if pn in range(8): + return pn else: - return False + print("pin name error!") -def write_data(pinName, val): - if "p0" in pinName.strip(): - set_write(pinName, 0, 0x01, val) - if "p1" in pinName.strip(): - set_write(pinName, 1, 0x02, val) - if "p2" in pinName.strip(): - set_write(pinName, 2, 0x04, val) - if "p3" in pinName.strip(): - set_write(pinName, 3, 0x08, val) - if "p4" in pinName.strip(): - set_write(pinName, 4, 0x10, val) - if "p5" in pinName.strip(): - set_write(pinName, 5, 0x20, val) - if "p6" in pinName.strip(): - set_write(pinName, 6, 0x40, val) - if "p7" in pinName.strip(): - set_write(pinName, 7, 0x80, val) - - -def set_mode(pinName, mode, pValue, rValue): - global pinModeFlag - cValue = pValue + 1 - if "p" + str(pValue) in pinName.strip(): - if "INPUT" in mode.strip() and isKthBitSet(pinModeFlag, cValue): - pinModeFlag = pinModeFlag - rValue - elif "OUTPUT" in mode.strip() and not isKthBitSet(pinModeFlag, cValue): - pinModeFlag = pinModeFlag + rValue - else: - print("Wrong IO Mode") - - -def set_write(pinName, pValue, rValue, val): - global writeData - with SMBus(smBusNum) as bus: - b = bus.read_byte(PCFAddress) - writeData = b - cValue = pValue + 1 - if "p" + str(pValue) in pinName.strip(): - if val == 0 and isKthBitSet(writeData, cValue): - writeData = writeData - rValue - with SMBus(smBusNum) as bus: - bus.write_byte(PCFAddress, writeData) - - elif val == 1 and not isKthBitSet(writeData, cValue): - writeData = writeData + rValue - with SMBus(smBusNum) as bus: - bus.write_byte(PCFAddress, writeData) - - -def checkPinNum(pinName): - if 0 < int(pinName.strip().replace("p")) < 8: +def isKthBitSet(n, k): + if n & (1 << (k - 1)): return True else: return False -def digitalRead(pinName): - global pinModeFlag - cValue = int(pinName.strip().replace("p", "")) + 1 - return read_data(cValue) - - -def digitalWrite(pinName, val): - global pinModeFlag - cValue = int(pinName.strip().replace("p", "")) + 1 - if isKthBitSet(pinModeFlag, cValue): +def digitalWrite(pinName, val, addr, flg): + if isKthBitSet(flg, pinNameToNum(pinName)+1): if "HIGH" in val.strip(): - write_data(pinName, 1) + write_data(pinNameToNum(pinName), 1, flg, addr) elif "LOW" in val.strip(): - write_data(pinName, 0) + write_data(pinNameToNum(pinName), 0, flg, addr) else: print("You can not Write Input Pin") -# pin_mode("p0", "INPUT") -# digitalRead("p0") -# digitalWrite("p0", "HIGH") -# print("{0:b}".format(pinModeFlag)) +def write_data(pnNum, val, smbs, flg, addr): + if isKthBitSet(flg, pnNum+1): + with SMBus(smbs) as bus: + wr = bus.read_byte(addr) + if val == 0 and isKthBitSet(wr, pnNum+1): + wr = wr - int(math.pow(2, pnNum)) + with SMBus(smbs) as bus: + bus.write_byte(addr, wr) + elif val == 1 and not isKthBitSet(wr, pnNum+1): + wr = wr + int(math.pow(2, pnNum)) + with SMBus(smbs) as bus: + bus.write_byte(addr, wr) + + diff --git a/pcf8574_io/__init__.py b/pcf8574_io/__init__.py index 41c7fc1..0ef9c62 100644 --- a/pcf8574_io/__init__.py +++ b/pcf8574_io/__init__.py @@ -6,14 +6,15 @@ class PCF: def __init__(self, address, status): self.address = address self.status = status - PCF85.setup(address, 1, status) + self.pinModeFlag = 0x00 + self.smBusNum = 1 + PCF85.setup(address, self.smBusNum, status) def pin_mode(self, PinName, Mode): - return PCF85.pin_mode(PinName, Mode) + self.pinModeFlag = PCF85.pin_mode(PinName, Mode, self.pinModeFlag) def digital_read(self, PinName): - return PCF85.digitalRead(PinName) + return PCF85.digitalRead(PinName, self.smBusNum, self.address) def digital_write(self, PinName, Val): - return PCF85.digitalWrite(PinName, Val) - + PCF85.digitalWrite(PinName, Val, self.address, self.pinModeFlag) diff --git a/setup.py b/setup.py index 96a44c3..8d2f50a 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name='pcf8574_io', - version='0.0.3', + version='0.0.4', description='setup pin mode and read, write to PCF8574 pins', long_description=open('README.txt').read() + '\n\n' + open('CHANGELOG.txt').read(), url='', diff --git a/test.py b/test.py index c8b8a4e..81a301d 100644 --- a/test.py +++ b/test.py @@ -1,8 +1,8 @@ import pcf8574_io # you can use up to 8 PCF8574 boards -p1 = pcf8574_io.PCF(0x20, True) -p2 = pcf8574_io.PCF(0x21, False) +p1 = pcf8574_io +p2 = pcf8574_io # p0 to p7 are the pins name # INPUT or OUTPUT is the mode