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