Skip to content

Commit

Permalink
[gpio] Add support for RGB LED
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 13, 2016
1 parent f90acf8 commit 686fbce
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ a shared set of drivers provided using the `gobot/platforms/gpio` package:
- [GPIO](https://en.wikipedia.org/wiki/General_Purpose_Input/Output) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/platforms/gpio)
- Analog Sensor
- Button
- Direct Pin
- Digital Sensor
- Buzzer
- Direct Pin
- Grove Button
- Grove Buzzer
Expand All @@ -132,10 +131,13 @@ a shared set of drivers provided using the `gobot/platforms/gpio` package:
- Grove Relay
- Grove Rotary Dial
- Grove Sound Sensor
- Grove Temperature Sensor
- Grove Touch Sensor
- LED
- Makey Button
- Motor
- Relay
- RGB LED
- Servo

Support for devices that use Inter-Integrated Circuit (I2C) have a shared set of
Expand Down
35 changes: 35 additions & 0 deletions examples/edison_rgb_led.go
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()
}
2 changes: 1 addition & 1 deletion platforms/gpio/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 The Hybrid Group
Copyright (c) 2013-2016 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 14 additions & 1 deletion platforms/gpio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ Gobot has a extensible system for connecting to hardware devices. The following

- Analog Sensor
- Button
- Buzzer
- Direct Pin
- Grove Touch Sensor
- Grove Sound Sensor
- Grove Button
- Grove Buzzer
- Grove Light Sensor
- Grove Piezo Vibration Sensor
- Grove LED
- Grove Rotary Dial
- Grove Relay
- Grove Temperature Sensor
- LED
- Makey Button
- Motor
- Relay
- RGB LED
- Servo

More drivers are coming soon...
More drivers are coming soon...
154 changes: 154 additions & 0 deletions platforms/gpio/rgb_led_driver.go
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()
}
82 changes: 82 additions & 0 deletions platforms/gpio/rgb_led_driver_test.go
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"))
}

0 comments on commit 686fbce

Please sign in to comment.