Skip to content

Commit 9321607

Browse files
author
Bryan Siepert
committed
fix for ustruct/struct import error in Issue #1
1 parent 08c32be commit 9321607

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

adafruit_mpl3115a2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import time
3232

3333
from micropython import const
34-
import ustruct
34+
try:
35+
import ustruct as struct
36+
except ImportError:
37+
import struct
3538

3639
import adafruit_bus_device.i2c_device as i2c_device
3740

@@ -214,7 +217,7 @@ def altitude(self):
214217
# Reconstruct signed 32-bit altitude value (actually 24 bits shifted up
215218
# and then scaled down).
216219
self._BUFFER[3] = 0 # Top 3 bytes of buffer were read from the chip.
217-
altitude = ustruct.unpack('>i', self._BUFFER[0:4])[0]
220+
altitude = struct.unpack('>i', self._BUFFER[0:4])[0]
218221
# Scale down to meters.
219222
return altitude / 65535.0
220223

@@ -228,7 +231,7 @@ def temperature(self):
228231
# Read 2 bytes of data from temp register.
229232
self._read_into(_MPL3115A2_REGISTER_TEMP_MSB, self._BUFFER, count=2)
230233
# Reconstruct signed 12-bit value.
231-
temperature = ustruct.unpack('>h', self._BUFFER[0:2])[0]
234+
temperature = struct.unpack('>h', self._BUFFER[0:2])[0]
232235
temperature >>= 4
233236
# Scale down to degrees Celsius.
234237
return temperature / 16.0

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Uncomment the below if you use native CircuitPython modules such as
2020
# digitalio, micropython and busio. List the modules you use. Without it, the
2121
# autodoc module docs will fail to generate with a warning.
22-
autodoc_mock_imports = ["micropython", "adafruit_bus_device", "adafruit_bus_device.i2c_device", "ustruct"]
22+
autodoc_mock_imports = ["micropython", "adafruit_bus_device", "adafruit_bus_device.i2c_device", "struct"]
2323

2424
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/bus_device/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
2525

0 commit comments

Comments
 (0)