mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-01 13:48:57 +08:00
Update i2c package
This commit is contained in:
parent
96cb031261
commit
6134a23132
@ -1,61 +0,0 @@
|
||||
package gobotI2C
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type BlinkM struct {
|
||||
gobot.Driver
|
||||
Adaptor I2cInterface
|
||||
}
|
||||
|
||||
func NewBlinkM(a I2cInterface) *BlinkM {
|
||||
w := new(BlinkM)
|
||||
w.Adaptor = a
|
||||
w.Events = make(map[string]chan interface{})
|
||||
w.Commands = []string{
|
||||
"RgbC",
|
||||
"FadeC",
|
||||
"ColorC",
|
||||
"FirmwareVersionC",
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func (self *BlinkM) Start() bool {
|
||||
self.Adaptor.I2cStart(0x09)
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("o")[0])})
|
||||
self.Rgb(0, 0, 0)
|
||||
return true
|
||||
}
|
||||
func (self *BlinkM) Init() bool { return true }
|
||||
func (self *BlinkM) Halt() bool { return true }
|
||||
|
||||
func (self *BlinkM) Rgb(r byte, g byte, b byte) {
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("n")[0])})
|
||||
self.Adaptor.I2cWrite([]uint16{uint16(r), uint16(g), uint16(b)})
|
||||
}
|
||||
|
||||
func (self *BlinkM) Fade(r byte, g byte, b byte) {
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("c")[0])})
|
||||
self.Adaptor.I2cWrite([]uint16{uint16(r), uint16(g), uint16(b)})
|
||||
}
|
||||
|
||||
func (self *BlinkM) FirmwareVersion() string {
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("Z")[0])})
|
||||
data := self.Adaptor.I2cRead(uint16(2))
|
||||
if len(data) != 2 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%v.%v", data[0], data[1])
|
||||
}
|
||||
|
||||
func (self *BlinkM) Color() []byte {
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("g")[0])})
|
||||
data := self.Adaptor.I2cRead(uint16(3))
|
||||
if len(data) != 3 {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
return []byte{byte(data[0]), byte(data[1]), byte(data[2])}
|
||||
}
|
62
i2c/blinkm_driver.go
Normal file
62
i2c/blinkm_driver.go
Normal file
@ -0,0 +1,62 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type BlinkMDriver struct {
|
||||
gobot.Driver
|
||||
Adaptor I2cInterface
|
||||
}
|
||||
|
||||
func NewBlinkMDriver(a I2cInterface) *BlinkMDriver {
|
||||
return &BlinkMDriver{
|
||||
Driver: gobot.Driver{
|
||||
Commands: []string{
|
||||
"RgbC",
|
||||
"FadeC",
|
||||
"ColorC",
|
||||
"FirmwareVersionC",
|
||||
},
|
||||
},
|
||||
Adaptor: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) Start() bool {
|
||||
b.Adaptor.I2cStart(0x09)
|
||||
b.Adaptor.I2cWrite([]uint16{uint16([]byte("o")[0])})
|
||||
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) {
|
||||
b.Adaptor.I2cWrite([]uint16{uint16([]byte("n")[0])})
|
||||
b.Adaptor.I2cWrite([]uint16{uint16(red), uint16(green), uint16(blue)})
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
|
||||
b.Adaptor.I2cWrite([]uint16{uint16([]byte("c")[0])})
|
||||
b.Adaptor.I2cWrite([]uint16{uint16(red), uint16(green), uint16(blue)})
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) FirmwareVersion() string {
|
||||
b.Adaptor.I2cWrite([]uint16{uint16([]byte("Z")[0])})
|
||||
data := b.Adaptor.I2cRead(uint16(2))
|
||||
if len(data) != 2 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%v.%v", data[0], data[1])
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) Color() []byte {
|
||||
b.Adaptor.I2cWrite([]uint16{uint16([]byte("g")[0])})
|
||||
data := b.Adaptor.I2cRead(uint16(3))
|
||||
if len(data) != 3 {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
return []byte{byte(data[0]), byte(data[1]), byte(data[2])}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
@ -7,16 +7,16 @@ import (
|
||||
|
||||
var _ = Describe("BlinkM", func() {
|
||||
var (
|
||||
someAdaptor TestAdaptor
|
||||
someDriver *BlinkM
|
||||
t TestAdaptor
|
||||
b *BlinkMDriver
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
someDriver = NewBlinkM(someAdaptor)
|
||||
b = NewBlinkMDriver(t)
|
||||
})
|
||||
|
||||
It("Must be able to Start", func() {
|
||||
Expect(someDriver.Start()).To(Equal(true))
|
||||
Expect(b.Start()).To(Equal(true))
|
||||
})
|
||||
|
||||
PIt("Should be able to set Rgb", func() {
|
@ -1,21 +1,21 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
// blinkm
|
||||
func (self *BlinkM) FirmwareVersionC(params map[string]interface{}) string {
|
||||
return self.FirmwareVersion()
|
||||
func (b *BlinkMDriver) FirmwareVersionC(params map[string]interface{}) string {
|
||||
return b.FirmwareVersion()
|
||||
}
|
||||
func (self *BlinkM) ColorC(params map[string]interface{}) []byte {
|
||||
return self.Color()
|
||||
func (b *BlinkMDriver) ColorC(params map[string]interface{}) []byte {
|
||||
return b.Color()
|
||||
}
|
||||
func (self *BlinkM) RgbC(params map[string]interface{}) {
|
||||
r := byte(params["r"].(float64))
|
||||
g := byte(params["g"].(float64))
|
||||
b := byte(params["b"].(float64))
|
||||
self.Rgb(r, g, b)
|
||||
func (b *BlinkMDriver) RgbC(params map[string]interface{}) {
|
||||
red := byte(params["red"].(float64))
|
||||
green := byte(params["green"].(float64))
|
||||
blue := byte(params["blue"].(float64))
|
||||
b.Rgb(red, green, blue)
|
||||
}
|
||||
func (self *BlinkM) FadeC(params map[string]interface{}) {
|
||||
r := byte(params["r"].(float64))
|
||||
g := byte(params["g"].(float64))
|
||||
b := byte(params["b"].(float64))
|
||||
self.Fade(r, g, b)
|
||||
func (b *BlinkMDriver) FadeC(params map[string]interface{}) {
|
||||
red := byte(params["red"].(float64))
|
||||
green := byte(params["green"].(float64))
|
||||
blue := byte(params["blue"].(float64))
|
||||
b.Fade(red, green, blue)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
@ -1,35 +0,0 @@
|
||||
package gobotI2C
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type HMC6352 struct {
|
||||
gobot.Driver
|
||||
Adaptor I2cInterface
|
||||
Heading uint16
|
||||
}
|
||||
|
||||
func NewHMC6352(a I2cInterface) *HMC6352 {
|
||||
d := new(HMC6352)
|
||||
d.Adaptor = a
|
||||
d.Events = make(map[string]chan interface{})
|
||||
d.Commands = []string{}
|
||||
return d
|
||||
}
|
||||
|
||||
func (self *HMC6352) Start() bool {
|
||||
self.Adaptor.I2cStart(0x21)
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("A")[0])})
|
||||
|
||||
gobot.Every(self.Interval, func() {
|
||||
self.Adaptor.I2cWrite([]uint16{uint16([]byte("A")[0])})
|
||||
ret := self.Adaptor.I2cRead(2)
|
||||
if len(ret) == 2 {
|
||||
self.Heading = (ret[1] + ret[0]*256) / 10
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
func (self *HMC6352) Init() bool { return true }
|
||||
func (self *HMC6352) Halt() bool { return true }
|
33
i2c/hmc6352_driver.go
Normal file
33
i2c/hmc6352_driver.go
Normal file
@ -0,0 +1,33 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type HMC6352Driver struct {
|
||||
gobot.Driver
|
||||
Adaptor I2cInterface
|
||||
Heading uint16
|
||||
}
|
||||
|
||||
func NewHMC6352Driver(a I2cInterface) *HMC6352Driver {
|
||||
return &HMC6352Driver{
|
||||
Adaptor: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HMC6352Driver) Start() bool {
|
||||
h.Adaptor.I2cStart(0x21)
|
||||
h.Adaptor.I2cWrite([]uint16{uint16([]byte("A")[0])})
|
||||
|
||||
gobot.Every("1s", func() {
|
||||
h.Adaptor.I2cWrite([]uint16{uint16([]byte("A")[0])})
|
||||
ret := h.Adaptor.I2cRead(2)
|
||||
if len(ret) == 2 {
|
||||
h.Heading = (ret[1] + ret[0]*256) / 10
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
func (self *HMC6352Driver) Init() bool { return true }
|
||||
func (self *HMC6352Driver) Halt() bool { return true }
|
@ -1,4 +1,4 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
@ -7,16 +7,15 @@ import (
|
||||
|
||||
var _ = Describe("HMC6352", func() {
|
||||
var (
|
||||
someAdaptor TestAdaptor
|
||||
someDriver *HMC6352
|
||||
t TestAdaptor
|
||||
h *HMC6352Driver
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
someDriver = NewHMC6352(someAdaptor)
|
||||
someDriver.Interval = "1s"
|
||||
h = NewHMC6352Driver(t)
|
||||
})
|
||||
|
||||
It("Must be able to Start", func() {
|
||||
Expect(someDriver.Start()).To(Equal(true))
|
||||
Expect(h.Start()).To(Equal(true))
|
||||
})
|
||||
})
|
@ -1,4 +1,4 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
type I2cInterface interface {
|
||||
I2cStart(byte)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
type TestAdaptor struct {
|
||||
}
|
||||
|
@ -1,40 +1,42 @@
|
||||
package gobotI2C
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type Wiichuck struct {
|
||||
type WiichuckDriver struct {
|
||||
gobot.Driver
|
||||
Adaptor I2cInterface
|
||||
joystick map[string]float64
|
||||
data map[string]float64
|
||||
}
|
||||
|
||||
func NewWiichuck(a I2cInterface) *Wiichuck {
|
||||
w := new(Wiichuck)
|
||||
w.Adaptor = a
|
||||
w.Events = make(map[string]chan interface{})
|
||||
w.Events["z_button"] = make(chan interface{})
|
||||
w.Events["c_button"] = make(chan interface{})
|
||||
w.Events["joystick"] = make(chan interface{})
|
||||
w.joystick = map[string]float64{
|
||||
"sy_origin": -1,
|
||||
"sx_origin": -1,
|
||||
func NewWiichuckDriver(a I2cInterface) *WiichuckDriver {
|
||||
return &WiichuckDriver{
|
||||
Driver: gobot.Driver{
|
||||
Events: map[string]chan interface{}{
|
||||
"z_button": make(chan interface{}),
|
||||
"c_button": make(chan interface{}),
|
||||
"joystick": make(chan interface{}),
|
||||
},
|
||||
},
|
||||
joystick: map[string]float64{
|
||||
"sy_origin": -1,
|
||||
"sx_origin": -1,
|
||||
},
|
||||
data: map[string]float64{
|
||||
"sx": 0,
|
||||
"sy": 0,
|
||||
"z": 0,
|
||||
"c": 0,
|
||||
},
|
||||
}
|
||||
w.data = map[string]float64{
|
||||
"sx": 0,
|
||||
"sy": 0,
|
||||
"z": 0,
|
||||
"c": 0,
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Wiichuck) Start() bool {
|
||||
func (w *WiichuckDriver) Start() bool {
|
||||
w.Adaptor.I2cStart(byte(0x52))
|
||||
gobot.Every(w.Interval, func() {
|
||||
gobot.Every("100ms", func() {
|
||||
w.Adaptor.I2cWrite([]uint16{uint16(0x40), uint16(0x00)})
|
||||
w.Adaptor.I2cWrite([]uint16{uint16(0x00)})
|
||||
new_value := w.Adaptor.I2cRead(uint16(6))
|
||||
@ -44,10 +46,10 @@ func (w *Wiichuck) Start() bool {
|
||||
})
|
||||
return true
|
||||
}
|
||||
func (w *Wiichuck) Init() bool { return true }
|
||||
func (w *Wiichuck) Halt() bool { return true }
|
||||
func (w *WiichuckDriver) Init() bool { return true }
|
||||
func (w *WiichuckDriver) Halt() bool { return true }
|
||||
|
||||
func (w *Wiichuck) update(value []uint16) {
|
||||
func (w *WiichuckDriver) update(value []uint16) {
|
||||
if w.isEncrypted(value) {
|
||||
fmt.Println("Encrypted bytes from wii device!")
|
||||
} else {
|
||||
@ -58,17 +60,17 @@ func (w *Wiichuck) update(value []uint16) {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Wiichuck) setJoystickDefaultValue(joystick_axis string, default_value float64) {
|
||||
func (w *WiichuckDriver) setJoystickDefaultValue(joystick_axis string, default_value float64) {
|
||||
if w.joystick[joystick_axis] == -1 {
|
||||
w.joystick[joystick_axis] = default_value
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Wiichuck) calculateJoystickValue(axis float64, origin float64) float64 {
|
||||
func (w *WiichuckDriver) calculateJoystickValue(axis float64, origin float64) float64 {
|
||||
return float64(axis - origin)
|
||||
}
|
||||
|
||||
func (w *Wiichuck) isEncrypted(value []uint16) bool {
|
||||
func (w *WiichuckDriver) isEncrypted(value []uint16) bool {
|
||||
if value[0] == value[1] && value[2] == value[3] && value[4] == value[5] {
|
||||
return true
|
||||
} else {
|
||||
@ -76,32 +78,32 @@ func (w *Wiichuck) isEncrypted(value []uint16) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Wiichuck) decode(x uint16) float64 {
|
||||
func (w *WiichuckDriver) decode(x uint16) float64 {
|
||||
return float64((x ^ 0x17) + 0x17)
|
||||
}
|
||||
|
||||
func (w *Wiichuck) adjustOrigins() {
|
||||
func (w *WiichuckDriver) adjustOrigins() {
|
||||
w.setJoystickDefaultValue("sy_origin", w.data["sy"])
|
||||
w.setJoystickDefaultValue("sx_origin", w.data["sx"])
|
||||
}
|
||||
|
||||
func (w *Wiichuck) updateButtons() {
|
||||
func (w *WiichuckDriver) updateButtons() {
|
||||
if w.data["c"] == 0 {
|
||||
w.Events["c_button"] <- ""
|
||||
gobot.Publish(w.Events["c_button"], true)
|
||||
}
|
||||
if w.data["z"] == 0 {
|
||||
w.Events["z_button"] <- ""
|
||||
gobot.Publish(w.Events["z_button"], true)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Wiichuck) updateJoystick() {
|
||||
w.Events["joystick"] <- map[string]float64{
|
||||
func (w *WiichuckDriver) updateJoystick() {
|
||||
gobot.Publish(w.Events["joystick"], map[string]float64{
|
||||
"x": w.calculateJoystickValue(w.data["sx"], w.joystick["sx_origin"]),
|
||||
"y": w.calculateJoystickValue(w.data["sy"], w.joystick["sy_origin"]),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (w *Wiichuck) parse(value []uint16) {
|
||||
func (w *WiichuckDriver) parse(value []uint16) {
|
||||
w.data["sx"] = w.decode(value[0])
|
||||
w.data["sy"] = w.decode(value[1])
|
||||
w.data["z"] = float64(uint8(w.decode(value[5])) & 0x01)
|
21
i2c/wiichuck_driver_test.go
Normal file
21
i2c/wiichuck_driver_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Wiichuck", func() {
|
||||
var (
|
||||
t TestAdaptor
|
||||
w *WiichuckDriver
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
w = NewWiichuckDriver(t)
|
||||
})
|
||||
|
||||
PIt("Must be able to Start", func() {
|
||||
Expect(w.Start()).To(Equal(true))
|
||||
})
|
||||
})
|
@ -1,22 +0,0 @@
|
||||
package gobotI2C
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Wiichuck", func() {
|
||||
var (
|
||||
someAdaptor TestAdaptor
|
||||
someDriver *Wiichuck
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
someDriver = NewWiichuck(someAdaptor)
|
||||
someDriver.Interval = "100ms"
|
||||
})
|
||||
|
||||
It("Must be able to Start", func() {
|
||||
Expect(someDriver.Start()).To(Equal(true))
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user