Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed9378 committed Oct 31, 2020
1 parent 755a52d commit f01b5f8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 122 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
159 changes: 48 additions & 111 deletions pcf8574_io/PCF85.py
Original file line number Diff line number Diff line change
@@ -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)


11 changes: 6 additions & 5 deletions pcf8574_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='',
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f01b5f8

Please sign in to comment.