Skip to content

Commit

Permalink
Merge pull request #9 from tannewt/struct_opt
Browse files Browse the repository at this point in the history
Optimize UnaryStruct memory use for when its unused
  • Loading branch information
ladyada committed Feb 1, 2018
2 parents a4bc90c + 9ed054e commit 0d45dca
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions adafruit_register/i2c_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ class UnaryStruct:
"""
def __init__(self, register_address, struct_format):
self.format = struct_format
self.buffer = bytearray(1+struct.calcsize(self.format))
self.buffer[0] = register_address
self.address = register_address

def __get__(self, obj, objtype=None):
buf = bytearray(1+struct.calcsize(self.format))
buf[0] = self.address
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.readinto(self.buffer, start=1)
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])[0]
obj.i2c_device.write(buf, end=1, stop=False)
obj.i2c_device.readinto(buf, start=1)
return struct.unpack_from(self.format, buf, 1)[0]

def __set__(self, obj, value):
struct.pack_into(self.format, self.buffer, 1, value)
buf = bytearray(1+struct.calcsize(self.format))
buf[0] = self.address
struct.pack_into(self.format, buf, 1, value)
with obj.i2c_device:
obj.i2c_device.write(self.buffer)
obj.i2c_device.write(buf)

0 comments on commit 0d45dca

Please sign in to comment.