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
089bfaf548
commit
cdb563ab02
@ -12,11 +12,11 @@ type BlinkMDriver struct {
|
||||
|
||||
func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
||||
b := &BlinkMDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"BlinkMDriver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
}
|
||||
|
||||
b.Driver.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
|
||||
@ -44,7 +44,7 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) adaptor() I2cInterface {
|
||||
return b.Driver.Adaptor.(I2cInterface)
|
||||
return b.Driver.Adaptor().(I2cInterface)
|
||||
}
|
||||
|
||||
func (b *BlinkMDriver) Start() bool {
|
||||
|
@ -12,22 +12,23 @@ type HMC6352Driver struct {
|
||||
|
||||
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
|
||||
return &HMC6352Driver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"HMC6352Driver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HMC6352Driver) adaptor() I2cInterface {
|
||||
return h.Driver.Adaptor.(I2cInterface)
|
||||
return h.Driver.Adaptor().(I2cInterface)
|
||||
}
|
||||
|
||||
func (h *HMC6352Driver) Start() bool {
|
||||
h.adaptor().I2cStart(0x21)
|
||||
h.adaptor().I2cWrite([]byte("A"))
|
||||
|
||||
gobot.Every(h.Interval, func() {
|
||||
gobot.Every(h.Interval(), func() {
|
||||
h.adaptor().I2cWrite([]byte("A"))
|
||||
ret := h.adaptor().I2cRead(2)
|
||||
if len(ret) == 2 {
|
||||
|
@ -16,8 +16,9 @@ func (t *i2cTestAdaptor) Finalize() bool { return true }
|
||||
|
||||
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
|
||||
return &i2cTestAdaptor{
|
||||
Adaptor: gobot.Adaptor{
|
||||
Name: name,
|
||||
},
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
name,
|
||||
"I2cTestAdaptor",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -12,16 +12,12 @@ type WiichuckDriver struct {
|
||||
}
|
||||
|
||||
func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
return &WiichuckDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Events: map[string]*gobot.Event{
|
||||
"z_button": gobot.NewEvent(),
|
||||
"c_button": gobot.NewEvent(),
|
||||
"joystick": gobot.NewEvent(),
|
||||
},
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
w := &WiichuckDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"WiichuckDriver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
joystick: map[string]float64{
|
||||
"sy_origin": -1,
|
||||
"sx_origin": -1,
|
||||
@ -33,15 +29,20 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
"c": 0,
|
||||
},
|
||||
}
|
||||
|
||||
w.AddEvent("z")
|
||||
w.AddEvent("c")
|
||||
w.AddEvent("joystick")
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *WiichuckDriver) adaptor() I2cInterface {
|
||||
return w.Driver.Adaptor.(I2cInterface)
|
||||
return w.Driver.Adaptor().(I2cInterface)
|
||||
}
|
||||
|
||||
func (w *WiichuckDriver) Start() bool {
|
||||
w.adaptor().I2cStart(0x52)
|
||||
gobot.Every(w.Interval, func() {
|
||||
gobot.Every(w.Interval(), func() {
|
||||
w.adaptor().I2cWrite([]byte{0x40, 0x00})
|
||||
w.adaptor().I2cWrite([]byte{0x00})
|
||||
newValue := w.adaptor().I2cRead(6)
|
||||
@ -93,15 +94,15 @@ func (w *WiichuckDriver) adjustOrigins() {
|
||||
|
||||
func (w *WiichuckDriver) updateButtons() {
|
||||
if w.data["c"] == 0 {
|
||||
gobot.Publish(w.Events["c_button"], true)
|
||||
gobot.Publish(w.Event("c"), true)
|
||||
}
|
||||
if w.data["z"] == 0 {
|
||||
gobot.Publish(w.Events["z_button"], true)
|
||||
gobot.Publish(w.Event("z"), true)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiichuckDriver) updateJoystick() {
|
||||
gobot.Publish(w.Events["joystick"], map[string]float64{
|
||||
gobot.Publish(w.Event("joystick"), map[string]float64{
|
||||
"x": w.calculateJoystickValue(w.data["sx"], w.joystick["sx_origin"]),
|
||||
"y": w.calculateJoystickValue(w.data["sy"], w.joystick["sy_origin"]),
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user