-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs_example1.py
84 lines (68 loc) · 2.55 KB
/
inputs_example1.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import smbus
import ctypes
from gpiozero import Button
from signal import pause
from time import sleep
import subprocess as sp
from mcp23008 import *
def clear_interrupt():
intcap = bus.read_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_INTCAP)
def init_mcp23008():
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_IODIR, MCP23008_IODIR_PIN_INPUT)
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_GPPU, MCP23008_GPPU_PIN_EN)
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_GPINTEN, 0xFF)
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_INTCON, 0x00)
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_IOCON, 0x3A)
bus.write_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_DEFVAL, 0x00)
#intf = bus.read_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_INTF)
#intcap = bus.read_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_INTCAP)
def interrupt_handling():
global mcp_gpio
global int_flag
mcp_gpio = bus.read_byte_data(MCP23008_DEFAULT_ADDRESS, MCP23008_REG_GPIO)
int_flag = 1
c_uint8 = ctypes.c_uint8
class Flags_bits( ctypes.LittleEndianStructure ):
_fields_ = [
("bit0", c_uint8, 1 ), # asByte & 1
("bit1", c_uint8, 1 ), # asByte & 2
("bit2", c_uint8, 1 ), # asByte & 4
("bit3", c_uint8, 1 ), # asByte & 8
("bit4", c_uint8, 1 ), # asByte & 16
("bit5", c_uint8, 1 ), # asByte & 32
("bit6", c_uint8, 1 ), # asByte & 64
("bit7", c_uint8, 1 ), # asByte & 128
]
class Flags( ctypes.Union ):
_anonymous_ = ("bit",)
_fields_ = [
("bit", Flags_bits ),
("asByte", c_uint8 )
]
def print_on_screen():
sp.call('clear',shell=True)
pins = Flags()
pins.asByte = mcp_gpio
print("Input_1: %i" % pins.bit0)
print("Input_2: %i" % pins.bit1)
print("Input_3: %i" % pins.bit2)
print("Input_4: %i" % pins.bit3)
print("Input_5: %i" % pins.bit4)
print("Input_6: %i" % pins.bit5)
print("Input_7: %i" % pins.bit6)
print("Input_8: %i" % pins.bit7)
# Get I2C bus
bus = smbus.SMBus(1)
init_mcp23008()
clear_interrupt()
int_flag = 0
interrupt = Button(27, pull_up=False, hold_time=0.001)
interrupt.when_pressed = interrupt_handling
interrupt.when_held = clear_interrupt
print("When button is pressed you'll see a message")
while True:
if int_flag == 1:
print_on_screen()
int_flag = 0
sleep(0.05)