mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Add optional interval parameter for drivers which poll at an interval
This commit is contained in:
parent
61e70f7571
commit
7050410217
@ -22,7 +22,7 @@ type AnalogSensorDriver struct {
|
||||
//
|
||||
// Adds the following API Commands:
|
||||
// "Read" - See AnalogSensor.Read
|
||||
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
||||
func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *AnalogSensorDriver {
|
||||
d := &AnalogSensorDriver{
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
@ -32,6 +32,10 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
d.interval = v[0]
|
||||
}
|
||||
|
||||
d.AddEvent("data")
|
||||
d.AddEvent("error")
|
||||
d.AddCommand("Read", func(params map[string]interface{}) interface{} {
|
||||
|
@ -20,7 +20,7 @@ type MakeyButtonDriver struct {
|
||||
}
|
||||
|
||||
// NewMakeyButtonDriver returns a new MakeyButtonDriver given a DigitalRead, name and pin.
|
||||
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
|
||||
func NewMakeyButtonDriver(a DigitalReader, name string, pin string, v ...time.Duration) *MakeyButtonDriver {
|
||||
m := &MakeyButtonDriver{
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
@ -30,6 +30,10 @@ func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButton
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
m.interval = v[0]
|
||||
}
|
||||
|
||||
m.AddEvent("error")
|
||||
m.AddEvent("push")
|
||||
m.AddEvent("release")
|
||||
|
@ -29,8 +29,7 @@ func (h *HMC6352Driver) adaptor() I2cInterface {
|
||||
return h.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
// using specified interval to update Heading
|
||||
// Start initialized the hmc6352
|
||||
func (h *HMC6352Driver) Start() (errs []error) {
|
||||
if err := h.adaptor().I2cStart(0x21); err != nil {
|
||||
return []error{err}
|
||||
|
@ -38,13 +38,17 @@ type MPL115A2Driver struct {
|
||||
}
|
||||
|
||||
// NewMPL115A2Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPL115A2Driver(a I2cInterface, name string) *MPL115A2Driver {
|
||||
func NewMPL115A2Driver(a I2cInterface, name string, v ...time.Duration) *MPL115A2Driver {
|
||||
m := &MPL115A2Driver{
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
Eventer: gobot.NewEventer(),
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
m.interval = v[0]
|
||||
}
|
||||
m.AddEvent("error")
|
||||
return m
|
||||
}
|
||||
|
@ -32,23 +32,28 @@ type ThreeDData struct {
|
||||
}
|
||||
|
||||
type MPU6050Driver struct {
|
||||
name string
|
||||
connection gobot.Connection
|
||||
interval time.Duration
|
||||
gobot.Eventer
|
||||
name string
|
||||
connection gobot.Connection
|
||||
interval time.Duration
|
||||
Accelerometer ThreeDData
|
||||
Gyroscope ThreeDData
|
||||
Temperature int16
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewMPU6050Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPU6050Driver(a I2cInterface, name string) *MPU6050Driver {
|
||||
func NewMPU6050Driver(a I2cInterface, name string, v ...time.Duration) *MPU6050Driver {
|
||||
m := &MPU6050Driver{
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
interval: 10 * time.Millisecond,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
m.interval = v[0]
|
||||
}
|
||||
|
||||
m.AddEvent("error")
|
||||
return m
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ type WiichuckDriver struct {
|
||||
// "z"- Get's triggered every interval amount of time if the z button is pressed
|
||||
// "c" - Get's triggered every interval amount of time if the c button is pressed
|
||||
// "joystick" - Get's triggered every "interval" amount of time if a joystick event occured, you can access values x, y
|
||||
func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
func NewWiichuckDriver(a I2cInterface, name string, v ...time.Duration) *WiichuckDriver {
|
||||
w := &WiichuckDriver{
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
@ -42,6 +42,10 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
},
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
w.interval = v[0]
|
||||
}
|
||||
|
||||
w.AddEvent("z")
|
||||
w.AddEvent("c")
|
||||
w.AddEvent("joystick")
|
||||
|
@ -17,10 +17,10 @@ type JoystickDriver struct {
|
||||
name string
|
||||
interval time.Duration
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
configPath string
|
||||
config joystickConfig
|
||||
poll func() sdl.Event
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// pair is a JSON representation of name and id
|
||||
@ -50,7 +50,7 @@ type joystickConfig struct {
|
||||
// It adds the following events:
|
||||
// (button)_press - triggered when (button) is pressed
|
||||
// (button)_release - triggered when (button) is released
|
||||
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
|
||||
func NewJoystickDriver(a *JoystickAdaptor, name string, config string, v ...time.Duration) *JoystickDriver {
|
||||
d := &JoystickDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
@ -59,6 +59,11 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
|
||||
poll: func() sdl.Event {
|
||||
return sdl.PollEvent()
|
||||
},
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
d.interval = v[0]
|
||||
}
|
||||
|
||||
d.AddEvent("error")
|
||||
|
@ -24,7 +24,7 @@ type MavlinkInterface interface {
|
||||
// It add the following events:
|
||||
// "packet" - triggered when a new packet is read
|
||||
// "message" - triggered when a new valid message is processed
|
||||
func NewMavlinkDriver(a *MavlinkAdaptor, name string) *MavlinkDriver {
|
||||
func NewMavlinkDriver(a *MavlinkAdaptor, name string, v ...time.Duration) *MavlinkDriver {
|
||||
m := &MavlinkDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
@ -32,6 +32,10 @@ func NewMavlinkDriver(a *MavlinkAdaptor, name string) *MavlinkDriver {
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
m.interval = v[0]
|
||||
}
|
||||
|
||||
m.AddEvent("packet")
|
||||
m.AddEvent("message")
|
||||
m.AddEvent("error")
|
||||
|
@ -22,7 +22,7 @@ type CameraDriver struct {
|
||||
|
||||
// NewCameraDriver creates a new driver with specified name and source.
|
||||
// It also creates a start function to either set camera as a File or Camera capture.
|
||||
func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
||||
func NewCameraDriver(name string, source interface{}, v ...time.Duration) *CameraDriver {
|
||||
c := &CameraDriver{
|
||||
name: name,
|
||||
Eventer: gobot.NewEventer(),
|
||||
@ -41,6 +41,10 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
||||
},
|
||||
}
|
||||
|
||||
if len(v) > 0 {
|
||||
c.interval = v[0]
|
||||
}
|
||||
|
||||
c.AddEvent("frame")
|
||||
|
||||
return c
|
||||
|
Loading…
x
Reference in New Issue
Block a user