Skip to content

Commit bc817c4

Browse files
authored
Merge pull request #3 from bablokb/fix_write_command
fix _write_command(): use single i2c-transaction
2 parents 2c25099 + 68e72ba commit bc817c4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

adafruit_sen6x.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,20 @@ def _write_command(
264264
data: Optional list of 16-bit values to write
265265
execution_time: Time to wait after command (seconds)
266266
"""
267+
268+
buffer = struct.pack(">H", command)
267269
with self.i2c_device as i2c:
268270
# Write command (MSB first)
269-
i2c.write(struct.pack(">H", command))
270-
271-
# Write data if provided
272-
if data is not None:
271+
if data is None:
272+
i2c.write(buffer)
273+
else:
273274
for value in data:
274275
# Pack 16-bit value
275276
value_bytes = struct.pack(">H", value)
276277
# Calculate and append CRC
277278
crc = self._crc8(value_bytes)
278-
i2c.write(value_bytes + bytes([crc]))
279+
buffer += value_bytes + bytes([crc])
280+
i2c.write(buffer)
279281

280282
# Wait for command execution
281283
time.sleep(execution_time)

0 commit comments

Comments
 (0)