Exemple #1
0
func (j *JoystickDriver) Start() bool {
	go func() {
		var event sdl.Event
		for {
			for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
				switch data := event.(type) {
				case *sdl.JoyAxisEvent:
					if data.Which == j.adaptor().joystick.InstanceID() {
						axis := j.findName(data.Axis, j.config.Axis)
						if axis == "" {
							fmt.Println("Unknown Axis:", data.Axis)
						} else {
							gobot.Publish(j.Event(axis), data.Value)
						}
					}
				case *sdl.JoyButtonEvent:
					if data.Which == j.adaptor().joystick.InstanceID() {
						button := j.findName(data.Button, j.config.Buttons)
						if button == "" {
							fmt.Println("Unknown Button:", data.Button)
						} else {
							if data.State == 1 {
								gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
							} else {
								gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
							}
						}
					}
				case *sdl.JoyHatEvent:
					if data.Which == j.adaptor().joystick.InstanceID() {
						hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
						if hat == "" {
							fmt.Println("Unknown Hat:", data.Hat, data.Value)
						} else {
							gobot.Publish(j.Event(hat), true)
						}
					}
				}
			}
			time.Sleep(10 * time.Millisecond)
		}
	}()
	return true
}
Exemple #2
0
// NewJoystickDriver returns a new JoystickDriver with a polling interval of
// 10 Milliseconds given a JoystickAdaptor, name and json button configuration
// file location.
//
// Optinally accepts:
//  time.Duration: Interval at which the JoystickDriver is polled for new information
func NewJoystickDriver(a *JoystickAdaptor, name string, config string, v ...time.Duration) *JoystickDriver {
	d := &JoystickDriver{
		name:       name,
		connection: a,
		Eventer:    gobot.NewEventer(),
		configPath: config,
		poll: func() sdl.Event {
			return sdl.PollEvent()
		},
		interval: 10 * time.Millisecond,
		halt:     make(chan bool, 0),
	}

	if len(v) > 0 {
		d.interval = v[0]
	}

	d.AddEvent("error")
	return d
}