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
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
<?php
namespace App\Subroutines;
use App\Application;
use App\Concerns\InteractiveConsole;
use App\Model\Fan\HwmonFan;
use App\Service\Utils;
use Hugga\Console;
use Hugga\Input\Question\Choice;
use Hugga\Input\Question\Confirmation;
class InitializeConfig
{
use InteractiveConsole;
/** @var Application */
protected $app;
/**
* @param Application $app
*/
public function __construct(Application $app, Console $console)
{
$this->app = $app;
$this->console = $console;
}
public function main(): int
{
$fanConfig = $this->app->fanConfig;
$this->info('No fans configured. Detecting fans...');
$fans = $this->detectControllableFans();
if (count($fans) > 0) {
foreach ($fans as $fan) {
$this->line('');
$this->info('Fan ' . $fan->name);
$start = $this->detectStartValue($fan);
$fan->setStartValue($start);
do {
$answer = $this->ask(new Choice([
'name fan',
'adjust start value',
'define limit',
'add fan',
'skip fan',
], 'What next?', 'name fan'));
switch ($answer) {
case 'adjust start value':
do {
$fan->setPwm(0);
$startValue = $this->ask('What value to try?', $start);
$fan->setPwm($startValue);
} while (!$this->ask(new Confirmation('Use that value?')));
$fan->resetState();
$fan->setStartValue($startValue);
break;
case 'define limit':
do {
$fan->setPwm(255);
$maxValue = $this->ask('What value to try?', 255);
$fan->setPwm($maxValue);
Utils::waitForStableRpm($fan);
$this->line('fan runs at ' . $fan->getCurrentSpeed() . ' rpm now');
} while (!$this->ask(new Confirmation('Use that value?')));
$fan->resetState();
$fan->setMaxValue($maxValue);
break;
case 'name fan':
$name = $this->ask('Name of the fan: ', $fan->name);
$fan->name = $name;
break;
case 'add fan':
$fanConfig->addFan($fan);
break 2;
case 'skip fan':
break 2;
default:
$this->warn('not implemented');
break;
}
} while (true);
}
$fanConfig->save();
return 0;
}
$this->error('We could not find any fan to control.');
return 1;
}
/**
* @return array|HwmonFan[]
*/
protected function detectControllableFans(): array
{
$fans = [];
$inputs = glob(HWMON_PATH . '/hwmon*/fan*_input');
$inputs = array_reduce($inputs, function ($inputs, $input) {
if (!preg_match('~(hwmon(\d+)/(fan\d+))_input$~', $input, $match)) {
return $inputs;
}
$hwmon = trim(file_get_contents(HWMON_PATH . '/hwmon' . $match[2] . '/name'));
$fan = $match[3];
$pwm = preg_replace('~fan(\d+)~', 'pwm$1', $fan);
$name = $hwmon . '/' . $fan;
if (!file_exists(HWMON_PATH . '/hwmon' . $match[2] . '/' . $pwm)) {
return $inputs;
}
$options = [
'hwmon' => $hwmon,
'fan' => $fan,
'pwm' => $pwm,
];
$inputs[$name] = [
'fan' => new HwmonFan($name, $options),
'options' => $options,
'hwmon' => 'hwmon' . $match[2],
];
return $inputs;
}, []);
$unknownInputs = [];
foreach ($inputs as $name => $input) {
[$min, $max] = $this->testPwm($input['fan']);
if ($max === 0 || $min > $max*0.4) {
$unknownInputs[$name] = $input;
continue;
}
$this->line(sprintf('%s at full speed: %d; stopped: %d', $name, $max, $min));
$fans[] = $input['fan'];
}
if (count($unknownInputs) > 0) {
$this->info(PHP_EOL . 'Some fans could not be assigned to a pwm. Trying remaining pwms...');
$assignedPwms = array_map(fn(HwmonFan $fan) => $fan->toArray()['options']['pwm'], $fans);
$pwms = array_reduce(glob(HWMON_PATH . '/hwmon*/pwm*'), function ($pwms, $pwm) use ($assignedPwms) {
if (!preg_match('~(hwmon(\d+)/(pwm\d+))$~', $pwm, $match) || in_array(basename($pwm), $assignedPwms)) {
return $pwms;
}
$hwmon = $match[2];
if (!isset($pwms[$hwmon])) {
$pwms[$hwmon] = [];
}
$pwms[$hwmon][] = basename($pwm);
return $pwms;
}, []);
foreach ($unknownInputs as $id => $input) {
if (!isset($pwms[$input['hwmon']])) {
continue;
}
foreach ($pwms[$input['hwmon']] as $pwm) {
$name = $input['fan']->name;
$options = $input['options'];
if ($options['pwm'] === $pwm) {
continue;
}
$options['pwm'] = $pwm;
$fan = new HwmonFan($name, $options);
[$min, $max] = $this->testPwm($fan);
if ($max === 0 || $min > $max*0.4) {
continue;
}
$fans[] = $fan;
continue 2;
}
$this->info($id . ' not connected or not controllable');
}
}
return $fans;
}
protected function testPwm(HwmonFan $fan): array
{
$fan->setPwm(255);
Utils::waitForStableRpm($fan);
$fullSpeed = $fan->getCurrentSpeed();
if ($fullSpeed === 0) {
$fan->resetState();
return [0, 0];
}
$fan->setPwm(0);
Utils::waitForStableRpm($fan);
$stopSpeed = $fan->getCurrentSpeed();
$fan->resetState();
return [$stopSpeed, $fullSpeed];
}
protected function detectStartValue(HwmonFan $fan): int
{
$this->info('Detecting start value for ' . $fan->name);
$fan->setPwm(0);
Utils::waitForStableRpm($fan);
$minSpeed = $fan->getCurrentSpeed() * 1.1;
$pwm = 0;
do {
$fan->setPwm($pwm += 5);
Utils::waitForStableRpm($fan);
$currentSpeed = $fan->getCurrentSpeed();
$this->line(sprintf('%d rpm with pwm value %d', $currentSpeed, $pwm));
} while ($minSpeed >= $currentSpeed);
$fan->resetState();
return $pwm;
}
}