Skip to content

Instantly share code, notes, and snippets.

@danieltroger
Last active March 8, 2019 21:15
Show Gist options
  • Save danieltroger/d1730fe5b3fff468852463fe9e4158d6 to your computer and use it in GitHub Desktop.
Save danieltroger/d1730fe5b3fff468852463fe9e4158d6 to your computer and use it in GitHub Desktop.
Ubuntu, mac mini late 2012 fan control
<?php
/*
This thing probably works on any mac, you have to understand it (lol) and adjust it to the correct fan and min-max speed though. I'll make it easier to costumize later.
What it does?
It tries to keep your cpu cooler than 95 degress celsius. The fan kicks in at 60 degress. And then rises linearly with the heat, until it runs with it's max - 5500 rpm - at 95 degress.
It always averages the cpu temperature of 20 seconds to avoid "flickering", and speed changes are performed smoothly over a 5 sekond period.
-------------------------------------------------------------------------------
Copyright (c) 2017 Daniel Troger
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
define("speed",5);
function gettemp()
{
return ((int) file_get_contents("/sys/class/thermal/thermal_zone0/temp"))/1000;
}
function settemp($s)
{
if($s < 1800){$s = 1800;}
if($s > 5500){$s = 5500;}
if(cs() == $s) return;
file_put_contents("/sys/devices/platform/applesmc.768/fan1_output",$s);
}
if(file_get_contents("/sys/devices/platform/applesmc.768/fan1_manual") !== "1\n")
{
echo "set fan mode to manual\n";
file_put_contents("/sys/devices/platform/applesmc.768/fan1_manual","1\n");
}
$temp = 0;
$samples = Array();
$r = 0;
$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$pid = pcntl_fork();
if ($pid == -1) {settemp(5500);die('Could not fork');}
else if ($pid) {
fclose($sockets[0]);
while(1)
{
$temp = gettemp();
echo "{$temp} degrees celsius, " . cs() . " rpm" . "\r";
if($temp > 100){settemp(5500);}else{
if($r > 150)
{
fwrite($sockets[1],round(array_sum($samples)/count($samples),0) . PHP_EOL);
$samples = Array();
$r = 0;
}
}
$samples[] = calcspeed($temp);
usleep(100000);
$r++;
}
pcntl_wait($status);
} else {
fclose($sockets[1]);
while(1){
$rpm = (int) fgets($sockets[0]);
$diff = $rpm-cs();
$c = cs();
$steps = 100*speed;
$step = round($diff/$steps);
for($i = 0; $i < $steps; $i++)
{
$n = $c+$step;
settemp($n);
$c = $n;
usleep(10000);
}
settemp($rpm);
}
}
function cs(){return (int) file_get_contents("/sys/devices/platform/applesmc.768/fan1_output");}
function calcspeed($temp)
{
return round((1100/7)*($temp-60),0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment