mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-15 02:52:41 +00:00
snippets/ev3/i2c_extra: update i2c
Reflect that we read one byte by default. Also allow a second way to send one byte, by specifying a register with no data.
This commit is contained in:
@@ -12,7 +12,7 @@ device = I2CDevice(Port.S2, 0xD2 >> 1)
|
||||
# Read one byte from the device.
|
||||
# For this device, we can read the Who Am I
|
||||
# register (0x0F) for the expected value: 211.
|
||||
if 211 not in device.read(0x0F, 1):
|
||||
if 211 not in device.read(0x0F):
|
||||
raise OSError("Device is not attached")
|
||||
|
||||
# To write data, create a bytes object of one
|
||||
|
||||
@@ -18,11 +18,18 @@ device.read(reg=None, length=1)
|
||||
# Read 0 bytes from no particular register:
|
||||
device.read(reg=None, length=0)
|
||||
|
||||
# I2C write operations consist of a register byte followed
|
||||
# by a series of data bytes. Depending on your device, you
|
||||
# can choose to skip the register or data as follows:
|
||||
|
||||
# Recommended for writing:
|
||||
device.write(reg=0x22, data=b'\x08')
|
||||
|
||||
# Write 1 byte to no particular register:
|
||||
device.write(reg=None, data=b'\x08')
|
||||
|
||||
# Write 0 bytes to a particular register:
|
||||
device.write(reg=0x08, data=None)
|
||||
|
||||
# Write 0 bytes to no particular register:
|
||||
device.write(reg=None, data=b'')
|
||||
device.write(reg=None, data=None)
|
||||
|
||||
Reference in New Issue
Block a user