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