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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlinkMDriver struct {
|
|
|
|
gobot.Driver
|
|
|
|
}
|
|
|
|
|
2014-05-23 12:33:05 +08:00
|
|
|
func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
2014-06-12 09:59:30 +08:00
|
|
|
b := &BlinkMDriver{
|
2014-07-08 07:59:19 +08:00
|
|
|
Driver: *gobot.NewDriver(
|
|
|
|
name,
|
|
|
|
"BlinkMDriver",
|
|
|
|
a.(gobot.AdaptorInterface),
|
|
|
|
),
|
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("FirmwareVersion", func(params map[string]interface{}) interface{} {
|
2014-06-12 09:59:30 +08:00
|
|
|
return b.FirmwareVersion()
|
|
|
|
})
|
2014-07-10 09:32:27 +08:00
|
|
|
b.AddCommand("Color", func(params map[string]interface{}) interface{} {
|
2014-06-12 09:59:30 +08:00
|
|
|
return b.Color()
|
|
|
|
})
|
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))
|
|
|
|
b.Rgb(red, green, blue)
|
|
|
|
return nil
|
|
|
|
})
|
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))
|
|
|
|
b.Fade(red, green, blue)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return b
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2014-06-16 08:22:50 +08:00
|
|
|
func (b *BlinkMDriver) adaptor() I2cInterface {
|
2014-07-10 09:32:27 +08:00
|
|
|
return b.Adaptor().(I2cInterface)
|
2014-06-16 08:22:50 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 09:54:41 +08:00
|
|
|
func (b *BlinkMDriver) Start() bool {
|
2014-06-16 08:22:50 +08:00
|
|
|
b.adaptor().I2cStart(0x09)
|
|
|
|
b.adaptor().I2cWrite([]byte("o"))
|
2014-04-28 09:54:41 +08:00
|
|
|
b.Rgb(0, 0, 0)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (b *BlinkMDriver) Init() bool { return true }
|
|
|
|
func (b *BlinkMDriver) Halt() bool { return true }
|
|
|
|
|
|
|
|
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) {
|
2014-06-16 08:22:50 +08:00
|
|
|
b.adaptor().I2cWrite([]byte("n"))
|
|
|
|
b.adaptor().I2cWrite([]byte{red, green, blue})
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
|
2014-06-16 08:22:50 +08:00
|
|
|
b.adaptor().I2cWrite([]byte("c"))
|
|
|
|
b.adaptor().I2cWrite([]byte{red, green, blue})
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlinkMDriver) FirmwareVersion() string {
|
2014-06-16 08:22:50 +08:00
|
|
|
b.adaptor().I2cWrite([]byte("Z"))
|
|
|
|
data := b.adaptor().I2cRead(2)
|
2014-04-28 09:54:41 +08:00
|
|
|
if len(data) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v.%v", data[0], data[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlinkMDriver) Color() []byte {
|
2014-06-16 08:22:50 +08:00
|
|
|
b.adaptor().I2cWrite([]byte("g"))
|
|
|
|
data := b.adaptor().I2cRead(3)
|
2014-04-28 09:54:41 +08:00
|
|
|
if len(data) != 3 {
|
|
|
|
return make([]byte, 0)
|
|
|
|
}
|
2014-06-07 07:58:17 +08:00
|
|
|
return []byte{data[0], data[1], data[2]}
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|