Browse code

minor revision

Gandolf authored on 07/06/2021 21:04:25
Showing 1 changed files
... ...
@@ -70,8 +70,9 @@ class ina260:
70 70
 
71 71
         if self.debugMode:
72 72
             data = self.getInfo()
73
+            print(self)
73 74
             print("manufacturer ID: %s %s\n"\
74
-                  "configuration register: %s %s\n" % data)
75
+                  "INA260 configuration register: %s %s\n" % data)
75 76
     ## end def
76 77
 
77 78
     def getInfo(self):
Browse code

minor revision

Gandolf authored on 07/03/2021 01:42:15
Showing 1 changed files
... ...
@@ -57,16 +57,24 @@ class ina260:
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 INA260 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
+            data = self.getInfo()
73
+            print("manufacturer ID: %s %s\n"\
74
+                  "configuration register: %s %s\n" % data)
67 75
     ## end def
68 76
 
69
-    def status(self):
77
+    def getInfo(self):
70 78
         # Read manufacture identification data.
71 79
         mfcid = self.bus.read_i2c_block_data(self.sensorAddr, ID_REG, 2)
72 80
         mfcidB1 = format(mfcid[0], "08b")
... ...
@@ -91,7 +99,7 @@ class ina260:
91 99
         # Get the current data from the sensor.
92 100
         # INA260 returns the data in two bytes formatted as follows
93 101
         #        -------------------------------------------------
94
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
102
+        #    bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
95 103
         #        -------------------------------------------------
96 104
         # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
97 105
         #        -------------------------------------------------
... ...
@@ -100,6 +108,12 @@ class ina260:
100 108
         # The current is returned in d15-d0, a two's complement,
101 109
         # 16 bit number.  This means that d15 is the sign bit.        
102 110
         data=self.bus.read_i2c_block_data(self.sensorAddr, CUR_REG, 2)
111
+
112
+        if self.debugMode:
113
+            dataB1 = format(data[0], "08b")
114
+            dataB2 = format(data[1], "08b")
115
+            print("current register: %s %s" % (dataB1, dataB2))
116
+
103 117
         # Format into a 16 bit word.
104 118
         bdata = data[0] << 8 | data[1]
105 119
         # Convert from two's complement to integer.
... ...
@@ -126,7 +140,7 @@ class ina260:
126 140
         # Get the voltage data from the sensor.
127 141
         # INA260 returns the data in two bytes formatted as follows
128 142
         #        -------------------------------------------------
129
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
143
+        #    bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
130 144
         #        -------------------------------------------------
131 145
         # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
132 146
         #        -------------------------------------------------
... ...
@@ -134,6 +148,12 @@ class ina260:
134 148
         #        -------------------------------------------------
135 149
         # The voltage is returned in d15-d0 as an unsigned integer.
136 150
         data=self.bus.read_i2c_block_data(self.sensorAddr, VOLT_REG, 2)
151
+
152
+        if self.debugMode:
153
+            dataB1 = format(data[0], "08b")
154
+            dataB2 = format(data[1], "08b")
155
+            print("voltage register: %s %s" % (dataB1, dataB2))
156
+
137 157
         # Convert data to volts.
138 158
         volts = (data[0] << 8 | data[1]) * 0.00125 # LSB is 1.25 mV
139 159
         return volts
... ...
@@ -143,7 +163,7 @@ class ina260:
143 163
         # Get the wattage data from the sensor.
144 164
         # INA260 returns the data in two bytes formatted as follows
145 165
         #        -------------------------------------------------
146
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |