Skip to content

Commit aa5240c

Browse files
committed
sampling and filtering configurable after init
1 parent 5fc5474 commit aa5240c

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

adafruit_max31856.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,37 +151,58 @@ class MAX31856:
151151
# Tony says this isn't re-entrant or thread safe!
152152
_BUFFER = bytearray(4)
153153

154-
def __init__( # pylint: disable=too-many-arguments,invalid-name
155-
self,
156-
spi,
157-
cs,
158-
thermocouple_type=ThermocoupleType.K,
159-
sampling=1,
160-
filter_50hz=False,
161-
):
154+
def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
162155
self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1)
163156

164157
# assert on any fault
165158
self._write_u8(_MAX31856_MASK_REG, 0x0)
166159
# configure open circuit faults
167-
cr0_reg = _MAX31856_CR0_OCFAULT0
168-
# configure mains filtering: 60Hz (default) or 50Hz
169-
if filter_50hz:
170-
cr0_reg |= _MAX31856_CR0_50HZ
160+
self._write_u8(_MAX31856_CR0_REG, _MAX31856_CR0_OCFAULT0)
171161

172-
self._write_u8(_MAX31856_CR0_REG, cr0_reg)
162+
# set thermocouple type
163+
self._set_thermocouple_type(thermocouple_type)
173164

174-
# set number of samples
175-
if sampling not in _AVGSEL_CONSTS:
176-
raise ValueError("Sampling must be one of 1,2,4,8,16")
177-
avgsel = _AVGSEL_CONSTS[sampling]
178-
179-
# set thermocouple type and averaging mode
180-
# the CR1 reg is composed of AVGSEL + TCTYPE, so we set both at the same time
181-
conf_reg_1 = avgsel + int(thermocouple_type)
165+
def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType):
166+
# get current value of CR1 Reg
167+
conf_reg_1 = self._read_register(_MAX31856_CR1_REG, 1)[0]
168+
conf_reg_1 &= 0xF0 # mask off bottom 4 bits
169+
# add the new value for the TC type
170+
conf_reg_1 |= int(thermocouple_type) & 0x0F
171+
self._write_u8(_MAX31856_CR1_REG, conf_reg_1)
182172

173+
def set_sampling(self, num_samples: int):
174+
"""
175+
Sets the number of samples averaged by the sensor in each reading.
176+
:param int num_samples: number of samples (1, 2, 4, 8 or 16).
177+
"""
178+
# This option is set in bits 4-6 of register CR1.
179+
if num_samples not in _AVGSEL_CONSTS:
180+
raise ValueError("Num_samples must be one of 1,2,4,8,16")
181+
avgsel = _AVGSEL_CONSTS[num_samples]
182+
# get current value of CR1 Reg
183+
conf_reg_1 = self._read_register(_MAX31856_CR1_REG, 1)[0]
184+
conf_reg_1 &= 0b10001111 # clear bits 4-6
185+
# OR the AVGSEL bits (4-6)
186+
conf_reg_1 |= avgsel
183187
self._write_u8(_MAX31856_CR1_REG, conf_reg_1)
184188

189+
def select_mains_filtering(self, frequency: int = 60):
190+
"""
191+
Select mains filtering frequency (50/60Hz).
192+
Note that filtering is always enabled, and set to 60Hz by default.
193+
Use this function to instead filter 50Hz in 50Hz localities.
194+
:param int frequency: mains frequency (must be either 50 or 60)"""
195+
# this value is stored in bit 0 of register CR0.
196+
# get current value of CR0 Reg
197+
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0]
198+
if frequency == 50:
199+
conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit
200+
elif frequency == 60:
201+
conf_reg_0 &= ~_MAX31856_CR0_50HZ # clear the 50hz bit
202+
else:
203+
raise ValueError("Frequency must be 50 or 60")
204+
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
205+
185206
@property
186207
def temperature(self):
187208
"""Measure the temperature of the sensor and wait for the result.

0 commit comments

Comments
 (0)