Micropython port

This commit is contained in:
Pierrick C 2020-03-29 17:55:28 +02:00
parent 1809692fde
commit 701a8d6b7e

View File

@ -23,10 +23,11 @@
`adafruit_max31865` `adafruit_max31865`
==================================================== ====================================================
CircuitPython module for the MAX31865 platinum RTD temperature sensor. See MicroPython module for the MAX31865 platinum RTD temperature sensor. See
examples/simpletest.py for an example of the usage. examples/simpletest.py for an example of the usage.
* Author(s): Tony DiCola * Author(s): Tony DiCola
* micropython port: arofarn
Implementation Notes Implementation Notes
-------------------- --------------------
@ -44,16 +45,15 @@ Implementation Notes
**Software and Dependencies:** **Software and Dependencies:**
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: * Micropython firmware :
https://github.com/adafruit/circuitpython/releases https://micropython.org/download
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
""" """
import math import math
import time import time
from machine import Pin
from micropython import const from micropython import const
import adafruit_bus_device.spi_device as spi_device
__version__ = "0.0.0-auto.0" __version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX31865.git" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX31865.git"
@ -91,21 +91,29 @@ _RTD_B = -5.775e-7
class MAX31865: class MAX31865:
"""Driver for the MAX31865 thermocouple amplifier.""" """Driver for the MAX31865 thermocouple amplifier."""
# Class-level buffer for reading and writing data with the sensor. def __init__(
# This reduces memory allocations but means the code is not re-entrant or self, spi, cs, *, rtd_nominal=100, ref_resistor=430.0,
# thread safe! wires=2, filter_frequency=60
_BUFFER = bytearray(3) ):
def __init__(self, spi, cs, *, rtd_nominal=100, ref_resistor=430.0, wires=2):
self.rtd_nominal = rtd_nominal self.rtd_nominal = rtd_nominal
self.ref_resistor = ref_resistor self.ref_resistor = ref_resistor
self._device = spi_device.SPIDevice( self._spi = spi
spi, cs, baudrate=500000, polarity=0, phase=1 self._cs = cs
) self._cs.init(mode=Pin.OUT, pull=Pin.PULL_UP)
self._cs.value(1)
# Set 50Hz or 60Hz filter.
if filter_frequency not in (50, 60):
raise ValueError("Filter_frequency must be a value of 50 or 60!")
config = self._read_u8(_MAX31865_CONFIG_REG)
if filter_frequency == 50:
config |= _MAX31865_CONFIG_FILT50HZ
else:
config &= ~_MAX31865_CONFIG_FILT50HZ
# Set wire config register based on the number of wires specified. # Set wire config register based on the number of wires specified.
if wires not in (2, 3, 4): if wires not in (2, 3, 4):
raise ValueError("Wires must be a value of 2, 3, or 4!") raise ValueError("Wires must be a value of 2, 3, or 4!")
config = self._read_u8(_MAX31865_CONFIG_REG)
if wires == 3: if wires == 3:
config |= _MAX31865_CONFIG_3WIRE config |= _MAX31865_CONFIG_3WIRE
else: else:
@ -119,26 +127,30 @@ class MAX31865:
# pylint: disable=no-member # pylint: disable=no-member
def _read_u8(self, address): def _read_u8(self, address):
# Read an 8-bit unsigned value from the specified 8-bit address. # Read an 8-bit unsigned value from the specified 8-bit address.
with self._device as device: buf=bytearray(1)
self._BUFFER[0] = address & 0x7F self._cs.value(0)
device.write(self._BUFFER, end=1) self._spi.write(bytes([address & 0x7F]))
device.readinto(self._BUFFER, end=1) self._spi.readinto(buf, 1)
return self._BUFFER[0] self._cs.value(1)
return buf[0]
def _read_u16(self, address): def _read_u16(self, address):
# Read a 16-bit BE unsigned value from the specified 8-bit address. # Read a 16-bit BE unsigned value from the specified 8-bit address.
with self._device as device: buf=bytearray(2)
self._BUFFER[0] = address & 0x7F self._cs.value(0)
device.write(self._BUFFER, end=1) self._spi.write(bytes([address & 0x7F]))
device.readinto(self._BUFFER, end=2) self._spi.readinto(buf, 1)
return (self._BUFFER[0] << 8) | self._BUFFER[1] self._cs.value(1)
return (buf[0] << 8) | buf[1]
def _write_u8(self, address, val): def _write_u8(self, address, val):
# Write an 8-bit unsigned value to the specified 8-bit address. # Write an 8-bit unsigned value to the specified 8-bit address.
with self._device as device: buf=bytearray(2)
self._BUFFER[0] = (address | 0x80) & 0xFF buf[0] = (address | 0x80) & 0xFF
self._BUFFER[1] = val & 0xFF buf[1] = val & 0xFF
device.write(self._BUFFER, end=2) self._cs.value(0)
self._spi.write(buf)
self._cs.value(1)
# pylint: enable=no-member # pylint: enable=no-member