-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathonewire_connection.go
68 lines (53 loc) · 2.08 KB
/
onewire_connection.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
package onewire
import (
"sync"
"gobot.io/x/gobot/v2"
)
// onewireConnection is the common implementation of the 1-wire bus interface.
type onewireConnection struct {
onewireSystem gobot.OneWireSystemDevicer
mutex *sync.Mutex
}
// NewConnection uses the given 1-wire system device and provides it as gobot.OneWireOperations.
func NewConnection(onewireSystem gobot.OneWireSystemDevicer) *onewireConnection {
return &onewireConnection{onewireSystem: onewireSystem, mutex: &sync.Mutex{}}
}
// ID returns the device id in the form "family code"-"serial number". Implements gobot.OneWireOperations.
func (d *onewireConnection) ID() string {
return d.onewireSystem.ID()
}
// ReadData reads the data according the command, e.g. from the specified file on sysfs bus.
// Implements gobot.OneWireOperations.
func (c *onewireConnection) ReadData(command string, data []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.onewireSystem.ReadData(command, data)
}
// WriteData writes the data according the command, e.g. to the specified file on sysfs bus.
// Implements gobot.OneWireOperations.
func (c *onewireConnection) WriteData(command string, data []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.onewireSystem.WriteData(command, data)
}
// ReadInteger reads the value according the command, e.g. to the specified file on sysfs bus.
// Implements gobot.OneWireOperations.
func (c *onewireConnection) ReadInteger(command string) (int, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.onewireSystem.ReadInteger(command)
}
// WriteInteger writes the value according the command, e.g. to the specified file on sysfs bus.
// Implements gobot.OneWireOperations.
func (c *onewireConnection) WriteInteger(command string, val int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.onewireSystem.WriteInteger(command, val)
}
// Close connection to underlying 1-wire device. Implements functions of onewire.Connection respectively
// gobot.OneWireOperations.
func (c *onewireConnection) Close() error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.onewireSystem.Close()
}