Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpio(hcsr04): add driver for ultrasonic ranging module #1012

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Support for many devices that use General Purpose Input/Output (GPIO) have
a shared set of drivers provided using the `gobot/drivers/gpio` package:

- [GPIO](https://en.wikipedia.org/wiki/General_Purpose_Input/Output) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/gpio)
- AIP1640 LED
- AIP1640 LED Dot Matrix/7 Segment Controller
- Button
- Buzzer
- Direct Pin
Expand All @@ -281,8 +281,11 @@ a shared set of drivers provided using the `gobot/drivers/gpio` package:
- Grove Magnetic Switch
- Grove Relay
- Grove Touch Sensor
- HC-SR04 Ultrasonic Ranging Module
- HD44780 LCD controller
- LED
- Makey Button
- MAX7219 LED Dot Matrix
- Motor
- Proximity Infra Red (PIR) Motion Sensor
- Relay
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ before_test:
build_script:
- go test -v -cpu=2 .
- go test -v -cpu=2 ./drivers/aio/...
- go test -v -cpu=2 ./drivers/i2c/...
- go test -v -cpu=2 ./platforms/ble/...
- go test -v -cpu=2 ./platforms/dji/...
- go test -v -cpu=2 ./platforms/firmata/...
- go test -v -cpu=2 ./platforms/ble/...
- go test -v -cpu=2 ./platforms/joystick/...
- go test -v -cpu=2 ./platforms/parrot/...
- go test -v -cpu=2 ./platforms/sphero/...
Expand Down
7 changes: 5 additions & 2 deletions drivers/gpio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/r

Gobot has a extensible system for connecting to hardware devices. The following GPIO devices are currently supported:

- AIP1640 LED Dot Matrix/7 Segment Controller
- Button
- Buzzer
- Direct Pin
- EasyDriver
- Grove Button
- Grove Buzzer
- Grove LED
- Grove Magnetic Switch
- Grove Relay
- Grove Touch Sensor
- HC-SR04 Ultrasonic Ranging Module
- HD44780 LCD controller
- LED
- Makey Button
- MAX7219 LED Dot Matrix
- Motor
- Proximity Infra Red (PIR) Motion Sensor
- Relay
- RGB LED
- Servo
- Stepper Motor
- TM1638 LED Controller

More drivers are coming soon...
62 changes: 62 additions & 0 deletions drivers/gpio/gpio.go → drivers/gpio/gpio_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package gpio

import (
"errors"
"sync"

"gobot.io/x/gobot/v2"
)

var (
Expand Down Expand Up @@ -61,3 +64,62 @@ type DigitalWriter interface {
type DigitalReader interface {
DigitalRead(string) (val int, err error)
}

// Driver implements the interface gobot.Driver.
type Driver struct {
name string
connection gobot.Adaptor
afterStart func() error
beforeHalt func() error
gobot.Commander
mutex *sync.Mutex // mutex often needed to ensure that write-read sequences are not interrupted
}

// NewDriver creates a new generic and basic gpio gobot driver.
func NewDriver(a gobot.Adaptor, name string) *Driver {
d := &Driver{
name: gobot.DefaultName(name),
connection: a,
afterStart: func() error { return nil },
beforeHalt: func() error { return nil },
Commander: gobot.NewCommander(),
mutex: &sync.Mutex{},
}

return d
}

// Name returns the name of the gpio device.
func (d *Driver) Name() string {
return d.name
}

// SetName sets the name of the gpio device.
func (d *Driver) SetName(name string) {
d.name = name
}

// Connection returns the connection of the gpio device.
func (d *Driver) Connection() gobot.Connection {
return d.connection.(gobot.Connection)
}

// Start initializes the gpio device.
func (d *Driver) Start() error {
d.mutex.Lock()
defer d.mutex.Unlock()

// currently there is nothing to do here for the driver

return d.afterStart()
}

// Halt halts the gpio device.
func (d *Driver) Halt() error {
d.mutex.Lock()
defer d.mutex.Unlock()

// currently there is nothing to do after halt for the driver

return d.beforeHalt()
}
78 changes: 78 additions & 0 deletions drivers/gpio/gpio_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package gpio

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"gobot.io/x/gobot/v2"
)

var _ gobot.Driver = (*Driver)(nil)

func initTestDriverWithStubbedAdaptor() (*Driver, *gpioTestAdaptor) {
a := newGpioTestAdaptor()
d := NewDriver(a, "GPIO_BASIC")
return d, a
}

func initTestDriver() *Driver {
d, _ := initTestDriverWithStubbedAdaptor()
return d
}

func TestNewDriver(t *testing.T) {
// arrange
a := newGpioTestAdaptor()
// act
var di interface{} = NewDriver(a, "GPIO_BASIC")
// assert
d, ok := di.(*Driver)
if !ok {
t.Errorf("NewDriver() should have returned a *Driver")
}
assert.Contains(t, d.name, "GPIO_BASIC")
assert.Equal(t, a, d.connection)
assert.NoError(t, d.afterStart())
assert.NoError(t, d.beforeHalt())
assert.NotNil(t, d.Commander)
assert.NotNil(t, d.mutex)
}

func TestSetName(t *testing.T) {
// arrange
d := initTestDriver()
// act
d.SetName("TESTME")
// assert
assert.Equal(t, "TESTME", d.Name())
}

func TestConnection(t *testing.T) {
// arrange
d, a := initTestDriverWithStubbedAdaptor()
// act, assert
assert.Equal(t, a, d.Connection())
}

func TestStart(t *testing.T) {
// arrange
d := initTestDriver()
// act, assert
assert.NoError(t, d.Start())
// arrange after start function
d.afterStart = func() error { return fmt.Errorf("after start error") }
// act, assert
assert.ErrorContains(t, d.Start(), "after start error")
}

func TestHalt(t *testing.T) {
// arrange
d := initTestDriver()
// act, assert
assert.NoError(t, d.Halt())
// arrange after start function
d.beforeHalt = func() error { return fmt.Errorf("before halt error") }
// act, assert
assert.ErrorContains(t, d.Halt(), "before halt error")
}
Loading