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.
Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
f90acf8
commit 686fbce
Showing
6 changed files
with
290 additions
and
4 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,35 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hybridgroup/gobot" | ||
"github.com/hybridgroup/gobot/platforms/gpio" | ||
"github.com/hybridgroup/gobot/platforms/intel-iot/edison" | ||
) | ||
|
||
func main() { | ||
gbot := gobot.NewGobot() | ||
|
||
e := edison.NewEdisonAdaptor("edison") | ||
led := gpio.NewRgbLedDriver(e, "led", "3", "5", "6") | ||
|
||
work := func() { | ||
gobot.Every(1*time.Second, func() { | ||
r := uint8(gobot.Rand(255)) | ||
g := uint8(gobot.Rand(255)) | ||
b := uint8(gobot.Rand(255)) | ||
led.SetRGB(r, g, b) | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("rgbBot", | ||
[]gobot.Connection{e}, | ||
[]gobot.Device{led}, | ||
work, | ||
) | ||
|
||
gbot.AddRobot(robot) | ||
|
||
gbot.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
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,154 @@ | ||
package gpio | ||
|
||
import "github.com/hybridgroup/gobot" | ||
|
||
var _ gobot.Driver = (*RgbLedDriver)(nil) | ||
|
||
// RgbLedDriver represents a digital RGB Led | ||
type RgbLedDriver struct { | ||
pinRed string | ||
redColor byte | ||
pinGreen string | ||
greenColor byte | ||
pinBlue string | ||
blueColor byte | ||
name string | ||
connection DigitalWriter | ||
high bool | ||
gobot.Commander | ||
} | ||
|
||
// NewRgbLedDriver return a new RgbLedDriver given a DigitalWriter, name and | ||
// 3 pins: redPin, greenPin, and bluePin | ||
// | ||
// Adds the following API Commands: | ||
// "SetRGB" - See RgbLedDriver.SetRGB | ||
// "Toggle" - See RgbLedDriver.Toggle | ||
// "On" - See RgbLedDriver.On | ||
// "Off" - See RgbLedDriver.Off | ||
func NewRgbLedDriver(a DigitalWriter, name string, redPin string, greenPin string, bluePin string) *RgbLedDriver { | ||
l := &RgbLedDriver{ | ||
name: name, | ||
pinRed: redPin, | ||
pinGreen: greenPin, | ||
pinBlue: bluePin, | ||
connection: a, | ||
high: false, | ||
Commander: gobot.NewCommander(), | ||
} | ||
|
||
l.AddCommand("SetRGB", func(params map[string]interface{}) interface{} { | ||
r := byte(params["r"].(int)) | ||
g := byte(params["g"].(int)) | ||
b := byte(params["b"].(int)) | ||
return l.SetRGB(r, g, b) | ||
}) | ||
|
||
l.AddCommand("Toggle", func(params map[string]interface{}) interface{} { | ||
return l.Toggle() | ||
}) | ||
|
||
l.AddCommand("On", func(params map[string]interface{}) interface{} { | ||
return l.On() | ||
}) | ||
|
||
l.AddCommand("Off", func(params map[string]interface{}) interface{} { | ||
return l.Off() | ||
}) | ||
|
||
return l | ||
} | ||
|
||
// Start implements the Driver interface | ||
func (l *RgbLedDriver) Start() (errs []error) { return } | ||
|
||
// Halt implements the Driver interface | ||
func (l *RgbLedDriver) Halt() (errs []error) { return } | ||
|
||
// Name returns the LedDrivers name | ||
func (l *RgbLedDriver) Name() string { return l.name } | ||
|
||
// Pin returns the RgbLedDrivers redPin | ||
func (l *RgbLedDriver) Pin() string { return l.pinRed } | ||
|
||
// RedPin returns the RgbLedDrivers redPin | ||
func (l *RgbLedDriver) RedPin() string { return l.pinRed } | ||
|
||
// GreenPin returns the RgbLedDrivers redPin | ||
func (l *RgbLedDriver) GreenPin() string { return l.pinGreen } | ||
|
||
// BluePin returns the RgbLedDrivers bluePin | ||
func (l *RgbLedDriver) BluePin() string { return l.pinBlue } | ||
|
||
// Connection returns the RgbLedDriver Connection | ||
func (l *RgbLedDriver) Connection() gobot.Connection { | ||
return l.connection.(gobot.Connection) | ||
} | ||
|
||
// State return true if the led is On and false if the led is Off | ||
func (l *RgbLedDriver) State() bool { | ||
return l.high | ||
} | ||
|
||
// On sets the led's pins to their various states | ||
func (l *RgbLedDriver) On() (err error) { | ||
if err = l.SetLevel(l.pinRed, l.redColor); err != nil { | ||
return | ||
} | ||
|
||
if err = l.SetLevel(l.pinGreen, l.greenColor); err != nil { | ||
return | ||
} | ||
|
||
if err = l.SetLevel(l.pinBlue, l.blueColor); err != nil { | ||
return | ||
} | ||
|
||
l.high = true | ||
return | ||
} | ||
|
||
// Off sets the led to black. | ||
func (l *RgbLedDriver) Off() (err error) { | ||
if err = l.SetLevel(l.pinRed, 0); err != nil { | ||
return | ||
} | ||
|
||
if err = l.SetLevel(l.pinGreen, 0); err != nil { | ||
return | ||
} | ||
|
||
if err = l.SetLevel(l.pinBlue, 0); err != nil { | ||
return | ||
} | ||
|
||
l.high = false | ||
return | ||
} | ||
|
||
// Toggle sets the led to the opposite of it's current state | ||
func (l *RgbLedDriver) Toggle() (err error) { | ||
if l.State() { | ||
err = l.Off() | ||
} else { | ||
err = l.On() | ||
} | ||
return | ||
} | ||
|
||
// SetLevel sets the led to the specified color level | ||
func (l *RgbLedDriver) SetLevel(pin string, level byte) (err error) { | ||
if writer, ok := l.connection.(PwmWriter); ok { | ||
return writer.PwmWrite(pin, level) | ||
} | ||
return ErrPwmWriteUnsupported | ||
} | ||
|
||
// SetRGB sets the Red Green Blue value of the LED. | ||
func (l *RgbLedDriver) SetRGB(r, g, b byte) error { | ||
l.redColor = r | ||
l.greenColor = g | ||
l.blueColor = b | ||
|
||
return l.On() | ||
} |
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,82 @@ | ||
package gpio | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/hybridgroup/gobot/gobottest" | ||
) | ||
|
||
func initTestRgbLedDriver(conn DigitalWriter) *RgbLedDriver { | ||
testAdaptorDigitalWrite = func() (err error) { | ||
return nil | ||
} | ||
testAdaptorPwmWrite = func() (err error) { | ||
return nil | ||
} | ||
return NewRgbLedDriver(conn, "bot", "1", "2", "3") | ||
} | ||
|
||
func TestRgbLedDriver(t *testing.T) { | ||
var err interface{} | ||
|
||
d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor")) | ||
|
||
gobottest.Assert(t, d.Name(), "bot") | ||
gobottest.Assert(t, d.Pin(), "1") | ||
gobottest.Assert(t, d.RedPin(), "1") | ||
gobottest.Assert(t, d.GreenPin(), "2") | ||
gobottest.Assert(t, d.BluePin(), "3") | ||
gobottest.Assert(t, d.Connection().Name(), "adaptor") | ||
|
||
testAdaptorDigitalWrite = func() (err error) { | ||
return errors.New("write error") | ||
} | ||
testAdaptorPwmWrite = func() (err error) { | ||
return errors.New("pwm error") | ||
} | ||
|
||
err = d.Command("Toggle")(nil) | ||
gobottest.Assert(t, err.(error), errors.New("pwm error")) | ||
|
||
err = d.Command("On")(nil) | ||
gobottest.Assert(t, err.(error), errors.New("pwm error")) | ||
|
||
err = d.Command("Off")(nil) | ||
gobottest.Assert(t, err.(error), errors.New("pwm error")) | ||
|
||
err = d.Command("SetRGB")(map[string]interface{}{"r": 0xff, "g": 0xff, "b": 0xff}) | ||
gobottest.Assert(t, err.(error), errors.New("pwm error")) | ||
|
||
} | ||
|
||
func TestRgbLedDriverStart(t *testing.T) { | ||
d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor")) | ||
gobottest.Assert(t, len(d.Start()), 0) | ||
} | ||
|
||
func TestRgbLedDriverHalt(t *testing.T) { | ||
d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor")) | ||
gobottest.Assert(t, len(d.Halt()), 0) | ||
} | ||
|
||
func TestRgbLedDriverToggle(t *testing.T) { | ||
d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor")) | ||
d.Off() | ||
d.Toggle() | ||
gobottest.Assert(t, d.State(), true) | ||
d.Toggle() | ||
gobottest.Assert(t, d.State(), false) | ||
} | ||
|
||
func TestRgbLedDriverSetLevel(t *testing.T) { | ||
d := initTestRgbLedDriver(&gpioTestDigitalWriter{}) | ||
gobottest.Assert(t, d.SetLevel("1", 150), ErrPwmWriteUnsupported) | ||
|
||
d = initTestRgbLedDriver(newGpioTestAdaptor("adaptor")) | ||
testAdaptorPwmWrite = func() (err error) { | ||
err = errors.New("pwm error") | ||
return | ||
} | ||
gobottest.Assert(t, d.SetLevel("1", 150), errors.New("pwm error")) | ||
} |