Skip to content

Commit fc396af

Browse files
JosipKucigithub-actions[bot]
authored andcommitted
Apply Ruff formatting (MicroPython-safe)
1 parent 64a64d0 commit fc396af

21 files changed

+667
-524
lines changed
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# FILE: MCP23017-interrupts.py
1+
# FILE: MCP23017-interrupts.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example showing how to configure interrupts on both INTA and INTB
44
# ports. For this example you have to make the following connections:
55
# INTA/INTB(IO Expander) - GPIO32 (Dasduino board)
66
# The IO expander generates the interrupt and the board detects it
77
# WORKS WITH: IO expander MCP23017 breakout: www.solde.red/333007
8-
# LAST UPDATED: 2025-07-17
8+
# LAST UPDATED: 2025-07-17
99

1010
from machine import I2C, Pin
1111
from mcp23017 import *
@@ -21,36 +21,38 @@
2121
# Configure which pin will trigger interrupts (using pin 8 in this example)
2222
INTERRUPT_PIN = A7
2323
BUTTON_PIN = A7 # Using pin 8 as our interrupt source
24-
interruptHappened=0
24+
interruptHappened = 0
2525

2626
# Configure the button pin as input with pull-up and interrupt
27-
mcp.pin(BUTTON_PIN,
28-
mode=1, # input
29-
pullup=1, # enable pull-up resistor
30-
interrupt_enable=1, # enable interrupt
31-
interrupt_compare_default=1, # compare against default value
32-
default_value=1) # interrupt when pin goes low (button press)
27+
mcp.pin(
28+
BUTTON_PIN,
29+
mode=1, # input
30+
pullup=1, # enable pull-up resistor
31+
interrupt_enable=1, # enable interrupt
32+
interrupt_compare_default=1, # compare against default value
33+
default_value=1,
34+
) # interrupt when pin goes low (button press)
3335

3436
# Configure the MCP23017 interrupt settings
3537
mcp.config(
3638
interrupt_polarity=1, # active-high interrupt (set to 0 for active-low)
37-
interrupt_mirror=1, # mirror INTA/INTB (both pins show same interrupt)
38-
interrupt_open_drain=0 # push-pull output (set to 1 for open-drain)
39+
interrupt_mirror=1, # mirror INTA/INTB (both pins show same interrupt)
40+
interrupt_open_drain=0, # push-pull output (set to 1 for open-drain)
3941
)
4042

4143
# Set up the physical interrupt pin on the microcontroller
4244
# Connect this to either INTA or INTB pin of the MCP23017
4345
mcp_int_pin = Pin(32, Pin.IN, Pin.PULL_UP) # Using GPIO23, adjust for your board
4446

47+
4548
# Interrupt Service Routine (ISR)
4649
def mcp_interrupt_handler(pin):
4750
# Disable interrupts temporarily while we handle this one
4851
mcp.pin(BUTTON_PIN, interrupt_enable=0)
4952
# Trigger variable must use keyword global to not be viewed as local
5053
global interruptHappened
51-
interruptHappened=1
52-
53-
54+
interruptHappened = 1
55+
5456

5557
# Configure the interrupt handler
5658
mcp_int_pin.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=mcp_interrupt_handler)
@@ -61,13 +63,13 @@ def mcp_interrupt_handler(pin):
6163
# Main loop doesn't need to do anything until an interrupt happens
6264
while True:
6365
# Check if interrupt trigger was set to 1
64-
if interruptHappened==1:
66+
if interruptHappened == 1:
6567
# Inform user
6668
print("Interrupt detected!")
6769
# Small delay for debouncing
6870
time.sleep(0.5)
6971
# Reset trigger
70-
interruptHappened=0
72+
interruptHappened = 0
7173
# Re-enable the interrupt
7274
mcp.pin(BUTTON_PIN, interrupt_enable=1)
73-
time.sleep(0.01)
75+
time.sleep(0.01)
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# FILE: MCP23017-portConfigLists.py
1+
# FILE: MCP23017-portConfigLists.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
3-
# BRIEF: An example showing how to configure specific pins as input or output using
4-
# virtual pins in the form of a list, more information available here:
3+
# BRIEF: An example showing how to configure specific pins as input or output using
4+
# virtual pins in the form of a list, more information available here:
55
# https://github.com/mcauser/micropython-mcp23017/tree/master/examples/interfaces
66
# WORKS WITH: IO expander MCP23017 breakout: www.solde.red/333007
7-
# LAST UPDATED: 2025-07-17
7+
# LAST UPDATED: 2025-07-17
88

99
from machine import I2C, Pin
1010
from mcp23017 import *
@@ -16,9 +16,9 @@
1616

1717
# Pins range from A0 - A7 and B0 - B7
1818
# Set variables to hold pin values
19-
ledPin=A0
19+
ledPin = A0
2020

21-
buttonPin=B1
21+
buttonPin = B1
2222

2323
# Create MCP23017 instance with default address 0x27
2424
mcp = MCP23017()
@@ -27,12 +27,11 @@
2727
mcp[buttonPin].input(pull=1)
2828

2929

30-
3130
# Infinite loop
3231
while True:
3332
# If the button is pressed, turn on the LED
34-
if(mcp[buttonPin].value()==LOW):
33+
if mcp[buttonPin].value() == LOW:
3534
mcp[ledPin].output(val=HIGH)
3635
# Else it stays off
37-
else:
38-
mcp[ledPin].output(val=LOW)
36+
else:
37+
mcp[ledPin].output(val=LOW)
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# FILE: MCP23017-portConfigMethods.py
1+
# FILE: MCP23017-portConfigMethods.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
3-
# BRIEF: An example showing how to configure specific pins as input or output using
4-
# the method approach, more information available here:
3+
# BRIEF: An example showing how to configure specific pins as input or output using
4+
# the method approach, more information available here:
55
# https://github.com/mcauser/micropython-mcp23017/tree/master/examples/interfaces
66
# WORKS WITH: IO expander MCP23017 breakout: www.solde.red/333007
7-
# LAST UPDATED: 2025-07-17
7+
# LAST UPDATED: 2025-07-17
88

99
from machine import I2C, Pin
1010
from mcp23017 import *
@@ -16,23 +16,22 @@
1616

1717
# Pins range from A0 - A7 and B0 - B7
1818
# Set variables to hold pin values
19-
ledPin=A0
19+
ledPin = A0
2020

21-
buttonPin=B1
21+
buttonPin = B1
2222

2323
# Create MCP23017 instance with default address 0x27
2424
mcp = MCP23017()
2525

2626
# Configure the button pin to an input_pullup configuration
27-
mcp.pin(buttonPin,mode=INPUT_PULLUP)
28-
27+
mcp.pin(buttonPin, mode=INPUT_PULLUP)
2928

3029

3130
# Infinite loop
3231
while True:
3332
# If the button is pressed, turn on the LED
34-
if(mcp.pin(buttonPin)==LOW):
33+
if mcp.pin(buttonPin) == LOW:
3534
mcp.pin(pin=ledPin, mode=OUTPUT, value=HIGH)
3635
# Else it stays off
37-
else:
38-
mcp.pin(pin=ledPin, mode=OUTPUT, value=LOW)
36+
else:
37+
mcp.pin(pin=ledPin, mode=OUTPUT, value=LOW)

0 commit comments

Comments
 (0)