Skip to content

Commit

Permalink
gpio(hcsr04): add driver for ultrasonic ranging module (hybridgroup#1012
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gen2thomas authored Oct 27, 2023
1 parent f7f4820 commit f219a40
Show file tree
Hide file tree
Showing 18 changed files with 987 additions and 8 deletions.
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

0 comments on commit f219a40

Please sign in to comment.