forked from a1exwang/uhubctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan.rb
48 lines (41 loc) · 768 Bytes
/
fan.rb
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
#!/usr/bin/env ruby
# Your device port
@port = 2
mode = nil
if ARGV[0] == 'rate'
mode = :rate
rate = ARGV[1].to_f
elsif ARGV[0] == 'sine'
mode = :sine
period = ARGV[1].to_f
else
exit
end
# Set fan rate for 'delta' seconds time
def set_rate(rate, delta)
down_time = (1 - rate) * delta
up_time = rate * delta
`./uhubctl -p #{@port} -a on &> /dev/null`
sleep(up_time)
`./uhubctl -p #{@port} -a off &> /dev/null`
sleep(down_time)
end
def sine(period, delta)
t = 0
loop do |x|
v = Math.sin(2 * Math::PI * t / period).abs
puts("v #{v}")
set_rate(v, delta)
sleep(delta)
t += delta
end
end
delta = 0.01
case mode
when :rate
loop { set_rate(rate, delta) }
when :sine
sine(period, delta)
else
raise RuntimeError
end