Skip to content

Commit 7bbc5f6

Browse files
authored
Merge pull request #82 from druck13/extended_colours
Extended colours
2 parents c8ea18b + 3f741d4 commit 7bbc5f6

6 files changed

Lines changed: 96 additions & 44 deletions

File tree

examples/automatic.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111

1212
parser = argparse.ArgumentParser()
13-
parser.add_argument('--threshold', type=float, default=-1, help='Temperature threshold in degrees C to enable fan')
14-
parser.add_argument('--hysteresis', type=float, default=-1, help='Distance from threshold before fan is disabled')
15-
1613
parser.add_argument('--off-threshold', type=float, default=55.0, help='Temperature threshold in degrees C to enable fan')
1714
parser.add_argument('--on-threshold', type=float, default=65.0, help='Temperature threshold in degrees C to disable fan')
15+
parser.add_argument('--low-temp', type=float, default=None, help='Temperature at which the LED is greeen')
16+
parser.add_argument('--high-temp', type=float, default=None, help='Temperature for which LED is red')
1817
parser.add_argument('--delay', type=float, default=2.0, help='Delay, in seconds, between temperature readings')
1918
parser.add_argument('--preempt', action='store_true', default=False, help='Monitor CPU frequency and activate cooling premptively')
2019
parser.add_argument('--verbose', action='store_true', default=False, help='Output temp and fan status messages')
2120
parser.add_argument('--nobutton', action='store_true', default=False, help='Disable button input')
2221
parser.add_argument('--noled', action='store_true', default=False, help='Disable LED control')
2322
parser.add_argument('--brightness', type=float, default=255.0, help='LED brightness, from 0 to 255')
23+
parser.add_argument('--extended-colours', action='store_true', default=False, help='Extend LED colours for outside of normal low to high range')
2424

2525
args = parser.parse_args()
2626

@@ -35,13 +35,26 @@ def clean_exit(signum, frame):
3535
def update_led_temperature(temp):
3636
led_busy.acquire()
3737
temp = float(temp)
38-
temp -= args.off_threshold
39-
temp /= float(args.on_threshold - args.off_threshold)
40-
temp = max(0, min(1, temp))
41-
temp = 1.0 - temp
42-
temp *= 120.0
43-
temp /= 360.0
44-
r, g, b = [int(c * 255.0) for c in colorsys.hsv_to_rgb(temp, 1.0, args.brightness / 255.0)]
38+
if temp < args.low_temp and args.extended_colours:
39+
# Between minimum temp and low temp, set LED to blue through to green
40+
temp -= min_temp
41+
temp /= float(args.low_temp - min_temp)
42+
temp = max(0, temp)
43+
hue = (120.0 / 360.0) + ((1.0 - temp) * 120.0 / 360.0)
44+
elif temp > args.high_temp and args.extended_colours:
45+
# Between high temp and maximum temp, set LED to red through to magenta
46+
temp -= args.high_temp
47+
temp /= float(max_temp - args.high_temp)
48+
temp = min(1, temp)
49+
hue = 1.0 - (temp * 60.0 / 360.0)
50+
else:
51+
# In the normal low temp to high temp range, set LED to green through to red
52+
temp -= args.low_temp
53+
temp /= float(args.high_temp - args.low_temp)
54+
temp = max(0, min(1, temp))
55+
hue = (1.0 - temp) * 120.0 / 360.0
56+
57+
r, g, b = [int(c * 255.0) for c in colorsys.hsv_to_rgb(hue, 1.0, args.brightness / 255.0)]
4558
fanshim.set_light(r, g, b)
4659
led_busy.release()
4760

@@ -76,14 +89,6 @@ def set_automatic(status):
7689
last_change = 0
7790

7891

79-
if args.threshold > -1 or args.hysteresis > -1:
80-
print("""
81-
The --threshold and --hysteresis parameters have been deprecated.
82-
Use --on-threshold and --off-threshold instead!
83-
""")
84-
sys.exit(1)
85-
86-
8792
fanshim = FanShim()
8893
fanshim.set_hold_time(1.0)
8994
fanshim.set_fan(False)
@@ -93,18 +98,20 @@ def set_automatic(status):
9398
enable = False
9499
is_fast = False
95100
last_change = 0
101+
min_temp = 30
102+
max_temp = 85
96103
signal.signal(signal.SIGTERM, clean_exit)
97104

98105
if args.noled:
99106
led_busy.acquire()
100107
fanshim.set_light(0, 0, 0)
101108
led_busy.release()
102109

103-
t = get_cpu_temp()
104-
if t >= args.threshold:
105-
last_change = get_cpu_temp()
106-
set_fan(True)
110+
if args.low_temp is None:
111+
args.low_temp = args.off_threshold
107112

113+
if args.high_temp is None:
114+
args.high_temp = args.on_threshold
108115

109116
if not args.nobutton:
110117
@fanshim.on_release()

examples/button.py

100644100755
File mode changed.

examples/install-service.sh

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ POSITIONAL_ARGS=()
1010
NOLED="no"
1111
NOBUTTON="no"
1212
BRIGHTNESS=255
13+
EXTCOLOURS="no"
1314
PYTHON="python3"
1415
PIP="pip3"
1516
PSUTIL_MIN_VERSION="5.6.7"
1617

1718
ON_THRESHOLD_SET=false
1819
OFF_THRESHOLD_SET=false
1920

20-
OLD_THRESHOLD=""
21-
OLD_HYSTERESIS=""
22-
2321
SERVICE_PATH=/etc/systemd/system/pimoroni-fanshim.service
2422

25-
USAGE="sudo ./install-service.sh --off-threshold <n> --on-threshold <n> --delay <n> --brightness <n> --venv <python_virtual_environment> (--preempt) (--noled) (--nobutton)"
23+
USAGE="sudo ./install-service.sh --off-threshold <n> --on-threshold <n> --delay <n> --brightness <n> --low-temp <n> --high-temp <n> --venv <python_virtual_environment> (--preempt) (--noled) (--nobutton) (--extended-colours)"
2624

2725
# Convert Python path to absolute for systemd
2826
PYTHON=`type -P $PYTHON`
@@ -69,13 +67,13 @@ while [[ $# -gt 0 ]]; do
6967
shift
7068
shift
7169
;;
72-
-t|--threshold)
73-
OLD_THRESHOLD="error"
70+
-G|--low-temp)
71+
LOW_TEMP="$2"
7472
shift
7573
shift
7674
;;
77-
-h|--hysteresis)
78-
OLD_HYSTERESIS="error"
75+
-R|--high-temp)
76+
HIGH_TEMP="$2"
7977
shift
8078
shift
8179
;;
@@ -96,6 +94,15 @@ while [[ $# -gt 0 ]]; do
9694
shift
9795
shift
9896
;;
97+
-x|--extended-colours)
98+
if [ "$2" == "yes" ] || [ "$2" == "no" ]; then
99+
EXTCOLOURS="$2"
100+
shift
101+
else
102+
EXTCOLOURS="yes"
103+
fi
104+
shift
105+
;;
99106
*)
100107
if [[ $1 == -* ]]; then
101108
printf "Unrecognised option: $1\n";
@@ -132,12 +139,6 @@ set -- "${POSITIONAL_ARGS[@]}"
132139

133140
EXTRA_ARGS=""
134141

135-
if [ "$OLD_THRESHOLD" == "error" ] || [ "$OLD_HYSTERESIS" == "error" ]; then
136-
printf "The --threshold and --hysteresis parameters are deprecated.\n"
137-
printf "Use --off-threshold and --on-threshold instead.\n"
138-
exit 1
139-
fi
140-
141142
if [ "$PREEMPT" == "yes" ]; then
142143
EXTRA_ARGS+=' --preempt'
143144
fi
@@ -150,6 +151,10 @@ if [ "$NOBUTTON" == "yes" ]; then
150151
EXTRA_ARGS+=' --nobutton'
151152
fi
152153

154+
if [ "$EXTCOLOURS" == "yes" ]; then
155+
EXTRA_ARGS+=' --extended-colours'
156+
fi
157+
153158
if ! [ "$1" == "" ]; then
154159
if [ $ON_THRESHOLD_SET ]; then
155160
printf "Refusing to overwrite explicitly set On Threshold ($ON_THRESHOLD) with positional argument!\n"
@@ -168,16 +173,26 @@ if ! [ "$2" == "" ]; then
168173
(( OFF_THRESHOLD = ON_THRESHOLD - $2 ))
169174
fi
170175

176+
if [ "$LOW_TEMP" == "" ]; then
177+
LOW_TEMP=$OFF_THRESHOLD
178+
fi
179+
180+
if [ "$HIGH_TEMP" == "" ]; then
181+
HIGH_TEMP=$ON_THRESHOLD
182+
fi
171183

172184
cat << EOF
173185
Setting up with:
174-
Off Threshold: $OFF_THRESHOLD C
175-
On Threshold: $ON_THRESHOLD C
176-
Delay: $DELAY seconds
177-
Preempt: $PREEMPT
178-
Disable LED: $NOLED
179-
Disable Button: $NOBUTTON
180-
Brightness: $BRIGHTNESS
186+
Off Threshold: $OFF_THRESHOLD C
187+
On Threshold: $ON_THRESHOLD C
188+
Low Temp: $LOW_TEMP C
189+
High Temp: $HIGH_TEMP C
190+
Delay: $DELAY seconds
191+
Preempt: $PREEMPT
192+
Disable LED: $NOLED
193+
Disable Button: $NOBUTTON
194+
Brightness: $BRIGHTNESS
195+
Extended Colours: $EXTCOLOURS
181196
182197
To change these options, run:
183198
$USAGE
@@ -195,7 +210,7 @@ After=multi-user.target
195210
[Service]
196211
Type=simple
197212
WorkingDirectory=$(pwd)
198-
ExecStart=$PYTHON $(pwd)/automatic.py --on-threshold $ON_THRESHOLD --off-threshold $OFF_THRESHOLD --delay $DELAY --brightness $BRIGHTNESS $EXTRA_ARGS
213+
ExecStart=$PYTHON $(pwd)/automatic.py --on-threshold $ON_THRESHOLD --off-threshold $OFF_THRESHOLD --low-temp $LOW_TEMP --high-temp $HIGH_TEMP --delay $DELAY --brightness $BRIGHTNESS $EXTRA_ARGS
199214
Restart=on-failure
200215
201216
[Install]

examples/led.py

100644100755
File mode changed.

examples/led2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
from fanshim import FanShim
3+
import time
4+
import colorsys
5+
6+
fanshim = FanShim()
7+
fanshim.set_fan(False)
8+
9+
try:
10+
for i in range(-10, 21):
11+
print("Temp index %d" % i)
12+
# Normal temperature range 0 to 1, cold < 0, hot > 1
13+
temp = i/10.0
14+
if temp < 0.0:
15+
# hue of blue through to green
16+
hue = (120.0 / 360.0) - (temp * 120.0 / 360.0)
17+
elif temp > 1.0:
18+
# hue of red to through to magenta
19+
hue = ((1.0 - temp) * 60.0 / 360.0) + 1.0
20+
else:
21+
# hue of green through to red
22+
hue = (1.0 - temp) * 120.0 / 360.0
23+
24+
r, g, b = [int(c * 255.0) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
25+
fanshim.set_light(r, g, b)
26+
27+
time.sleep(1.0)
28+
29+
except KeyboardInterrupt:
30+
pass

examples/toggle.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)