Skip to content

Commit 517bf46

Browse files
committed
Features added.
1 parent ab94c36 commit 517bf46

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

adafruit_mcp9600.py

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The MIT License (MIT)
22
#
33
# Copyright (c) 2019 Dan Cogliano for Adafruit Industries
4+
# Copyright (c) 2019 Kattni Rembor for Adafruit Industries
45
#
56
# Permission is hereby granted, free of charge, to any person obtaining a copy
67
# of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +27,7 @@
2627
CircuitPython driver for the MCP9600 thermocouple I2C amplifier
2728
2829
29-
* Author(s): Dan Cogliano
30+
* Author(s): Dan Cogliano, Kattni Rembor
3031
3132
Implementation Notes
3233
--------------------
@@ -63,8 +64,15 @@
6364
_REGISTER_VERSION = const(0x20)
6465

6566

66-
class BurstModeSamples:
67-
"""An enum-like class representing the options for number of burst mode samples."""
67+
class MCP9600:
68+
"""Interface to the MCP9600 thermocouple amplifier breakout"""
69+
70+
# Shutdown mode options
71+
NORMAL = 0b00
72+
SHUTDOWN = 0b01
73+
BURST = 0b10
74+
75+
# Burst mode sample options
6876
BURST_SAMPLES_1 = 0b000
6977
BURST_SAMPLES_2 = 0b001
7078
BURST_SAMPLES_4 = 0b010
@@ -74,17 +82,6 @@ class BurstModeSamples:
7482
BURST_SAMPLES_64 = 0b110
7583
BURST_SAMPLES_128 = 0b111
7684

77-
78-
class ShutdownModes:
79-
"""An enum-like class representing the options for shutdown modes"""
80-
NORMAL = 0b00
81-
SHUTDOWN = 0b01
82-
BURST = 0b10
83-
84-
85-
class MCP9600:
86-
"""Interface to the MCP9600 thermocouple amplifier breakout"""
87-
8885
# Alert temperature monitor options
8986
AMBIENT = 1
9087
THERMOCOUPLE = 0
@@ -101,35 +98,42 @@ class MCP9600:
10198
INTERRUPT = 1 # Interrupt clear option must be set when using this mode!
10299
COMPARATOR = 0
103100

104-
# TODO: Get clarification on this and potentially update names
105-
# Interrupt clear options
106-
CLEAR_INTERRUPT_FLAG = 1
107-
NORMAL_STATE = 0
108-
109101
# Ambient (cold-junction) temperature sensor resolution options
110102
AMBIENT_RESOLUTION_0_0625 = 0 # 0.0625 degrees Celsius
111103
AMBIENT_RESOLUTION_0_25 = 1 # 0.25 degrees Celsius
112104

113105
# STATUS - 0x4
114106
burst_complete = RWBit(0x4, 7)
107+
"""Burst complete"""
115108
temperature_update = RWBit(0x4, 6)
109+
"""Temperature update"""
116110
input_range = ROBit(0x4, 4)
111+
"""Input range"""
117112
alert_1 = ROBit(0x4, 0)
113+
"""Alert 1 status."""
118114
alert_2 = ROBit(0x4, 1)
115+
"""Alert 2 status."""
119116
alert_3 = ROBit(0x4, 2)
117+
"""Alert 3 status."""
120118
alert_4 = ROBit(0x4, 3)
119+
"""Alert 4 status."""
121120
# Device Configuration - 0x6
122121
ambient_resolution = RWBit(0x6, 7)
122+
"""Ambient (cold-junction) temperature resolution. Options are ``AMBIENT_RESOLUTION_0_0625``
123+
(0.0625 degrees Celsius) or ``AMBIENT_RESOLUTION_0_25`` (0.25 degrees Celsius)."""
123124
burst_mode_samples = RWBits(3, 0x6, 2)
125+
"""The number of samples taken during a burst in burst mode. Options are ``BURST_SAMPLES_1``,
126+
``BURST_SAMPLES_2``, ``BURST_SAMPLES_4``, ``BURST_SAMPLES_8``, ``BURST_SAMPLES_16``,
127+
``BURST_SAMPLES_32``, ``BURST_SAMPLES_64``, ``BURST_SAMPLES_128``."""
124128
shutdown_mode = RWBits(2, 0x6, 0)
129+
"""Shutdown modes. Options are ``NORMAL``, ``SHUTDOWN``, and ``BURST``."""
125130
# Alert 1 Configuration - 0x8
126131
_alert_1_interrupt_clear = RWBit(0x8, 7)
127132
_alert_1_monitor = RWBit(0x8, 4)
128133
_alert_1_temp_direction = RWBit(0x8, 3)
129134
_alert_1_state = RWBit(0x8, 2)
130135
_alert_1_mode = RWBit(0x8, 1)
131136
_alert_1_enable = RWBit(0x8, 0)
132-
"""Set to ``True`` to enable alert output. Set to ``False`` to disable alert output."""
133137
# Alert 2 Configuration - 0x9
134138
_alert_2_interrupt_clear = RWBit(0x9, 7)
135139
_alert_2_monitor = RWBit(0x9, 4)
@@ -194,8 +198,54 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS, tctype="K", tcfilter=0):
194198

195199
def alert_config(self, *, alert_number, alert_temp_source, alert_temp_limit, alert_hysteresis,
196200
alert_temp_direction, alert_mode, alert_state):
197-
""" For rising temps, hysteresis is below alert limit; falling temps, above alert limit
198-
Alert is enabled by default when set. To disable, use alert_disable method."""
201+
"""Configure a specified alert pin. Alert is enabled by default when alert is configured.
202+
To disable an alert pin, use ``alert_disable``.
203+
:param int alert_number: The alert pin number. Must be 1-4.
204+
:param alert_temp_source: The temperature source to monitor for the alert. Options are:
205+
``THERMOCOUPLE`` or ``AMBIENT``.
206+
:param float alert_temp_limit: The temperature in degrees Celsius at which the alert should
207+
trigger. For rising temperatures, the alert will trigger when the
208+
temperature rises above this limit. For falling temperatures, the
209+
alert will trigger when the temperature falls below this limit.
210+
:param float alert_hysteresis: The alert hysteresis range. Must be 0-255 degrees Celsius.
211+
For rising temperatures, the hysteresis is below alert limit. For
212+
falling temperatures, the hysteresis is above alert limit. See
213+
data-sheet for further information.
214+
:param alert_temp_direction: The direction the temperature must change to trigger the alert.
215+
Options are ``RISING`` (heating up) or ``FALLING`` (cooling
216+
down).
217+
:param alert_mode: The alert mode. Options are ``COMPARATOR`` or ``INTERRUPT``. If setting
218+
mode to ``INTERRUPT``, use ``alert_interrupt_clear`` to clear the
219+
interrupt flag.
220+
:param alert_state: Alert pin output state. Options are ``ACTIVE_HIGH`` or ``ACTIVE_LOW``.
221+
222+
223+
For example, to configure alert 1:
224+
225+
.. code-block:: python
226+
227+
import board
228+
import busio
229+
import digitalio
230+
import adafruit_mcp9600
231+
232+
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
233+
mcp = adafruit_mcp9600.MCP9600(i2c)
234+
alert_1 = digitalio.DigitalInOut(board.D5)
235+
alert_1.switch_to_input()
236+
237+
mcp.burst_mode_samples = mcp.BURST_SAMPLES_1
238+
mcp.ambient_resolution = mcp.AMBIENT_RESOLUTION_0_25
239+
mcp.alert_config(alert_number=1, alert_temp_source=mcp.THERMOCOUPLE,
240+
alert_temp_limit=25, alert_hysteresis=0,
241+
alert_temp_direction=mcp.RISING, alert_mode=mcp.COMPARATOR,
242+
alert_state=mcp.ACTIVE_LOW)
243+
244+
"""
245+
if alert_number not in (1, 2, 3, 4):
246+
raise ValueError("Alert pin number must be 1-4.")
247+
if not (0 <= alert_hysteresis < 256):
248+
raise ValueError("Hysteresis value must be 0-255.")
199249
setattr(self, '_alert_%d_monitor' % alert_number, alert_temp_source)
200250
setattr(self, '_alert_%d_temperature_limit' % alert_number, int(alert_temp_limit / 0.0625))
201251
setattr(self, '_alert_%d_hysteresis' % alert_number, alert_hysteresis)
@@ -205,9 +255,12 @@ def alert_config(self, *, alert_number, alert_temp_source, alert_temp_limit, ale
205255
setattr(self, '_alert_%d_enable' % alert_number, True)
206256

207257
def alert_disable(self, alert_number):
258+
"""Configuring an alert using ``alert_config()`` enables the specified alert by default.
259+
Use ``alert_disable`` to disable an alert pin."""
208260
setattr(self, '_alert_%d_enable' % alert_number, False)
209261

210262
def alert_interrupt_clear(self, alert_number, interrupt_clear=True):
263+
"""Setting ``interrupt_clear==True`` clears the interrupt flag."""
211264
setattr(self, '_alert_%d_interrupt_clear' % alert_number, interrupt_clear)
212265

213266
@property
@@ -225,7 +278,6 @@ def ambient_temperature(self):
225278
value -= 4096
226279
return value
227280

228-
229281
@property
230282
def temperature(self):
231283
""" Hot junction temperature in Celsius """

0 commit comments

Comments
 (0)