forked from romilly/quick2wire-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c-button-blink
More file actions
executable file
·36 lines (31 loc) · 888 Bytes
/
i2c-button-blink
File metadata and controls
executable file
·36 lines (31 loc) · 888 Bytes
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
#!/usr/bin/env python3
import sys
from contextlib import closing
from itertools import cycle
from time import sleep
import quick2wire.i2c as i2c
from quick2wire.parts.mcp23017 import MCP23017
from quick2wire.parts.mcp23x17 import In, Out
# usage: i2c-button-blink [led_pin button_pin]
# led_number defaults to 0
# button_pin defaults to 3
def the_pin_numbers():
if len(sys.argv) < 3:
return (0, 3)
return (int(sys.argv[1]), int(sys.argv[3]))
i2c_address = 0x20
pin_bank = 1
led_pin, button_pin = the_pin_numbers()
with closing(i2c.I2CMaster()) as bus:
chip = MCP23017(bus, 0x20)
chip.reset()
led = chip[pin_bank][led_pin]
led.direction = Out
button = chip[pin_bank][button_pin]
button.direction = In
try:
while True:
led.value = (not led.value) and button.value
sleep(0.5)
finally:
chip.reset()