Skip to content

Commit a1a94ac

Browse files
committed
Add another example file for SPI devices.
Fix copyright information.
1 parent 91d2485 commit a1a94ac

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

adafruit_mcp230xx/mcp23s08.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
22
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2021 Red_M
34
#
45
# SPDX-License-Identifier: MIT
56

@@ -9,7 +10,7 @@
910
1011
CircuitPython module for the MCP23S08 I2C I/O extenders.
1112
12-
* Author(s): Tony DiCola
13+
* Author(s): Tony DiCola, Romy Bompart (2020), Red_M (2021)
1314
"""
1415

1516
from micropython import const

adafruit_mcp230xx/mcp23s17.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
22
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2021 Red_M
34
#
45
# SPDX-License-Identifier: MIT
56

adafruit_mcp230xx/mcp23sxx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
22
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2021 Red_M
34
#
45
# SPDX-License-Identifier: MIT
56

@@ -9,7 +10,7 @@
910
1011
CircuitPython module for the MCP23S17 SPI I/O extenders.
1112
12-
* Author(s): Tony DiCola, Romy Bompart (2020), Red_M (2021)
13+
* Author(s): Romy Bompart (2020), Red_M (2021)
1314
"""
1415

1516
from .mcp23xxx import MCP23XXX

adafruit_mcp230xx/mcp23xxx.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2019 Carter Nelson
1+
# SPDX-FileCopyrightText: 2021 Red_M
32
#
43
# SPDX-License-Identifier: MIT
54

@@ -9,7 +8,7 @@
98
109
CircuitPython module for the MCP23017, MCP23008 I2C and MCP23S17, MCP23S08 SPI I/O extenders.
1110
12-
* Author(s): Tony DiCola, Romy Bompart (2020), Red_M (2021)
11+
* Author(s): Red_M
1312
"""
1413

1514
from adafruit_bus_device import i2c_device, spi_device
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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()

examples/mcp23Sxx_simpletest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2021 Red_M
23
#
34
# SPDX-License-Identifier: MIT
45

0 commit comments

Comments
 (0)