-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.py
59 lines (49 loc) · 1.75 KB
/
gpio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import smbus
# MCP23008 Register Addresses
IODIR = 0x00 # GPIO direction register
#GPPU = 0x06 # Pull-up resistor register
GPIOREG = 0x09 # GPIO register
OLAT = 0x0A # Output latch register
class MCP23008:
def __init__(self, bus, address):
self.bus = bus
self.address = address
self.init_device()
def init_device(self):
try:
# Initialize all GPIO pins as outputs and set to LOW
self.bus.write_byte_data(self.address, IODIR, 0x00) # All pins as outputs
self.bus.write_byte_data(self.address, OLAT, 0x00) # All pins LOW
except:
print("Warning! MCP23008 unavailable at I2C address " + str(self.address))
def set_pin(self, pin, value):
try:
# Set the state of a specific pin (0 or 1)
current_value = self.bus.read_byte_data(self.address, OLAT)
if value:
current_value |= (1 << pin) # Set bit
else:
current_value &= ~(1 << pin) # Clear bit
self.bus.write_byte_data(self.address, OLAT, current_value)
except:
pass
def get_pin(self, pin):
# Get the state of a specific pin
return (self.bus.read_byte_data(self.address, GPIOREG) >> pin) & 0x01
class GPIO:
def __init__(self):
try:
bus = smbus.SMBus(1) # Initialize I2C bus
# I2C addresses of the MCP23008 devices
i2c_addresses = [0x20, 0x21, 0x23]
# Initialize MCP23008 devices and store them in a list
self.mcp_devices = [MCP23008(bus, addr) for addr in i2c_addresses]
except:
print("MCP23008 GPIO expanders not detected!")
# Find MCP23008 device by I2C address
def set_pin_from_address(self, i2c_address, pin, value):
if hasattr(self, "mcp_devices") and self.mcp_devices is not None:
for mcp in self.mcp_devices:
if mcp.address == i2c_address:
mcp.set_pin(pin,value)
return None