Browse code

minor revision

Gandolf authored on 07/06/2021 21:04:25
Showing 1 changed files
... ...
@@ -71,6 +71,7 @@ class tmp102:
71 71
         if self.debugMode:
72 72
             # Read the TMP102 configuration register.
73 73
             data = self.getInfo()
74
+            print(self)
74 75
             print("configuration register: %s %s\n" % data)
75 76
     ## end def
76 77
 
Browse code

minor revision

Gandolf authored on 07/03/2021 01:42:15
Showing 1 changed files
... ...
@@ -57,17 +57,25 @@ class tmp102:
57 57
     # register.
58 58
     def __init__(self, sAddr=DEFAULT_BUS_ADDRESS,
59 59
                  sbus=DEFAULT_BUS_NUMBER,
60
-                 config=DEFAULT_CONFIG): 
60
+                 config=DEFAULT_CONFIG,
61
+                 debug=False): 
61 62
         # Instantiate a smbus object
62 63
         self.sensorAddr = sAddr
63 64
         self.bus = smbus.SMBus(sbus)
65
+        self.debugMode = debug
66
+ 
64 67
         # Initialize TMP102 sensor.  
65 68
         initData = [(config >> 8), (config & 0x00FF)]
66 69
         self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
70
+
71
+        if self.debugMode:
72
+            # Read the TMP102 configuration register.
73
+            data = self.getInfo()
74
+            print("configuration register: %s %s\n" % data)
67 75
     ## end def
68 76
 
69 77
     # Reads the configuration register (two bytes).
70
-    def status(self):
78
+    def getInfo(self):
71 79
         # Read configuration data
72 80
         config = self.bus.read_i2c_block_data(self.sensorAddr, CONFIG_REG, 2)
73 81
         configB1 = format(config[0], "08b")
... ...
@@ -99,6 +107,12 @@ class tmp102:
99 107
         # The temperature is returned in d11-d0, a two's complement,
100 108
         # 12 bit number.  This means that d11 is the sign bit.
101 109
         data=self.bus.read_i2c_block_data(self.sensorAddr, TEMP_REG, 2)
110
+
111
+        if self.debugMode:
112
+            dataB1 = format(data[0], "08b")
113
+            dataB2 = format(data[1], "08b")
114
+            print("Temperature Reg: %s %s" % (dataB1, dataB2))
115
+
102 116
         # Format into a 12 bit word.
103 117
         bData = ( data[0] << 8 | data[1] ) >> 4
104 118
         # Convert from two's complement to integer.
... ...
@@ -121,24 +135,18 @@ class tmp102:
121 135
 
122 136
 def testclass():
123 137
     # Initialize the smbus and TMP102 sensor.
124
-    ts1 = tmp102(0x48, 1)
125
-    # Read the TMP102 configuration register.
126
-    data = ts1.status()
127
-    print "configuration register: %s %s\n" % data
138
+    ts1 = tmp102(0x48, 1, debug=True)
128 139
     # Print out sensor values.
129 140
     bAl = False
130 141
     while True:
131
-        regdata = ts1.getTempReg()
132 142
         tempC = ts1.getTempC()
133 143
         tempF = ts1.getTempF()
134 144
         if bAl:
135 145
             bAl = False
136
-            print("\033[42;30mTemperature Reg: %s %s\033[m" % regdata)
137
-            print("\033[42;30m%6.2f%sC  %6.2f%s                 \033[m" % \
146
+            print("\033[42;30m%6.2f%sC  %6.2f%sF                 \033[m" % \
138 147
                   (tempC, DEGSYM, tempF, DEGSYM))
139 148
         else:
140 149
             bAl = True
141
-            print("Temperature Reg: %s %s" % regdata)
142 150
             print("%6.2f%sC  %6.2f%sF" % \
143 151
                   (tempC, DEGSYM, tempF, DEGSYM))
144 152
         time.sleep(2)
Browse code

added class methods for configuring sensor; added exception catching for json formatting code

Gandolf authored on 06/20/2021 00:40:59
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/python2
2 2
 #
3 3
 # Module: tmp102.py
4 4
 #
... ...
@@ -31,12 +31,23 @@ import smbus
31 31
 import time
32 32
 
33 33
 # Define constants
34
-DEGSYM = u'\xb0'
34
+DEGSYM = u'\xB0'
35 35
 
36 36
 # Define TMP102 Device Registers
37 37
 CONFIG_REG = 0x1
38 38
 TEMP_REG = 0x0
39 39
 
40
+# Define default sm bus address.
41
+DEFAULT_BUS_ADDRESS = 0x48
42
+DEFAULT_BUS_NUMBER = 1
43
+
44
+# Define the default sensor configuration.  See the TMP102 data sheet
45
+# for meaning of each bit.  The following bytes are written to the
46
+# configuration register
47
+#     byte 1: 01100000
48
+#     byte 2: 10100000
49
+DEFAULT_CONFIG = 0x60A0
50
+
40 51
 class tmp102:
41 52
 
42 53
     # Initialize the TMP102 sensor at the supplied address (default
... ...
@@ -44,16 +55,14 @@ class tmp102:
44 55
     # a new SMBus object for each instance of this class.  Writes
45 56
     # configuration data (two bytes) to the TMP102 configuration
46 57
     # register.
47
-    def __init__(self, sAddr=0x48, sbus=1): 
58
+    def __init__(self, sAddr=DEFAULT_BUS_ADDRESS,
59
+                 sbus=DEFAULT_BUS_NUMBER,
60
+                 config=DEFAULT_CONFIG): 
48 61
         # Instantiate a smbus object
49 62
         self.sensorAddr = sAddr
50 63
         self.bus = smbus.SMBus(sbus)
51
-        # Initialize TMP102 sensor.  See the data sheet for meaning of
52
-        # each bit.  The following bytes are written to the configuration
53
-        # register
54
-        #     byte 1: 01100000
55
-        #     byte 2: 10100000
56
-        initData = [0x60, 0xA0]
64
+        # Initialize TMP102 sensor.  
65
+        initData = [(config >> 8), (config & 0x00FF)]
57 66
         self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
58 67
     ## end def
59 68
 
... ...
@@ -95,7 +104,7 @@ class tmp102:
95 104
         # Convert from two's complement to integer.
96 105
         # If d11 is 1, the the number is a negative two's complement
97 106
         # number.  The absolute value is 2^12 - 1 minus the value
98
-        # of d10-d0 taken as a positive number.
107
+        # of d11-d0 taken as a positive number.
99 108
         if bData > 0x7FF:  # all greater values are negative numbers
100 109
             bData = -(0xFFF - bData)  # 0xFFF is 2^12 - 1
101 110
         # convert integer data to Celsius
... ...
@@ -124,14 +133,14 @@ def testclass():
124 133
         tempF = ts1.getTempF()
125 134
         if bAl:
126 135
             bAl = False
127
-            print "\033[42;30mTemperature Reg: %s %s\033[m" % regdata
128
-            print "\033[42;30m%6.2f%sC  %6.2f%s                 \033[m" % \
129
-                  (tempC, DEGSYM, tempF, DEGSYM)
136
+            print("\033[42;30mTemperature Reg: %s %s\033[m" % regdata)
137
+            print("\033[42;30m%6.2f%sC  %6.2f%s                 \033[m" % \
138
+                  (tempC, DEGSYM, tempF, DEGSYM))
130 139
         else:
131 140
             bAl = True
132
-            print "Temperature Reg: %s %s" % regdata
133
-            print "%6.2f%sC  %6.2f%sF" % \
134
-                  (tempC, DEGSYM, tempF, DEGSYM)
141
+            print("Temperature Reg: %s %s" % regdata)
142
+            print("%6.2f%sC  %6.2f%sF" % \
143
+                  (tempC, DEGSYM, tempF, DEGSYM))
135 144
         time.sleep(2)
136 145
     ## end while
137 146
 ## end def
Browse code

modify charts

Gandolf authored on 06/07/2021 20:50:02
Showing 1 changed files
1 1
old mode 100644
2 2
new mode 100755
... ...
@@ -66,6 +66,15 @@ class tmp102:
66 66
         return (configB1, configB2)
67 67
     ## end def
68 68
 
69
+    def getTempReg(self):
70
+        # Read temperature register and return raw binary data for test
71
+        # and debug.
72
+        data = self.bus.read_i2c_block_data(self.sensorAddr, TEMP_REG, 2)
73
+        dataB1 = format(data[0], "08b")
74
+        dataB2 = format(data[1], "08b")
75
+        return (dataB1, dataB2)
76
+    ## end def
77
+
69 78
     # Gets the temperature in binary format and converts to degrees
70 79
     # Celsius.
71 80
     def getTempC(self):
... ...
@@ -110,14 +119,17 @@ def testclass():
110 119
     # Print out sensor values.
111 120
     bAl = False
112 121
     while True:
122
+        regdata = ts1.getTempReg()
113 123
         tempC = ts1.getTempC()
114 124
         tempF = ts1.getTempF()
115 125
         if bAl:
116 126
             bAl = False
117
-            print "\033[42;30m%6.2f%sC  %6.2f%sF\033[m" % \
127
+            print "\033[42;30mTemperature Reg: %s %s\033[m" % regdata
128
+            print "\033[42;30m%6.2f%sC  %6.2f%s                 \033[m" % \
118 129
                   (tempC, DEGSYM, tempF, DEGSYM)
119 130
         else:
120 131
             bAl = True
132
+            print "Temperature Reg: %s %s" % regdata
121 133
             print "%6.2f%sC  %6.2f%sF" % \
122 134
                   (tempC, DEGSYM, tempF, DEGSYM)
123 135
         time.sleep(2)
Browse code

Initial release

Gandolf authored on 05/29/2021 20:13:39
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,129 @@
1
+#!/usr/bin/python
2
+#
3
+# Module: tmp102.py
4
+#
5
+# Description: This module acts as an interface between the TMP102 sensor
6
+# and downstream applications that use the data.  Class methods get
7
+# temperature data from the TMP102 sensor. It acts as a library module that
8
+# can be imported into and called from other Python programs.
9
+#
10
+# Copyright 2021 Jeff Owrey
11
+#    This program is free software: you can redistribute it and/or modify
12
+#    it under the terms of the GNU General Public License as published by
13
+#    the Free Software Foundation, either version 3 of the License, or
14
+#    (at your option) any later version.
15
+#
16
+#    This program is distributed in the hope that it will be useful,
17
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+#    GNU General Public License for more details.
20
+#
21
+#    You should have received a copy of the GNU General Public License
22
+#    along with this program.  If not, see http://www.gnu.org/license.
23
+#
24
+# Revision History
25
+#   * v10 released 01 June 2021 by J L Owrey; first release
26
+#
27
+#2345678901234567890123456789012345678901234567890123456789012345678901234567890
28
+
29
+# Import the I2C interface library
30
+import smbus
31
+import time
32
+
33
+# Define constants
34
+DEGSYM = u'\xb0'
35
+
36
+# Define TMP102 Device Registers
37
+CONFIG_REG = 0x1
38
+TEMP_REG = 0x0
39
+
40
+class tmp102:
41
+
42
+    # Initialize the TMP102 sensor at the supplied address (default
43
+    # address is 0x48), and supplied bus (default is 1).  Creates
44
+    # a new SMBus object for each instance of this class.  Writes
45
+    # configuration data (two bytes) to the TMP102 configuration
46
+    # register.
47
+    def __init__(self, sAddr=0x48, sbus=1): 
48
+        # Instantiate a smbus object
49
+        self.sensorAddr = sAddr
50
+        self.bus = smbus.SMBus(sbus)
51
+        # Initialize TMP102 sensor.  See the data sheet for meaning of
52
+        # each bit.  The following bytes are written to the configuration
53
+        # register
54
+        #     byte 1: 01100000
55
+        #     byte 2: 10100000
56
+        initData = [0x60, 0xA0]
57
+        self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
58
+    ## end def
59
+
60
+    # Reads the configuration register (two bytes).
61
+    def status(self):
62
+        # Read configuration data
63
+        config = self.bus.read_i2c_block_data(self.sensorAddr, CONFIG_REG, 2)
64
+        configB1 = format(config[0], "08b")
65
+        configB2 = format(config[1], "08b")
66
+        return (configB1, configB2)
67
+    ## end def
68
+
69
+    # Gets the temperature in binary format and converts to degrees
70
+    # Celsius.
71
+    def getTempC(self):
72
+        # Get temperature data from the sensor.
73
+        # TMP102 returns the data in two bytes formatted as follows
74
+        #        -------------------------------------------------
75
+        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
76
+        #        -------------------------------------------------
77
+        # byte 1 | d11 | d10 | d9  | d8  | d7  | d6  | d5  | d4  |
78
+        #        -------------------------------------------------
79
+        # byte 2 | d3  | d2  | d1  | d0  | 0   |  0  |  0  |  0  |
80
+        #        -------------------------------------------------
81
+        # The temperature is returned in d11-d0, a two's complement,
82
+        # 12 bit number.  This means that d11 is the sign bit.
83
+        data=self.bus.read_i2c_block_data(self.sensorAddr, TEMP_REG, 2)
84
+        # Format into a 12 bit word.
85
+        bData = ( data[0] << 8 | data[1] ) >> 4
86
+        # Convert from two's complement to integer.
87
+        # If d11 is 1, the the number is a negative two's complement
88
+        # number.  The absolute value is 2^12 - 1 minus the value
89
+        # of d10-d0 taken as a positive number.
90
+        if bData > 0x7FF:  # all greater values are negative numbers
91
+            bData = -(0xFFF - bData)  # 0xFFF is 2^12 - 1
92
+        # convert integer data to Celsius
93
+        tempC = bData * 0.0625 # LSB is 0.0625 deg Celsius
94
+        return tempC
95
+    ## end def
96
+
97
+    def getTempF(self):
98
+        # Convert Celsius to Fahrenheit using standard formula.
99
+        tempF = (9./5.) * self.getTempC() + 32.
100
+        return tempF
101
+    ## end def
102
+## end class
103
+
104
+def testclass():
105
+    # Initialize the smbus and TMP102 sensor.
106
+    ts1 = tmp102(0x48, 1)
107
+    # Read the TMP102 configuration register.
108
+    data = ts1.status()
109
+    print "configuration register: %s %s\n" % data
110
+    # Print out sensor values.
111
+    bAl = False
112
+    while True:
113
+        tempC = ts1.getTempC()
114
+        tempF = ts1.getTempF()
115
+        if bAl:
116
+            bAl = False
117
+            print "\033[42;30m%6.2f%sC  %6.2f%sF\033[m" % \
118
+                  (tempC, DEGSYM, tempF, DEGSYM)
119
+        else:
120
+            bAl = True
121
+            print "%6.2f%sC  %6.2f%sF" % \
122
+                  (tempC, DEGSYM, tempF, DEGSYM)
123
+        time.sleep(2)
124
+    ## end while
125
+## end def
126
+
127
+if __name__ == '__main__':
128
+    testclass()
129
+