From 6134a23132fa0bc843e8f7b5828a853ffbec3c6c Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sun, 27 Apr 2014 18:54:41 -0700 Subject: [PATCH] Update i2c package --- i2c/blinkm.go | 61 --------------- i2c/blinkm_driver.go | 62 ++++++++++++++++ i2c/{blinkm_test.go => blinkm_driver_test.go} | 10 +-- i2c/commands.go | 30 ++++---- i2c/gobot-i2c_suite_test.go | 2 +- i2c/hmc6352.go | 35 --------- i2c/hmc6352_driver.go | 33 +++++++++ ...hmc6352_test.go => hmc6352_driver_test.go} | 11 ++- i2c/i2c.go | 2 +- i2c/test_helper.go | 2 +- i2c/{wiichuck.go => wiichuck_driver.go} | 74 ++++++++++--------- i2c/wiichuck_driver_test.go | 21 ++++++ i2c/wiichuck_test.go | 22 ------ 13 files changed, 182 insertions(+), 183 deletions(-) delete mode 100644 i2c/blinkm.go create mode 100644 i2c/blinkm_driver.go rename i2c/{blinkm_test.go => blinkm_driver_test.go} (75%) delete mode 100644 i2c/hmc6352.go create mode 100644 i2c/hmc6352_driver.go rename i2c/{hmc6352_test.go => hmc6352_driver_test.go} (51%) rename i2c/{wiichuck.go => wiichuck_driver.go} (50%) create mode 100644 i2c/wiichuck_driver_test.go delete mode 100644 i2c/wiichuck_test.go diff --git a/i2c/blinkm.go b/i2c/blinkm.go deleted file mode 100644 index 9cadcabb..00000000 --- a/i2c/blinkm.go +++ /dev/null @@ -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])} -} diff --git a/i2c/blinkm_driver.go b/i2c/blinkm_driver.go new file mode 100644 index 00000000..c32cef83 --- /dev/null +++ b/i2c/blinkm_driver.go @@ -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])} +} diff --git a/i2c/blinkm_test.go b/i2c/blinkm_driver_test.go similarity index 75% rename from i2c/blinkm_test.go rename to i2c/blinkm_driver_test.go index 3832b82f..bb8402c8 100644 --- a/i2c/blinkm_test.go +++ b/i2c/blinkm_driver_test.go @@ -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() { diff --git a/i2c/commands.go b/i2c/commands.go index 80e90a5d..eac151ac 100644 --- a/i2c/commands.go +++ b/i2c/commands.go @@ -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) } diff --git a/i2c/gobot-i2c_suite_test.go b/i2c/gobot-i2c_suite_test.go index bf38326d..a4378ce6 100644 --- a/i2c/gobot-i2c_suite_test.go +++ b/i2c/gobot-i2c_suite_test.go @@ -1,4 +1,4 @@ -package gobotI2C +package i2c import ( . "github.com/onsi/ginkgo" diff --git a/i2c/hmc6352.go b/i2c/hmc6352.go deleted file mode 100644 index 93291f4e..00000000 --- a/i2c/hmc6352.go +++ /dev/null @@ -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 } diff --git a/i2c/hmc6352_driver.go b/i2c/hmc6352_driver.go new file mode 100644 index 00000000..f4e0982a --- /dev/null +++ b/i2c/hmc6352_driver.go @@ -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 } diff --git a/i2c/hmc6352_test.go b/i2c/hmc6352_driver_test.go similarity index 51% rename from i2c/hmc6352_test.go rename to i2c/hmc6352_driver_test.go index a341bc83..9ad8d7a1 100644 --- a/i2c/hmc6352_test.go +++ b/i2c/hmc6352_driver_test.go @@ -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)) }) }) diff --git a/i2c/i2c.go b/i2c/i2c.go index bdaff88b..f7c335a2 100644 --- a/i2c/i2c.go +++ b/i2c/i2c.go @@ -1,4 +1,4 @@ -package gobotI2C +package i2c type I2cInterface interface { I2cStart(byte) diff --git a/i2c/test_helper.go b/i2c/test_helper.go index df214a0e..6347135f 100644 --- a/i2c/test_helper.go +++ b/i2c/test_helper.go @@ -1,4 +1,4 @@ -package gobotI2C +package i2c type TestAdaptor struct { } diff --git a/i2c/wiichuck.go b/i2c/wiichuck_driver.go similarity index 50% rename from i2c/wiichuck.go rename to i2c/wiichuck_driver.go index 146dd1ce..ff9200b2 100644 --- a/i2c/wiichuck.go +++ b/i2c/wiichuck_driver.go @@ -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) diff --git a/i2c/wiichuck_driver_test.go b/i2c/wiichuck_driver_test.go new file mode 100644 index 00000000..6c4cabc1 --- /dev/null +++ b/i2c/wiichuck_driver_test.go @@ -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)) + }) +}) diff --git a/i2c/wiichuck_test.go b/i2c/wiichuck_test.go deleted file mode 100644 index 25f5e0c6..00000000 --- a/i2c/wiichuck_test.go +++ /dev/null @@ -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)) - }) -})