@@ -11,11 +11,11 @@ import (
11
11
"fmt"
12
12
"log"
13
13
"os"
14
- "sync"
15
- "time"
16
-
17
14
"periph.io/x/conn/v3/gpio"
18
15
"periph.io/x/conn/v3/physic"
16
+ "periph.io/x/conn/v3/pin"
17
+ "sync"
18
+ "time"
19
19
)
20
20
21
21
// LineConfigOverride is an override for a LineSet configuration.
@@ -147,6 +147,14 @@ func (ls *LineSet) Lines() []*LineSetLine {
147
147
return ls .lines
148
148
}
149
149
150
+ func (ls * LineSet ) Pins () []pin.Pin {
151
+ pins := make ([]pin.Pin , len (ls .lines ))
152
+ for ix , l := range ls .lines {
153
+ pins [ix ] = l
154
+ }
155
+ return pins
156
+ }
157
+
150
158
// Interrupt any calls to WaitForEdge().
151
159
func (ls * LineSet ) Halt () error {
152
160
if ls .fEdge != nil {
@@ -163,33 +171,33 @@ func (ls *LineSet) Halt() error {
163
171
// bits is the values for each line in the bit set.
164
172
//
165
173
// mask is a bitmask indicating which bits should be applied.
166
- func (ls * LineSet ) Out (bits , mask uint64 ) error {
174
+ func (ls * LineSet ) Out (bits , mask gpio. GPIOValue ) error {
167
175
ls .mu .Lock ()
168
176
defer ls .mu .Unlock ()
169
177
var data gpio_v2_line_values
170
- data .bits = bits
178
+ data .bits = uint64 ( bits )
171
179
if mask == 0 {
172
180
mask = (1 << ls .LineCount ()) - 1
173
181
}
174
- data .mask = mask
182
+ data .mask = uint64 ( mask )
175
183
return ioctl_set_gpio_v2_line_values (uintptr (ls .fd ), & data )
176
184
}
177
185
178
186
// Read the pins in this LineSet. This is done as one syscall to the
179
187
// operating system and will be very fast. mask is a bitmask of set pins
180
188
// to read. If 0, then all pins are read.
181
- func (ls * LineSet ) Read (mask uint64 ) (uint64 , error ) {
189
+ func (ls * LineSet ) Read (mask gpio. GPIOValue ) (gpio. GPIOValue , error ) {
182
190
ls .mu .Lock ()
183
191
defer ls .mu .Unlock ()
184
192
if mask == 0 {
185
193
mask = (1 << ls .LineCount ()) - 1
186
194
}
187
195
var lvalues gpio_v2_line_values
188
- lvalues .mask = mask
196
+ lvalues .mask = uint64 ( mask )
189
197
if err := ioctl_get_gpio_v2_line_values (uintptr (ls .fd ), & lvalues ); err != nil {
190
198
return 0 , err
191
199
}
192
- return lvalues .bits , nil
200
+ return gpio . GPIOValue ( lvalues .bits ) , nil
193
201
}
194
202
195
203
func (ls * LineSet ) MarshalJSON () ([]byte , error ) {
@@ -216,7 +224,7 @@ func (ls *LineSet) String() string {
216
224
// then the edge returned will be gpio.NoEdge
217
225
//
218
226
// err - Error value if any.
219
- func (ls * LineSet ) WaitForEdge (timeout time.Duration ) (number uint32 , edge gpio.Edge , err error ) {
227
+ func (ls * LineSet ) WaitForEdge (timeout time.Duration ) (number int , edge gpio.Edge , err error ) {
220
228
number = 0
221
229
edge = gpio .NoEdge
222
230
if ls .fEdge == nil {
@@ -248,21 +256,28 @@ func (ls *LineSet) WaitForEdge(timeout time.Duration) (number uint32, edge gpio.
248
256
} else if event .Id == _GPIO_V2_LINE_EVENT_FALLING_EDGE {
249
257
edge = gpio .FallingEdge
250
258
}
251
- number = uint32 (event .Offset )
259
+ number = int (event .Offset )
252
260
return
253
261
}
254
262
255
- // ByOffset returns a line by it's offset in the LineSet.
256
- func (ls * LineSet ) ByOffset (offset int ) * LineSetLine {
263
+ // ByOffset returns a line by it's offset in the LineSet. See ByName() for an
264
+ // example that casts the return value to a LineSetLine
265
+ func (ls * LineSet ) ByOffset (offset int ) pin.Pin {
257
266
if offset < 0 || offset >= len (ls .lines ) {
258
267
return nil
259
268
}
260
269
return ls .lines [offset ]
261
270
}
262
271
263
- // ByName returns a Line by name from the LineSet.
264
- func (ls * LineSet ) ByName (name string ) * LineSetLine {
265
-
272
+ // ByName returns a Line by name from the LineSet. To cast the returned value
273
+ // to a LineSet line, use:
274
+ //
275
+ // var lsl *gpioioctl.LineSetLine
276
+ // lsl, ok := ls.ByNumber(line0.Number()).(*gpioioctl.LineSetLine)
277
+ // if !ok {
278
+ // log.Fatal("error converting to LineSetLine")
279
+ // }
280
+ func (ls * LineSet ) ByName (name string ) pin.Pin {
266
281
for _ , line := range ls .lines {
267
282
if line .Name () == name {
268
283
return line
@@ -272,8 +287,9 @@ func (ls *LineSet) ByName(name string) *LineSetLine {
272
287
}
273
288
274
289
// LineNumber Return a line from the LineSet via it's GPIO line
275
- // number.
276
- func (ls * LineSet ) ByNumber (number int ) * LineSetLine {
290
+ // number. See ByName() for an example that casts the return value to a
291
+ // LineSetLine
292
+ func (ls * LineSet ) ByNumber (number int ) pin.Pin {
277
293
for _ , line := range ls .lines {
278
294
if line .Number () == number {
279
295
return line
@@ -325,7 +341,7 @@ func (lsl *LineSetLine) Edge() gpio.Edge {
325
341
326
342
// Out writes to this specific GPIO line.
327
343
func (lsl * LineSetLine ) Out (l gpio.Level ) error {
328
- var mask , bits uint64
344
+ var mask , bits gpio. GPIOValue
329
345
mask = 1 << lsl .offset
330
346
if l {
331
347
bits |= mask
@@ -353,7 +369,7 @@ func (lsl *LineSetLine) In(pull gpio.Pull, edge gpio.Edge) error {
353
369
354
370
// Read returns the value of this specific line.
355
371
func (lsl * LineSetLine ) Read () gpio.Level {
356
- var mask uint64 = 1 << lsl .offset
372
+ var mask gpio. GPIOValue = 1 << lsl .offset
357
373
bits , err := lsl .parent .Read (mask )
358
374
if err != nil {
359
375
log .Printf ("LineSetLine.Read() Error reading line %d. Error: %s\n " , lsl .number , err )
@@ -411,6 +427,7 @@ func (lsl *LineSetLine) Offset() uint32 {
411
427
}
412
428
413
429
// Ensure that Interfaces for these types are implemented fully.
430
+ var _ gpio.Group = & LineSet {}
414
431
var _ gpio.PinIO = & LineSetLine {}
415
432
var _ gpio.PinIn = & LineSetLine {}
416
433
var _ gpio.PinOut = & LineSetLine {}
0 commit comments