forked from samonzeweb/argononefan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan.go
102 lines (85 loc) · 2.63 KB
/
fan.go
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
/*
* Copyright 2024 Markus W Mahlberg
*
* fan.go is part of argononefan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package argononefan
import (
"fmt"
"gobot.io/x/gobot/platforms/raspi"
)
// DefaultFanAddress is the default address of the fan on the i2c bus
// of the ArgonOne case.
// This should never change, as it is a hardware constant.
// However it can be overridden using the WithAddress option.
const DefaultFanAddress = 0x1A
// FanOption is a function that configures a Fan instance.
type FanOption func(*Fan) error
// Fan is a type that represents the fan in the ArgonOne case.
type Fan struct {
bus int
address int
}
// Connect opens a connection to the fan on bus 0 at address 0x1A.
// Those values can be overridden using the WithBus and WithAddress options.
func Connect(opts ...FanOption) (*Fan, error) {
f := &Fan{
bus: 0,
address: DefaultFanAddress,
}
for _, opt := range opts {
if err := opt(f); err != nil {
return nil, fmt.Errorf("error creating fan: %w", err)
}
}
return f, nil
}
// OnBus is an option that sets the bus the fan resides on.
func OnBus(bus int) FanOption {
return func(f *Fan) error {
f.bus = bus
return nil
}
}
// WithAddress is an option that sets the address of the fan on the i2c bus.
// This should never change, as it is a hardware constant.
// However it can be overridden using the this option.
// USE WITH CAUTION, as it can cause the fan to stop working if set to an incorrect value.
// You can use the i2cdetect command to find the correct address.
func WithAddress(address int) FanOption {
return func(f *Fan) error {
f.address = address
return nil
}
}
// SetSpeed sets the fan speed.
func (f *Fan) SetSpeed(speed int) error {
if speed < 0 || speed > 100 {
return fmt.Errorf("desired fan speed is out of range: %d", speed)
}
a := raspi.NewAdaptor()
defer a.Finalize()
conn, err := a.GetConnection(f.address, f.bus)
if err != nil {
return fmt.Errorf("can't connect to i2c bus: %w", err)
}
defer conn.Close()
err = conn.WriteByte(byte(speed))
if err != nil {
return fmt.Errorf("can't write fan seed: %W", err)
}
return nil
}