Skip to content

Commit 83facdf

Browse files
committed
Changed the names of functions and variables to easier to read names.
Added Interrupt example using Rpi.GPIO.add_event_detect() and a callback.
1 parent b7edd4d commit 83facdf

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

adafruit_mcp230xx/mcp23017.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,32 @@ def io_control(self, val):
239239
self._write_u8(_MCP23017_IOCON, val)
240240

241241
@property
242-
def int_flg(self):
242+
def int_flag(self):
243243
"""Returns a list with the pin numbers that caused an interrupt
244244
port A ----> pins 0-7
245245
port B ----> pins 8-15
246246
"""
247247
intf = self._read_u16le(_MCP23017_INTFA)
248-
flg = [i for i in range(16) if intf & (1 << i)]
249-
return flg
248+
flags = [pin for pin in range(16) if intf & (1 << pin)]
249+
return flags
250250

251251
@property
252-
def int_flga(self):
253-
"""Returns a list of pin numbers that coused an interrupt in port A
252+
def int_flaga(self):
253+
"""Returns a list of pin numbers that caused an interrupt in port A
254254
pins: 0-7
255255
"""
256256
intfa = self._read_u8(_MCP23017_INTFA)
257-
flga = [i for i in range(8) if intfa & (1 << i)]
258-
return flga
257+
flags = [pin for pin in range(8) if intfa & (1 << pin)]
258+
return flags
259259

260260
@property
261-
def int_flgb(self):
262-
"""Returns a list of pin numbers that coused an interrupt in port B
261+
def int_flagb(self):
262+
"""Returns a list of pin numbers that caused an interrupt in port B
263263
pins: 8-15
264264
"""
265265
intfb = self._read_u8(_MCP23017_INTFB)
266-
flgb = [i+8 for i in range(8) if intfb & (1 << i)]
267-
return flgb
266+
flags = [pin+8 for pin in range(8) if intfb & (1 << pin)]
267+
return flags
268268

269269
def clear_ints(self):
270270
"""Clears interrupts by reading INTCAP."""
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import board
2+
import busio
3+
from RPi import GPIO
4+
from digitalio import DigitalInOut, Direction, Pull
5+
from adafruit_mcp230xx.mcp23017 import MCP23017
6+
from time import sleep
7+
8+
# Initialize the I2C bus:
9+
i2c = busio.I2C(board.SCL, board.SDA)
10+
11+
# Initialize the MCP23017 chip on the bonnet
12+
mcp = MCP23017(i2c)
13+
14+
# Optionally change the address of the device if you set any of the A0, A1, A2
15+
# pins. Specify the new address with a keyword parameter:
16+
#mcp = MCP23017(i2c, address=0x21) # MCP23017 w/ A0 set
17+
18+
# Make a list of all the pins (a.k.a 0-16)
19+
pins = []
20+
for pin in range(0, 16):
21+
pins.append(mcp.get_pin(pin))
22+
23+
# Set all the pins to input
24+
for pin in pins:
25+
pin.direction = Direction.INPUT
26+
pin.pull = Pull.UP
27+
28+
# Set up to check all the port B pins (pins 8-15) w/interrupts!
29+
mcp.interrupt_enable = 0xFFFF # Enable Interrupts in all pins
30+
# If intcon is set to 0's we will get interrupts on
31+
# both button presses and button releases
32+
mcp.interrupt_configuration = 0x0000 # interrupt on any change
33+
mcp.io_control = 0x44 # Interrupt as open drain and mirrored
34+
mcp.clear_ints() # Interrupts need to be cleared initially
35+
36+
# Or, we can ask to be notified CONTINUOUSLY if a pin goes LOW (button press)
37+
# we won't get an IRQ pulse when the pin is HIGH!
38+
#mcp.interrupt_configuration = 0xFFFF # notify pin value
39+
#mcp.default_value = 0xFFFF # default value is 'high' so notify whenever 'low'
40+
41+
def print_interrupt(port):
42+
'''Callback function to be called when an Interrupt occurs.'''
43+
flga = mcp.int_flaga
44+
flgb = mcp.int_flagb
45+
for pin in mcp.int_flag:
46+
print("Pin number: {} changed to: {}".format(pin, pins[pin].value ))
47+
mcp.clear_ints()
48+
49+
# connect either interrupt pin to the Raspberry pi's pin 17.
50+
# They were previously configured as mirrored.
51+
GPIO.setmode(GPIO.BCM)
52+
interrupt = 17
53+
GPIO.setup(interrupt, GPIO.IN, GPIO.PUD_UP) # Set up Pi's pin as input, pull up
54+
55+
# The add_event_detect fuction will call our print_interrupt callback function
56+
# every time an interrupt gets triggered.
57+
GPIO.add_event_detect(interrupt, GPIO.FALLING, callback=print_interrupt, bouncetime=10)
58+
59+
# The following lines are so the program runs for at least 60 seconds,
60+
# during that time it will detect any pin interrupt and print out the pin number
61+
# that changed state and its current state.
62+
# The program can be terminated using Ctrl+C. It doesn't matter how it
63+
# terminates it will always run GPIO.cleanup().
64+
try:
65+
print("When button is pressed you'll see a message")
66+
sleep(60) # You could run your main while loop here.
67+
print("Time's up. Finished!")
68+
finally:
69+
GPIO.cleanup()

0 commit comments

Comments
 (0)