-
Notifications
You must be signed in to change notification settings - Fork 110
/
i2c.go
195 lines (167 loc) · 4.36 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
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
package gpsrtk
import (
"context"
"errors"
"fmt"
"io"
"sync"
"github.com/edaniels/golog"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/config"
"go.viam.com/rdk/registry"
rdkutils "go.viam.com/rdk/utils"
)
type i2cCorrectionSource struct {
correctionReader io.ReadCloser // reader for rctm corrections only
logger golog.Logger
bus board.I2C
addr byte
cancelCtx context.Context
cancelFunc func()
activeBackgroundWorkers sync.WaitGroup
errMu sync.Mutex
lastError error
}
func newI2CCorrectionSource(
ctx context.Context,
deps registry.Dependencies,
cfg config.Component,
logger golog.Logger,
) (correctionSource, error) {
attr, ok := cfg.ConvertedAttributes.(*StationConfig)
if !ok {
return nil, rdkutils.NewUnexpectedTypeError(attr, cfg.ConvertedAttributes)
}
b, err := board.FromDependencies(deps, attr.Board)
if err != nil {
return nil, fmt.Errorf("gps init: failed to find board: %w", err)
}
localB, ok := b.(board.LocalBoard)
if !ok {
return nil, fmt.Errorf("board %s is not local", attr.Board)
}
i2cbus, ok := localB.I2CByName(attr.I2CBus)
if !ok {
return nil, fmt.Errorf("gps init: failed to find i2c bus %s", attr.I2CBus)
}
addr := attr.I2cAddr
if addr == -1 {
return nil, errors.New("must specify gps i2c address")
}
cancelCtx, cancelFunc := context.WithCancel(ctx)
s := &i2cCorrectionSource{
bus: i2cbus,
addr: byte(addr),
cancelCtx: cancelCtx,
cancelFunc: cancelFunc,
logger: logger,
}
return s, s.lastError
}
func (s *i2cCorrectionSource) setLastError(err error) {
s.errMu.Lock()
defer s.errMu.Unlock()
s.lastError = err
}
// Start reads correction data from the i2c address and sends it into the correctionReader.
func (s *i2cCorrectionSource) Start(ready chan<- bool) {
// currently not checking if rtcm message is valid, need to figure out how to integrate constant I2C byte message with rtcm3 scanner
s.activeBackgroundWorkers.Add(1)
defer s.activeBackgroundWorkers.Done()
var w *io.PipeWriter
s.correctionReader, w = io.Pipe()
ready <- true
// open I2C handle every time
handle, err := s.bus.OpenHandle(s.addr)
if err != nil {
s.logger.Errorf("can't open gps i2c handle: %s", err)
s.setLastError(err)
return
}
// read from handle and pipe to correctionSource
buffer, err := handle.Read(context.Background(), 1024)
if err != nil {
s.logger.Debug("Could not read from handle")
}
_, err = w.Write(buffer)
if err != nil {
s.logger.Errorf("Error writing RTCM message: %s", err)
s.setLastError(err)
return
}
// close I2C handle
err = handle.Close()
if err != nil {
s.logger.Debug("failed to close handle: %s", err)
s.setLastError(err)
return
}
for err == nil {
select {
case <-s.cancelCtx.Done():
return
default:
}
// Open I2C handle every time
handle, err := s.bus.OpenHandle(s.addr)
if err != nil {
s.logger.Errorf("can't open gps i2c handle: %s", err)
s.setLastError(err)
return
}
// read from handle and pipe to correctionSource
buffer, err := handle.Read(context.Background(), 1024)
if err != nil {
s.logger.Debug("Could not read from handle")
}
_, err = w.Write(buffer)
if err != nil {
s.logger.Errorf("Error writing RTCM message: %s", err)
s.setLastError(err)
return
}
// close I2C handle
err = handle.Close()
if err != nil {
s.logger.Debug("failed to close handle: %s", err)
s.setLastError(err)
return
}
}
}
// Reader returns the i2cCorrectionSource's correctionReader if it exists.
func (s *i2cCorrectionSource) Reader() (io.ReadCloser, error) {
if s.correctionReader == nil {
return nil, errors.New("no stream")
}
return s.correctionReader, s.lastError
}
// Close shuts down the i2cCorrectionSource.
func (s *i2cCorrectionSource) Close() error {
s.cancelFunc()
s.activeBackgroundWorkers.Wait()
// close correction reader
if s.correctionReader != nil {
if err := s.correctionReader.Close(); err != nil {
return err
}
s.correctionReader = nil
}
return s.lastError
}
// PMTK checksums commands by XORing together each byte.
func addChk(data []byte) []byte {
chk := checksum(data)
newCmd := []byte("$")
newCmd = append(newCmd, data...)
newCmd = append(newCmd, []byte("*")...)
newCmd = append(newCmd, chk)
return newCmd
}
func checksum(data []byte) byte {
var chk byte
for _, b := range data {
chk ^= b
}
return chk
}