From c51f1e3add015c71edc499b65a60c6e75fe683e2 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 26 Nov 2016 21:48:10 +0100 Subject: [PATCH 01/27] beaglebone: add support for kernel 4.x Signed-off-by: deadprogram --- platforms/beaglebone/README.md | 12 +++ platforms/beaglebone/beaglebone_adaptor.go | 116 ++++----------------- platforms/beaglebone/beaglebone_pins.go | 91 ++++++++++++++++ 3 files changed, 125 insertions(+), 94 deletions(-) create mode 100644 platforms/beaglebone/beaglebone_pins.go diff --git a/platforms/beaglebone/README.md b/platforms/beaglebone/README.md index bee82a81..80bbe049 100644 --- a/platforms/beaglebone/README.md +++ b/platforms/beaglebone/README.md @@ -62,3 +62,15 @@ func main() { robot.Start() } ``` + +### Using with older kernel version + +The new default for the Gobot Beaglebone adaptor is for the 4.x kernel, such as that used with the Debian Jessie. + +If you want to use the older kernel version, such as that on the Angstrom Linux, use the `SetKernel()` function as follows: + +```go +beagleboneAdaptor := beaglebone.NewAdaptor() +beagleboneAdaptor.SetKernel("3") + +``` diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index a5fce0dc..fb6bc30f 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -14,107 +14,16 @@ import ( "github.com/hybridgroup/gobot/sysfs" ) -var slots = "/sys/devices/bone_capemgr.*" -var ocp = "/sys/devices/ocp.*" var usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:" var glob = func(pattern string) (matches []string, err error) { return filepath.Glob(pattern) } -var pins = map[string]int{ - "P8_3": 38, - "P8_4": 39, - "P8_5": 34, - "P8_6": 35, - "P8_7": 66, - "P8_8": 67, - "P8_9": 69, - "P8_10": 68, - "P8_11": 45, - "P8_12": 44, - "P8_13": 23, - "P8_14": 26, - "P8_15": 47, - "P8_16": 46, - "P8_17": 27, - "P8_18": 65, - "P8_19": 22, - "P8_20": 63, - "P8_21": 62, - "P8_22": 37, - "P8_23": 36, - "P8_24": 33, - "P8_25": 32, - "P8_26": 61, - "P8_27": 86, - "P8_28": 88, - "P8_29": 87, - "P8_30": 89, - "P8_31": 10, - "P8_32": 11, - "P8_33": 9, - "P8_34": 81, - "P8_35": 8, - "P8_36": 80, - "P8_37": 78, - "P8_38": 79, - "P8_39": 76, - "P8_40": 77, - "P8_41": 74, - "P8_42": 75, - "P8_43": 72, - "P8_44": 73, - "P8_45": 70, - "P8_46": 71, - "P9_11": 30, - "P9_12": 60, - "P9_13": 31, - "P9_14": 50, - "P9_15": 48, - "P9_16": 51, - "P9_17": 5, - "P9_18": 4, - "P9_19": 13, - "P9_20": 12, - "P9_21": 3, - "P9_22": 2, - "P9_23": 49, - "P9_24": 15, - "P9_25": 117, - "P9_26": 14, - "P9_27": 115, - "P9_28": 113, - "P9_29": 111, - "P9_30": 112, - "P9_31": 110, -} - -var pwmPins = map[string]string{ - "P9_14": "P9_14", - "P9_21": "P9_21", - "P9_22": "P9_22", - "P9_29": "P9_29", - "P9_42": "P9_42", - "P8_13": "P8_13", - "P8_34": "P8_34", - "P8_45": "P8_45", - "P8_46": "P8_46", -} - -var analogPins = map[string]string{ - "P9_39": "AIN0", - "P9_40": "AIN1", - "P9_37": "AIN2", - "P9_38": "AIN3", - "P9_33": "AIN4", - "P9_36": "AIN5", - "P9_35": "AIN6", -} - // Adaptor is the gobot.Adaptor representation for the Beaglebone type Adaptor struct { name string + kernel string digitalPins []sysfs.DigitalPin pwmPins map[string]*pwmPin i2cDevice sysfs.I2cDevice @@ -127,16 +36,26 @@ type Adaptor struct { func NewAdaptor() *Adaptor { b := &Adaptor{ name: "Beaglebone", + kernel: "4", digitalPins: make([]sysfs.DigitalPin, 120), pwmPins: make(map[string]*pwmPin), } + b.setSlots() + return b +} + +func (b *Adaptor) setSlots() { + ocp := "/sys/devices/ocp.*" g, _ := glob(ocp) b.ocp = g[0] + + slots := "/sys/devices/bone_capemgr.*" + if b.kernel == "4" { + slots = "/sys/devices/platform/bone_capemgr" + } g, _ = glob(slots) b.slots = fmt.Sprintf("%v/slots", g[0]) - - return b } // Name returns the Adaptor name @@ -145,6 +64,15 @@ func (b *Adaptor) Name() string { return b.name } // SetName sets the Adaptor name func (b *Adaptor) SetName(n string) { b.name = n } +// Kernel returns the kernel major version for the BeagleBone +func (b *Adaptor) Kernel() string { return b.kernel } + +// SetKernel sets the Adaptor kernel +func (b *Adaptor) SetKernel(n string) { + b.kernel = n + b.setSlots() +} + // Connect initializes the pwm and analog dts. func (b *Adaptor) Connect() error { if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil { diff --git a/platforms/beaglebone/beaglebone_pins.go b/platforms/beaglebone/beaglebone_pins.go new file mode 100644 index 00000000..b0b7faca --- /dev/null +++ b/platforms/beaglebone/beaglebone_pins.go @@ -0,0 +1,91 @@ +package beaglebone + +var pins = map[string]int{ + "P8_3": 38, + "P8_4": 39, + "P8_5": 34, + "P8_6": 35, + "P8_7": 66, + "P8_8": 67, + "P8_9": 69, + "P8_10": 68, + "P8_11": 45, + "P8_12": 44, + "P8_13": 23, + "P8_14": 26, + "P8_15": 47, + "P8_16": 46, + "P8_17": 27, + "P8_18": 65, + "P8_19": 22, + "P8_20": 63, + "P8_21": 62, + "P8_22": 37, + "P8_23": 36, + "P8_24": 33, + "P8_25": 32, + "P8_26": 61, + "P8_27": 86, + "P8_28": 88, + "P8_29": 87, + "P8_30": 89, + "P8_31": 10, + "P8_32": 11, + "P8_33": 9, + "P8_34": 81, + "P8_35": 8, + "P8_36": 80, + "P8_37": 78, + "P8_38": 79, + "P8_39": 76, + "P8_40": 77, + "P8_41": 74, + "P8_42": 75, + "P8_43": 72, + "P8_44": 73, + "P8_45": 70, + "P8_46": 71, + "P9_11": 30, + "P9_12": 60, + "P9_13": 31, + "P9_14": 50, + "P9_15": 48, + "P9_16": 51, + "P9_17": 5, + "P9_18": 4, + "P9_19": 13, + "P9_20": 12, + "P9_21": 3, + "P9_22": 2, + "P9_23": 49, + "P9_24": 15, + "P9_25": 117, + "P9_26": 14, + "P9_27": 115, + "P9_28": 113, + "P9_29": 111, + "P9_30": 112, + "P9_31": 110, +} + +var pwmPins = map[string]string{ + "P9_14": "P9_14", + "P9_21": "P9_21", + "P9_22": "P9_22", + "P9_29": "P9_29", + "P9_42": "P9_42", + "P8_13": "P8_13", + "P8_34": "P8_34", + "P8_45": "P8_45", + "P8_46": "P8_46", +} + +var analogPins = map[string]string{ + "P9_39": "AIN0", + "P9_40": "AIN1", + "P9_37": "AIN2", + "P9_38": "AIN3", + "P9_33": "AIN4", + "P9_36": "AIN5", + "P9_35": "AIN6", +} From 0d02bdc29f1b5696993631476726430ea0d6e478 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 26 Nov 2016 21:56:22 +0100 Subject: [PATCH 02/27] beaglebone: correct ocp mapping in support for kernel 4.x Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index fb6bc30f..a2e79779 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -47,13 +47,16 @@ func NewAdaptor() *Adaptor { func (b *Adaptor) setSlots() { ocp := "/sys/devices/ocp.*" + slots := "/sys/devices/bone_capemgr.*" + + if b.kernel == "4" { + ocp = "/sys/devices/platform/ocp/ocp*" + slots = "/sys/devices/platform/bone_capemgr" + } + g, _ := glob(ocp) b.ocp = g[0] - slots := "/sys/devices/bone_capemgr.*" - if b.kernel == "4" { - slots = "/sys/devices/platform/bone_capemgr" - } g, _ = glob(slots) b.slots = fmt.Sprintf("%v/slots", g[0]) } From 055fcbf42395426d2afd588cafca935deb23aa48 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 13:37:23 +0100 Subject: [PATCH 03/27] beaglebone: mapping and tests to match the 4.x kernel Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 8 ++-- .../beaglebone/beaglebone_adaptor_test.go | 40 +++++++++---------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index a2e79779..4e6e6b60 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -14,8 +14,6 @@ import ( "github.com/hybridgroup/gobot/sysfs" ) -var usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:" - var glob = func(pattern string) (matches []string, err error) { return filepath.Glob(pattern) } @@ -27,6 +25,7 @@ type Adaptor struct { digitalPins []sysfs.DigitalPin pwmPins map[string]*pwmPin i2cDevice sysfs.I2cDevice + usrLed string ocp string helper string slots string @@ -48,10 +47,12 @@ func NewAdaptor() *Adaptor { func (b *Adaptor) setSlots() { ocp := "/sys/devices/ocp.*" slots := "/sys/devices/bone_capemgr.*" + b.usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:" if b.kernel == "4" { ocp = "/sys/devices/platform/ocp/ocp*" slots = "/sys/devices/platform/bone_capemgr" + b.usrLed = "/sys/class/leds/beaglebone:green:" } g, _ := glob(ocp) @@ -148,7 +149,8 @@ func (b *Adaptor) DigitalRead(pin string) (val int, err error) { // valid usr pin values are usr0, usr1, usr2 and usr3 func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) { if strings.Contains(pin, "usr") { - fi, err := sysfs.OpenFile(usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666) + fmt.Println(b.usrLed + pin + "/brightness") + fi, err := sysfs.OpenFile(b.usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666) defer fi.Close() if err != nil { return err diff --git a/platforms/beaglebone/beaglebone_adaptor_test.go b/platforms/beaglebone/beaglebone_adaptor_test.go index 55199129..f9132284 100644 --- a/platforms/beaglebone/beaglebone_adaptor_test.go +++ b/platforms/beaglebone/beaglebone_adaptor_test.go @@ -42,7 +42,7 @@ func (n *NullReadWriteCloser) Read(b []byte) (int, error) { return len(b), nil } -var closeErr error = nil +var closeErr error func (n *NullReadWriteCloser) Close() error { return closeErr @@ -54,16 +54,16 @@ func TestBeagleboneAdaptor(t *testing.T) { } fs := sysfs.NewMockFilesystem([]string{ "/dev/i2c-1", - "/sys/devices/bone_capemgr.4", - "/sys/devices/ocp.3", - "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness", - "/sys/devices/ocp.3/helper.5", - "/sys/devices/ocp.3/helper.5/AIN1", - "/sys/devices/ocp.3/pwm_test_P9_14.5", - "/sys/devices/ocp.3/pwm_test_P9_14.5/run", - "/sys/devices/ocp.3/pwm_test_P9_14.5/period", - "/sys/devices/ocp.3/pwm_test_P9_14.5/polarity", - "/sys/devices/ocp.3/pwm_test_P9_14.5/duty", + "/sys/devices/platform/bone_capemgr", + "/sys/devices/platform/ocp/ocp4", + "/sys/class/leds/beaglebone:green:usr1/brightness", + "/sys/devices/platform/ocp/ocp4/helper.5", + "/sys/devices/platform/ocp/ocp4/helper.5/AIN1", + "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5", + "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/run", + "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period", + "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/polarity", + "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty", "/sys/class/gpio/export", "/sys/class/gpio/unexport", "/sys/class/gpio/gpio60/value", @@ -74,12 +74,12 @@ func TestBeagleboneAdaptor(t *testing.T) { sysfs.SetFilesystem(fs) a := NewAdaptor() - a.slots = "/sys/devices/bone_capemgr.4" - a.ocp = "/sys/devices/ocp.3" + a.slots = "/sys/devices/platform/bone_capemgr" + a.ocp = "/sys/devices/platform/ocp/ocp4" a.Connect() - a.helper = "/sys/devices/ocp.3/helper.5" + a.helper = "/sys/devices/platform/ocp/ocp4/helper.5" // PWM glob = func(pattern string) (matches []string, err error) { @@ -91,29 +91,29 @@ func TestBeagleboneAdaptor(t *testing.T) { a.PwmWrite("P9_14", 175) gobottest.Assert( t, - fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents, + fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period"].Contents, "500000", ) gobottest.Assert( t, - fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents, + fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty"].Contents, "343137", ) a.ServoWrite("P9_14", 100) gobottest.Assert( t, - fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents, + fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period"].Contents, "16666666", ) gobottest.Assert( t, - fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents, + fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty"].Contents, "1898148", ) // Analog - fs.Files["/sys/devices/ocp.3/helper.5/AIN1"].Contents = "567\n" + fs.Files["/sys/devices/platform/ocp/ocp4/helper.5/AIN1"].Contents = "567\n" i, _ := a.AnalogRead("P9_40") gobottest.Assert(t, i, 567) @@ -123,7 +123,7 @@ func TestBeagleboneAdaptor(t *testing.T) { // DigitalIO a.DigitalWrite("usr1", 1) gobottest.Assert(t, - fs.Files["/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:usr1/brightness"].Contents, + fs.Files["/sys/class/leds/beaglebone:green:usr1/brightness"].Contents, "1", ) From 848f93756f44a205f31f53b3cdc741c4a57f3fed Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 13:41:37 +0100 Subject: [PATCH 04/27] beaglebone: mapping and tests to match the 4.x kernel Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 1 - 1 file changed, 1 deletion(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index 4e6e6b60..92c55d95 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -149,7 +149,6 @@ func (b *Adaptor) DigitalRead(pin string) (val int, err error) { // valid usr pin values are usr0, usr1, usr2 and usr3 func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) { if strings.Contains(pin, "usr") { - fmt.Println(b.usrLed + pin + "/brightness") fi, err := sysfs.OpenFile(b.usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666) defer fi.Close() if err != nil { From 24479a230df815783ac2354837204e31f30b4918 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 13:59:49 +0100 Subject: [PATCH 05/27] beaglebone: map usr LEDs to match all kernels Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index 92c55d95..b85d826a 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -47,12 +47,11 @@ func NewAdaptor() *Adaptor { func (b *Adaptor) setSlots() { ocp := "/sys/devices/ocp.*" slots := "/sys/devices/bone_capemgr.*" - b.usrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:" + b.usrLed = "/sys/class/leds/beaglebone:green:" if b.kernel == "4" { ocp = "/sys/devices/platform/ocp/ocp*" slots = "/sys/devices/platform/bone_capemgr" - b.usrLed = "/sys/class/leds/beaglebone:green:" } g, _ := glob(ocp) From 20235ffad066d1c0015233ad0f36d376c9a9d570 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 20:24:24 +0100 Subject: [PATCH 06/27] beaglebone: auto-detect Linux kernel version Signed-off-by: deadprogram --- platforms/beaglebone/README.md | 12 ------------ platforms/beaglebone/beaglebone_adaptor.go | 22 ++++++++++++---------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/platforms/beaglebone/README.md b/platforms/beaglebone/README.md index 80bbe049..bee82a81 100644 --- a/platforms/beaglebone/README.md +++ b/platforms/beaglebone/README.md @@ -62,15 +62,3 @@ func main() { robot.Start() } ``` - -### Using with older kernel version - -The new default for the Gobot Beaglebone adaptor is for the 4.x kernel, such as that used with the Debian Jessie. - -If you want to use the older kernel version, such as that on the Angstrom Linux, use the `SetKernel()` function as follows: - -```go -beagleboneAdaptor := beaglebone.NewAdaptor() -beagleboneAdaptor.SetKernel("3") - -``` diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index b85d826a..a158f56a 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "os/exec" "path/filepath" "strconv" "strings" @@ -35,7 +36,6 @@ type Adaptor struct { func NewAdaptor() *Adaptor { b := &Adaptor{ name: "Beaglebone", - kernel: "4", digitalPins: make([]sysfs.DigitalPin, 120), pwmPins: make(map[string]*pwmPin), } @@ -47,13 +47,15 @@ func NewAdaptor() *Adaptor { func (b *Adaptor) setSlots() { ocp := "/sys/devices/ocp.*" slots := "/sys/devices/bone_capemgr.*" - b.usrLed = "/sys/class/leds/beaglebone:green:" - if b.kernel == "4" { + b.kernel = getKernel() + if string(b.kernel[0]) == "4" { ocp = "/sys/devices/platform/ocp/ocp*" slots = "/sys/devices/platform/bone_capemgr" } + b.usrLed = "/sys/class/leds/beaglebone:green:" + g, _ := glob(ocp) b.ocp = g[0] @@ -67,15 +69,9 @@ func (b *Adaptor) Name() string { return b.name } // SetName sets the Adaptor name func (b *Adaptor) SetName(n string) { b.name = n } -// Kernel returns the kernel major version for the BeagleBone +// Kernel returns the Linux kernel version for the BeagleBone func (b *Adaptor) Kernel() string { return b.kernel } -// SetKernel sets the Adaptor kernel -func (b *Adaptor) SetKernel(n string) { - b.kernel = n - b.setSlots() -} - // Connect initializes the pwm and analog dts. func (b *Adaptor) Connect() error { if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil { @@ -327,3 +323,9 @@ func ensureSlot(slots, item string) (err error) { } return } + +func getKernel() string { + result, _ := exec.Command("uname", "-r").Output() + + return strings.TrimSpace(string(result)) +} From 37882b17f5842cd3b6abe8cf073875bbfd84a7e4 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 30 Nov 2016 10:40:28 +0100 Subject: [PATCH 07/27] beaglebone: corrected platform version mapping, thanks @wfernandes Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index a158f56a..cd81c390 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -49,7 +49,7 @@ func (b *Adaptor) setSlots() { slots := "/sys/devices/bone_capemgr.*" b.kernel = getKernel() - if string(b.kernel[0]) == "4" { + if b.kernel[:1] == "4" { ocp = "/sys/devices/platform/ocp/ocp*" slots = "/sys/devices/platform/bone_capemgr" } From 4aa0a6416e7f5325438a7dfa84f418eba0563cf2 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 21:56:09 +0100 Subject: [PATCH 08/27] chip: auto-detect OS version to adjust pin mappings Signed-off-by: deadprogram --- platforms/chip/README.md | 4 ---- platforms/chip/chip_adaptor.go | 36 +++++++++++++++++++++++++++-- platforms/chip/chip_adaptor_test.go | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/platforms/chip/README.md b/platforms/chip/README.md index 44943f05..0701a247 100644 --- a/platforms/chip/README.md +++ b/platforms/chip/README.md @@ -2,10 +2,6 @@ The [CHIP](http://www.getchip.com/) is a small, inexpensive ARM based single board computer, with many different IO interfaces available on the [pin headers](http://docs.getchip.com/#pin-headers). -The current ChipAdaptor will only work with CHIP devices running OS 4.3. -This is because of the change in the GPIO expander pins between kernel -versions OS 4.3 vs. OS 4.4. See [here](http://docs.getchip.com/chip.html#gpio) for more details. - For documentation about the CHIP platform click [here](http://docs.getchip.com/). ## How to Install diff --git a/platforms/chip/chip_adaptor.go b/platforms/chip/chip_adaptor.go index 6c34b118..d8946477 100644 --- a/platforms/chip/chip_adaptor.go +++ b/platforms/chip/chip_adaptor.go @@ -2,18 +2,22 @@ package chip import ( "errors" + "os/exec" + "strings" multierror "github.com/hashicorp/go-multierror" "github.com/hybridgroup/gobot/sysfs" ) +// Adaptor represents a Gobot Adaptor for a C.H.I.P. type Adaptor struct { name string digitalPins map[int]sysfs.DigitalPin + pinMap map[string]int i2cDevice sysfs.I2cDevice } -var pins = map[string]int{ +var pinsOriginal = map[string]int{ "XIO-P0": 408, "XIO-P1": 409, "XIO-P2": 410, @@ -24,12 +28,25 @@ var pins = map[string]int{ "XIO-P7": 415, } +var pins44 = map[string]int{ + "XIO-P0": 1016, + "XIO-P1": 1017, + "XIO-P2": 1018, + "XIO-P3": 1019, + "XIO-P4": 1020, + "XIO-P5": 1021, + "XIO-P6": 1022, + "XIO-P7": 1023, +} + // NewAdaptor creates a C.H.I.P. Adaptor func NewAdaptor() *Adaptor { c := &Adaptor{ name: "CHIP", digitalPins: make(map[int]sysfs.DigitalPin), } + + c.setPins() return c } @@ -61,8 +78,17 @@ func (c *Adaptor) Finalize() (err error) { return } +func (c *Adaptor) setPins() { + kernel := getKernel() + if string(kernel[0:2]) == "4.4" { + c.pinMap = pins44 + } else { + c.pinMap = pinsOriginal + } +} + func (c *Adaptor) translatePin(pin string) (i int, err error) { - if val, ok := pins[pin]; ok { + if val, ok := c.pinMap[pin]; ok { i = val } else { err = errors.New("Not a valid pin") @@ -140,3 +166,9 @@ func (c *Adaptor) I2cRead(address int, size int) (data []byte, err error) { _, err = c.i2cDevice.Read(data) return } + +func getKernel() string { + result, _ := exec.Command("uname", "-r").Output() + + return strings.TrimSpace(string(result)) +} diff --git a/platforms/chip/chip_adaptor_test.go b/platforms/chip/chip_adaptor_test.go index f8ce6a17..a7a8d558 100644 --- a/platforms/chip/chip_adaptor_test.go +++ b/platforms/chip/chip_adaptor_test.go @@ -38,7 +38,7 @@ func (n *NullReadWriteCloser) Read(b []byte) (int, error) { return len(b), nil } -var closeErr error = nil +var closeErr error func (n *NullReadWriteCloser) Close() error { return closeErr From c0651ab77e9c70e802740e66e2d32a465358e3fd Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 27 Nov 2016 22:10:04 +0100 Subject: [PATCH 09/27] chip: default to latest OS version to adjust pin mappings Signed-off-by: deadprogram --- platforms/chip/chip_adaptor.go | 6 +++--- platforms/chip/chip_adaptor_test.go | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/platforms/chip/chip_adaptor.go b/platforms/chip/chip_adaptor.go index d8946477..d10489dc 100644 --- a/platforms/chip/chip_adaptor.go +++ b/platforms/chip/chip_adaptor.go @@ -80,10 +80,10 @@ func (c *Adaptor) Finalize() (err error) { func (c *Adaptor) setPins() { kernel := getKernel() - if string(kernel[0:2]) == "4.4" { - c.pinMap = pins44 - } else { + if string(kernel[0:2]) == "4.3" { c.pinMap = pinsOriginal + } else { + c.pinMap = pins44 } } diff --git a/platforms/chip/chip_adaptor_test.go b/platforms/chip/chip_adaptor_test.go index a7a8d558..749e4fbb 100644 --- a/platforms/chip/chip_adaptor_test.go +++ b/platforms/chip/chip_adaptor_test.go @@ -55,18 +55,18 @@ func TestChipAdaptorDigitalIO(t *testing.T) { fs := sysfs.NewMockFilesystem([]string{ "/sys/class/gpio/export", "/sys/class/gpio/unexport", - "/sys/class/gpio/gpio408/value", - "/sys/class/gpio/gpio408/direction", - "/sys/class/gpio/gpio415/value", - "/sys/class/gpio/gpio415/direction", + "/sys/class/gpio/gpio1016/value", + "/sys/class/gpio/gpio1016/direction", + "/sys/class/gpio/gpio1023/value", + "/sys/class/gpio/gpio1023/direction", }) sysfs.SetFilesystem(fs) a.DigitalWrite("XIO-P0", 1) - gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio408/value"].Contents, "1") + gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio1016/value"].Contents, "1") - fs.Files["/sys/class/gpio/gpio415/value"].Contents = "1" + fs.Files["/sys/class/gpio/gpio1023/value"].Contents = "1" i, _ := a.DigitalRead("XIO-P7") gobottest.Assert(t, i, 1) From 7ac80c056798c4080da7237112e940ff2135928d Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 30 Nov 2016 10:37:08 +0100 Subject: [PATCH 10/27] chip: corrected platform version mapping, thanks @wfernandes Signed-off-by: deadprogram --- platforms/chip/chip_adaptor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/chip/chip_adaptor.go b/platforms/chip/chip_adaptor.go index d10489dc..ad3ba800 100644 --- a/platforms/chip/chip_adaptor.go +++ b/platforms/chip/chip_adaptor.go @@ -80,7 +80,7 @@ func (c *Adaptor) Finalize() (err error) { func (c *Adaptor) setPins() { kernel := getKernel() - if string(kernel[0:2]) == "4.3" { + if kernel[:3] == "4.3" { c.pinMap = pinsOriginal } else { c.pinMap = pins44 From 9fc8828383ec4b2af38f88ab96d1af851543c6e8 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Wed, 30 Nov 2016 22:22:54 +0100 Subject: [PATCH 11/27] platforms/firmata/client: use io.ReadFull This CL uses io.ReadFull to make sure exactly n bytes are read, and correctly handle io.EOF and co. Fixes #343. Signed-off-by: Sebastien Binet Conflicts: platforms/firmata/client/client.go --- platforms/firmata/client/client.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/platforms/firmata/client/client.go b/platforms/firmata/client/client.go index e0b93428..26fd557c 100644 --- a/platforms/firmata/client/client.go +++ b/platforms/firmata/client/client.go @@ -315,21 +315,9 @@ func (b *Client) write(data []byte) (err error) { return } -func (b *Client) read(length int) (buf []byte, err error) { - i := 0 - for length > 0 { - tmp := make([]byte, length) - if i, err = b.connection.Read(tmp); err != nil { - if err.Error() != "EOF" { - return - } - time.Sleep(5 * time.Millisecond) - } - if i > 0 { - buf = append(buf, tmp...) - length = length - i - } - } +func (b *Client) read(n int) (buf []byte, err error) { + buf = make([]byte, n) + _, err = io.ReadFull(b.connection, buf) return } From b8254e26b94ba8f1221484e4eeb3491fed362aac Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 30 Nov 2016 23:32:20 +0100 Subject: [PATCH 12/27] docs: Add missing godocs for keyboard platform Signed-off-by: deadprogram --- platforms/keyboard/README.md | 4 ++-- platforms/keyboard/doc.go | 5 +++-- platforms/keyboard/keyboard.go | 1 + platforms/keyboard/keyboard_driver.go | 18 +++++++++++++----- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/platforms/keyboard/README.md b/platforms/keyboard/README.md index 744400a5..feddea18 100644 --- a/platforms/keyboard/README.md +++ b/platforms/keyboard/README.md @@ -23,10 +23,10 @@ import ( ) func main() { - keys := keyboard.NewDriver("keyboard") + keys := keyboard.NewDriver() work := func() { - gobot.On(keys.Event("key"), func(data interface{}) { + keys.On(keyboard.Key, func(data interface{}) { key := data.(keyboard.KeyEvent) if key.Key == keyboard.A { diff --git a/platforms/keyboard/doc.go b/platforms/keyboard/doc.go index b0509c9f..5aef1acb 100644 --- a/platforms/keyboard/doc.go +++ b/platforms/keyboard/doc.go @@ -22,7 +22,7 @@ Example: keys := keyboard.NewDriver() work := func() { - keys.On(keys.Event("key"), func(data interface{}) { + keys.On(keyboard.Key, func(data interface{}) { key := data.(keyboard.KeyEvent) if key.Key == keyboard.A { @@ -39,9 +39,10 @@ Example: work, ) - gbot.Start() + robot.Start() } + For further information refer to keyboard README: https://github.com/hybridgroup/gobot/blob/master/platforms/keyboard/README.md */ diff --git a/platforms/keyboard/keyboard.go b/platforms/keyboard/keyboard.go index 63e4b436..91356422 100644 --- a/platforms/keyboard/keyboard.go +++ b/platforms/keyboard/keyboard.go @@ -7,6 +7,7 @@ import ( type bytes [3]byte +// KeyEvent contains data about a keyboard event type KeyEvent struct { Bytes bytes Key int diff --git a/platforms/keyboard/keyboard_driver.go b/platforms/keyboard/keyboard_driver.go index e66743de..eb0a8509 100644 --- a/platforms/keyboard/keyboard_driver.go +++ b/platforms/keyboard/keyboard_driver.go @@ -8,10 +8,11 @@ import ( ) const ( - // Keyboard event + // Key board event Key = "key" ) +// Driver is gobot software device to the keyboard type Driver struct { name string connect func(*Driver) (err error) @@ -20,6 +21,8 @@ type Driver struct { gobot.Eventer } +// NewDriver returns a new keyboard Driver. +// func NewDriver() *Driver { k := &Driver{ name: "Keyboard", @@ -60,12 +63,17 @@ func NewDriver() *Driver { return k } -func (k *Driver) Name() string { return k.name } -func (k *Driver) SetName(n string) { k.name = n } +// Name returns the Driver Name +func (k *Driver) Name() string { return k.name } + +// SetName sets the Driver Name +func (k *Driver) SetName(n string) { k.name = n } + +// Connection returns the Driver Connection func (k *Driver) Connection() gobot.Connection { return nil } // Start initializes keyboard by grabbing key events as they come in and -// publishing a key event +// publishing each as a key event func (k *Driver) Start() (err error) { if err = k.connect(k); err != nil { return err @@ -76,7 +84,7 @@ func (k *Driver) Start() (err error) { return } -// Halt stops camera driver +// Halt stops keyboard driver func (k *Driver) Halt() (err error) { if originalState != "" { return restore() From 56feca9a03e4ef64766f7b7f6b73f8a5483d6dd9 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 11:28:22 +0100 Subject: [PATCH 13/27] docs: Add missing godocs for Leap Motion platform Signed-off-by: deadprogram --- platforms/leap/leap_motion_adaptor.go | 25 +++++++++++++++++-------- platforms/leap/leap_motion_driver.go | 13 ++++++++++--- platforms/leap/parser.go | 6 +++++- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/platforms/leap/leap_motion_adaptor.go b/platforms/leap/leap_motion_adaptor.go index e082dac6..6ed63c59 100644 --- a/platforms/leap/leap_motion_adaptor.go +++ b/platforms/leap/leap_motion_adaptor.go @@ -6,6 +6,7 @@ import ( "golang.org/x/net/websocket" ) +// Adaptor is the Gobot Adaptor connection to the Leap Motion type Adaptor struct { name string port string @@ -13,27 +14,35 @@ type Adaptor struct { connect func(string) (io.ReadWriteCloser, error) } -// NewAdaptor creates a new leap motion adaptor using specified port +// NewAdaptor creates a new leap motion adaptor using specified port, +// which is this case is the host IP or name of the Leap Motion daemon func NewAdaptor(port string) *Adaptor { return &Adaptor{ name: "LeapMotion", port: port, - connect: func(port string) (io.ReadWriteCloser, error) { - return websocket.Dial("ws://"+port+"/v3.json", "", "http://"+port) + connect: func(host string) (io.ReadWriteCloser, error) { + return websocket.Dial("ws://"+host+"/v3.json", "", "http://"+host) }, } } -func (l *Adaptor) Name() string { return l.name } + +// Name returns the Adaptor Name +func (l *Adaptor) Name() string { return l.name } + +// SetName sets the Adaptor Name func (l *Adaptor) SetName(n string) { l.name = n } -func (l *Adaptor) Port() string { return l.port } + +// Port returns the Adaptor Port which is this case is the host IP or name +func (l *Adaptor) Port() string { return l.port } // Connect returns true if connection to leap motion is established successfully func (l *Adaptor) Connect() (err error) { - if ws, e := l.connect(l.Port()); e != nil { + ws, e := l.connect(l.Port()) + if e != nil { return e - } else { - l.ws = ws } + + l.ws = ws return } diff --git a/platforms/leap/leap_motion_driver.go b/platforms/leap/leap_motion_driver.go index 1155fd47..095f0e3f 100644 --- a/platforms/leap/leap_motion_driver.go +++ b/platforms/leap/leap_motion_driver.go @@ -17,6 +17,7 @@ const ( GestureEvent = "gesture" ) +// Driver the Gobot software device to the Leap Motion type Driver struct { name string connection gobot.Connection @@ -27,7 +28,7 @@ var receive = func(ws io.ReadWriteCloser, msg *[]byte) { websocket.Message.Receive(ws.(*websocket.Conn), msg) } -// NewLDriver creates a new leap motion driver +// NewDriver creates a new leap motion driver // // Adds the following events: // "message" - Gets triggered when receiving a message from leap motion @@ -45,8 +46,14 @@ func NewDriver(a *Adaptor) *Driver { l.AddEvent(GestureEvent) return l } -func (l *Driver) Name() string { return l.name } -func (l *Driver) SetName(n string) { l.name = n } + +// Name returns the Driver Name +func (l *Driver) Name() string { return l.name } + +// SetName sets the Driver Name +func (l *Driver) SetName(n string) { l.name = n } + +// Connection returns the Driver's Connection func (l *Driver) Connection() gobot.Connection { return l.connection } // adaptor returns leap motion adaptor diff --git a/platforms/leap/parser.go b/platforms/leap/parser.go index dbaf6096..729be146 100644 --- a/platforms/leap/parser.go +++ b/platforms/leap/parser.go @@ -4,6 +4,7 @@ import ( "encoding/json" ) +// Gesture is a Leap Motion gesture tht has been detected type Gesture struct { Direction []float64 `json:"direction"` Duration int `json:"duration"` @@ -17,6 +18,7 @@ type Gesture struct { Type string `json:"type"` } +// Hand is a Leap Motion hand tht has been detected type Hand struct { Direction []float64 `json:"direction"` ID int `json:"id"` @@ -32,6 +34,7 @@ type Hand struct { TimeVisible float64 `json:"TimeVisible"` } +// Pointable is a Leap Motion pointing motion tht has been detected type Pointable struct { Direction []float64 `json:"direction"` HandID int `json:"handId"` @@ -46,12 +49,13 @@ type Pointable struct { TouchZone string `json:"touchZone"` } +// InteractionBox is the area within which the gestural interaction has been detected type InteractionBox struct { Center []int `json:"center"` Size []float64 `json:"size"` } -// Base representation returned that holds every other objects +// Frame is the base representation returned that holds every other objects type Frame struct { CurrentFrameRate float64 `json:"currentFrameRate"` Gestures []Gesture `json:"gestures"` From 69b81d1411e47e04d7d4bb872f1802649beca06b Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 12:02:47 +0100 Subject: [PATCH 14/27] docs: Add missing godocs for Neurosky platform Signed-off-by: deadprogram --- platforms/neurosky/neurosky_adaptor.go | 18 +++-- platforms/neurosky/neurosky_driver.go | 91 +++++++++++++++------- platforms/neurosky/neurosky_driver_test.go | 20 ++--- 3 files changed, 86 insertions(+), 43 deletions(-) diff --git a/platforms/neurosky/neurosky_adaptor.go b/platforms/neurosky/neurosky_adaptor.go index f5326882..6acad6a7 100644 --- a/platforms/neurosky/neurosky_adaptor.go +++ b/platforms/neurosky/neurosky_adaptor.go @@ -1,3 +1,4 @@ +// Package neurosky is the Gobot platform for the Neurosky Mindwave EEG package neurosky import ( @@ -6,6 +7,7 @@ import ( "github.com/tarm/serial" ) +// Adaptor is the Gobot Adaptor for the Neurosky Mindwave type Adaptor struct { name string port string @@ -24,17 +26,23 @@ func NewAdaptor(port string) *Adaptor { } } -func (n *Adaptor) Name() string { return n.name } +// Name returns the Adaptor Name +func (n *Adaptor) Name() string { return n.name } + +// SetName sets the Adaptor Name func (n *Adaptor) SetName(name string) { n.name = name } -func (n *Adaptor) Port() string { return n.port } + +// Port returns the Adaptor port +func (n *Adaptor) Port() string { return n.port } // Connect returns true if connection to device is successful func (n *Adaptor) Connect() error { - if sp, err := n.connect(n); err != nil { + sp, err := n.connect(n) + if err != nil { return err - } else { - n.sp = sp } + + n.sp = sp return nil } diff --git a/platforms/neurosky/neurosky_driver.go b/platforms/neurosky/neurosky_driver.go index 6f0c1dda..fb3f990c 100644 --- a/platforms/neurosky/neurosky_driver.go +++ b/platforms/neurosky/neurosky_driver.go @@ -6,36 +6,65 @@ import ( "github.com/hybridgroup/gobot" ) -const BTSync byte = 0xAA +const ( + // BTSync is the sync code + BTSync byte = 0xAA -// CodeEx Extended code -const CodeEx byte = 0x55 + // CodeEx Extended code + CodeEx byte = 0x55 -// CodeSignalQuality POOR_SIGNAL quality 0-255 -const CodeSignalQuality byte = 0x02 + // CodeSignalQuality POOR_SIGNAL quality 0-255 + CodeSignalQuality byte = 0x02 -// CodeAttention ATTENTION eSense 0-100 -const CodeAttention byte = 0x04 + // CodeAttention ATTENTION eSense 0-100 + CodeAttention byte = 0x04 -// CodeMeditation MEDITATION eSense 0-100 -const CodeMeditation byte = 0x05 + // CodeMeditation MEDITATION eSense 0-100 + CodeMeditation byte = 0x05 -// CodeBlink BLINK strength 0-255 -const CodeBlink byte = 0x16 + // CodeBlink BLINK strength 0-255 + CodeBlink byte = 0x16 -// CodeWave RAW wave value: 2-byte big-endian 2s-complement -const CodeWave byte = 0x80 + // CodeWave RAW wave value: 2-byte big-endian 2s-complement + CodeWave byte = 0x80 -// CodeAsicEEG ASIC EEG POWER 8 3-byte big-endian integers -const CodeAsicEEG byte = 0x83 + // CodeAsicEEG ASIC EEG POWER 8 3-byte big-endian integers + CodeAsicEEG byte = 0x83 + // Extended event + Extended = "extended" + + // Signal event + Signal = "signal" + + // Attention event + Attention = "attention" + + // Meditation event + Meditation = "meditation" + + // Blink event + Blink = "blink" + + // Wave event + Wave = "wave" + + // EEG event + EEG = "eeg" + + // Error event + Error = "error" +) + +// Driver is the Gobot Driver for the Mindwave type Driver struct { name string connection gobot.Connection gobot.Eventer } -type EEG struct { +// EEGData is the EEG raw data returned from the Mindwave +type EEGData struct { Delta int Theta int LoAlpha int @@ -63,20 +92,26 @@ func NewDriver(a *Adaptor) *Driver { Eventer: gobot.NewEventer(), } - n.AddEvent("extended") - n.AddEvent("signal") - n.AddEvent("attention") - n.AddEvent("meditation") - n.AddEvent("blink") - n.AddEvent("wave") - n.AddEvent("eeg") - n.AddEvent("error") + n.AddEvent(Extended) + n.AddEvent(Signal) + n.AddEvent(Attention) + n.AddEvent(Meditation) + n.AddEvent(Blink) + n.AddEvent(Wave) + n.AddEvent(EEG) + n.AddEvent(Error) return n } + +// Connection returns the Driver's connection func (n *Driver) Connection() gobot.Connection { return n.connection } -func (n *Driver) Name() string { return n.name } -func (n *Driver) SetName(name string) { n.name = name } + +// Name returns the Driver name +func (n *Driver) Name() string { return n.name } + +// SetName sets the Driver name +func (n *Driver) SetName(name string) { n.name = name } // adaptor returns neurosky adaptor func (n *Driver) adaptor() *Adaptor { @@ -154,8 +189,8 @@ func (n *Driver) parsePacket(buf *bytes.Buffer) { } // parseEEG returns data converted into EEG map -func (n *Driver) parseEEG(data []byte) EEG { - return EEG{ +func (n *Driver) parseEEG(data []byte) EEGData { + return EEGData{ Delta: n.parse3ByteInteger(data[0:3]), Theta: n.parse3ByteInteger(data[3:6]), LoAlpha: n.parse3ByteInteger(data[6:9]), diff --git a/platforms/neurosky/neurosky_driver_test.go b/platforms/neurosky/neurosky_driver_test.go index 0ef15f97..304d5b6b 100644 --- a/platforms/neurosky/neurosky_driver_test.go +++ b/platforms/neurosky/neurosky_driver_test.go @@ -29,7 +29,7 @@ func TestNeuroskyDriver(t *testing.T) { func TestNeuroskyDriverStart(t *testing.T) { sem := make(chan bool, 0) d := initTestNeuroskyDriver() - d.Once(d.Event("error"), func(data interface{}) { + d.Once(d.Event(Error), func(data interface{}) { gobottest.Assert(t, data.(error), errors.New("read error")) sem <- true }) @@ -64,7 +64,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 1, 0x55, 0x00})) }() - d.On(d.Event("extended"), func(data interface{}) { + d.On(d.Event(Extended), func(data interface{}) { sem <- true }) @@ -80,7 +80,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x02, 100, 0x00})) }() - d.On(d.Event("signal"), func(data interface{}) { + d.On(d.Event(Signal), func(data interface{}) { gobottest.Assert(t, data.(byte), byte(100)) sem <- true }) @@ -93,7 +93,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x04, 40, 0x00})) }() - d.On(d.Event("attention"), func(data interface{}) { + d.On(d.Event(Attention), func(data interface{}) { gobottest.Assert(t, data.(byte), byte(40)) sem <- true }) @@ -106,7 +106,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x05, 60, 0x00})) }() - d.On(d.Event("meditation"), func(data interface{}) { + d.On(d.Event(Meditation), func(data interface{}) { gobottest.Assert(t, data.(byte), byte(60)) sem <- true }) @@ -119,7 +119,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 2, 0x16, 150, 0x00})) }() - d.On(d.Event("blink"), func(data interface{}) { + d.On(d.Event(Blink), func(data interface{}) { gobottest.Assert(t, data.(byte), byte(150)) sem <- true }) @@ -132,7 +132,7 @@ func TestNeuroskyDriverParse(t *testing.T) { d.parse(bytes.NewBuffer([]byte{0xAA, 0xAA, 4, 0x80, 0x00, 0x40, 0x11, 0x00})) }() - d.On(d.Event("wave"), func(data interface{}) { + d.On(d.Event(Wave), func(data interface{}) { gobottest.Assert(t, data.(int16), int16(16401)) sem <- true }) @@ -147,10 +147,10 @@ func TestNeuroskyDriverParse(t *testing.T) { 108, 0x00})) }() - d.On(d.Event("eeg"), func(data interface{}) { + d.On(d.Event(EEG), func(data interface{}) { gobottest.Assert(t, - data.(EEG), - EEG{ + data.(EEGData), + EEGData{ Delta: 1573241, Theta: 5832801, LoAlpha: 1703966, From 156822358b8d1b67db65537f9940f8b173ab992e Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 14:58:58 +0100 Subject: [PATCH 15/27] docs: Add missing godocs for OpenCV platform Signed-off-by: deadprogram --- platforms/opencv/camera_driver.go | 10 ++++++++-- platforms/opencv/window_driver.go | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/platforms/opencv/camera_driver.go b/platforms/opencv/camera_driver.go index 61952f05..7a387e90 100644 --- a/platforms/opencv/camera_driver.go +++ b/platforms/opencv/camera_driver.go @@ -19,6 +19,7 @@ const ( Frame = "frame" ) +// CameraDriver is the Gobot Driver for the OpenCV camera type CameraDriver struct { name string camera capture @@ -58,8 +59,13 @@ func NewCameraDriver(source interface{}, v ...time.Duration) *CameraDriver { return c } -func (c *CameraDriver) Name() string { return c.name } -func (c *CameraDriver) SetName(n string) { c.name = n } +// Name returns the Driver name +func (c *CameraDriver) Name() string { return c.name } + +// SetName sets the Driver name +func (c *CameraDriver) SetName(n string) { c.name = n } + +// Connection returns the Driver's connection func (c *CameraDriver) Connection() gobot.Connection { return nil } // Start initializes camera by grabbing a frame diff --git a/platforms/opencv/window_driver.go b/platforms/opencv/window_driver.go index 6be51fd4..73d4a6a5 100644 --- a/platforms/opencv/window_driver.go +++ b/platforms/opencv/window_driver.go @@ -9,6 +9,7 @@ type window interface { ShowImage(*cv.IplImage) } +// WindowDriver is the Gobot Driver for the OpenCV window type WindowDriver struct { name string window window @@ -26,8 +27,13 @@ func NewWindowDriver() *WindowDriver { } } -func (w *WindowDriver) Name() string { return w.name } -func (w *WindowDriver) SetName(n string) { w.name = n } +// Name returns the Driver name +func (w *WindowDriver) Name() string { return w.name } + +// SetName sets the Driver name +func (w *WindowDriver) SetName(n string) { w.name = n } + +// Connection returns the Driver's connection func (w *WindowDriver) Connection() gobot.Connection { return nil } // Start starts window thread and driver From 5db81fa72337fd110bc45ff83fe333b32c93ca27 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 15:01:49 +0100 Subject: [PATCH 16/27] docs: Add missing godocs for Particle platform Signed-off-by: deadprogram --- platforms/particle/adaptor.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/platforms/particle/adaptor.go b/platforms/particle/adaptor.go index ef19cce8..3cc99eb5 100644 --- a/platforms/particle/adaptor.go +++ b/platforms/particle/adaptor.go @@ -13,6 +13,7 @@ import ( "github.com/hybridgroup/gobot" ) +// Adaptor is the Gobot Adaptor for Particle type Adaptor struct { name string DeviceID string @@ -21,6 +22,7 @@ type Adaptor struct { gobot.Eventer } +// Event is an event emitted by the Particle cloud type Event struct { Name string Data string @@ -47,7 +49,10 @@ func NewAdaptor(deviceID string, accessToken string) *Adaptor { } } -func (s *Adaptor) Name() string { return s.name } +// Name returns the Adaptor name +func (s *Adaptor) Name() string { return s.name } + +// SetName sets the Adaptor name func (s *Adaptor) SetName(n string) { s.name = n } // Connect returns true if connection to Particle Photon or Electron is successful From 833597bc3060e94508aa26baf80a36b851cdf1b7 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 15:08:01 +0100 Subject: [PATCH 17/27] raspi: Add missing godocs and small refactors for platform Signed-off-by: deadprogram --- platforms/raspi/raspi_adaptor.go | 95 ++------------------------- platforms/raspi/raspi_adaptor_test.go | 2 +- platforms/raspi/raspi_pin_map.go | 88 +++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 89 deletions(-) create mode 100644 platforms/raspi/raspi_pin_map.go diff --git a/platforms/raspi/raspi_adaptor.go b/platforms/raspi/raspi_adaptor.go index f14be977..c25aa963 100644 --- a/platforms/raspi/raspi_adaptor.go +++ b/platforms/raspi/raspi_adaptor.go @@ -17,6 +17,7 @@ var readFile = func() ([]byte, error) { return ioutil.ReadFile("/proc/cpuinfo") } +// Adaptor is the Gobot Adaptor for the Raspberry Pi type Adaptor struct { name string revision string @@ -26,93 +27,6 @@ type Adaptor struct { i2cDevice sysfs.I2cDevice } -var pins = map[string]map[string]int{ - "3": map[string]int{ - "1": 0, - "2": 2, - "3": 2, - }, - "5": map[string]int{ - "1": 1, - "2": 3, - "3": 3, - }, - "7": map[string]int{ - "*": 4, - }, - "8": map[string]int{ - "*": 14, - }, - "10": map[string]int{ - "*": 15, - }, - "11": map[string]int{ - "*": 17, - }, - "12": map[string]int{ - "*": 18, - }, - "13": map[string]int{ - "1": 21, - "2": 27, - "3": 27, - }, - "15": map[string]int{ - "*": 22, - }, - "16": map[string]int{ - "*": 23, - }, - "18": map[string]int{ - "*": 24, - }, - "19": map[string]int{ - "*": 10, - }, - "21": map[string]int{ - "*": 9, - }, - "22": map[string]int{ - "*": 25, - }, - "23": map[string]int{ - "*": 11, - }, - "24": map[string]int{ - "*": 8, - }, - "26": map[string]int{ - "*": 7, - }, - "29": map[string]int{ - "3": 5, - }, - "31": map[string]int{ - "3": 6, - }, - "32": map[string]int{ - "3": 12, - }, - "33": map[string]int{ - "3": 13, - }, - "35": map[string]int{ - "3": 19, - }, - "36": map[string]int{ - "3": 16, - }, - "37": map[string]int{ - "3": 26, - }, - "38": map[string]int{ - "3": 20, - }, - "40": map[string]int{ - "3": 21, - }, -} - // NewAdaptor creates a Raspi Adaptor func NewAdaptor() *Adaptor { r := &Adaptor{ @@ -140,7 +54,10 @@ func NewAdaptor() *Adaptor { return r } -func (r *Adaptor) Name() string { return r.name } +// Name returns the Adaptor's name +func (r *Adaptor) Name() string { return r.name } + +// SetName sets the Adaptor's name func (r *Adaptor) SetName(n string) { r.name = n } // Connect starts connection with board and creates @@ -271,6 +188,7 @@ func (r *Adaptor) I2cRead(address int, size int) (data []byte, err error) { return } +// PwmWrite writes a PWM signal to the specified pin func (r *Adaptor) PwmWrite(pin string, val byte) (err error) { sysfsPin, err := r.pwmPin(pin) if err != nil { @@ -279,6 +197,7 @@ func (r *Adaptor) PwmWrite(pin string, val byte) (err error) { return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, gobot.FromScale(float64(val), 0, 255))) } +// ServoWrite writes a servo signal to the specified pin func (r *Adaptor) ServoWrite(pin string, angle byte) (err error) { sysfsPin, err := r.pwmPin(pin) if err != nil { diff --git a/platforms/raspi/raspi_adaptor_test.go b/platforms/raspi/raspi_adaptor_test.go index 751f3445..d50001e4 100644 --- a/platforms/raspi/raspi_adaptor_test.go +++ b/platforms/raspi/raspi_adaptor_test.go @@ -38,7 +38,7 @@ func (n *NullReadWriteCloser) Read(b []byte) (int, error) { return len(b), nil } -var closeErr error = nil +var closeErr error func (n *NullReadWriteCloser) Close() error { return closeErr diff --git a/platforms/raspi/raspi_pin_map.go b/platforms/raspi/raspi_pin_map.go new file mode 100644 index 00000000..429441bc --- /dev/null +++ b/platforms/raspi/raspi_pin_map.go @@ -0,0 +1,88 @@ +package raspi + +var pins = map[string]map[string]int{ + "3": map[string]int{ + "1": 0, + "2": 2, + "3": 2, + }, + "5": map[string]int{ + "1": 1, + "2": 3, + "3": 3, + }, + "7": map[string]int{ + "*": 4, + }, + "8": map[string]int{ + "*": 14, + }, + "10": map[string]int{ + "*": 15, + }, + "11": map[string]int{ + "*": 17, + }, + "12": map[string]int{ + "*": 18, + }, + "13": map[string]int{ + "1": 21, + "2": 27, + "3": 27, + }, + "15": map[string]int{ + "*": 22, + }, + "16": map[string]int{ + "*": 23, + }, + "18": map[string]int{ + "*": 24, + }, + "19": map[string]int{ + "*": 10, + }, + "21": map[string]int{ + "*": 9, + }, + "22": map[string]int{ + "*": 25, + }, + "23": map[string]int{ + "*": 11, + }, + "24": map[string]int{ + "*": 8, + }, + "26": map[string]int{ + "*": 7, + }, + "29": map[string]int{ + "3": 5, + }, + "31": map[string]int{ + "3": 6, + }, + "32": map[string]int{ + "3": 12, + }, + "33": map[string]int{ + "3": 13, + }, + "35": map[string]int{ + "3": 19, + }, + "36": map[string]int{ + "3": 16, + }, + "37": map[string]int{ + "3": 26, + }, + "38": map[string]int{ + "3": 20, + }, + "40": map[string]int{ + "3": 21, + }, +} From 9e7b01e575a6dc615afa10ca725598afcb2c404d Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 16:08:56 +0100 Subject: [PATCH 18/27] docs: Add missing godocs for Sphero platform Signed-off-by: deadprogram --- platforms/sphero/sphero_adaptor.go | 22 +++++++++++++++------- platforms/sphero/sphero_driver.go | 15 ++++++++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/platforms/sphero/sphero_adaptor.go b/platforms/sphero/sphero_adaptor.go index fd8c0d90..94da8b11 100644 --- a/platforms/sphero/sphero_adaptor.go +++ b/platforms/sphero/sphero_adaptor.go @@ -6,7 +6,7 @@ import ( "github.com/tarm/serial" ) -// Represents a Connection to a Sphero +// Adaptor represents a Connection to a Sphero type Adaptor struct { name string port string @@ -26,19 +26,27 @@ func NewAdaptor(port string) *Adaptor { } } -func (a *Adaptor) Name() string { return a.name } +// Name returns the Adaptor's name +func (a *Adaptor) Name() string { return a.name } + +// SetName sets the Adaptor's name func (a *Adaptor) SetName(n string) { a.name = n } -func (a *Adaptor) Port() string { return a.port } + +// Port returns the Adaptor's port +func (a *Adaptor) Port() string { return a.port } + +// SetPort sets the Adaptor's port func (a *Adaptor) SetPort(p string) { a.port = p } // Connect initiates a connection to the Sphero. Returns true on successful connection. func (a *Adaptor) Connect() (err error) { - if sp, e := a.connect(a.Port()); e != nil { + sp, e := a.connect(a.Port()) + if e != nil { return e - } else { - a.sp = sp - a.connected = true } + + a.sp = sp + a.connected = true return } diff --git a/platforms/sphero/sphero_driver.go b/platforms/sphero/sphero_driver.go index d9a2df2a..2f0bd2f5 100644 --- a/platforms/sphero/sphero_driver.go +++ b/platforms/sphero/sphero_driver.go @@ -26,7 +26,7 @@ type packet struct { checksum uint8 } -// Represents a Sphero 2.0 +// SpheroDriver Represents a Sphero 2.0 type SpheroDriver struct { name string connection gobot.Connection @@ -142,8 +142,13 @@ func NewSpheroDriver(a *Adaptor) *SpheroDriver { return s } -func (s *SpheroDriver) Name() string { return s.name } -func (s *SpheroDriver) SetName(n string) { s.name = n } +// Name returns the Driver Name +func (s *SpheroDriver) Name() string { return s.name } + +// SetName sets the Driver Name +func (s *SpheroDriver) SetName(n string) { s.name = n } + +// Connection returns the Driver's Connection func (s *SpheroDriver) Connection() gobot.Connection { return s.connection } func (s *SpheroDriver) adaptor() *Adaptor { @@ -283,7 +288,7 @@ func (s *SpheroDriver) Roll(speed uint8, heading uint16) { s.packetChannel <- s.craftPacket([]uint8{speed, uint8(heading >> 8), uint8(heading & 0xFF), 0x01}, 0x02, 0x30) } -// Configures and enables the Locator +// ConfigureLocator configures and enables the Locator func (s *SpheroDriver) ConfigureLocator(d LocatorConfig) { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, d) @@ -291,7 +296,7 @@ func (s *SpheroDriver) ConfigureLocator(d LocatorConfig) { s.packetChannel <- s.craftPacket(buf.Bytes(), 0x02, 0x13) } -// Enables sensor data streaming +// SetDataStreaming enables sensor data streaming func (s *SpheroDriver) SetDataStreaming(d DataStreamingConfig) { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, d) From 604d2342441d98d9211f1827bca6bd583c32b0db Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 20:40:11 +0100 Subject: [PATCH 19/27] core: use multierror when handling Robot Stop Signed-off-by: deadprogram --- robot.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/robot.go b/robot.go index e96503af..4415f13c 100644 --- a/robot.go +++ b/robot.go @@ -185,12 +185,20 @@ func (r *Robot) Start(args ...interface{}) (err error) { } // Stop stops a Robot's connections and Devices -func (r *Robot) Stop() (err error) { +func (r *Robot) Stop() error { + var result error log.Println("Stopping Robot", r.Name, "...") - err = r.Devices().Halt() + err := r.Devices().Halt() + if err != nil { + result = multierror.Append(result, err) + } err = r.Connections().Finalize() + if err != nil { + result = multierror.Append(result, err) + } + r.done <- true - return err + return result } // Devices returns all devices associated with this Robot. From 801b1096548375642daa15fb221d6cdc9f5b5239 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 1 Dec 2016 20:40:58 +0100 Subject: [PATCH 20/27] docs: Add missing godocs for Commander type Signed-off-by: deadprogram --- commander.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commander.go b/commander.go index c127b9c9..926c55b6 100644 --- a/commander.go +++ b/commander.go @@ -22,15 +22,18 @@ func NewCommander() Commander { } } +// Command returns the command interface whene passed a valid command name func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}) { command, _ = c.commands[name] return } +// Commands returns the entire map of valid commands func (c *commander) Commands() map[string]func(map[string]interface{}) interface{} { return c.commands } +// AddCommand adds a new command, when passed a command name and the command interface. func (c *commander) AddCommand(name string, command func(map[string]interface{}) interface{}) { c.commands[name] = command } From 3612b209f459569a786c65b6e36ac36159779b08 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 2 Dec 2016 00:01:05 +0100 Subject: [PATCH 21/27] chip: correct ]base for new 4.4 GPIO Signed-off-by: deadprogram --- examples/chip_blink.go | 2 +- platforms/chip/chip_adaptor.go | 16 ++++++++-------- platforms/chip/chip_adaptor_test.go | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/chip_blink.go b/examples/chip_blink.go index 01de5f76..0a629178 100644 --- a/examples/chip_blink.go +++ b/examples/chip_blink.go @@ -10,7 +10,7 @@ import ( func main() { chipAdaptor := chip.NewAdaptor() - led := gpio.NewLedDriver(chipAdaptor, "XIO-P0") + led := gpio.NewLedDriver(chipAdaptor, "XIO-P6") work := func() { gobot.Every(1*time.Second, func() { diff --git a/platforms/chip/chip_adaptor.go b/platforms/chip/chip_adaptor.go index ad3ba800..b3c14aad 100644 --- a/platforms/chip/chip_adaptor.go +++ b/platforms/chip/chip_adaptor.go @@ -29,14 +29,14 @@ var pinsOriginal = map[string]int{ } var pins44 = map[string]int{ - "XIO-P0": 1016, - "XIO-P1": 1017, - "XIO-P2": 1018, - "XIO-P3": 1019, - "XIO-P4": 1020, - "XIO-P5": 1021, - "XIO-P6": 1022, - "XIO-P7": 1023, + "XIO-P0": 1013, + "XIO-P1": 1014, + "XIO-P2": 1015, + "XIO-P3": 1016, + "XIO-P4": 1017, + "XIO-P5": 1018, + "XIO-P6": 1019, + "XIO-P7": 1020, } // NewAdaptor creates a C.H.I.P. Adaptor diff --git a/platforms/chip/chip_adaptor_test.go b/platforms/chip/chip_adaptor_test.go index 749e4fbb..e2f3e9ed 100644 --- a/platforms/chip/chip_adaptor_test.go +++ b/platforms/chip/chip_adaptor_test.go @@ -55,18 +55,18 @@ func TestChipAdaptorDigitalIO(t *testing.T) { fs := sysfs.NewMockFilesystem([]string{ "/sys/class/gpio/export", "/sys/class/gpio/unexport", - "/sys/class/gpio/gpio1016/value", - "/sys/class/gpio/gpio1016/direction", - "/sys/class/gpio/gpio1023/value", - "/sys/class/gpio/gpio1023/direction", + "/sys/class/gpio/gpio1013/value", + "/sys/class/gpio/gpio1013/direction", + "/sys/class/gpio/gpio1020/value", + "/sys/class/gpio/gpio1020/direction", }) sysfs.SetFilesystem(fs) a.DigitalWrite("XIO-P0", 1) - gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio1016/value"].Contents, "1") + gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio1013/value"].Contents, "1") - fs.Files["/sys/class/gpio/gpio1023/value"].Contents = "1" + fs.Files["/sys/class/gpio/gpio1020/value"].Contents = "1" i, _ := a.DigitalRead("XIO-P7") gobottest.Assert(t, i, 1) From 50cacd5745ca1cb6c92893c23813895e10547224 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 2 Dec 2016 09:56:37 +0100 Subject: [PATCH 22/27] nats: refactor and skip tests that require actual server connection Signed-off-by: deadprogram --- .travis.yml | 6 ------ platforms/nats/nats_adaptor.go | 25 +++++++++-------------- platforms/nats/nats_adaptor_test.go | 31 +++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index a8762faf..efbc7191 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,12 +16,6 @@ before_install: - sudo add-apt-repository -y ppa:zoogie/sdl2-snapshots - sudo apt-get update - sudo apt-get install --force-yes libcv-dev libcvaux-dev libhighgui-dev libopencv-dev libsdl2-dev libsdl2-image-dev libsdl2 libusb-dev xvfb unzip libgtk2.0-0 - - mkdir -p gnatsd - - cd gnatsd - - "wget https://github.com/nats-io/gnatsd/releases/download/v0.9.4/gnatsd-v0.9.4-linux-amd64.zip" - - unzip -j gnatsd-v0.9.4-linux-amd64.zip - - ./gnatsd -p 4222 & - - ./gnatsd -p 4223 --user test --pass testwd & - cd $HOME/gopath/src/github.com/hybridgroup/gobot - go get github.com/axw/gocov/gocov - go get github.com/mattn/goveralls diff --git a/platforms/nats/nats_adaptor.go b/platforms/nats/nats_adaptor.go index 0ac38be2..fd557a11 100644 --- a/platforms/nats/nats_adaptor.go +++ b/platforms/nats/nats_adaptor.go @@ -1,9 +1,6 @@ package nats -import ( - multierror "github.com/hashicorp/go-multierror" - "github.com/nats-io/nats" -) +import "github.com/nats-io/nats" // Adaptor is a configuration struct for interacting with a NATS server. // Name is a logical name for the adaptor/nats server connection. @@ -16,6 +13,7 @@ type Adaptor struct { username string password string client *nats.Conn + connect func() (*nats.Conn, error) } // NewAdaptor populates a new NATS Adaptor. @@ -24,6 +22,9 @@ func NewAdaptor(host string, clientID int) *Adaptor { name: "NATS", Host: host, clientID: clientID, + connect: func() (*nats.Conn, error) { + return nats.Connect("nats://" + host) + }, } } @@ -34,6 +35,9 @@ func NewAdaptorWithAuth(host string, clientID int, username string, password str clientID: clientID, username: username, password: password, + connect: func() (*nats.Conn, error) { + return nats.Connect("nats://" + username + ":" + password + "@" + host) + }, } } @@ -45,18 +49,7 @@ func (a *Adaptor) SetName(n string) { a.name = n } // Connect makes a connection to the Nats server. func (a *Adaptor) Connect() (err error) { - auth := "" - if a.username != "" && a.password != "" { - auth = a.username + ":" + a.password + "@" - } - - defaultURL := "nats://" + auth + a.Host - - var e error - a.client, e = nats.Connect(defaultURL) - if e != nil { - err = multierror.Append(err, e) - } + a.client, err = a.connect() return } diff --git a/platforms/nats/nats_adaptor_test.go b/platforms/nats/nats_adaptor_test.go index 65f6c744..eb8dbc8f 100644 --- a/platforms/nats/nats_adaptor_test.go +++ b/platforms/nats/nats_adaptor_test.go @@ -5,41 +5,58 @@ import ( "fmt" "testing" - multierror "github.com/hashicorp/go-multierror" "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/gobottest" + "github.com/nats-io/nats" ) var _ gobot.Adaptor = (*Adaptor)(nil) -func TestNatsAdaptorReturnsHost(t *testing.T) { +func initTestNatsAdaptor() *Adaptor { a := NewAdaptor("localhost:4222", 9999) + a.connect = func() (*nats.Conn, error) { + c := &nats.Conn{} + return c, nil + } + return a +} + +func TestNatsAdaptorReturnsHost(t *testing.T) { + a := initTestNatsAdaptor() gobottest.Assert(t, a.Host, "localhost:4222") } +// TODO: implement this test without requiring actual server connection func TestNatsAdaptorPublishWhenConnected(t *testing.T) { - a := NewAdaptor("localhost:4222", 9999) + t.Skip("TODO: implement this test without requiring actual server connection") + a := initTestNatsAdaptor() a.Connect() data := []byte("o") gobottest.Assert(t, a.Publish("test", data), true) } +// TODO: implement this test without requiring actual server connection func TestNatsAdaptorOnWhenConnected(t *testing.T) { - a := NewAdaptor("localhost:4222", 9999) + t.Skip("TODO: implement this test without requiring actual server connection") + a := initTestNatsAdaptor() a.Connect() gobottest.Assert(t, a.On("hola", func(data []byte) { fmt.Println("hola") }), true) } +// TODO: implement this test without requiring actual server connection func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) { + t.Skip("TODO: implement this test without requiring actual server connection") a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd") a.Connect() data := []byte("o") gobottest.Assert(t, a.Publish("test", data), true) } +// TODO: implement this test without requiring actual server connection func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) { + t.Skip("TODO: implement this test without requiring actual server connection") a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd") a.Connect() gobottest.Assert(t, a.On("hola", func(data []byte) { @@ -47,11 +64,9 @@ func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) { }), true) } -func TestNatsAdaptorConnect(t *testing.T) { +func TestNatsAdaptorFailedConnect(t *testing.T) { a := NewAdaptor("localhost:9999", 9999) - var expected error - expected = multierror.Append(expected, errors.New("nats: no servers available for connection")) - gobottest.Assert(t, a.Connect(), expected) + gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection")) } func TestNatsAdaptorFinalize(t *testing.T) { From 470fa5b6dd07835110503eb8901861a67f3e290f Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 2 Dec 2016 10:05:54 +0100 Subject: [PATCH 23/27] nats: corrected godocs comment Signed-off-by: deadprogram --- platforms/nats/nats_adaptor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/nats/nats_adaptor.go b/platforms/nats/nats_adaptor.go index fd557a11..32484565 100644 --- a/platforms/nats/nats_adaptor.go +++ b/platforms/nats/nats_adaptor.go @@ -53,7 +53,7 @@ func (a *Adaptor) Connect() (err error) { return } -// Disconnect from the nats server. Returns an error if the client doesn't exist. +// Disconnect from the nats server. func (a *Adaptor) Disconnect() (err error) { if a.client != nil { a.client.Close() From 4c724bfb672d9c629ba7b7f2e1ebce3f24e2b9a3 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 3 Dec 2016 10:12:09 +0100 Subject: [PATCH 24/27] docs: update Beaglebone README for latest install info Signed-off-by: deadprogram --- platforms/beaglebone/README.md | 59 ++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/platforms/beaglebone/README.md b/platforms/beaglebone/README.md index bee82a81..c6de6a72 100644 --- a/platforms/beaglebone/README.md +++ b/platforms/beaglebone/README.md @@ -5,31 +5,11 @@ The BeagleBone is an ARM based single board computer, with many different GPIO i For more info about the BeagleBone platform click [here](http://beagleboard.org/Products/BeagleBone+Black). ## How to Install + ``` go get -d -u github.com/hybridgroup/gobot/... && go install github.com/hybridgroup/gobot/platforms/beaglebone ``` -## Cross compiling for the Beaglebone Black -You must first configure your Go environment for arm linux cross compiling - -```bash -$ cd $GOROOT/src -$ GOOS=linux GOARCH=arm ./make.bash --no-clean -``` - -Then compile your Gobot program with - -```bash -$ GOARM=7 GOARCH=arm GOOS=linux go build examples/beaglebone_blink.go -``` - -If you are running the official Angstrom or Debian linux through the usb->ethernet connection, you can simply upload your program and execute it with - -```bash -$ scp beaglebone_blink root@192.168.7.2:/home/root/ -$ ssh -t root@192.168.7.2 "./beaglebone_blink" -``` - ## How to Use ```go @@ -62,3 +42,40 @@ func main() { robot.Start() } ``` + +## How to Connect + +### Compiling + +Simply compile your Gobot program like this: + +```bash +$ GOARM=7 GOARCH=arm GOOS=linux go build examples/beaglebone_blink.go +``` + +If you are running the official Debian Linux through the usb->ethernet connection, or are connected to the board using WiFi, you can simply upload your program and execute it with the `scp` command like this: + +```bash +$ scp beaglebone_blink root@192.168.7.2:/home/root/ +$ ssh -t root@192.168.7.2 "./beaglebone_blink" +``` + +### Updating your board to the latest OS + +We recommend updating your BeagleBone to the latest Debian OS. It is very easy to do this using the Etcher (https://etcher.io/) utility program. + +First, download the latest BeagleBone OS from http://beagleboard.org/latest-images + +Now, use Etcher to create an SD card with the OS image you have downloaded. + +Once you have created the SD card, boot your BeagleBone using the new image as follows: + +- Insert SD card into your (powered-down) board, hold down the USER/BOOT button (if using Black) and apply power, either by the USB cable or 5V adapter. + +- If using an original BeagleBone, you are done. + +- If using BeagleBone Black and desire to write the image to your on-board eMMC, you'll need to follow the instructions at http://elinux.org/Beagleboard:BeagleBoneBlack_Debian#Flashing_eMMC. When the flashing is complete, all 4 USRx LEDs will be steady on or off. The latest Debian flasher images automatically power down the board upon completion. This can take up to 45 minutes. Power-down your board, remove the SD card and apply power again to be complete. + +These instructions come from the Beagleboard web site's "Getting Started" page located here: + +http://beagleboard.org/getting-started From fc2d98f9e73a0a61aa318ab12aa2c3f7be008f06 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 3 Dec 2016 13:17:31 +0100 Subject: [PATCH 25/27] beagleboard: now works on kernel 4.4+ of Debian Signed-off-by: deadprogram --- examples/beaglebone_blink_usr_led.go | 2 +- platforms/beaglebone/README.md | 4 +- platforms/beaglebone/beaglebone_adaptor.go | 51 ++++++++++++------- .../beaglebone/beaglebone_adaptor_test.go | 10 ++-- platforms/beaglebone/beaglebone_pins.go | 12 ++++- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/examples/beaglebone_blink_usr_led.go b/examples/beaglebone_blink_usr_led.go index 34479e4b..3774fc0c 100644 --- a/examples/beaglebone_blink_usr_led.go +++ b/examples/beaglebone_blink_usr_led.go @@ -10,7 +10,7 @@ import ( func main() { beagleboneAdaptor := beaglebone.NewAdaptor() - led := gpio.NewLedDriver(beagleboneAdaptor, "usr0") + led := gpio.NewLedDriver(beagleboneAdaptor, "usr1") work := func() { gobot.Every(1*time.Second, func() { diff --git a/platforms/beaglebone/README.md b/platforms/beaglebone/README.md index c6de6a72..8cc6dcca 100644 --- a/platforms/beaglebone/README.md +++ b/platforms/beaglebone/README.md @@ -50,7 +50,7 @@ func main() { Simply compile your Gobot program like this: ```bash -$ GOARM=7 GOARCH=arm GOOS=linux go build examples/beaglebone_blink.go +$ GOARCH=arm GOOS=linux go build examples/beaglebone_blink.go ``` If you are running the official Debian Linux through the usb->ethernet connection, or are connected to the board using WiFi, you can simply upload your program and execute it with the `scp` command like this: @@ -72,7 +72,7 @@ Once you have created the SD card, boot your BeagleBone using the new image as f - Insert SD card into your (powered-down) board, hold down the USER/BOOT button (if using Black) and apply power, either by the USB cable or 5V adapter. -- If using an original BeagleBone, you are done. +- If all you want to do it boot once from the SD card, it should now be booting. - If using BeagleBone Black and desire to write the image to your on-board eMMC, you'll need to follow the instructions at http://elinux.org/Beagleboard:BeagleBoneBlack_Debian#Flashing_eMMC. When the flashing is complete, all 4 USRx LEDs will be steady on or off. The latest Debian flasher images automatically power down the board upon completion. This can take up to 45 minutes. Power-down your board, remove the SD card and apply power again to be complete. diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index cd81c390..c510d859 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -21,15 +21,16 @@ var glob = func(pattern string) (matches []string, err error) { // Adaptor is the gobot.Adaptor representation for the Beaglebone type Adaptor struct { - name string - kernel string - digitalPins []sysfs.DigitalPin - pwmPins map[string]*pwmPin - i2cDevice sysfs.I2cDevice - usrLed string - ocp string - helper string - slots string + name string + kernel string + digitalPins []sysfs.DigitalPin + pwmPins map[string]*pwmPin + i2cDevice sysfs.I2cDevice + usrLed string + ocp string + analogPath string + analogPinMap map[string]string + slots string } // NewAdaptor returns a new Beaglebone Adaptor @@ -74,20 +75,32 @@ func (b *Adaptor) Kernel() string { return b.kernel } // Connect initializes the pwm and analog dts. func (b *Adaptor) Connect() error { - if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil { - return err + // enable analog + if b.kernel[:1] == "4" { + if err := ensureSlot(b.slots, "BB-ADC"); err != nil { + return err + } + + b.analogPath = "/sys/bus/iio/devices/iio:device0" + b.analogPinMap = analogPins44 + } else { + if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil { + return err + } + + g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp)) + if err != nil { + return err + } + b.analogPath = g[0] + b.analogPinMap = analogPins3 } + // enable pwm if err := ensureSlot(b.slots, "am33xx_pwm"); err != nil { return err } - g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp)) - if err != nil { - return err - } - b.helper = g[0] - return nil } @@ -165,7 +178,7 @@ func (b *Adaptor) AnalogRead(pin string) (val int, err error) { if err != nil { return } - fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.helper, analogPin), os.O_RDONLY, 0644) + fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644) defer fi.Close() if err != nil { @@ -233,7 +246,7 @@ func (b *Adaptor) translatePwmPin(pin string) (value string, err error) { // translateAnalogPin converts analog pin name to pin position func (b *Adaptor) translateAnalogPin(pin string) (value string, err error) { - for key, value := range analogPins { + for key, value := range b.analogPinMap { if key == pin { return value, nil } diff --git a/platforms/beaglebone/beaglebone_adaptor_test.go b/platforms/beaglebone/beaglebone_adaptor_test.go index f9132284..4316c565 100644 --- a/platforms/beaglebone/beaglebone_adaptor_test.go +++ b/platforms/beaglebone/beaglebone_adaptor_test.go @@ -57,8 +57,8 @@ func TestBeagleboneAdaptor(t *testing.T) { "/sys/devices/platform/bone_capemgr", "/sys/devices/platform/ocp/ocp4", "/sys/class/leds/beaglebone:green:usr1/brightness", - "/sys/devices/platform/ocp/ocp4/helper.5", - "/sys/devices/platform/ocp/ocp4/helper.5/AIN1", + "/sys/bus/iio/devices/iio:device0", + "/sys/bus/iio/devices/iio:device0/in_voltage1_raw", "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5", "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/run", "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period", @@ -79,7 +79,7 @@ func TestBeagleboneAdaptor(t *testing.T) { a.Connect() - a.helper = "/sys/devices/platform/ocp/ocp4/helper.5" + a.analogPath = "/sys/bus/iio/devices/iio:device0" // PWM glob = func(pattern string) (matches []string, err error) { @@ -113,11 +113,11 @@ func TestBeagleboneAdaptor(t *testing.T) { ) // Analog - fs.Files["/sys/devices/platform/ocp/ocp4/helper.5/AIN1"].Contents = "567\n" + fs.Files["/sys/bus/iio/devices/iio:device0/in_voltage1_raw"].Contents = "567\n" i, _ := a.AnalogRead("P9_40") gobottest.Assert(t, i, 567) - i, err := a.AnalogRead("P9_99") + _, err := a.AnalogRead("P9_99") gobottest.Assert(t, err, errors.New("Not a valid pin")) // DigitalIO diff --git a/platforms/beaglebone/beaglebone_pins.go b/platforms/beaglebone/beaglebone_pins.go index b0b7faca..693af349 100644 --- a/platforms/beaglebone/beaglebone_pins.go +++ b/platforms/beaglebone/beaglebone_pins.go @@ -80,7 +80,7 @@ var pwmPins = map[string]string{ "P8_46": "P8_46", } -var analogPins = map[string]string{ +var analogPins3 = map[string]string{ "P9_39": "AIN0", "P9_40": "AIN1", "P9_37": "AIN2", @@ -89,3 +89,13 @@ var analogPins = map[string]string{ "P9_36": "AIN5", "P9_35": "AIN6", } + +var analogPins44 = map[string]string{ + "P9_39": "in_voltage0_raw", + "P9_40": "in_voltage1_raw", + "P9_37": "in_voltage2_raw", + "P9_38": "in_voltage3_raw", + "P9_33": "in_voltage4_raw", + "P9_36": "in_voltage5_raw", + "P9_35": "in_voltage6_raw", +} From 8b985415639b9fcda67d6af4e5fdfbf6f6d7868e Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 3 Dec 2016 14:03:05 +0100 Subject: [PATCH 26/27] beaglebone: more testing of 4.4 kernel Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 1 + platforms/beaglebone/beaglebone_adaptor_test.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index c510d859..55dc5dda 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -178,6 +178,7 @@ func (b *Adaptor) AnalogRead(pin string) (val int, err error) { if err != nil { return } + fmt.Printf("%v/%v", b.analogPath, analogPin) fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644) defer fi.Close() diff --git a/platforms/beaglebone/beaglebone_adaptor_test.go b/platforms/beaglebone/beaglebone_adaptor_test.go index 4316c565..1926a8aa 100644 --- a/platforms/beaglebone/beaglebone_adaptor_test.go +++ b/platforms/beaglebone/beaglebone_adaptor_test.go @@ -57,7 +57,6 @@ func TestBeagleboneAdaptor(t *testing.T) { "/sys/devices/platform/bone_capemgr", "/sys/devices/platform/ocp/ocp4", "/sys/class/leds/beaglebone:green:usr1/brightness", - "/sys/bus/iio/devices/iio:device0", "/sys/bus/iio/devices/iio:device0/in_voltage1_raw", "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5", "/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/run", From 50394b6f5ab357fcb8536e418bfcb970365b06a7 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 3 Dec 2016 14:09:38 +0100 Subject: [PATCH 27/27] beaglebone: set test target kernel version Signed-off-by: deadprogram --- platforms/beaglebone/beaglebone_adaptor.go | 1 - platforms/beaglebone/beaglebone_adaptor_test.go | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/beaglebone/beaglebone_adaptor.go b/platforms/beaglebone/beaglebone_adaptor.go index 55dc5dda..c510d859 100644 --- a/platforms/beaglebone/beaglebone_adaptor.go +++ b/platforms/beaglebone/beaglebone_adaptor.go @@ -178,7 +178,6 @@ func (b *Adaptor) AnalogRead(pin string) (val int, err error) { if err != nil { return } - fmt.Printf("%v/%v", b.analogPath, analogPin) fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644) defer fi.Close() diff --git a/platforms/beaglebone/beaglebone_adaptor_test.go b/platforms/beaglebone/beaglebone_adaptor_test.go index 1926a8aa..84d96de7 100644 --- a/platforms/beaglebone/beaglebone_adaptor_test.go +++ b/platforms/beaglebone/beaglebone_adaptor_test.go @@ -75,6 +75,7 @@ func TestBeagleboneAdaptor(t *testing.T) { a := NewAdaptor() a.slots = "/sys/devices/platform/bone_capemgr" a.ocp = "/sys/devices/platform/ocp/ocp4" + a.kernel = "4.4" a.Connect()