forked from MichaIng/DietPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdietpi-fan_control
448 lines (321 loc) · 12.2 KB
/
dietpi-fan_control
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash
{
#////////////////////////////////////
# DietPi CPU fan PWM script
#
#////////////////////////////////////
# Created by MichaIng / [email protected] / dietpi.com
#
#////////////////////////////////////
#
# Info:
# - Location: /boot/dietpi/dietpi-fan_control
# - Allows to toggle CPU fan temp control:
# - Set trip point temps, e.g. 35°C, 50°C, 65°C
# - Attach fan speeds to trip points, e.g. 0%, 45%, 70%, 95%
# - Example result:
# - Fan off below 35°C
# - 45% fan speed at 35°C - 50°C
# - 70% fan speed at 50°C - 65°C
# - 95% fan speed above 65°C
# or
# - Set static fan speed, e.g. 95% for permanent high load OC setups
# - ToDo: Runs at boot (/boot/dietpi/preboot) to auto-apply chosen settings.
# - ToDo: Can be accessed from within "dietpi-config"
#
# Usage:
AVAIABLE_COMMANDS="
Available commands:
dietpi-fan_control => Interactive whiptail menu to choose settings and create $FP_SETTINGS.
dietpi-fan_control 1 => Non-interactively apply settings from $FP_SETTINGS.
"
#////////////////////////////////////
# Import DietPi-Globals ---------------------------------------------------------------
. /boot/dietpi/func/dietpi-globals
readonly G_PROGRAM_NAME='DietPi-Fan_control'
G_CHECK_ROOT_USER
G_INIT
# Import DietPi-Globals ---------------------------------------------------------------
# File paths
readonly FP_SETTINGS='/boot/dietpi/.dietpi-fan_control'
readonly FP_TEMP_CONTROLLED='/sys/devices/platform/pwm-fan/hwmon/hwmon0/automatic'
readonly FP_TRIP_TEMPS='/sys/devices/virtual/thermal/thermal_zone0/trip_point_0_temp'
readonly FP_TRIP_SPEEDS='/sys/devices/platform/pwm-fan/hwmon/hwmon0/fan_speed'
readonly FP_STATIC_SPEED='/sys/devices/platform/pwm-fan/hwmon/hwmon0/pwm1'
# Exit path for unsupported devices
if ! [[ -e $FP_TEMP_CONTROLLED && -e $FP_TRIP_TEMPS && -e $FP_TRIP_SPEEDS && -e $FP_STATIC_SPEED ]]; then
# DEBUG
echo "$FP_TEMP_CONTROLLED: $(<$FP_TEMP_CONTROLLED)"
echo "$FP_TRIP_TEMPS: $(<$FP_TRIP_TEMPS)"
echo "$FP_TRIP_SPEEDS: $(<$FP_TRIP_SPEEDS)"
echo "$FP_STATIC_SPEED: $(<$FP_STATIC_SPEED)"
G_DIETPI-NOTIFY 1 'CPU fan control is not available on your device. Aborting...'
exit 1
fi
# Grab valid input
INPUT=0
if [[ $* ]]; then
if [[ $* == 1 ]]; then
INPUT=$*
else
G_DIETPI-NOTIFY 1 "Invalid input argument ($*) found. Aborting...\n$AVAIABLE_COMMANDS"
exit 1
fi
fi
#-----------------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------------
# Read currently applied fan control values
Read_Control_Files(){
# Read current fan control toggle
TEMP_CONTROLLED_CURRENT=$(<$FP_TEMP_CONTROLLED)
# Read current trip point temperatures, assuming values of CPU0 for all CPUs
local fp_target i=0
TRIP_TEMPS_CURRENT=
for fp_target in /sys/devices/virtual/thermal/thermal_zone0/trip_point_*_temp
do
# Convert "XY000" to XY°C
TRIP_TEMPS_CURRENT+=" $(( $(</sys/devices/virtual/thermal/thermal_zone0/trip_point_${i}_temp) / 1000 ))"
((i++))
done
# Remove leading white space
TRIP_TEMPS_CURRENT=${TRIP_TEMPS_CURRENT# }
# Read current trip point fan speeds
TRIP_SPEEDS_CURRENT=
for i in $(<$FP_TRIP_SPEEDS)
do
# Convert 0-255 to 0-100%
TRIP_SPEEDS_CURRENT+=" $(( $i * 100 / 255 ))"
done
# Remove leading white space
TRIP_SPEEDS_CURRENT=${TRIP_SPEEDS_CURRENT# }
# Read current static fan speed
# - Convert 0-255 to 0-100%
STATIC_SPEED_CURRENT=$(( $(<$FP_STATIC_SPEED) * 100 / 255 ))
}
# Read settings from file
# - $TEMP_CONTROLLED=0|1; On|Off
# - $TRIP_TEMPS='XX YY ZZ'; XX°C YY°C ZZ°C
# - $TRIP_SPEEDS='AAA BBB CCC DDD'; AAA*100/255 %...
# - $STATIC_SPEED=EEE; EEE*100/255 %
Read_Settings(){ . $FP_SETTINGS; }
# Verify valid settings, before applying
Verify_Settings(){
# $TEMP_CONTROLLED is expected to be either 0 or 1.
if [[ $TEMP_CONTROLLED != [01] ]]; then
G_DIETPI-NOTIFY 1 "Invalid setting: \$TEMP_CONTROLLED=$TEMP_CONTROLLED"
return 1
elif (( $TEMP_CONTROLLED )); then
# $TRIP_SPEEDS and $TRIP_TEMPS are expected to follow scheme: [0-9]+[[:blank:]][0-9]+...
# $TRIP_TEMPS need to match than $TRIP_POINT_COUNT: Amount of actual CPU0 thermal zone control files
# $TRIP_SPEEDS values need to be exactly one more: Min speed + one each temp trip
if ! [[ $TRIP_TEMPS =~ ^[0-9[:blank:]]+$ && $TRIP_SPEEDS =~ ^[0-9[:blank:]]+$ ]] ||
! (( $(wc -w <<< "$TRIP_TEMPS") == $TRIP_POINT_COUNT && $(wc -w <<< "$TRIP_SPEEDS") == $TRIP_POINT_COUNT + 1 )); then
G_DIETPI-NOTIFY 1 "Invalid settings: \$TRIP_TEMPS=$TRIP_TEMPS; \$TRIP_SPEEDS=$TRIP_SPEEDS"
return 1
fi
else
# $STATIC_SPEED is expected to be an integer.
if ! disable_error=1 G_CHECK_VALIDINT "$STATIC_SPEED"; then
G_DIETPI-NOTIFY 1 "Invalid setting: \$STATIC_SPEED=$STATIC_SPEED"
return 1
fi
fi
G_DIETPI-NOTIFY 0 'Valid settings verified' # DEBUG
return 0
}
# Write settings to file
Write_Settings(){
# Write fan control toggle to settings file
echo "TEMP_CONTROLLED=$TEMP_CONTROLLED" > $FP_SETTINGS
if (( $TEMP_CONTROLLED )); then
# Write trip points and attached temperatures to settings file
cat << _EOF_ >> $FP_SETTINGS
TRIP_TEMPS='$TRIP_TEMPS'
TRIP_SPEEDS='$TRIP_SPEEDS'
_EOF_
else
# - Write static fan speed to settings file
echo "STATIC_SPEED=$STATIC_SPEED" >> $FP_SETTINGS
fi
}
# Apply chosen settings to control files
Apply_Settings(){
#-----------------------------------------------------------------------------------
G_DIETPI-NOTIFY 3 $G_PROGRAM_NAME 'Applying CPU fan settings'
#-----------------------------------------------------------------------------------
# Apply fan control toggle
echo "$TEMP_CONTROLLED" > $FP_TEMP_CONTROLLED
if (( $TEMP_CONTROLLED )); then
local i=0
# Apply trip point temps
local j=0 temp=0 fp_target
for ((i=0; i<$G_HW_CPU_CORES; i++))
do
j=0
for temp in $TRIP_TEMPS
do
fp_target="/sys/devices/virtual/thermal/thermal_zone${i}/trip_point_${j}_temp"
# DEBUG: Check for file existence, otherwise break for current CPU
[[ ! -e $fp_target ]] && G_DIETPI-NOTIFY 1 "Trip point temperature file missing: $fp_target. Skipping..." && break
# Convert XY°C to XY000
echo "${temp}000" > $fp_target
((j++))
done
done
# Apply trip point fan speeds
local trip_speeds_target
for i in $TRIP_SPEEDS
do
# Convert 0-100% to 0-255
trip_speeds_target+=" $(( $i * 255 / 100 ))"
done
# - Remove leading white space
trip_speeds_target=${trip_speeds_target# }
echo "$trip_speeds_target" > $FP_TRIP_SPEEDS
else
# Apply static fan speed
# - Convert 0-100% to 0-255
echo $(( $STATIC_SPEED * 255 / 100 )) > $FP_STATIC_SPEED
fi
}
#-----------------------------------------------------------------------------------
# Menus
#-----------------------------------------------------------------------------------
Reset_Menu(){
if G_WHIP_YESNO 'Reset fan control settings\n\nYour fan control settings file will be removed and current selections purged. This will take effect after next reboot.\n\nDo you want to continue?'; then
[[ -f $FP_SETTINGS ]] && G_EXEC_DESC="Removing $FP_SETTINGS" G_EXEC rm $FP_SETTINGS
G_EXEC_DESC='Resetting current selections' G_EXEC unset TEMP_CONTROLLED TRIP_TEMPS TRIP_SPEEDS STATIC_SPEED
fi
}
Temps_Menu(){
G_WHIP_DEFAULT_ITEM=$TRIP_TEMPS
G_WHIP_INPUTBOX "Please enter $TRIP_POINT_COUNT space-separated temperature points in °C.
EG: To have trip points at 35°C, 50°C and 65°C, enter\n \"35 50 65\"" && TRIP_TEMPS=$G_WHIP_RETURNED_VALUE
}
Speeds_Menu(){
G_WHIP_DEFAULT_ITEM=$TRIP_SPEEDS
G_WHIP_INPUTBOX "Please enter $(( $TRIP_POINT_COUNT + 1 )) space-separated fan speeds in % (percent) of the maximum possible speed.
EG: To disable the fan below $(( $(<$FP_TRIP_TEMPS) / 1000 ))°C, run it at 45% above, 70% after reaching $(( $(<${FP_TRIP_TEMPS/0_temp/1_temp}) / 1000 ))°C and 95% above $(( $(<${FP_TRIP_TEMPS/0_temp/2_temp}) / 1000 ))°C, enter
\"0 45 70 95\"" && TRIP_SPEEDS=$G_WHIP_RETURNED_VALUE
}
Static_Speed_Menu(){
G_WHIP_DEFAULT_ITEM=$STATIC_SPEED
G_WHIP_INPUTBOX "Please enter the desired static fan speeds in % (percent) of the maximum possible speed.
EG: To run the fan at 50%, enter\n \"60\"" && STATIC_SPEED=$G_WHIP_RETURNED_VALUE
}
Reboot_Menu(){
G_WHIP_BUTTON_CANCEL_TEXT='Later'
if G_WHIP_YESNO 'Reboot now?\n\nThe system default fan controls are automatically applied after next reboot. Do you want to reboot now?'; then
reboot
exit 0
fi
}
Main_Menu(){
local i
# Read currently applied fan controls, to generate status message
Read_Control_Files
local whip_message
if (( $TEMP_CONTROLLED_CURRENT )); then
# Current control mode
whip_message+='Mode: Temperature controlled'
# Current trip point temps
whip_message+='\nTemp points: '
for i in $TRIP_TEMPS_CURRENT
do
whip_message+=" ${i}°C"
done
# Current trip point speeds
whip_message+='\nFan speeds:'
for i in $TRIP_SPEEDS_CURRENT
do
whip_message+=" ${i}%"
done
else
# Current control mode
whip_message+='Mode: Static fan speed'
# Static fan speed
whip_message+="\nFan speed: ${STATIC_SPEED_CURRENT}%"
fi
# Control mode setting
TEMP_CONTROLLED=${TEMP_CONTROLLED:-$TEMP_CONTROLLED_CURRENT}
local mode_text='Static fan speed'
(( $TEMP_CONTROLLED )) && mode_text='Temperature controlled'
G_WHIP_MENU_ARRAY=(
'Reset' 'Remove settings file and reset selections'
'Mode' "Selected: $mode_text"
)
if (( $TEMP_CONTROLLED )); then
# Trip point temperature settings
TRIP_TEMPS=${TRIP_TEMPS:-$TRIP_TEMPS_CURRENT}
local trip_temps_text
for i in $TRIP_TEMPS
do
trip_temps_text+=" ${i}°C"
done
G_WHIP_MENU_ARRAY+=( 'Temp points' "Selected: $trip_temps_text" )
# Trip point speed settings
TRIP_SPEEDS=${TRIP_SPEEDS:-$TRIP_SPEEDS_CURRENT}
local trip_speeds_text
for i in $TRIP_SPEEDS
do
trip_speeds_text+=" ${i}%"
done
G_WHIP_MENU_ARRAY+=( 'Fan speeds' "Selected:$trip_speeds_text" )
else
# - Static fan speed setting
STATIC_SPEED=${STATIC_SPEED:-$STATIC_SPEED_CURRENT}
G_WHIP_MENU_ARRAY+=( 'Fan speed' "Selected: ${STATIC_SPEED}%" )
fi
G_WHIP_MENU_ARRAY+=( 'Apply' 'Apply selected settings now and automatically on boot' )
G_WHIP_BUTTON_CANCEL_TEXT='Exit'
if G_WHIP_MENU "$whip_message"; then
if [[ $G_WHIP_RETURNED_VALUE == 'Reset' ]]; then
Reset_Menu
Reboot_Menu
elif [[ $G_WHIP_RETURNED_VALUE == 'Mode' ]]; then
(( $TEMP_CONTROLLED )) && TEMP_CONTROLLED=0 || TEMP_CONTROLLED=1
elif [[ $G_WHIP_RETURNED_VALUE == 'Temp points' ]]; then
Temps_Menu
elif [[ $G_WHIP_RETURNED_VALUE == 'Fan speeds' ]]; then
Speeds_Menu
elif [[ $G_WHIP_RETURNED_VALUE == 'Fan speed' ]]; then
Static_Speed_Menu
elif [[ $G_WHIP_RETURNED_VALUE == 'Apply' ]]; then
if Verify_Settings; then
Apply_Settings
Write_Settings
else
G_WHIP_MSG '[FAILURE] Invalid settings found\n\nYour chosen settings are invalid. Please re-select or reset and follow the instructions within the selection menus.\n\nNothing got applied.'
fi
fi
else
exit 0
fi
}
#/////////////////////////////////////////////////////////////////////////////////////
# Main Loop
#/////////////////////////////////////////////////////////////////////////////////////
# Read possibly variable amount of trip points
TRIP_POINT_COUNT=$(find /sys/devices/virtual/thermal/thermal_zone0 -name 'trip_point_*_temp' | wc -l)
if (( $INPUT == 1 )); then
# Exit, if no settings file exists
if [[ ! -f $FP_SETTINGS ]]; then
G_DIETPI-NOTIFY 1 'No CPU settings file found. Please start "dietpi-fan_control" first. Aborting...'
exit 1
fi
Read_Settings
Verify_Settings && Apply_Settings
elif (( $INPUT == 0 )); then
# Read settings file, to generate menu entries and use current settings as default.
[[ -f $FP_SETTINGS ]] && Read_Settings
while :
do
Main_Menu
done
fi
#-----------------------------------------------------------------------------------
G_DIETPI-NOTIFY -1 0 "$G_PROGRAM_NAME"
exit 0
#-----------------------------------------------------------------------------------
}