Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas committed Jan 4, 2025
1 parent 9acd5db commit 95d8dec
Show file tree
Hide file tree
Showing 23 changed files with 281 additions and 209 deletions.
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
5 changes: 3 additions & 2 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/beaglebone/pocketbeagle"
)

// Wiring
Expand All @@ -23,14 +23,15 @@ 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)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ type Adaptor struct {
*adaptors.PWMPinsAdaptor
*adaptors.I2cBusAdaptor
*adaptors.SpiBusAdaptor
usrLed string
pwmPinTranslate func(string) (string, int, error)
usrLed string
}

// NewAdaptor returns a new Beaglebone Black/Green Adaptor
Expand All @@ -52,11 +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{},
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 @@ -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 @@ -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"
113 changes: 113 additions & 0 deletions platforms/beagleboard/pocketbeagle/pocketbeagle_adaptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package pocketbeagle

import (
"fmt"
"os"

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/beagleboard/beaglebone"
"gobot.io/x/gobot/v2/system"
)

const pwmPeriodDefault = 500000 // 0.5 ms = 2 kHz

// PocketBeagleAdaptor is the Gobot Adaptor for the PocketBeagle
// For more information check out:
//
// http://beagleboard.org/pocket
type PocketBeagleAdaptor struct {
*beaglebone.Adaptor
sys *system.Accesser
}

// NewAdaptor creates a new Adaptor for the PocketBeagle
// tested with sysfs and cdev on "Linux BeagleBone 5.10.168-ti-r71"
//
// Optional parameters:
//
// adaptors.WithGpioCdevAccess(): use character device driver instead of sysfs
// adaptors.WithSpiGpioAccess(sclk, ncs, sdo, sdi): use GPIO's instead of /dev/spidev#.#
// adaptors.WithGpiosPullUp(pins): will be silently ignored (for some pins)
// adaptors.WithGpioDebounce(inPinNum, debounceTime): is only supported for debounceTime < 8ms
//
// Optional parameters for PWM, see [adaptors.NewPWMPinsAdaptor]
func NewAdaptor(opts ...interface{}) *PocketBeagleAdaptor {
sys := system.NewAccesser()
a := PocketBeagleAdaptor{
Adaptor: beaglebone.NewAdaptor(opts...),
sys: sys,
}

a.SetName(gobot.DefaultName("PocketBeagle"))

var digitalPinsOpts []adaptors.DigitalPinsOptionApplier
pwmPinsOpts := []adaptors.PwmPinsOptionApplier{adaptors.WithPWMDefaultPeriod(pwmPeriodDefault)}
for _, opt := range opts {
switch o := opt.(type) {
case adaptors.DigitalPinsOptionApplier:
digitalPinsOpts = append(digitalPinsOpts, o)
case adaptors.PwmPinsOptionApplier:
pwmPinsOpts = append(pwmPinsOpts, o)
case func(system.Optioner):
// do nothing, already applied on NewAdaptor(opts)
default:
panic(fmt.Sprintf("'%s' can not be applied on adaptor '%s'", opt, a.Name()))
}
}

analogPinTranslator := adaptors.NewAnalogPinTranslator(sys, analogPinMap)
digitalPinTranslator := adaptors.NewDigitalPinTranslator(sys, gpioPinDefinitions)
pwmPinTranslator := adaptors.NewPWMPinTranslator(sys, pwmPinMap)

a.AnalogPinsAdaptor = adaptors.NewAnalogPinsAdaptor(sys, analogPinTranslator.Translate)
a.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys,
a.getTranslateAndMuxDigitalPinFunc(digitalPinTranslator.Translate), digitalPinsOpts...)
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.getTranslateAndMuxPWMPinFunc(pwmPinTranslator.Translate),
pwmPinsOpts...)

return &a
}

func (a *PocketBeagleAdaptor) getTranslateAndMuxDigitalPinFunc(
digitalPinTranslate func(id string) (string, int, error),
) func(id string) (string, int, error) {
return func(id string) (string, int, error) {
if a.sys.IsSysfsDigitalPinAccess() {
// mux is done by id, not by line
if err := a.muxPin(id, "gpio"); err != nil {
return "", -1, err
}
}

return digitalPinTranslate(id)
}
}

func (a *PocketBeagleAdaptor) 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
}

return path, channel, nil
}
}

func (a *PocketBeagleAdaptor) muxPin(pin, cmd string) error {
path := fmt.Sprintf("/sys/devices/platform/ocp/ocp:%s_pinmux/state", pin)
fi, e := a.sys.OpenFile(path, os.O_WRONLY, 0o666)
defer fi.Close() //nolint:staticcheck // for historical reasons
if e != nil {
return e
}
_, e = fi.WriteString(cmd)
return e
}
Loading

0 comments on commit 95d8dec

Please sign in to comment.