Skip to content

Commit 83ea49c

Browse files
authored
Merge pull request #27 from SAK917/master
Handle multiple HALLCONF values (Resolves #19)
2 parents 9a0d067 + 180f091 commit 83ea49c

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

adafruit_mlx90393.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,55 @@
9393
OSR_2 = 0x2
9494
OSR_3 = 0x3
9595

96-
_HALLCONF = const(0x0C) # Hall plate spinning rate adjust.
96+
# Hall plate spinning rate adjustment
97+
# Must be 0x0C (default) or 0x00
98+
_HALLCONF = const(0x0C)
9799

98100
STATUS_OK = 0x3
99101

100102
# The lookup table below allows you to convert raw sensor data to uT
101103
# using the appropriate (gain/resolution-dependant) lsb-per-uT
102104
# coefficient below. Note that the z axis has a different coefficient
103-
# than the x and y axis. Assumes HALLCONF = 0x0C!
105+
# than the x and y axis.
104106
_LSB_LOOKUP = (
105-
# 5x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
106-
((0.751, 1.210), (1.502, 2.420), (3.004, 4.840), (6.009, 9.680)),
107-
# 4x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
108-
((0.601, 0.968), (1.202, 1.936), (2.403, 3.872), (4.840, 7.744)),
109-
# 3x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
110-
((0.451, 0.726), (0.901, 1.452), (1.803, 2.904), (3.605, 5.808)),
111-
# 2.5x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
112-
((0.376, 0.605), (0.751, 1.210), (1.502, 2.420), (3.004, 4.840)),
113-
# 2x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
114-
((0.300, 0.484), (0.601, 0.968), (1.202, 1.936), (2.403, 3.872)),
115-
# 1.667x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
116-
((0.250, 0.403), (0.501, 0.807), (1.001, 1.613), (2.003, 3.227)),
117-
# 1.333x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
118-
((0.200, 0.323), (0.401, 0.645), (0.801, 1.291), (1.602, 2.581)),
119-
# 1x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
120-
((0.150, 0.242), (0.300, 0.484), (0.601, 0.968), (1.202, 1.936)),
107+
# HALLCONF = 0x0C (default)
108+
(
109+
# 5x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
110+
((0.751, 1.210), (1.502, 2.420), (3.004, 4.840), (6.009, 9.680)),
111+
# 4x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
112+
((0.601, 0.968), (1.202, 1.936), (2.403, 3.872), (4.840, 7.744)),
113+
# 3x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
114+
((0.451, 0.726), (0.901, 1.452), (1.803, 2.904), (3.605, 5.808)),
115+
# 2.5x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
116+
((0.376, 0.605), (0.751, 1.210), (1.502, 2.420), (3.004, 4.840)),
117+
# 2x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
118+
((0.300, 0.484), (0.601, 0.968), (1.202, 1.936), (2.403, 3.872)),
119+
# 1.667x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
120+
((0.250, 0.403), (0.501, 0.807), (1.001, 1.613), (2.003, 3.227)),
121+
# 1.333x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
122+
((0.200, 0.323), (0.401, 0.645), (0.801, 1.291), (1.602, 2.581)),
123+
# 1x gain: res0(xy)(z), res1(xy)(z), res2(xy)(z), res3(xy)(z)
124+
((0.150, 0.242), (0.300, 0.484), (0.601, 0.968), (1.202, 1.936)),
125+
),
126+
# HALLCONF = 0x00
127+
(
128+
# GAIN_SEL = 0, 5x gain
129+
((0.787, 1.267), (1.573, 2.534), (3.146, 5.068), (6.292, 10.137)),
130+
# GAIN_SEL = 1, 4x gain
131+
((0.629, 1.014), (1.258, 2.027), (2.517, 4.055), (5.034, 8.109)),
132+
# GAIN_SEL = 2, 3x gain
133+
((0.472, 0.760), (0.944, 1.521), (1.888, 3.041), (3.775, 6.082)),
134+
# GAIN_SEL = 3, 2.5x gain
135+
((0.393, 0.634), (0.787, 1.267), (1.573, 2.534), (3.146, 5.068)),
136+
# GAIN_SEL = 4, 2x gain
137+
((0.315, 0.507), (0.629, 1.014), (1.258, 2.027), (2.517, 4.055)),
138+
# GAIN_SEL = 5, 1.667x gain
139+
((0.262, 0.422), (0.524, 0.845), (1.049, 1.689), (2.097, 3.379)),
140+
# GAIN_SEL = 6, 1.333x gain
141+
((0.210, 0.338), (0.419, 0.676), (0.839, 1.352), (1.678, 2.703)),
142+
# GAIN_SEL = 7, 1x gain
143+
((0.157, 0.253), (0.315, 0.507), (0.629, 1.014), (1.258, 2.027)),
144+
),
121145
)
122146

123147
# Lookup table for conversion times for different filter and
@@ -442,9 +466,17 @@ def magnetic(self):
442466
"""
443467
x, y, z = self.read_data
444468

469+
# Check for valid HALLCONF value and set _LSB_LOOKUP index
470+
if _HALLCONF == 0x0C:
471+
hallconf_index = 0
472+
elif _HALLCONF == 0x00:
473+
hallconf_index = 1
474+
else:
475+
raise ValueError("Incorrect HALLCONF value, must be '0x0C' or '0x00'.")
476+
445477
# Convert the raw integer values to uT based on gain and resolution
446-
x *= _LSB_LOOKUP[self._gain_current][self._res_x][0]
447-
y *= _LSB_LOOKUP[self._gain_current][self._res_y][0]
448-
z *= _LSB_LOOKUP[self._gain_current][self._res_z][1]
478+
x *= _LSB_LOOKUP[hallconf_index][self._gain_current][self._res_x][0]
479+
y *= _LSB_LOOKUP[hallconf_index][self._gain_current][self._res_y][0]
480+
z *= _LSB_LOOKUP[hallconf_index][self._gain_current][self._res_z][1]
449481

450482
return x, y, z

0 commit comments

Comments
 (0)