mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
core: Continue refactoring to allow 'metal' development using Gobot libs.
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
0e25f29a1b
commit
e15961348f
@ -26,6 +26,10 @@ type Eventer interface {
|
|||||||
Publish(name string, data interface{})
|
Publish(name string, data interface{})
|
||||||
// Subscribe to any events from this eventer
|
// Subscribe to any events from this eventer
|
||||||
Subscribe() (events eventChannel)
|
Subscribe() (events eventChannel)
|
||||||
|
// Event handler
|
||||||
|
On(name string, f func(s interface{})) (err error)
|
||||||
|
// Event handler, only exectues one time
|
||||||
|
Once(name string, f func(s interface{})) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEventer returns a new Eventer.
|
// NewEventer returns a new Eventer.
|
||||||
|
@ -143,19 +143,19 @@ func (b *Client) Connect(conn io.ReadWriteCloser) (err error) {
|
|||||||
|
|
||||||
initFunc := b.ProtocolVersionQuery
|
initFunc := b.ProtocolVersionQuery
|
||||||
|
|
||||||
gobot.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
||||||
initFunc = b.FirmwareQuery
|
initFunc = b.FirmwareQuery
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
||||||
initFunc = b.CapabilitiesQuery
|
initFunc = b.CapabilitiesQuery
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
b.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
||||||
initFunc = b.AnalogMappingQuery
|
initFunc = b.AnalogMappingQuery
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
b.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
||||||
initFunc = func() error { return nil }
|
initFunc = func() error { return nil }
|
||||||
b.ReportDigital(0, 1)
|
b.ReportDigital(0, 1)
|
||||||
b.ReportDigital(1, 1)
|
b.ReportDigital(1, 1)
|
||||||
@ -177,7 +177,7 @@ func (b *Client) Connect(conn io.ReadWriteCloser) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := b.process(); err != nil {
|
if err := b.process(); err != nil {
|
||||||
gobot.Publish(b.Event("Error"), err)
|
b.Publish(b.Event("Error"), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -343,7 +343,7 @@ func (b *Client) process() (err error) {
|
|||||||
case ProtocolVersion == messageType:
|
case ProtocolVersion == messageType:
|
||||||
b.ProtocolVersion = fmt.Sprintf("%v.%v", buf[1], buf[2])
|
b.ProtocolVersion = fmt.Sprintf("%v.%v", buf[1], buf[2])
|
||||||
|
|
||||||
gobot.Publish(b.Event("ProtocolVersion"), b.ProtocolVersion)
|
b.Publish(b.Event("ProtocolVersion"), b.ProtocolVersion)
|
||||||
case AnalogMessageRangeStart <= messageType &&
|
case AnalogMessageRangeStart <= messageType &&
|
||||||
AnalogMessageRangeEnd >= messageType:
|
AnalogMessageRangeEnd >= messageType:
|
||||||
|
|
||||||
@ -353,7 +353,7 @@ func (b *Client) process() (err error) {
|
|||||||
if len(b.analogPins) > pin {
|
if len(b.analogPins) > pin {
|
||||||
if len(b.pins) > b.analogPins[pin] {
|
if len(b.pins) > b.analogPins[pin] {
|
||||||
b.pins[b.analogPins[pin]].Value = int(value)
|
b.pins[b.analogPins[pin]].Value = int(value)
|
||||||
gobot.Publish(b.Event(fmt.Sprintf("AnalogRead%v", pin)), b.pins[b.analogPins[pin]].Value)
|
b.Publish(b.Event(fmt.Sprintf("AnalogRead%v", pin)), b.pins[b.analogPins[pin]].Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case DigitalMessageRangeStart <= messageType &&
|
case DigitalMessageRangeStart <= messageType &&
|
||||||
@ -367,7 +367,7 @@ func (b *Client) process() (err error) {
|
|||||||
if len(b.pins) > pinNumber {
|
if len(b.pins) > pinNumber {
|
||||||
if b.pins[pinNumber].Mode == Input {
|
if b.pins[pinNumber].Mode == Input {
|
||||||
b.pins[pinNumber].Value = int((portValue >> (byte(i) & 0x07)) & 0x01)
|
b.pins[pinNumber].Value = int((portValue >> (byte(i) & 0x07)) & 0x01)
|
||||||
gobot.Publish(b.Event(fmt.Sprintf("DigitalRead%v", pinNumber)), b.pins[pinNumber].Value)
|
b.Publish(b.Event(fmt.Sprintf("DigitalRead%v", pinNumber)), b.pins[pinNumber].Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -412,7 +412,7 @@ func (b *Client) process() (err error) {
|
|||||||
}
|
}
|
||||||
n ^= 1
|
n ^= 1
|
||||||
}
|
}
|
||||||
gobot.Publish(b.Event("CapabilityQuery"), nil)
|
b.Publish(b.Event("CapabilityQuery"), nil)
|
||||||
case AnalogMappingResponse:
|
case AnalogMappingResponse:
|
||||||
pinIndex := 0
|
pinIndex := 0
|
||||||
b.analogPins = []int{}
|
b.analogPins = []int{}
|
||||||
@ -427,7 +427,7 @@ func (b *Client) process() (err error) {
|
|||||||
b.AddEvent(fmt.Sprintf("AnalogRead%v", pinIndex))
|
b.AddEvent(fmt.Sprintf("AnalogRead%v", pinIndex))
|
||||||
pinIndex++
|
pinIndex++
|
||||||
}
|
}
|
||||||
gobot.Publish(b.Event("AnalogMappingQuery"), nil)
|
b.Publish(b.Event("AnalogMappingQuery"), nil)
|
||||||
case PinStateResponse:
|
case PinStateResponse:
|
||||||
pin := currentBuffer[2]
|
pin := currentBuffer[2]
|
||||||
b.pins[pin].Mode = int(currentBuffer[3])
|
b.pins[pin].Mode = int(currentBuffer[3])
|
||||||
@ -440,7 +440,7 @@ func (b *Client) process() (err error) {
|
|||||||
b.pins[pin].State = int(uint(b.pins[pin].State) | uint(currentBuffer[6])<<14)
|
b.pins[pin].State = int(uint(b.pins[pin].State) | uint(currentBuffer[6])<<14)
|
||||||
}
|
}
|
||||||
|
|
||||||
gobot.Publish(b.Event(fmt.Sprintf("PinState%v", pin)), b.pins[pin])
|
b.Publish(b.Event(fmt.Sprintf("PinState%v", pin)), b.pins[pin])
|
||||||
case I2CReply:
|
case I2CReply:
|
||||||
reply := I2cReply{
|
reply := I2cReply{
|
||||||
Address: int(byte(currentBuffer[2]) | byte(currentBuffer[3])<<7),
|
Address: int(byte(currentBuffer[2]) | byte(currentBuffer[3])<<7),
|
||||||
@ -458,7 +458,7 @@ func (b *Client) process() (err error) {
|
|||||||
byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7,
|
byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
gobot.Publish(b.Event("I2cReply"), reply)
|
b.Publish(b.Event("I2cReply"), reply)
|
||||||
case FirmwareQuery:
|
case FirmwareQuery:
|
||||||
name := []byte{}
|
name := []byte{}
|
||||||
for _, val := range currentBuffer[4:(len(currentBuffer) - 1)] {
|
for _, val := range currentBuffer[4:(len(currentBuffer) - 1)] {
|
||||||
@ -467,10 +467,10 @@ func (b *Client) process() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.FirmwareName = string(name[:])
|
b.FirmwareName = string(name[:])
|
||||||
gobot.Publish(b.Event("FirmwareQuery"), b.FirmwareName)
|
b.Publish(b.Event("FirmwareQuery"), b.FirmwareName)
|
||||||
case StringData:
|
case StringData:
|
||||||
str := currentBuffer[2:]
|
str := currentBuffer[2:]
|
||||||
gobot.Publish(b.Event("StringData"), string(str[:len(str)-1]))
|
b.Publish(b.Event("StringData"), string(str[:len(str)-1]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -174,7 +174,7 @@ func TestProcess(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
test.init()
|
test.init()
|
||||||
gobot.Once(b.Event(test.event), func(data interface{}) {
|
b.Once(b.Event(test.event), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, test.expected)
|
gobottest.Assert(t, data, test.expected)
|
||||||
sem <- true
|
sem <- true
|
||||||
})
|
})
|
||||||
@ -202,19 +202,19 @@ func TestConnect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
gobot.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
||||||
response = testFirmwareResponse()
|
response = testFirmwareResponse()
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
||||||
response = testCapabilitiesResponse()
|
response = testCapabilitiesResponse()
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
b.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
||||||
response = testAnalogMappingResponse()
|
response = testAnalogMappingResponse()
|
||||||
})
|
})
|
||||||
|
|
||||||
gobot.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
b.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
||||||
response = testProtocolResponse()
|
response = testProtocolResponse()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ type firmataBoard interface {
|
|||||||
I2cWrite(int, []byte) error
|
I2cWrite(int, []byte) error
|
||||||
I2cConfig(int) error
|
I2cConfig(int) error
|
||||||
ServoConfig(int, int, int) error
|
ServoConfig(int, int, int) error
|
||||||
Event(string) *gobot.Event
|
Event(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// FirmataAdaptor is the Gobot Adaptor for Firmata based boards
|
// FirmataAdaptor is the Gobot Adaptor for Firmata based boards
|
||||||
@ -33,6 +33,7 @@ type FirmataAdaptor struct {
|
|||||||
board firmataBoard
|
board firmataBoard
|
||||||
conn io.ReadWriteCloser
|
conn io.ReadWriteCloser
|
||||||
openSP func(port string) (io.ReadWriteCloser, error)
|
openSP func(port string) (io.ReadWriteCloser, error)
|
||||||
|
gobot.Eventer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts:
|
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts:
|
||||||
@ -53,6 +54,7 @@ func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
|
|||||||
openSP: func(port string) (io.ReadWriteCloser, error) {
|
openSP: func(port string) (io.ReadWriteCloser, error) {
|
||||||
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
|
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
|
||||||
},
|
},
|
||||||
|
Eventer: gobot.NewEventer(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
@ -230,7 +232,7 @@ func (f *FirmataAdaptor) I2cRead(address int, size int) (data []byte, err error)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gobot.Once(f.board.Event("I2cReply"), func(data interface{}) {
|
f.Once(f.board.Event("I2cReply"), func(data interface{}) {
|
||||||
ret <- data.(client.I2cReply).Data
|
ret <- data.(client.I2cReply).Data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ func TestFirmataAdaptorI2cRead(t *testing.T) {
|
|||||||
i2cReply := client.I2cReply{Data: i}
|
i2cReply := client.I2cReply{Data: i}
|
||||||
go func() {
|
go func() {
|
||||||
<-time.After(10 * time.Millisecond)
|
<-time.After(10 * time.Millisecond)
|
||||||
gobot.Publish(a.board.Event("I2cReply"), i2cReply)
|
a.Publish(a.board.Event("I2cReply"), i2cReply)
|
||||||
}()
|
}()
|
||||||
data, err := a.I2cRead(0x00, 1)
|
data, err := a.I2cRead(0x00, 1)
|
||||||
gobottest.Assert(t, err, nil)
|
gobottest.Assert(t, err, nil)
|
||||||
|
@ -72,20 +72,20 @@ func (h *MPL115A2Driver) Start() (errs []error) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if err := h.connection.I2cWrite(mpl115a2Address, []byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
|
if err := h.connection.I2cWrite(mpl115a2Address, []byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
|
||||||
gobot.Publish(h.Event(Error), err)
|
h.Publish(h.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
}
|
}
|
||||||
<-time.After(5 * time.Millisecond)
|
<-time.After(5 * time.Millisecond)
|
||||||
|
|
||||||
if err := h.connection.I2cWrite(mpl115a2Address, []byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
|
if err := h.connection.I2cWrite(mpl115a2Address, []byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
|
||||||
gobot.Publish(h.Event(Error), err)
|
h.Publish(h.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.connection.I2cRead(mpl115a2Address, 4)
|
ret, err := h.connection.I2cRead(mpl115a2Address, 4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gobot.Publish(h.Event(Error), err)
|
h.Publish(h.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(ret) == 4 {
|
if len(ret) == 4 {
|
||||||
|
@ -74,13 +74,13 @@ func (h *MPU6050Driver) Start() (errs []error) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if err := h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
|
if err := h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
|
||||||
gobot.Publish(h.Event(Error), err)
|
h.Publish(h.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err := h.connection.I2cRead(mpu6050Address, 14)
|
ret, err := h.connection.I2cRead(mpu6050Address, 14)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gobot.Publish(h.Event(Error), err)
|
h.Publish(h.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(ret)
|
buf := bytes.NewBuffer(ret)
|
||||||
@ -111,12 +111,12 @@ func (h *MPU6050Driver) initialize() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setFullScaleGyroRange
|
// setFullScaleGyroRange
|
||||||
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_GYRO_CONFIG,
|
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_GYRO_CONFIG,
|
||||||
MPU6050_GCONFIG_FS_SEL_BIT,
|
MPU6050_GCONFIG_FS_SEL_BIT,
|
||||||
MPU6050_GCONFIG_FS_SEL_LENGTH,
|
MPU6050_GCONFIG_FS_SEL_LENGTH,
|
||||||
MPU6050_GYRO_FS_250}); err != nil {
|
MPU6050_GYRO_FS_250}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// setFullScaleAccelRange
|
// setFullScaleAccelRange
|
||||||
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_CONFIG,
|
if err = h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_CONFIG,
|
||||||
|
@ -69,23 +69,23 @@ func (w *WiichuckDriver) Start() (errs []error) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if err := w.connection.I2cWrite(wiichuckAddress, []byte{0x40, 0x00}); err != nil {
|
if err := w.connection.I2cWrite(wiichuckAddress, []byte{0x40, 0x00}); err != nil {
|
||||||
gobot.Publish(w.Event(Error), err)
|
w.Publish(w.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
<-time.After(w.pauseTime)
|
<-time.After(w.pauseTime)
|
||||||
if err := w.connection.I2cWrite(wiichuckAddress, []byte{0x00}); err != nil {
|
if err := w.connection.I2cWrite(wiichuckAddress, []byte{0x00}); err != nil {
|
||||||
gobot.Publish(w.Event(Error), err)
|
w.Publish(w.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
<-time.After(w.pauseTime)
|
<-time.After(w.pauseTime)
|
||||||
newValue, err := w.connection.I2cRead(wiichuckAddress, 6)
|
newValue, err := w.connection.I2cRead(wiichuckAddress, 6)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gobot.Publish(w.Event(Error), err)
|
w.Publish(w.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(newValue) == 6 {
|
if len(newValue) == 6 {
|
||||||
if err = w.update(newValue); err != nil {
|
if err = w.update(newValue); err != nil {
|
||||||
gobot.Publish(w.Event(Error), err)
|
w.Publish(w.Event(Error), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,16 +146,16 @@ func (w *WiichuckDriver) adjustOrigins() {
|
|||||||
// updateButtons publishes "c" and "x" events if present in data
|
// updateButtons publishes "c" and "x" events if present in data
|
||||||
func (w *WiichuckDriver) updateButtons() {
|
func (w *WiichuckDriver) updateButtons() {
|
||||||
if w.data["c"] == 0 {
|
if w.data["c"] == 0 {
|
||||||
gobot.Publish(w.Event(C), true)
|
w.Publish(w.Event(C), true)
|
||||||
}
|
}
|
||||||
if w.data["z"] == 0 {
|
if w.data["z"] == 0 {
|
||||||
gobot.Publish(w.Event(Z), true)
|
w.Publish(w.Event(Z), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateJoystick publishes event with current x and y values for joystick
|
// updateJoystick publishes event with current x and y values for joystick
|
||||||
func (w *WiichuckDriver) updateJoystick() {
|
func (w *WiichuckDriver) updateJoystick() {
|
||||||
gobot.Publish(w.Event(Joystick), map[string]float64{
|
w.Publish(w.Event(Joystick), map[string]float64{
|
||||||
"x": w.calculateJoystickValue(w.data["sx"], w.joystick["sx_origin"]),
|
"x": w.calculateJoystickValue(w.data["sx"], w.joystick["sx_origin"]),
|
||||||
"y": w.calculateJoystickValue(w.data["sy"], w.joystick["sy_origin"]),
|
"y": w.calculateJoystickValue(w.data["sy"], w.joystick["sy_origin"]),
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user