Browse Source

Add mcp9808.py from github.com/kfricke/micropython-mcp9808

master
Bernhard Fröhlich 2 years ago
parent
commit
c715aa8d28
2 changed files with 152 additions and 0 deletions
  1. 30
    0
      LICENSE
  2. 122
    0
      mcp9808.py

+ 30
- 0
LICENSE View File

@@ -1,3 +1,6 @@
1
+bluething-sensor License:
2
+=========================
3
+
1 4
 MIT License
2 5
 
3 6
 Copyright (c) 2016 Bernhard Fröhlich
@@ -19,3 +22,30 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 22
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 23
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 24
 SOFTWARE.
25
+
26
+
27
+micropython-mcp9808 License:
28
+============================
29
+
30
+The MIT License (MIT)
31
+
32
+Copyright (c) 2016 Kai Fricke
33
+
34
+Permission is hereby granted, free of charge, to any person obtaining a copy
35
+of this software and associated documentation files (the "Software"), to deal
36
+in the Software without restriction, including without limitation the rights
37
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38
+copies of the Software, and to permit persons to whom the Software is
39
+furnished to do so, subject to the following conditions:
40
+
41
+The above copyright notice and this permission notice shall be included in all
42
+copies or substantial portions of the Software.
43
+
44
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50
+SOFTWARE.
51
+

+ 122
- 0
mcp9808.py View File

@@ -0,0 +1,122 @@
1
+from machine import I2C
2
+
3
+R_CFG    = const(1)
4
+R_B_UP   = const(2)
5
+R_B_LOW  = const(3)
6
+R_B_CRIT = const(4)
7
+R_A_TEMP = const(5)
8
+R_M_ID   = const(6)
9
+R_D_ID   = const(7)
10
+R_T_RES  = const(8)
11
+
12
+T_RES_MIN = const(0)
13
+T_RES_LOW = const(1)
14
+T_RES_AVG = const(2)
15
+T_RES_MAX = const(3)
16
+
17
+class MCP9808(object):
18
+    """
19
+    This class implements an interface to the MCP9808 temprature sensor from
20
+    Microchip.
21
+    """
22
+
23
+    def __init__(self, i2c=None, addr=0x18):
24
+        """
25
+        Initialize a sensor object on the given I2C bus and accessed by the
26
+        given address.
27
+        """
28
+        if i2c == None or i2c.__class__ != I2C:
29
+            raise ValueError('I2C object needed as argument!')
30
+        self._i2c = i2c
31
+        self._addr = addr
32
+        self._check_device()
33
+
34
+    def _send(self, buf):
35
+        """
36
+        Sends the given bufer object over I2C to the sensor.
37
+        """
38
+        self._i2c.send(buf, self._addr)
39
+
40
+    def _recv(self, n):
41
+        """
42
+        Read bytes from the sensor using I2C. The byte count must be specified
43
+        as an argument.
44
+        Returns a bytearray containing the result.
45
+        """
46
+        return self._i2c.recv(n, self._addr)
47
+
48
+    def _check_device(self):
49
+        """
50
+        Tries to identify the manufacturer and device identifiers.
51
+        """
52
+        self._send(R_M_ID)
53
+        self._m_id = self._recv(2)
54
+        if not self._m_id == b'\x00T':
55
+            raise Exception("Invalid manufacturer ID: '%s'!" % self._m_id)
56
+        self._send(R_D_ID)
57
+        self._d_id = self._recv(2)
58
+        if not self._d_id == b'\x04\x00':
59
+            raise Exception("Invalid device or revision ID: '%s'!" % self._d_id)
60
+
61
+    def set_shutdown_mode(self, shdn=True):
62
+        """
63
+        Set sensor into shutdown mode to draw less than 1 uA and disable
64
+        continous temperature conversion.
65
+        """
66
+        if shdn.__class__ != bool:
67
+            raise ValueError('Boolean argument needed to set shutdown mode!')
68
+        self._send(R_CFG)
69
+        cfg = self._recv(2)
70
+        b = bytearray()
71
+        b.append(R_CFG)
72
+        if shdn:
73
+            b.append(cfg[0] | 1)
74
+        else:
75
+            b.append(cfg[0] & ~1)
76
+        b.append(cfg[1])
77
+        self._send(b)
78
+
79
+    def get_temp(self):
80
+        """
81
+        Read temperature in degree celsius and return float value.
82
+        """
83
+        self._send(R_A_TEMP)
84
+        raw = self._recv(2)
85
+        u = (raw[0] & 0x0f) << 4
86
+        l = raw[1] / 16
87
+        if raw[0] & 0x10 == 0x10:
88
+            temp = 256 - (u + l)
89
+        else:
90
+            temp = u + l
91
+        return temp
92
+
93
+    def get_temp_int(self):
94
+        """
95
+        Read a temperature in degree celsius and return a tuple of two parts.
96
+        The first part is the decimal patr and the second the fractional part
97
+        of the value.
98
+        This method does avoid floating point arithmetic completely to support
99
+        plattforms missing float support.
100
+        """
101
+        self._send(R_A_TEMP)
102
+        raw = self._recv(2)
103
+        u = (raw[0] & 0xf) << 4
104
+        l = raw[1] >> 4
105
+        if raw[0] & 0x10 == 0x10:
106
+            temp = 256 - (u + l)
107
+            frac = 256 - (raw[1] & 0x0f) * 100 >> 4
108
+        else:
109
+            temp = u + l
110
+            frac = (raw[1] & 0x0f) * 100 >> 4
111
+        return temp, frac
112
+
113
+    def set_resolution(self, r):
114
+        """
115
+        Sets the temperature resolution.
116
+        """
117
+        if r not in [T_RES_MIN, T_RES_LOW, T_RES_AVG, T_RES_MAX]:
118
+            raise ValueError('Invalid temperature resolution requested!')
119
+        b = bytearray()
120
+        b.append(R_T_RES)
121
+        b.append(r)
122
+        self._send(b)

Loading…
Cancel
Save