-
Notifications
You must be signed in to change notification settings - Fork 110
/
i2c.go
53 lines (42 loc) · 1.72 KB
/
i2c.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
package board
import (
"context"
)
// I2C represents a shareable I2C bus on the board.
type I2C interface {
// OpenHandle locks returns a handle interface that MUST be closed when done.
// you cannot have 2 open for the same addr
OpenHandle(addr byte) (I2CHandle, error)
}
// I2CHandle is similar to an io handle. It MUST be closed to release the bus.
type I2CHandle interface {
Write(ctx context.Context, tx []byte) error
Read(ctx context.Context, count int) ([]byte, error)
ReadByteData(ctx context.Context, register byte) (byte, error)
WriteByteData(ctx context.Context, register, data byte) error
ReadWordData(ctx context.Context, register byte) (uint16, error)
WriteWordData(ctx context.Context, register byte, data uint16) error
// Close closes the handle and releases the lock on the bus.
Close() error
}
// An I2CRegister is a lightweight wrapper around a handle for a particular register.
type I2CRegister struct {
Handle I2CHandle
Register byte
}
// ReadByteData reads a byte from the I2C channel register.
func (reg *I2CRegister) ReadByteData(ctx context.Context) (byte, error) {
return reg.Handle.ReadByteData(ctx, reg.Register)
}
// WriteByteData writes a byte to the I2C channel register.
func (reg *I2CRegister) WriteByteData(ctx context.Context, data byte) error {
return reg.Handle.WriteByteData(ctx, reg.Register, data)
}
// ReadWordData reads a word from the I2C channel register.
func (reg *I2CRegister) ReadWordData(ctx context.Context) (uint16, error) {
return reg.Handle.ReadWordData(ctx, reg.Register)
}
// WriteWordData writes a word to the I2C channel register.
func (reg *I2CRegister) WriteWordData(ctx context.Context, data uint16) error {
return reg.Handle.WriteWordData(ctx, reg.Register, data)
}