2014-04-28 09:54:41 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-10 09:32:27 +08:00
|
|
|
|
2014-04-28 09:54:41 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
2014-11-23 11:38:39 +08:00
|
|
|
var _ gobot.Driver = (*BlinkMDriver)(nil)
|
2014-11-17 04:25:48 +08:00
|
|
|
|
2015-07-04 09:57:29 +08:00
|
|
|
const blinkmAddress = 0x09
|
|
|
|
|
2014-04-28 09:54:41 +08:00
|
|
|
type BlinkMDriver struct {
|
2014-11-23 11:38:39 +08:00
|
|
|
name string
|
2014-11-30 04:10:23 +08:00
|
|
|
connection I2c
|
2014-11-23 11:38:39 +08:00
|
|
|
gobot.Commander
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
// NewBlinkMDriver creates a new BlinkMDriver.
|
2014-10-21 01:42:01 +08:00
|
|
|
//
|
|
|
|
// Adds the following API commands:
|
|
|
|
// Rgb - sets RGB color
|
|
|
|
// Fade - fades the RGB color
|
|
|
|
// FirmwareVersion - returns the version of the current Frimware
|
|
|
|
// Color - returns the color of the LED.
|
2016-09-25 20:08:18 +08:00
|
|
|
func NewBlinkMDriver(a I2c) *BlinkMDriver {
|
2014-06-12 09:59:30 +08:00
|
|
|
b := &BlinkMDriver{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "BlinkM",
|
2014-11-30 04:10:23 +08:00
|
|
|
connection: a,
|
2014-11-23 11:38:39 +08:00
|
|
|
Commander: gobot.NewCommander(),
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
2014-06-12 09:59:30 +08:00
|
|
|
|
2014-07-10 09:32:27 +08:00
|
|
|
b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
|
2014-06-12 09:59:30 +08:00
|
|
|
red := byte(params["red"].(float64))
|
|
|
|
green := byte(params["green"].(float64))
|
|
|
|
blue := byte(params["blue"].(float64))
|
2014-11-20 08:56:48 +08:00
|
|
|
return b.Rgb(red, green, blue)
|
2014-06-12 09:59:30 +08:00
|
|
|
})
|
2014-07-10 09:32:27 +08:00
|
|
|
b.AddCommand("Fade", func(params map[string]interface{}) interface{} {
|
2014-06-12 09:59:30 +08:00
|
|
|
red := byte(params["red"].(float64))
|
|
|
|
green := byte(params["green"].(float64))
|
|
|
|
blue := byte(params["blue"].(float64))
|
2014-11-20 08:56:48 +08:00
|
|
|
return b.Fade(red, green, blue)
|
2014-06-12 09:59:30 +08:00
|
|
|
})
|
2014-09-12 04:38:08 +08:00
|
|
|
b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
|
2014-11-20 08:56:48 +08:00
|
|
|
version, err := b.FirmwareVersion()
|
|
|
|
return map[string]interface{}{"version": version, "err": err}
|
2014-09-12 04:38:08 +08:00
|
|
|
})
|
|
|
|
b.AddCommand("Color", func(params map[string]interface{}) interface{} {
|
2014-11-20 08:56:48 +08:00
|
|
|
color, err := b.Color()
|
|
|
|
return map[string]interface{}{"color": color, "err": err}
|
2014-09-12 04:38:08 +08:00
|
|
|
})
|
2014-06-12 09:59:30 +08:00
|
|
|
|
|
|
|
return b
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
2014-11-23 11:38:39 +08:00
|
|
|
func (b *BlinkMDriver) Name() string { return b.name }
|
2016-09-25 20:08:18 +08:00
|
|
|
func (b *BlinkMDriver) SetName(n string) { b.name = n }
|
2014-11-30 04:10:23 +08:00
|
|
|
func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }
|
2014-04-28 09:54:41 +08:00
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// adaptor returns I2C adaptor
|
2014-06-16 08:22:50 +08:00
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
// Start writes start bytes
|
2014-11-20 15:21:19 +08:00
|
|
|
func (b *BlinkMDriver) Start() (errs []error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err := b.connection.I2cStart(blinkmAddress); err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2015-07-04 09:57:29 +08:00
|
|
|
if err := b.connection.I2cWrite(blinkmAddress, []byte("o")); err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
2014-10-21 01:42:01 +08:00
|
|
|
|
|
|
|
// Halt returns true if device is halted successfully
|
2014-11-20 15:21:19 +08:00
|
|
|
func (b *BlinkMDriver) Halt() (errs []error) { return }
|
2014-04-28 09:54:41 +08:00
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// Rgb sets color using r,g,b params
|
2014-11-20 08:56:48 +08:00
|
|
|
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) (err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err = b.connection.I2cWrite(blinkmAddress, []byte("n")); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2015-07-04 09:57:29 +08:00
|
|
|
err = b.connection.I2cWrite(blinkmAddress, []byte{red, green, blue})
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// Fade removes color using r,g,b params
|
2014-11-20 08:56:48 +08:00
|
|
|
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) (err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err = b.connection.I2cWrite(blinkmAddress, []byte("c")); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2015-07-04 09:57:29 +08:00
|
|
|
err = b.connection.I2cWrite(blinkmAddress, []byte{red, green, blue})
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// FirmwareVersion returns version with MAYOR.minor format
|
2014-11-20 08:56:48 +08:00
|
|
|
func (b *BlinkMDriver) FirmwareVersion() (version string, err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err = b.connection.I2cWrite(blinkmAddress, []byte("Z")); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2015-07-04 09:57:29 +08:00
|
|
|
data, err := b.connection.I2cRead(blinkmAddress, 2)
|
2014-11-20 08:56:48 +08:00
|
|
|
if len(data) != 2 || err != nil {
|
|
|
|
return
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
2014-11-20 08:56:48 +08:00
|
|
|
return fmt.Sprintf("%v.%v", data[0], data[1]), nil
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// Color returns an array with current rgb color
|
2014-11-20 08:56:48 +08:00
|
|
|
func (b *BlinkMDriver) Color() (color []byte, err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err = b.connection.I2cWrite(blinkmAddress, []byte("g")); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2015-07-04 09:57:29 +08:00
|
|
|
data, err := b.connection.I2cRead(blinkmAddress, 3)
|
2014-11-20 08:56:48 +08:00
|
|
|
if len(data) != 3 || err != nil {
|
|
|
|
return []byte{}, err
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
2014-11-20 08:56:48 +08:00
|
|
|
return []byte{data[0], data[1], data[2]}, nil
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|