Skip to content

Commit daeb114

Browse files
committed
Merge branch 'burtyb-master'
2 parents 42cadfc + bf375cd commit daeb114

3 files changed

Lines changed: 74 additions & 22 deletions

File tree

examples/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ Complete example for monitoring temperature and automatic fan control.
1919

2020
The LED should light up Green when the fan is enabled, and Red when it's not.
2121

22-
The script supports three arguments:
22+
The script supports these arguments:
23+
24+
* `--on-threshold N` the temperature at which to turn the fan on, in degrees C (default 65)
25+
* `--off-threshold N` the temperature at which to turn the fan off, in degrees C (default 55)
26+
* `--delay N` the delay between subsequent temperature readings, in seconds (default 2)
27+
* `--preempt` preemptively kick in the fan when the CPU frequency is raised (default off)
28+
29+
Deprecated arguments
2330

2431
* `--threshold N` the temperature at which the fan should turn on, in degrees C (default 55)
2532
* `--hysteresis N` the change in temperature needed to trigger a fan state change, in degrees C (default 5)
26-
* `--delay N` the delay between subsequent temperature readings, in seconds
27-
* `--preempt` preemptively kick in the fan when the CPU frequency is raised
2833

2934
You can use systemd or crontab to run this example as a fan controller service on your Pi.
3035

@@ -43,13 +48,13 @@ sudo systemctl stop pimoroni-fanshim.service
4348
If you need to change the threshold, hysteresis or delay you can add them as arguments to the installer:
4449

4550
```
46-
sudo ./install-service.sh --threshold <threshold> --hysteresis <hysteresis> --delay <delay>
51+
sudo ./install-service.sh --on-threshold 65 --off-threshold 55 --delay 2
4752
```
4853

4954
To enable CPU-frequency based control:
5055

5156
```
52-
sudo ./install-service.sh --threshold <threshold> --hysteresis <hysteresis> --delay <delay> --preempt
57+
sudo ./install-service.sh --on-threshold 65 --off-threshold 55 --delay 2 --preempt
5358
```
5459

5560
You can also add `--noled` to disable LED control and/or `--nobutton` to disable button input.

examples/automatic.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99

1010
parser = argparse.ArgumentParser()
11-
parser.add_argument('--threshold', type=float, default=55.0, help='Temperature threshold in degrees C to enable fan')
12-
parser.add_argument('--hysteresis', type=float, default=5.0, help='Distance from threshold before fan is disabled')
11+
parser.add_argument('--threshold', type=float, default=-1, help='Temperature threshold in degrees C to enable fan')
12+
parser.add_argument('--hysteresis', type=float, default=-1, help='Distance from threshold before fan is disabled')
13+
14+
parser.add_argument('--off-threshold', type=float, default=55.0, help='Temperature threshold in degrees C to enable fan')
15+
parser.add_argument('--on-threshold', type=float, default=65.0, help='Temperature threshold in degrees C to disable fan')
1316
parser.add_argument('--delay', type=float, default=2.0, help='Delay, in seconds, between temperature readings')
1417
parser.add_argument('--preempt', action='store_true', default=False, help='Monitor CPU frequency and activate cooling premptively')
1518
parser.add_argument('--verbose', action='store_true', default=False, help='Output temp and fan status messages')
@@ -21,7 +24,8 @@
2124

2225
def clean_exit(signum, frame):
2326
set_fan(False)
24-
fanshim.set_light(0, 0, 0)
27+
if not args.noled:
28+
fanshim.set_light(0, 0, 0)
2529
sys.exit(0)
2630

2731

@@ -65,6 +69,14 @@ def set_automatic(status):
6569
last_change = 0
6670

6771

72+
if args.threshold > -1 or args.hysteresis > -1:
73+
print("""
74+
The --threshold and --hysteresis parameters have been deprecated.
75+
Use --on-threshold and --off-threshold instead!
76+
""")
77+
sys.exit(1)
78+
79+
6880
fanshim = FanShim()
6981
fanshim.set_hold_time(1.0)
7082
fanshim.set_fan(False)
@@ -102,19 +114,31 @@ def held_handler():
102114

103115
signal.signal(signal.SIGTERM, clean_exit)
104116

117+
update_led(fanshim.get_fan())
118+
enable = False
119+
is_fast = False
120+
last_change = 0
121+
105122
try:
106123
while True:
107-
update_led(fanshim.get_fan())
108124
t = get_cpu_temp()
109125
f = get_cpu_freq()
126+
was_fast = is_fast
127+
is_fast = (int(f.current) == int(f.max))
110128
if args.verbose:
111129
print("Current: {:05.02f} Target: {:05.02f} Freq {: 5.02f} Automatic: {} On: {}".format(t, args.threshold, f.current / 1000.0, armed, enabled))
112-
if abs(last_change - t) > args.hysteresis and armed:
113-
enable = (t >= args.threshold)
114-
if args.preempt:
115-
enable = enable or (int(f.current) == int(f.max))
116-
if set_fan(enable):
117-
last_change = t
130+
131+
if args.preempt and is_fast and was_fast:
132+
enable = True
133+
elif armed:
134+
if t >= args.on_threshold:
135+
enable = True
136+
elif t <= args.off_threshold:
137+
enable = False
138+
139+
if set_fan(enable):
140+
last_change = t
141+
118142
time.sleep(args.delay)
119143
except KeyboardInterrupt:
120144
pass

examples/install-service.sh

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#!/bin/bash
2-
THRESHOLD=55
2+
ON_THRESHOLD=65
3+
OFF_THRESHOLD=55
34
HYSTERESIS=5
45
DELAY=2
56
PREEMPT="no"
67
POSITIONAL_ARGS=()
8+
NOLED="no"
9+
NOBUTTON="no"
10+
11+
OLD_THRESHOLD=""
12+
OLD_HYSTERESIS=""
13+
714
SERVICE_PATH=/etc/systemd/system/pimoroni-fanshim.service
815

916
if ! [ -f "/usr/bin/python3" ]; then
@@ -42,13 +49,23 @@ while [[ $# -gt 0 ]]; do
4249
fi
4350
shift
4451
;;
52+
--on-threshold)
53+
ON_THRESHOLD="$2"
54+
shift
55+
shift
56+
;;
57+
--off-threshold)
58+
OFF_THRESHOLD="$2"
59+
shift
60+
shift
61+
;;
4562
-t|--threshold)
46-
THRESHOLD="$2"
63+
OLD_THRESHOLD="error"
4764
shift
4865
shift
4966
;;
5067
-h|--hysteresis)
51-
HYSTERESIS="$2"
68+
OLD_HYSTERESIS="error"
5269
shift
5370
shift
5471
;;
@@ -67,6 +84,12 @@ set -- "${POSITIONAL_ARGS[@]}"
6784

6885
EXTRA_ARGS=""
6986

87+
if [ "$OLD_THRESHOLD" == "error" ] || [ "$OLD_HYSTERESIS" == "error" ]; then
88+
printf "The --threshold and --hysteresis parameters are deprecated.\n"
89+
printf "Use --off-threshold and --on-threshold instead.\n"
90+
exit 1
91+
fi
92+
7093
if [ "$PREEMPT" == "yes" ]; then
7194
EXTRA_ARGS+=' --preempt'
7295
fi
@@ -90,15 +113,15 @@ fi
90113

91114
cat << EOF
92115
Setting up with:
93-
Threshold: $THRESHOLD C
94-
Hysteresis: $HYSTERESIS C
116+
Off Threshold: $OFF_THRESHOLD C
117+
On Threshold: $ON_THRESHOLD C
95118
Delay: $DELAY seconds
96119
Preempt: $PREEMPT
97120
No LED: $NOLED
98121
No Button: $NOBUTTON
99122
100123
To change these options, run:
101-
sudo ./install-service --threshold <n> --hysteresis <n> --delay <n> (--preempt) (--noled) (--nobutton)
124+
sudo ./install-service.sh --off-threshold <n> --on-threshold <n> --delay <n> (--preempt) (--noled) (--nobutton)
102125
103126
Or edit: $SERVICE_PATH
104127
@@ -113,7 +136,7 @@ After=multi-user.target
113136
[Service]
114137
Type=simple
115138
WorkingDirectory=$(pwd)
116-
ExecStart=$(pwd)/automatic.py --threshold $THRESHOLD --hysteresis $HYSTERESIS --delay $DELAY $EXTRA_ARGS
139+
ExecStart=$(pwd)/automatic.py --on-threshold $ON_THRESHOLD --off-threshold $OFF_THRESHOLD --delay $DELAY $EXTRA_ARGS
117140
Restart=on-failure
118141
119142
[Install]

0 commit comments

Comments
 (0)