Newer
Older
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
<?php
namespace App\Service;
use App\Application;
use App\Model\Fan\HwmonFan;
use Hugga\Console;
class Utils
{
public static function waitForStableRpm(HwmonFan $fan): int
{
static $waitTime = 600_000; // in micro seconds
static $count = 4;
$readings = [];
// get the first readings
for ($i = 0; $i < $count; $i++) {
$readings[] = $fan->getCurrentSpeed();
usleep($waitTime);
}
$min = min($readings);
$max = max($readings);
$average = array_sum($readings) / $count;
$variance = $average > 0 ? ($max - $min) / $average * 100 : 0;
// wait till variance is less than 2
while (array_sum($readings) > 0 && // fan has not stopped
$variance > 2
) {
Application::console()->line(sprintf(
'min: %d rpm, max: %d rpm, avg: %d rpm, variance: %f%%',
$min,
$max,
$average,
$variance
), Console::WEIGHT_DEBUG);
array_shift($readings);
$readings[] = $fan->getCurrentSpeed();
$min = min($readings);
$max = max($readings);
$average = array_sum($readings) / $count;
$variance = $average > 0 ? ($max - $min) / $average * 100 : 0;
usleep($waitTime);
}
return $average;
}
}