Skip to content

Commit

Permalink
pocketbeagle: introduce and use cdev by default (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Jan 6, 2025
1 parent ac0d59b commit 1f2c1dd
Show file tree
Hide file tree
Showing 30 changed files with 431 additions and 238 deletions.
26 changes: 26 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ Also those findings needs to be replaced, which usually affects developers, but
* `system.WithDigitalPinGpiodAccess()` --> `system.WithDigitalPinCdevAccess()`
* `IsGpiodDigitalPinAccess()` --> `IsCdevDigitalPinAccess()`

### PocketBeagle adaptor goes cdev

The beagle board "PocketBeagle" supports with latest images the Linux Kernel character device API, so the adaptor was
changed to use this as the default.

By "adaptors.WithSysfsAccess()" the old behavior can be forced. This is most likely needed if an old image with old
Kernel is used which does not support this new API.

A small renaming is also done, please search and replace:

```go
// old
...
"gobot.io/x/gobot/v2/platforms/beaglebone"
...
beaglebone.NewPocketBeagleAdaptor(...)
...

// new
...
"gobot.io/x/gobot/v2/platforms/beagleboard/pocketbeagle"
...
pocketbeagle.NewAdaptor(...)
...
```

## Switch from version 2.3.0 (ble and sphero adaptors affected)

### BLE drivers and client adaptor
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_basic_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"

"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_blink_usr_led.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_grove_accelerometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_led_brightness.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_led_brightness_with_analog_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/beaglebone_servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
Expand Down
16 changes: 12 additions & 4 deletions examples/beaglepocket_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/pocketbeagle"
)

// Wiring
Expand All @@ -23,29 +23,37 @@ import (
// LED's: the output pins are wired to the cathode of the LED, the anode is wired with a resistor (70-130Ohm for 20mA)
// to VCC
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed the state changes
// note: you can also use user LEDs, e.g. "usr0", "usr3"
func main() {
const (
inPinNum = "P1_34"
outPinNum = "P1_35"
outPinInvertedNum = "P1_36"
)

board := beaglebone.NewPocketBeagleAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum))
board := pocketbeagle.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum))

inPin := gpio.NewDirectPinDriver(board, inPinNum)
outPin := gpio.NewDirectPinDriver(board, outPinNum)
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum)

work := func() {
level := byte(1)

gobot.Every(500*time.Millisecond, func() {
read, err := inPin.DigitalRead()
fmt.Printf("pin %s state is %d\n", inPinNum, read)
if err != nil {
fmt.Println(err)
if level == 1 {
level = 0
} else {
level = 1
}
} else {
level = byte(read)
}

level := byte(read)

err = outPin.DigitalWrite(level)
fmt.Printf("pin %s is now %d\n", outPinNum, level)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions examples/nanopi_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func main() {
fmt.Printf("pin %s state is %d\n", inPinNum, read)
if err != nil {
fmt.Println(err)
if level == 1 {
level = 0
} else {
level = 1
}
} else {
level = byte(read)
}
Expand All @@ -64,12 +69,6 @@ func main() {
if err != nil {
fmt.Println(err)
}

if level == 1 {
level = 0
} else {
level = 1
}
})
}

Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions platforms/beaglebone/README.md → platforms/beagleboard/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Beaglebone
# BeagleBoard devices

The BeagleBone is an ARM based single board computer, with lots of GPIO, I2C, and analog interfaces built in.

Expand Down Expand Up @@ -35,12 +35,12 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)

func main() {
beagleboneAdaptor := beaglebone.NewAdaptor()
led := gpio.NewLedDriver(beagleboneAdaptor, "P9_12")
beagleBoneAdaptor := beaglebone.NewAdaptor()
led := gpio.NewLedDriver(beagleBoneAdaptor, "P9_12")

work := func() {
gobot.Every(1*time.Second, func() {
Expand All @@ -51,7 +51,7 @@ func main() {
}

robot := gobot.NewRobot("blinkBot",
[]gobot.Connection{beagleboneAdaptor},
[]gobot.Connection{beagleBoneAdaptor},
[]gobot.Device{led},
work,
)
Expand All @@ -62,7 +62,7 @@ func main() {
}
```

To use the PocketBeagle, use `beaglebone.NewPocketBeagleAdaptor()` like this:
To use the PocketBeagle, use `pocketbeagle.NewAdaptor()` like this:

```go
package main
Expand All @@ -72,12 +72,12 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/pocketbeagle"
)

func main() {
beagleboneAdaptor := beaglebone.NewPocketBeagleAdaptor()
led := gpio.NewLedDriver(beagleboneAdaptor, "P1_02")
pocketBeagleAdaptor := pocketbeagle.NewAdaptor()
led := gpio.NewLedDriver(pocketBeagleAdaptor, "P1_02")

work := func() {
gobot.Every(1*time.Second, func() {
Expand All @@ -88,7 +88,7 @@ func main() {
}

robot := gobot.NewRobot("pocketBeagleBot",
[]gobot.Connection{beagleboneAdaptor},
[]gobot.Connection{pocketBeagleAdaptor},
[]gobot.Device{led},
work,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ type Adaptor struct {
*adaptors.PWMPinsAdaptor
*adaptors.I2cBusAdaptor
*adaptors.SpiBusAdaptor
usrLed string
pinMap map[string]int
pwmPinTranslate func(string) (string, int, error)
usrLed string
}

// NewAdaptor returns a new Beaglebone Black/Green Adaptor
Expand All @@ -53,12 +51,10 @@ func NewAdaptor(opts ...interface{}) *Adaptor {
sys := system.NewAccesser(system.WithDigitalPinSysfsAccess())
pwmPinTranslator := adaptors.NewPWMPinTranslator(sys, bbbPwmPinMap)
a := &Adaptor{
name: gobot.DefaultName("BeagleboneBlack"),
sys: sys,
mutex: &sync.Mutex{},
pinMap: bbbPinMap,
pwmPinTranslate: pwmPinTranslator.Translate,
usrLed: "/sys/class/leds/beaglebone:green:",
name: gobot.DefaultName("BeagleboneBlack"),
sys: sys,
mutex: &sync.Mutex{},
usrLed: "/sys/class/leds/beaglebone:green:",
}

var digitalPinsOpts []adaptors.DigitalPinsOptionApplier
Expand All @@ -69,6 +65,8 @@ func NewAdaptor(opts ...interface{}) *Adaptor {
digitalPinsOpts = append(digitalPinsOpts, o)
case adaptors.PwmPinsOptionApplier:
pwmPinsOpts = append(pwmPinsOpts, o)
case func(system.Optioner):
o(sys)
default:
panic(fmt.Sprintf("'%s' can not be applied on adaptor '%s'", opt, a.name))
}
Expand All @@ -83,7 +81,8 @@ func NewAdaptor(opts ...interface{}) *Adaptor {

a.AnalogPinsAdaptor = adaptors.NewAnalogPinsAdaptor(sys, analogPinTranslator.Translate)
a.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, a.translateAndMuxDigitalPin, digitalPinsOpts...)
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.translateAndMuxPWMPin, pwmPinsOpts...)
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.getTranslateAndMuxPWMPinFunc(pwmPinTranslator.Translate),
pwmPinsOpts...)
a.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, i2cBusNumberValidator.Validate, defaultI2cBusNumber)
a.SpiBusAdaptor = adaptors.NewSpiBusAdaptor(sys, spiBusNumberValidator.Validate, defaultSpiBusNumber,
defaultSpiChipNumber, defaultSpiMode, defaultSpiBitsNumber, defaultSpiMaxSpeed)
Expand Down Expand Up @@ -165,7 +164,7 @@ func (a *Adaptor) DigitalWrite(id string, val byte) error {

// translatePin converts digital pin name to pin position
func (a *Adaptor) translateAndMuxDigitalPin(id string) (string, int, error) {
line, ok := a.pinMap[id]
line, ok := bbbPinMap[id]
if !ok {
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
}
Expand All @@ -176,17 +175,21 @@ func (a *Adaptor) translateAndMuxDigitalPin(id string) (string, int, error) {
return "", line, nil
}

func (a *Adaptor) translateAndMuxPWMPin(id string) (string, int, error) {
path, channel, err := a.pwmPinTranslate(id)
if err != nil {
return path, channel, err
}
func (a *Adaptor) getTranslateAndMuxPWMPinFunc(
pwmPinTranslate func(id string) (string, int, error),
) func(id string) (string, int, error) {
return func(id string) (string, int, error) {
path, channel, err := pwmPinTranslate(id)
if err != nil {
return path, channel, err
}

if err := a.muxPin(id, "pwm"); err != nil {
return "", -1, err
}
if err := a.muxPin(id, "pwm"); err != nil {
return "", -1, err
}

return path, channel, nil
return path, channel, nil
}
}

func (a *Adaptor) muxPin(pin, cmd string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ func TestNewAdaptor(t *testing.T) {
assert.NotNil(t, a.PWMPinsAdaptor)
assert.NotNil(t, a.I2cBusAdaptor)
assert.NotNil(t, a.SpiBusAdaptor)
assert.Equal(t, bbbPinMap, a.pinMap)
assert.NotNil(t, a.pwmPinTranslate)
assert.Equal(t, "/sys/class/leds/beaglebone:green:", a.usrLed)
assert.True(t, a.sys.IsSysfsDigitalPinAccess())
// act & assert
Expand Down Expand Up @@ -341,8 +339,10 @@ func Test_translateAndMuxPWMPin(t *testing.T) {
// arrange
muxPath := fmt.Sprintf("/sys/devices/platform/ocp/ocp:%s_pinmux/state", name)
fs.Add(muxPath)
pwmPinTranslator := adaptors.NewPWMPinTranslator(a.sys, bbbPwmPinMap)
translateAndMuxPWMPin := a.getTranslateAndMuxPWMPinFunc(pwmPinTranslator.Translate)
// act
path, channel, err := a.translateAndMuxPWMPin(name)
path, channel, err := translateAndMuxPWMPin(name)
// assert
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantDir, path)
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions platforms/beaglebone/doc.go → platforms/beagleboard/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package beaglebone provides the Gobot adaptor for the Beaglebone Black/Green, as well as a
Package beagleboard provides the Gobot adaptor for the Beaglebone Black/Green, as well as a
separate Adaptor for the PocketBeagle.
Installing:
Expand All @@ -15,7 +15,7 @@ Example:
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
)
func main() {
Expand All @@ -42,6 +42,6 @@ Example:
}
For more information refer to the beaglebone README:
https://github.com/hybridgroup/gobot/blob/release/platforms/beaglebone/README.md
https://github.com/hybridgroup/gobot/blob/release/platforms/beagleboard/README.md
*/
package beaglebone // import "gobot.io/x/gobot/v2/platforms/beaglebone"
package beagleboard // import "gobot.io/x/gobot/v2/platforms/beagleboard"
Loading

0 comments on commit 1f2c1dd

Please sign in to comment.