Skip to content

Updated class to use .magnetic property #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Usage Example
SENSOR = adafruit_mlx90393.MLX90393(I2C_BUS, gain=adafruit_mlx90393.GAIN_1X)

while True:
MX, MY, MZ = SENSOR.magnetic()
MX, MY, MZ = SENSOR.magnetic
print("[{}]".format(time.monotonic()))
print("X: {} uT".format(MX))
print("Y: {} uT".format(MY))
Expand Down
14 changes: 8 additions & 6 deletions adafruit_mlx90393.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def reset(self):
return self._status_last


@property
def read_data(self):
"""
Reads a single X/Y/Z sample from the magnetometer.
Expand All @@ -277,19 +278,20 @@ def read_data(self):
self._status_last, m_x, m_y, m_z = struct.unpack(">Bhhh", data)

# Return the raw int values if requested
return m_x, m_y, m_z
return (m_x, m_y, m_z)


@property
def magnetic(self):
"""
The processed magnetometer sensor values.
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
"""
m_x, m_y, m_z = self.read_data()
x, y, z = self.read_data

# Convert the raw integer values to uT based on gain and resolution
m_x *= _LSB_LOOKUP[self._gain_current][self._res_current][0]
m_y *= _LSB_LOOKUP[self._gain_current][self._res_current][0]
m_z *= _LSB_LOOKUP[self._gain_current][self._res_current][1]
x *= _LSB_LOOKUP[self._gain_current][self._res_current][0]
y *= _LSB_LOOKUP[self._gain_current][self._res_current][0]
z *= _LSB_LOOKUP[self._gain_current][self._res_current][1]

return m_x, m_y, m_z
return x, y, z
2 changes: 1 addition & 1 deletion examples/adafruit_mlx90393_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
SENSOR = adafruit_mlx90393.MLX90393(I2C_BUS, gain=adafruit_mlx90393.GAIN_1X)

while True:
MX, MY, MZ = SENSOR.magnetic()
MX, MY, MZ = SENSOR.magnetic
print("[{}]".format(time.monotonic()))
print("X: {} uT".format(MX))
print("Y: {} uT".format(MY))
Expand Down