Skip to content

Commit deddbb0

Browse files
committed
Fix writing state.
1 parent 84b6089 commit deddbb0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

adafruit_max9744.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class MAX9744:
5555
default with the AD1, AD2 pins.
5656
"""
5757

58+
# Global buffer for writing data. This saves memory use and prevents
59+
# heap fragmentation. However this is not thread-safe or re-entrant by
60+
# design!
61+
_BUFFER = bytearray(1)
62+
5863
def __init__(self, i2c, *, address=_MAX9744_DEFAULT_ADDRESS):
5964
# This device doesn't use registers and instead just accepts a single
6065
# command string over I2C. As a result we don't use bus device or
@@ -64,13 +69,13 @@ def __init__(self, i2c, *, address=_MAX9744_DEFAULT_ADDRESS):
6469

6570
def _write(self, val):
6671
# Perform a write to update the amplifier state.
67-
val &= 0xFF
6872
try:
6973
# Make sure bus is locked before write.
7074
while not self._i2c.try_lock():
7175
pass
7276
# Build bytes to send to device with updated value.
73-
self._i2c.writeto(self._address, val)
77+
self._BUFFER[0] = val & 0xFF
78+
self._i2c.writeto(self._address, self._BUFFER)
7479
finally:
7580
# Ensure bus is always unlocked.
7681
self._i2c.unlock()

0 commit comments

Comments
 (0)