Skip to content

Commit

Permalink
changes import ustruct to struct (#5)
Browse files Browse the repository at this point in the history
changes import ustruct to struct
  • Loading branch information
mrmcwethy authored and tannewt committed Oct 3, 2017
1 parent 082f62b commit 859c44c
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 @@ -20,7 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import ustruct
try:
import struct
except ImportError:
import ustruct as struct

class Struct:
"""
Expand All @@ -34,17 +37,17 @@ class Struct:
"""
def __init__(self, register_address, struct_format):
self.format = struct_format
self.buffer = bytearray(1+ustruct.calcsize(self.format))
self.buffer = bytearray(1+struct.calcsize(self.format))
self.buffer[0] = register_address

def __get__(self, obj, objtype=None):
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.read_into(self.buffer, start=1)
return ustruct.unpack_from(self.format, memoryview(self.buffer)[1:])
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])

def __set__(self, obj, value):
ustruct.pack_into(self.format, self.buffer, 1, *value)
struct.pack_into(self.format, self.buffer, 1, *value)
with obj.i2c_device:
obj.i2c_device.write(self.buffer)

Expand All @@ -60,16 +63,16 @@ class UnaryStruct:
"""
def __init__(self, register_address, struct_format):
self.format = struct_format
self.buffer = bytearray(1+ustruct.calcsize(self.format))
self.buffer = bytearray(1+struct.calcsize(self.format))
self.buffer[0] = register_address

def __get__(self, obj, objtype=None):
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.read_into(self.buffer, start=1)
return ustruct.unpack_from(self.format, memoryview(self.buffer)[1:])[0]
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])[0]

def __set__(self, obj, value):
ustruct.pack_into(self.format, self.buffer, 1, value)
struct.pack_into(self.format, self.buffer, 1, value)
with obj.i2c_device:
obj.i2c_device.write(self.buffer)

0 comments on commit 859c44c

Please sign in to comment.