Merge pull request #3 from bablokb/fix_write_command
Some checks failed
Build CI / test (push) Has been cancelled

fix _write_command(): use single i2c-transaction
This commit is contained in:
Liz 2025-06-21 07:23:00 -04:00 committed by GitHub
commit bc817c44c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -264,18 +264,20 @@ class SEN6x:
data: Optional list of 16-bit values to write data: Optional list of 16-bit values to write
execution_time: Time to wait after command (seconds) execution_time: Time to wait after command (seconds)
""" """
buffer = struct.pack(">H", command)
with self.i2c_device as i2c: with self.i2c_device as i2c:
# Write command (MSB first) # Write command (MSB first)
i2c.write(struct.pack(">H", command)) if data is None:
i2c.write(buffer)
# Write data if provided else:
if data is not None:
for value in data: for value in data:
# Pack 16-bit value # Pack 16-bit value
value_bytes = struct.pack(">H", value) value_bytes = struct.pack(">H", value)
# Calculate and append CRC # Calculate and append CRC
crc = self._crc8(value_bytes) crc = self._crc8(value_bytes)
i2c.write(value_bytes + bytes([crc])) buffer += value_bytes + bytes([crc])
i2c.write(buffer)
# Wait for command execution # Wait for command execution
time.sleep(execution_time) time.sleep(execution_time)