forked from romilly/quick2wire-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c-multibyte-read
More file actions
executable file
·37 lines (29 loc) · 1.1 KB
/
i2c-multibyte-read
File metadata and controls
executable file
·37 lines (29 loc) · 1.1 KB
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
#!/usr/bin/env python3
import quick2wire.i2c as i2c
address = 0x20
iodir_register = 0x00
iopol_register = 0x01
iocon_register = 0x05
gpio_register = 0x09
with i2c.I2CMaster() as bus:
# Ensure sequential addressing mode is on
bus.transaction(
i2c.writing_bytes(address, iocon_register, 0b00000000))
# Turn all pins to read mode
bus.transaction(
i2c.writing_bytes(address, iodir_register, 0b11111111))
# Alternate pin polarity
bus.transaction(
i2c.writing_bytes(address, iopol_register, 0b10101010))
# Read a single byte (the GPIO register)
gpio_state = bus.transaction(
i2c.writing_bytes(address, gpio_register),
i2c.reading(address, 1))[0][0]
# Read two bytes, the IODIR register and, thanks to sequential
# addressing mode, the next register, which is IOPOL
iodir_state, iopol_state = bus.transaction(
i2c.writing_bytes(address, iodir_register),
i2c.reading(address, 2))[0]
print("GPIO: ", bin(gpio_state)[2:])
print("IODIR:", bin(iodir_state)[2:])
print("IOPOL:", bin(iopol_state)[2:])