Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4bac10c

Browse files
committed
Merge branch 'ps-jay-auto-detect-sensors-and-offset'
2 parents df48ddd + 4d33112 commit 4bac10c

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,6 @@ The `snmp_passpersist` mode is Python 2 only because the upstream package is not
282282
* Calibration code by Joji Monma (@GM3D on Github)
283283
* Munin plugin by Alexander Schier (@allo- on Github)
284284
* PyPI package work and rewrite to `libusb1` by James Stewart (@amorphic on Github)
285-
* Reduced kernel messages and support multiple sensors by Philip Jay (@ps-jay on Github)
285+
* Reduced kernel messages, support multiple sensors, and support TEMPer1F_V1.3 by Philip Jay (@ps-jay on Github)
286286
* Python 3 compatibility and rewrite of cli.py to use argparse by Will Furnass (@willfurnass on Github)
287287
* TEMPerV1.4 support by Christian von Roques (@roques on Github)

temperusb/cli.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ def parse_args():
2020
parser.add_argument("-s", "--sensor_ids", choices=['0', '1', 'all'],
2121
help="IDs of sensors to use on the device " +
2222
"(multisensor devices only)", default='0')
23-
parser.add_argument("-S", "--sensor_count", choices=[1, 2], type=int,
24-
help="Specify the number of sensors on the device",
25-
default='1')
23+
parser.add_argument("-S", "--sensor_count", type=int,
24+
help="Override auto-detected number of sensors on the device")
2625
args = parser.parse_args()
2726

28-
if args.sensor_ids == 'all':
29-
args.sensor_ids = range(args.sensor_count)
30-
else:
31-
args.sensor_ids = [int(args.sensor_ids)]
32-
3327
return args
3428

3529

@@ -47,8 +41,16 @@ def main():
4741
readings = []
4842

4943
for dev in devs:
50-
dev.set_sensor_count(args.sensor_count)
51-
readings.append(dev.get_temperatures(sensors=args.sensor_ids))
44+
if args.sensor_count is not None:
45+
# Override auto-detection from args
46+
dev.set_sensor_count(int(args.sensor_count))
47+
48+
if args.sensor_ids == 'all':
49+
sensors = range(dev.get_sensor_count())
50+
else:
51+
sensors = [int(args.sensor_ids)]
52+
53+
readings.append(dev.get_temperatures(sensors=sensors))
5254

5355
for i, reading in enumerate(readings):
5456
output = ''

temperusb/temper.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def __init__(self, device, sensor_count=1):
8686
if self._ports == None:
8787
self._ports = find_ports(device)
8888
self.set_calibration_data()
89+
self.set_sensor_count(self.lookup_sensor_count())
8990
LOGGER.debug('Found device | Bus:{0} Ports:{1}'.format(
9091
self._bus, self._ports))
9192

@@ -119,6 +120,35 @@ def set_calibration_data(self, scale=None, offset=None):
119120
else:
120121
raise RuntimeError("Must set both scale and offset, or neither")
121122

123+
def lookup_offset(self, sensor):
124+
"""
125+
Lookup the number of sensors on the device by product name.
126+
"""
127+
if self._device.product == 'TEMPer1F_V1.3':
128+
# Has only 1 sensor, and it's at offset = 4
129+
return 4
130+
131+
# All others follow this pattern - if not, contribute here: https://github.com/padelt/temper-python/issues
132+
# Sensor 0 = Offset 2
133+
# Sensor 1 = Offset 4
134+
return (sensor + 1) * 2
135+
136+
def lookup_sensor_count(self):
137+
"""
138+
Lookup the number of sensors on the device by product name.
139+
"""
140+
if self._device.product == 'TEMPer1F_V1.3':
141+
return 1
142+
143+
# All others are two - if not the case, contribute here: https://github.com/padelt/temper-python/issues
144+
return 2
145+
146+
def get_sensor_count(self):
147+
"""
148+
Get number of sensors on the device.
149+
"""
150+
return self._sensor_count
151+
122152
def set_sensor_count(self, count):
123153
"""
124154
Set number of sensors on the device.
@@ -127,7 +157,7 @@ def set_sensor_count(self, count):
127157
"""
128158
# Currently this only supports 1 and 2 sensor models.
129159
# If you have the 8 sensor model, please contribute to the
130-
# discussion here: https://github.com/padelt/temper-python/issues/19
160+
# discussion here: https://github.com/padelt/temper-python/issues
131161
if count not in [1, 2,]:
132162
raise ValueError('Only sensor_count of 1 or 2 supported')
133163

@@ -261,7 +291,7 @@ def get_temperatures(self, sensors=None):
261291

262292
# Interpret device response
263293
for sensor in _sensors:
264-
offset = (sensor + 1) * 2
294+
offset = self.lookup_offset(sensor)
265295
celsius = data[offset] + data[offset+1] / 256.0
266296
celsius = celsius * self._scale + self._offset
267297
results[sensor] = {

0 commit comments

Comments
 (0)