Skip to content

Commit 6aaccc1

Browse files
authored
gpio(adaptors): fix so now gpiodev is used as default (hybridgroup#1112)
1 parent 302614e commit 6aaccc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1279
-626
lines changed

examples/beaglepocket_direct_pin.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//go:build example
2+
// +build example
3+
4+
//
5+
// Do not build by default.
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"time"
12+
13+
"gobot.io/x/gobot/v2"
14+
"gobot.io/x/gobot/v2/drivers/gpio"
15+
"gobot.io/x/gobot/v2/platforms/adaptors"
16+
"gobot.io/x/gobot/v2/platforms/beaglebone"
17+
)
18+
19+
// Wiring
20+
// PWR Pocket: P1.14, P2.23 (+3.3V, VCC); P1.15, P1.16, P1.22, P2.15, P2.21 (GND)
21+
// GPIO Pocket: header pin P1.34 is input, pin P1.35 is normal output, pin P1.36 is inverted output
22+
// Button: the input pin is wired with a button to GND, an external pull up resistor is needed (e.g. 2kOhm to VCC)
23+
// LED's: the output pins are wired to the cathode of the LED, the anode is wired with a resistor (70-130Ohm for 20mA)
24+
// to VCC
25+
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed the state changes
26+
func main() {
27+
const (
28+
inPinNum = "P1_34"
29+
outPinNum = "P1_35"
30+
outPinInvertedNum = "P1_36"
31+
)
32+
33+
board := beaglebone.NewPocketBeagleAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum))
34+
35+
inPin := gpio.NewDirectPinDriver(board, inPinNum)
36+
outPin := gpio.NewDirectPinDriver(board, outPinNum)
37+
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum)
38+
39+
work := func() {
40+
gobot.Every(500*time.Millisecond, func() {
41+
read, err := inPin.DigitalRead()
42+
fmt.Printf("pin %s state is %d\n", inPinNum, read)
43+
if err != nil {
44+
fmt.Println(err)
45+
}
46+
47+
level := byte(read)
48+
49+
err = outPin.DigitalWrite(level)
50+
fmt.Printf("pin %s is now %d\n", outPinNum, level)
51+
if err != nil {
52+
fmt.Println(err)
53+
}
54+
55+
err = outPinInverted.DigitalWrite(level)
56+
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level)
57+
if err != nil {
58+
fmt.Println(err)
59+
}
60+
})
61+
}
62+
63+
robot := gobot.NewRobot("pinBot",
64+
[]gobot.Connection{board},
65+
[]gobot.Device{inPin, outPin, outPinInverted},
66+
work,
67+
)
68+
69+
if err := robot.Start(); err != nil {
70+
panic(err)
71+
}
72+
}

platforms/adaptors/analogpinsadaptor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"gobot.io/x/gobot/v2/system"
99
)
1010

11-
type analogPinTranslator func(pin string) (path string, r, w bool, bufLen uint16, err error)
11+
type analogPinTranslator func(pin string) (path string, w bool, readBufLen uint16, err error)
1212

1313
// AnalogPinsAdaptor is a adaptor for analog pins, normally used for composition in platforms.
1414
// It is also usable for general sysfs access.
@@ -83,11 +83,11 @@ func (a *AnalogPinsAdaptor) analogPin(id string) (gobot.AnalogPinner, error) {
8383
pin := a.pins[id]
8484

8585
if pin == nil {
86-
path, r, w, bufLen, err := a.translate(id)
86+
path, w, readBufLen, err := a.translate(id)
8787
if err != nil {
8888
return nil, err
8989
}
90-
pin = a.sys.NewAnalogPin(path, r, w, bufLen)
90+
pin = a.sys.NewAnalogPin(path, w, readBufLen)
9191
a.pins[id] = pin
9292
}
9393

platforms/adaptors/analogpinsadaptor_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ func initTestAnalogPinsAdaptorWithMockedFilesystem(mockPaths []string) (*AnalogP
4040
return a, fs
4141
}
4242

43-
func testAnalogPinTranslator(id string) (string, bool, bool, uint16, error) {
43+
func testAnalogPinTranslator(id string) (string, bool, uint16, error) {
4444
switch id {
4545
case "read":
46-
return analogReadPath, true, false, 10, nil
46+
return analogReadPath, false, 10, nil
4747
case "write":
48-
return analogWritePath, false, true, 11, nil
48+
return analogWritePath, true, 0, nil
4949
case "read/write":
50-
return analogReadWritePath, true, true, 12, nil
50+
return analogReadWritePath, true, 12, nil
5151
case "read/write_string":
52-
return analogReadWriteStringPath, true, true, 13, nil
52+
return analogReadWriteStringPath, true, 13, nil
5353
}
5454

55-
return "", false, false, 0, fmt.Errorf("'%s' is not a valid id of an analog pin", id)
55+
return "", false, 0, fmt.Errorf("'%s' is not a valid id of an analog pin", id)
5656
}
5757

5858
func TestAnalogPinsConnect(t *testing.T) {
59-
translate := func(id string) (path string, r, w bool, bufLen uint16, err error) { return }
59+
translate := func(id string) (path string, w bool, bufLen uint16, err error) { return }
6060
a := NewAnalogPinsAdaptor(system.NewAccesser(), translate)
6161
assert.Equal(t, (map[string]gobot.AnalogPinner)(nil), a.pins)
6262

platforms/adaptors/analogpintranslator.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
)
88

99
type AnalogPinDefinition struct {
10-
Path string
11-
R bool // readable
12-
W bool // writable
13-
BufLen uint16
10+
Path string
11+
W bool // writable
12+
ReadBufLen uint16 // readable if buffer > 0
1413
}
1514

1615
type AnalogPinDefinitions map[string]AnalogPinDefinition
@@ -26,20 +25,20 @@ func NewAnalogPinTranslator(sys *system.Accesser, pinDefinitions AnalogPinDefini
2625
}
2726

2827
// Translate returns the sysfs path for the given id.
29-
func (pt *AnalogPinTranslator) Translate(id string) (string, bool, bool, uint16, error) {
28+
func (pt *AnalogPinTranslator) Translate(id string) (string, bool, uint16, error) {
3029
pinInfo, ok := pt.pinDefinitions[id]
3130
if !ok {
32-
return "", false, false, 0, fmt.Errorf("'%s' is not a valid id for an analog pin", id)
31+
return "", false, 0, fmt.Errorf("'%s' is not a valid id for an analog pin", id)
3332
}
3433

3534
path := pinInfo.Path
3635
info, err := pt.sys.Stat(path)
3736
if err != nil {
38-
return "", false, false, 0, fmt.Errorf("Error (%v) on access '%s'", err, path)
37+
return "", false, 0, fmt.Errorf("Error (%v) on access '%s'", err, path)
3938
}
4039
if info.IsDir() {
41-
return "", false, false, 0, fmt.Errorf("The item '%s' is a directory, which is not expected", path)
40+
return "", false, 0, fmt.Errorf("The item '%s' is a directory, which is not expected", path)
4241
}
4342

44-
return path, pinInfo.R, pinInfo.W, pinInfo.BufLen, nil
43+
return path, pinInfo.W, pinInfo.ReadBufLen, nil
4544
}

platforms/adaptors/analogpintranslator_test.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,28 @@ func TestNewAnalogPinTranslator(t *testing.T) {
2323

2424
func TestAnalogPinTranslatorTranslate(t *testing.T) {
2525
pinDefinitions := AnalogPinDefinitions{
26-
"thermal_zone0": {Path: "/sys/class/thermal/thermal_zone0/temp", R: true, W: false, BufLen: 7},
27-
"thermal_zone1": {Path: "/sys/class/thermal/thermal_zone1/temp", R: true, W: false, BufLen: 7},
26+
"thermal_zone0": {Path: "/sys/class/thermal/thermal_zone0/temp", W: false, ReadBufLen: 7},
27+
"thermal_zone1": {Path: "/sys/class/thermal/thermal_zone1/temp", W: false, ReadBufLen: 7},
2828
}
2929
mockedPaths := []string{
3030
"/sys/class/thermal/thermal_zone0/temp",
3131
"/sys/class/thermal/thermal_zone1/temp",
3232
}
3333
tests := map[string]struct {
34-
id string
35-
wantPath string
36-
wantReadable bool
37-
wantBufLen uint16
38-
wantErr string
34+
id string
35+
wantPath string
36+
wantBufLen uint16
37+
wantErr string
3938
}{
4039
"translate_thermal_zone0": {
41-
id: "thermal_zone0",
42-
wantPath: "/sys/class/thermal/thermal_zone0/temp",
43-
wantReadable: true,
44-
wantBufLen: 7,
40+
id: "thermal_zone0",
41+
wantPath: "/sys/class/thermal/thermal_zone0/temp",
42+
wantBufLen: 7,
4543
},
4644
"translate_thermal_zone1": {
47-
id: "thermal_zone1",
48-
wantPath: "/sys/class/thermal/thermal_zone1/temp",
49-
wantReadable: true,
50-
wantBufLen: 7,
45+
id: "thermal_zone1",
46+
wantPath: "/sys/class/thermal/thermal_zone1/temp",
47+
wantBufLen: 7,
5148
},
5249
"unknown_id": {
5350
id: "99",
@@ -61,15 +58,14 @@ func TestAnalogPinTranslatorTranslate(t *testing.T) {
6158
_ = sys.UseMockFilesystem(mockedPaths)
6259
pt := NewAnalogPinTranslator(sys, pinDefinitions)
6360
// act
64-
path, r, w, buf, err := pt.Translate(tc.id)
61+
path, w, buf, err := pt.Translate(tc.id)
6562
// assert
6663
if tc.wantErr != "" {
6764
require.EqualError(t, err, tc.wantErr)
6865
} else {
6966
require.NoError(t, err)
7067
}
7168
assert.Equal(t, tc.wantPath, path)
72-
assert.Equal(t, tc.wantReadable, r)
7369
assert.False(t, w)
7470
assert.Equal(t, tc.wantBufLen, buf)
7571
})

0 commit comments

Comments
 (0)