Skip to content

Commit

Permalink
examples: fix some missing checks of return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas committed Feb 11, 2024
1 parent d96aa52 commit 1929e40
Show file tree
Hide file tree
Showing 307 changed files with 2,805 additions and 1,243 deletions.
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,27 @@ jobs:
SOME=$(grep -L 'digispark' $(grep -L 'gocv' ${ALL}))
for e in ${SOME} ; do go vet "${e}" ; done
"fmt_check_examples":
docker:
- image: golangci/golangci-lint:v1.55.2
steps:
- checkout
- run:
name: Debug linter version
command: golangci-lint --version
- run:
# digispark needs libusb, opencv needs opencv
name: Check examples (except digispark, opencv)
command: |
ALL=$(grep -l -r --include "*.go" 'build example' ./)
SOME=$(grep -L 'digispark' $(grep -L 'gocv' ${ALL}))
for e in ${SOME} ; do golangci-lint run "${e}" --build-tags example --disable forcetypeassert --disable noctx ; done
workflows:
version: 2
build:
jobs:
- "test_core_and_drivers_with_coverage"
- "test_platforms"
- "check_examples"
- "fmt_check_examples"
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ EXAMPLES_NO_GOCV := $(shell grep -L 'gocv' $(ALL_EXAMPLES))
# used examples
EXAMPLES := $(EXAMPLES_NO_GOCV)

.PHONY: test test_race test_cover robeaux version_check fmt_check fmt_fix examples examples_check $(EXAMPLES)
.PHONY: test test_race test_cover robeaux version_check fmt_check fmt_fix examples examples_check examples_fmt_fix $(EXAMPLES)

# opencv platform currently skipped to prevent install of preconditions
including_except := $(shell go list ./... | grep -v platforms/opencv)
Expand Down Expand Up @@ -65,9 +65,15 @@ examples: $(EXAMPLES)
examples_check:
$(MAKE) CHECK=ON examples

examples_fmt_fix:
$(MAKE) CHECK=FMT examples

$(EXAMPLES):
ifeq ($(CHECK),ON)
go vet ./$@
else ifeq ($(CHECK),FMT)
gofumpt -l -w ./$@
golangci-lint run ./$@ --fix --build-tags example --disable forcetypeassert --disable noctx
else
go build -o /tmp/gobot_examples/$@ ./$@
endif
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func main() {

work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
})
}

Expand All @@ -87,7 +89,9 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
```

Expand Down Expand Up @@ -121,7 +125,9 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
```

Expand All @@ -141,13 +147,19 @@ import (

func main() {
e := edison.NewAdaptor()
e.Connect()
if err := e.Connect(); err != nil {
fmt.Println(err)
}

led := gpio.NewLedDriver(e, "13")
led.Start()
if err := led.Start(); err != nil {
fmt.Println(err)
}

for {
led.Toggle()
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
time.Sleep(1000 * time.Millisecond)
}
}
Expand Down Expand Up @@ -179,7 +191,7 @@ func NewSwarmBot(port string) *gobot.Robot {
work := func() {
spheroDriver.Stop()

spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
fmt.Println("Collision Detected!")
})

Expand Down Expand Up @@ -218,7 +230,9 @@ func main() {
master.AddRobot(NewSwarmBot(port))
}

master.Start()
if err := master.Start(); err != nil {
panic(err)
}
}
```

Expand Down
26 changes: 19 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Here is a "Classic Gobot" program that blinks an LED using an Arduino:
work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
})
}
Expand All @@ -37,7 +39,9 @@ Here is a "Classic Gobot" program that blinks an LED using an Arduino:
work,
)
robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
# Metal Gobot
Expand All @@ -55,13 +59,19 @@ pure idiomatic Golang code. For example:
func main() {
e := edison.NewAdaptor()
e.Connect()
if err := e.Connect(); err != nil {
fmt.Println(err)
}
led := gpio.NewLedDriver(e, "13")
led.Start()
if err := led.Start(); err != nil {
fmt.Println(err)
}
for {
led.Toggle()
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
time.Sleep(1000 * time.Millisecond)
}
}
Expand Down Expand Up @@ -90,7 +100,7 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm
work := func() {
spheroDriver.Stop()
spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
fmt.Println("Collision Detected!")
})
Expand Down Expand Up @@ -129,7 +139,9 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm
master.AddRobot(NewSwarmBot(port))
}
master.Start()
if err := master.Start(); err != nil {
panic(err)
}
}
Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.
Expand Down
4 changes: 2 additions & 2 deletions drivers/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import(
adaptor := sphero.NewAdaptor("/dev/rfcomm0")
spheroDriver := sphero.NewSpheroDriver(adaptor)
...
spheroDriver.On(sphero.Collision, func(data interface{}) {
_ = spheroDriver.On(sphero.Collision, func(data interface{}) {
...

// new
Expand All @@ -109,7 +109,7 @@ import(
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
spheroDriver := serial.NewSpheroDriver(adaptor)
...
spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
...
```
Expand Down
6 changes: 4 additions & 2 deletions examples/ardrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
drone := ardrone.NewDriver(ardroneAdaptor)

work := func() {
drone.On(ardrone.Flying, func(data interface{}) {
_ = drone.On(ardrone.Flying, func(data interface{}) {
gobot.After(3*time.Second, func() {
drone.Land()
})
Expand All @@ -32,5 +32,7 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
6 changes: 4 additions & 2 deletions examples/ardrone_face_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
window.WaitKey(1)
}
})
drone.On(ardrone.Flying, func(data interface{}) {
_ = drone.On(ardrone.Flying, func(data interface{}) {
gobot.After(1*time.Second, func() { drone.Up(0.2) })
gobot.After(2*time.Second, func() { drone.Hover() })
gobot.After(5*time.Second, func() {
Expand Down Expand Up @@ -83,5 +83,7 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
18 changes: 10 additions & 8 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ func main() {
rightY.Store(float64(0.0))

work := func() {
stick.On(joystick.SquarePress, func(data interface{}) {
_ = stick.On(joystick.SquarePress, func(data interface{}) {
drone.TakeOff()
})

stick.On(joystick.TrianglePress, func(data interface{}) {
_ = stick.On(joystick.TrianglePress, func(data interface{}) {
drone.Hover()
})

stick.On(joystick.XPress, func(data interface{}) {
_ = stick.On(joystick.XPress, func(data interface{}) {
drone.Land()
})

stick.On(joystick.LeftX, func(data interface{}) {
_ = stick.On(joystick.LeftX, func(data interface{}) {
val := float64(data.(int16))
leftX.Store(val)
})

stick.On(joystick.LeftY, func(data interface{}) {
_ = stick.On(joystick.LeftY, func(data interface{}) {
val := float64(data.(int16))
leftY.Store(val)
})

stick.On(joystick.RightX, func(data interface{}) {
_ = stick.On(joystick.RightX, func(data interface{}) {
val := float64(data.(int16))
rightX.Store(val)
})

stick.On(joystick.RightY, func(data interface{}) {
_ = stick.On(joystick.RightY, func(data interface{}) {
val := float64(data.(int16))
rightY.Store(val)
})
Expand Down Expand Up @@ -120,7 +120,9 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}

func getLeftStick() pair {
Expand Down
4 changes: 3 additions & 1 deletion examples/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
4 changes: 3 additions & 1 deletion examples/batty.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func main() {
})

gbot.AddRobot(r)
gbot.Start()
if err := gbot.Start(); err != nil {
fmt.Println(err)
}
}

var _ gobot.Adaptor = (*loopbackAdaptor)(nil)
Expand Down
10 changes: 8 additions & 2 deletions examples/beaglebone_basic_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package main

import (
"fmt"

"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/beaglebone"
)
Expand All @@ -17,11 +19,15 @@ func main() {
gpioPin := gpio.NewDirectPinDriver(beagleboneAdaptor, "P9_12")

// Initialize the internal representation of the pinout
beagleboneAdaptor.Connect()
if err := beagleboneAdaptor.Connect(); err != nil {
fmt.Println(err)
}

// Cast to byte because we are returning an int from a function
// and not passing in an int literal.
gpioPin.DigitalWrite(byte(myStateFunction()))
if err := gpioPin.DigitalWrite(byte(myStateFunction())); err != nil {
fmt.Println(err)
}
}

// myStateFunction determines what the GPIO state should be
Expand Down
9 changes: 7 additions & 2 deletions examples/beaglebone_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"fmt"
"time"

"gobot.io/x/gobot/v2"
Expand All @@ -20,7 +21,9 @@ func main() {

work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
})
}

Expand All @@ -30,5 +33,7 @@ func main() {
work,
)

robot.Start()
if err := robot.Start(); err != nil {
panic(err)
}
}
Loading

0 comments on commit 1929e40

Please sign in to comment.