-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Change Log | ||
========== | ||
|
||
0.0.1 (27/10/2020) | ||
------------------ | ||
- First Release |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2020 Ahmed Omar | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global-include *.txt *.py |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
### **Python driver for PCF8574 8bit IO Expander board** | ||
Developed for the Raspberry Pi, requires the python-smbus2 package to access the I2C bus. | ||
|
||
tested on raspberry pi 3b plus with two PCF8574 boards. | ||
|
||
|
||
First install smbus2 using: | ||
`pip install smbus2` | ||
then install the actual package using: | ||
`pip install pcf8574-io` | ||
|
||
|
||
Usage Example: | ||
```python | ||
import pcf8574_io | ||
|
||
# you can use up to 8 PCF8574 boards 0x20 and 0x21 are the I2C addresses | ||
p1 = pcf8574_io.PCF(0x20) | ||
p2 = pcf8574_io.PCF(0x21) | ||
|
||
# p0 to p7 are the pins name | ||
# INPUT or OUTPUT is the mode | ||
p1.pin_mode("p0", "INPUT") | ||
print(p1.digital_read("p0")) | ||
|
||
# you can write and read the output pins | ||
# use HIGH or LOW to set the pin HIGH is +5v LOW is 0v | ||
p1.pin_mode("p4", "OUTPUT") | ||
p1.digital_write("p4", "HIGH") | ||
print(p1.digital_read("p4")) | ||
|
||
# you can read and write up to 8 boards at the same time just make sure you ech board has a different address | ||
p2.pin_mode("p7", "OUTPUT") | ||
p2.digital_write("p7", "LOW") | ||
print(p2.digital_read("p7")) | ||
``` | ||
The board been used: | ||
![alt text](https://image.made-in-china.com/2f0j00CbvRKwBGGecA/Pcf8574-Io-Expansion-Board-I-O-Expander-I2c-Bus-Evaluation-Development-Module.jpg) | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from smbus2 import SMBus | ||
|
||
pinModeFlag = 0x00 | ||
PCFAddress = 0x20 | ||
smBusNum = 1 | ||
writeData = 0x00 | ||
|
||
|
||
def setup(PCFAdd, smBus): | ||
global pinModeFlag, writeData, PCFAddress, smBusNum | ||
pinModeFlag = 0x00 | ||
PCFAddress = PCFAdd | ||
smBusNum = smBus | ||
writeData = 0x00 | ||
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) | ||
else: | ||
print("Wrong Pin Name!") | ||
|
||
|
||
def isKthBitSet(n, k): | ||
if n & (1 << (k - 1)): | ||
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 | ||
else: | ||
return False | ||
|
||
|
||
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: | ||
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): | ||
if "HIGH" in val.strip(): | ||
write_data(pinName, 1) | ||
elif "LOW" in val.strip(): | ||
write_data(pinName, 0) | ||
else: | ||
print("You can not Write Input Pin") | ||
|
||
# pin_mode("p0", "INPUT") | ||
# digitalRead("p0") | ||
# digitalWrite("p0", "HIGH") | ||
|
||
# print("{0:b}".format(pinModeFlag)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import PCF85 | ||
|
||
|
||
class PCF: | ||
|
||
def __init__(self, address): | ||
self.address = address | ||
PCF85.setup(address, 1) | ||
|
||
def pin_mode(self, PinName, Mode): | ||
return PCF85.pin_mode(PinName, Mode) | ||
|
||
def digital_read(self, PinName): | ||
return PCF85.digitalRead(PinName) | ||
|
||
def digital_write(self, PinName, Val): | ||
return PCF85.digitalWrite(PinName, Val) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from setuptools import setup, find_packages | ||
|
||
classifiers = [ | ||
'Development Status :: 1 - Planning', | ||
'Intended Audience :: Education', | ||
'License :: OSI Approved :: MIT License', | ||
'Programming Language :: Python :: 3' | ||
] | ||
|
||
setup( | ||
name='pcf8574_io', | ||
version='', | ||
description='setup pin mode and read, write to PCF8574 pins', | ||
long_description=open('README.txt').read() + '\n\n' + open('CHANGELOG.txt').read(), | ||
url='', | ||
author='Ahmed Omar', | ||
author_email='ahmed.bm78@gmail.com', | ||
license='MIT', | ||
classifiers=classifiers, | ||
keywords='PCF8574', | ||
packages=find_packages(), | ||
install_requires=[''] | ||
) |