Skip to content

Commit 859c44c

Browse files
mrmcwethytannewt
authored andcommitted
changes import ustruct to struct (#5)
changes import ustruct to struct
1 parent 082f62b commit 859c44c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

adafruit_register/i2c_struct.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
import ustruct
23+
try:
24+
import struct
25+
except ImportError:
26+
import ustruct as struct
2427

2528
class Struct:
2629
"""
@@ -34,17 +37,17 @@ class Struct:
3437
"""
3538
def __init__(self, register_address, struct_format):
3639
self.format = struct_format
37-
self.buffer = bytearray(1+ustruct.calcsize(self.format))
40+
self.buffer = bytearray(1+struct.calcsize(self.format))
3841
self.buffer[0] = register_address
3942

4043
def __get__(self, obj, objtype=None):
4144
with obj.i2c_device:
4245
obj.i2c_device.write(self.buffer, end=1, stop=False)
4346
obj.i2c_device.read_into(self.buffer, start=1)
44-
return ustruct.unpack_from(self.format, memoryview(self.buffer)[1:])
47+
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])
4548

4649
def __set__(self, obj, value):
47-
ustruct.pack_into(self.format, self.buffer, 1, *value)
50+
struct.pack_into(self.format, self.buffer, 1, *value)
4851
with obj.i2c_device:
4952
obj.i2c_device.write(self.buffer)
5053

@@ -60,16 +63,16 @@ class UnaryStruct:
6063
"""
6164
def __init__(self, register_address, struct_format):
6265
self.format = struct_format
63-
self.buffer = bytearray(1+ustruct.calcsize(self.format))
66+
self.buffer = bytearray(1+struct.calcsize(self.format))
6467
self.buffer[0] = register_address
6568

6669
def __get__(self, obj, objtype=None):
6770
with obj.i2c_device:
6871
obj.i2c_device.write(self.buffer, end=1, stop=False)
6972
obj.i2c_device.read_into(self.buffer, start=1)
70-
return ustruct.unpack_from(self.format, memoryview(self.buffer)[1:])[0]
73+
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])[0]
7174

7275
def __set__(self, obj, value):
73-
ustruct.pack_into(self.format, self.buffer, 1, value)
76+
struct.pack_into(self.format, self.buffer, 1, value)
7477
with obj.i2c_device:
7578
obj.i2c_device.write(self.buffer)

0 commit comments

Comments
 (0)