-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathktemp.py
More file actions
118 lines (94 loc) · 3.05 KB
/
Copy pathktemp.py
File metadata and controls
118 lines (94 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# Example: periodically monitor readout and chip temperature.
#
# Copyright (c) 2018 Petr Mánek.
# This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE".
#
# SPDX-License-Identifier: MIT
import sys
import time
from katherine import Device
# The katherine C extension raises one of these for a device call that
# failed with a library error code (see katherine.pyx's check_return_code());
# each is constructed from katherine_strerror(), so str(e) is already the
# message a caller wants to show.
KATHERINE_ERRORS = (TimeoutError, ValueError, MemoryError, RuntimeError)
def parse_args(argv):
show_readout = False
show_chip = False
period = 1.0 # seconds
address = None
i = 1
while i < len(argv):
arg = argv[i]
if arg == '--readout':
show_readout = True
elif arg == '--chip':
show_chip = True
elif arg == '--period':
i += 1
if i >= len(argv):
return None
try:
period = float(argv[i])
except ValueError:
return None
if period <= 0:
return None
elif address is None:
address = arg
else:
return None
i += 1
if address is None:
return None
if not show_readout and not show_chip:
show_readout = True
show_chip = True
return show_readout, show_chip, period, address
def poll_temperatures(device, show_readout, show_chip):
readout_temp = 0.0
chip_temp = 0.0
try:
if show_readout:
readout_temp = device.get_readout_temperature()
if show_chip:
chip_temp = device.get_sensor_temperature()
except KATHERINE_ERRORS as e:
return False, str(e)
if show_readout and show_chip:
print('readout: %.1f C, chip: %.1f C' % (readout_temp, chip_temp))
elif show_readout:
print('readout: %.1f C' % readout_temp)
else:
print('chip: %.1f C' % chip_temp)
sys.stdout.flush()
return True, None
def main():
args = parse_args(sys.argv)
if args is None:
print('Usage: ktemp [--readout] [--chip] [--period <seconds>] <address>', file=sys.stderr)
sys.exit(1)
show_readout, show_chip, period, address = args
try:
device = Device(address)
except KATHERINE_ERRORS:
print('ktemp: no response from %s' % address, file=sys.stderr)
sys.exit(1)
had_success = False
try:
while True:
ok, err = poll_temperatures(device, show_readout, show_chip)
if ok:
had_success = True
elif not had_success:
print('ktemp: no response from %s' % address, file=sys.stderr)
sys.exit(1)
else:
print('ktemp: no response from %s: %s' % (address, err))
sys.stdout.flush()
time.sleep(period)
except KeyboardInterrupt:
sys.exit(0)
if __name__ == '__main__':
main()