-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathi2c-multibyte-read
More file actions
executable file
·39 lines (31 loc) · 1.16 KB
/
i2c-multibyte-read
File metadata and controls
executable file
·39 lines (31 loc) · 1.16 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
38
39
#!/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_buf = bytearray(1)
bus.transaction(
i2c.writing_bytes(address, gpio_register),
i2c.reading_into(address, gpio_buf))
# Read two bytes, the IODIR register and, thanks to sequential
# addressing mode, the next register, which is IOPOL
iodir_iopol_buf = bytearray(2)
bus.transaction(
i2c.writing_bytes(address, iodir_register),
i2c.reading_into(address, iodir_iopol_buf))
print("GPIO: ", bin(gpio_buf[0])[2:])
print("IODIR:", bin(iodir_iopol_buf[0])[2:])
print("IOPOL:", bin(iodir_iopol_buf[1])[2:])