forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hybridgroup#448 from ulisesflynn/gopigo3
First cut of the GoPiGo3 driver, lacking grove peripherals
- Loading branch information
Showing
11 changed files
with
814 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPI | ||
|
||
This package provides drivers for [spi](https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus) devices. It must be used along with the [raspberry pi](https://gobot.io/documentation/platforms/raspi) adaptor that supports the needed interfaces for spi devices. This uses the experimental [spi package](https://github.com/golang/exp/tree/master/io/spi) which only works on linux systems. | ||
|
||
## Getting Started | ||
|
||
## Installing | ||
``` | ||
go get -d -u gobot.io/x/gobot/... | ||
``` | ||
|
||
## Hardware Support | ||
Gobot has a extensible system for connecting to hardware devices. The following spi devices are currently supported: | ||
|
||
More drivers are coming soon... | ||
|
||
## Using A Different Bus or Address | ||
|
||
You can set a different SPI address or SPI bus than the default when initializing your SPI drivers by using optional parameters. Here is an example: | ||
|
||
```go | ||
blinkm := spi.NewGoPiGo3DriverDriver(e, spi.WithBus(0), spi.WithAddress(0x10), spi.With) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
Package spi provides Gobot drivers for spi devices. | ||
Uses "golang.org/x/exp/io/spi" for spi | ||
Installing: | ||
go get -d -u gobot.io/x/gobot | ||
For further information refer to spi README: | ||
https://github.com/hybridgroup/gobot/blob/master/drivers/spi/README.md | ||
*/ | ||
package spi // import "gobot.io/x/gobot/drivers/spi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package spi | ||
|
||
import ( | ||
xspi "golang.org/x/exp/io/spi" | ||
"time" | ||
) | ||
|
||
const ( | ||
// BusNotInitialized is the initial value for a bus | ||
BusNotInitialized = -1 | ||
) | ||
|
||
type SPIOperations interface { | ||
Close() error | ||
SetBitOrder(o xspi.Order) error | ||
SetBitsPerWord(bits int) error | ||
SetCSChange(leaveEnabled bool) error | ||
SetDelay(t time.Duration) error | ||
SetMaxSpeed(speed int) error | ||
SetMode(mode xspi.Mode) error | ||
Tx(w, r []byte) error | ||
} | ||
|
||
// SPIDevice is the interface to a specific spi bus | ||
type SPIDevice interface { | ||
SPIOperations | ||
} | ||
|
||
// Connector lets Adaptors provide the interface for Drivers | ||
// to get access to the SPI buses on platforms that support SPI. | ||
type Connector interface { | ||
// GetConnection returns a connection to device at the specified bus. | ||
// Bus numbering starts at index 0, the range of valid buses is | ||
// platform specific. | ||
GetSpiConnection(busNum, mode int, maxSpeed int64) (device Connection, err error) | ||
|
||
// GetDefaultBus returns the default SPI bus index | ||
GetSpiDefaultBus() int | ||
|
||
// GetDefaultMode returns the default SPI mode (0/1/2/3) | ||
GetSpiDefaultMode() int | ||
|
||
// GetSpiDefaultMaxSpeed returns the max SPI speed | ||
GetSpiDefaultMaxSpeed() int64 | ||
} | ||
|
||
// Connection is a connection to an SPI device with a specified address | ||
// on a specific bus. Used as an alternative to the SPI interface. | ||
// Implements SPIOperations to talk to the device, wrapping the | ||
// calls in SetAddress to always target the specified device. | ||
// Provided by an Adaptor by implementing the SPIConnector interface. | ||
type Connection SPIOperations | ||
|
||
type SpiConnection struct { | ||
bus SPIDevice | ||
mode int | ||
maxSpeed int64 | ||
} | ||
|
||
// NewConnection creates and returns a new connection to a specific | ||
// spi device on a bus and address | ||
func NewConnection(bus SPIDevice) (connection *SpiConnection) { | ||
return &SpiConnection{bus: bus} | ||
} | ||
|
||
func (c *SpiConnection) Close() error { | ||
return c.bus.Close() | ||
} | ||
|
||
func (c *SpiConnection) SetBitOrder(o xspi.Order) error { | ||
return c.bus.SetBitOrder(o) | ||
} | ||
|
||
func (c *SpiConnection) SetBitsPerWord(bits int) error { | ||
return c.bus.SetBitsPerWord(bits) | ||
} | ||
|
||
func (c *SpiConnection) SetCSChange(leaveEnabled bool) error { | ||
return c.bus.SetCSChange(leaveEnabled) | ||
} | ||
|
||
func (c *SpiConnection) SetDelay(t time.Duration) error { | ||
return c.bus.SetDelay(t) | ||
} | ||
|
||
func (c *SpiConnection) SetMaxSpeed(speed int) error { | ||
return c.bus.SetMaxSpeed(speed) | ||
} | ||
|
||
func (c *SpiConnection) SetMode(mode xspi.Mode) error { | ||
return c.bus.SetMode(mode) | ||
} | ||
|
||
func (c *SpiConnection) Tx(w, r []byte) error { | ||
return c.bus.Tx(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package spi | ||
|
||
type spiConfig struct { | ||
bus int | ||
} | ||
|
||
// Config is the interface which describes how a Driver can specify | ||
// optional SPI params such as which SPI bus it wants to use. | ||
type Config interface { | ||
// WithBus sets which bus to use | ||
WithBus(bus int) | ||
|
||
// GetBusOrDefault gets which bus to use | ||
GetBusOrDefault(def int) int | ||
} | ||
|
||
// NewConfig returns a new SPI Config. | ||
func NewConfig() Config { | ||
return &spiConfig{bus: BusNotInitialized} | ||
} | ||
|
||
// WithBus sets preferred bus to use. | ||
func (s *spiConfig) WithBus(bus int) { | ||
s.bus = bus | ||
} | ||
|
||
// GetBusOrDefault returns which bus to use, either the one set using WithBus(), | ||
// or the default value which is passed in as the one param. | ||
func (s *spiConfig) GetBusOrDefault(d int) int { | ||
if s.bus == BusNotInitialized { | ||
return d | ||
} | ||
return s.bus | ||
} | ||
|
||
// WithBus sets which bus to use as a optional param. | ||
func WithBus(bus int) func(Config) { | ||
return func(s Config) { | ||
s.WithBus(bus) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
g "gobot.io/x/gobot/platforms/gopigo3" | ||
"gobot.io/x/gobot/platforms/raspi" | ||
) | ||
|
||
func main() { | ||
raspiAdaptor := raspi.NewAdaptor() | ||
gopigo3 := g.NewGoPiGo3Driver(raspiAdaptor) | ||
|
||
work := func() { | ||
on := uint8(0xFF) | ||
gobot.Every(1000*time.Millisecond, func() { | ||
err := gopigo3.SetLED(g.LED_EYE_RIGHT, 0x00, 0x00, on) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
err = gopigo3.SetLED(g.LED_EYE_LEFT, ^on, 0x00, 0x00) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
on = ^on | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("gopigo3", | ||
[]gobot.Connection{raspiAdaptor}, | ||
[]gobot.Device{gopigo3}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# GoPiGo3 | ||
|
||
The GoPiGo3 is a robotics controller by Dexter Industries that is compatible with the Raspberry Pi. | ||
|
||
## How to Install | ||
|
||
``` | ||
go get -d -u gobot.io/x/gobot/... | ||
``` | ||
|
||
## How to Use | ||
This example will blink the left and right leds red/blue. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
g "gobot.io/x/gobot/platforms/gopigo3" | ||
"gobot.io/x/gobot/platforms/raspi" | ||
) | ||
|
||
func main() { | ||
raspiAdaptor := raspi.NewAdaptor() | ||
gopigo3 := g.NewGoPiGo3Driver(raspiAdaptor) | ||
|
||
work := func() { | ||
on := uint8(0xFF) | ||
gobot.Every(1000*time.Millisecond, func() { | ||
err := gopigo3.SetLED(g.LED_EYE_RIGHT, 0x00, 0x00, on) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
err = gopigo3.SetLED(g.LED_EYE_LEFT, ^on, 0x00, 0x00) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
on = ^on | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("gopigo3", | ||
[]gobot.Connection{raspiAdaptor}, | ||
[]gobot.Device{gopigo3}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} | ||
``` |
Oops, something went wrong.