2014-04-27 18:02:39 -07:00
|
|
|
package joystick
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-11-19 15:45:59 -08:00
|
|
|
"time"
|
2014-07-09 18:32:27 -07:00
|
|
|
|
2016-08-26 14:23:03 +02:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
2017-02-02 16:24:48 +01:00
|
|
|
"gobot.io/x/gobot"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2018-08-25 18:02:01 +02:00
|
|
|
const (
|
|
|
|
// Dualshock3 joystick configuration.
|
|
|
|
Dualshock3 = "dualshock3"
|
|
|
|
|
|
|
|
// Dualshock4 joystick configuration.
|
|
|
|
Dualshock4 = "dualshock4"
|
|
|
|
|
|
|
|
// TFlightHotasX flight stick configuration.
|
|
|
|
TFlightHotasX = "tflightHotasX"
|
|
|
|
|
2022-10-21 18:57:27 +02:00
|
|
|
// Configuration for Xbox 360 controller.
|
2018-08-25 18:02:01 +02:00
|
|
|
Xbox360 = "xbox360"
|
|
|
|
|
|
|
|
// Xbox360RockBandDrums controller configuration.
|
|
|
|
Xbox360RockBandDrums = "xbox360RockBandDrums"
|
2020-03-13 01:39:48 -07:00
|
|
|
|
2022-10-21 18:57:27 +02:00
|
|
|
// Configuration for the Xbox One controller.
|
|
|
|
XboxOne = "xboxOne"
|
2018-08-25 18:02:01 +02:00
|
|
|
)
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
// Driver represents a joystick
|
|
|
|
type Driver struct {
|
2014-11-22 19:54:32 -08:00
|
|
|
name string
|
|
|
|
interval time.Duration
|
|
|
|
connection gobot.Connection
|
2014-11-19 15:45:59 -08:00
|
|
|
configPath string
|
|
|
|
config joystickConfig
|
|
|
|
poll func() sdl.Event
|
2014-12-23 01:20:44 -08:00
|
|
|
halt chan bool
|
2014-11-28 18:37:03 -08:00
|
|
|
gobot.Eventer
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// pair is a JSON representation of name and id
|
2014-04-26 03:11:51 -07:00
|
|
|
type pair struct {
|
|
|
|
Name string `json:"name"`
|
2014-06-10 15:16:11 -07:00
|
|
|
ID int `json:"id"`
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// hat is a JSON representation of hat, name and id
|
2014-04-26 03:11:51 -07:00
|
|
|
type hat struct {
|
|
|
|
Hat int `json:"hat"`
|
|
|
|
Name string `json:"name"`
|
2014-06-10 15:16:11 -07:00
|
|
|
ID int `json:"id"`
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// joystickConfig is a JSON representation of configuration values
|
2014-04-26 03:11:51 -07:00
|
|
|
type joystickConfig struct {
|
|
|
|
Name string `json:"name"`
|
2015-01-03 05:06:08 -08:00
|
|
|
GUID string `json:"guid"`
|
2014-04-26 03:11:51 -07:00
|
|
|
Axis []pair `json:"axis"`
|
|
|
|
Buttons []pair `json:"buttons"`
|
|
|
|
Hats []hat `json:"Hats"`
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
// NewDriver returns a new Driver with a polling interval of
|
|
|
|
// 10 Milliseconds given a Joystick Adaptor and json button configuration
|
2015-01-03 05:06:08 -08:00
|
|
|
// file location.
|
2014-10-20 10:46:03 -05:00
|
|
|
//
|
2016-07-13 10:44:47 -06:00
|
|
|
// Optionally accepts:
|
2016-09-25 21:07:36 +02:00
|
|
|
// time.Duration: Interval at which the Driver is polled for new information
|
|
|
|
func NewDriver(a *Adaptor, config string, v ...time.Duration) *Driver {
|
|
|
|
d := &Driver{
|
2017-02-02 16:24:48 +01:00
|
|
|
name: gobot.DefaultName("Joystick"),
|
2014-11-22 19:54:32 -08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-19 15:45:59 -08:00
|
|
|
configPath: config,
|
2014-07-22 13:55:19 -07:00
|
|
|
poll: func() sdl.Event {
|
|
|
|
return sdl.PollEvent()
|
|
|
|
},
|
2014-11-28 18:37:03 -08:00
|
|
|
interval: 10 * time.Millisecond,
|
2014-12-23 01:20:44 -08:00
|
|
|
halt: make(chan bool, 0),
|
2014-11-28 18:37:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(v) > 0 {
|
|
|
|
d.interval = v[0]
|
2014-04-27 18:02:39 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-11-19 15:45:59 -08:00
|
|
|
d.AddEvent("error")
|
2014-04-26 03:11:51 -07:00
|
|
|
return d
|
|
|
|
}
|
2015-01-03 05:06:08 -08:00
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
// Name returns the Drivers name
|
|
|
|
func (j *Driver) Name() string { return j.name }
|
2015-01-03 05:06:08 -08:00
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
// SetName sets the Drivers name
|
|
|
|
func (j *Driver) SetName(n string) { j.name = n }
|
|
|
|
|
|
|
|
// Connection returns the Drivers connection
|
|
|
|
func (j *Driver) Connection() gobot.Connection { return j.connection }
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// adaptor returns joystick adaptor
|
2016-09-25 21:07:36 +02:00
|
|
|
func (j *Driver) adaptor() *Adaptor {
|
|
|
|
return j.Connection().(*Adaptor)
|
2014-06-15 17:22:50 -07:00
|
|
|
}
|
|
|
|
|
2015-01-03 05:06:08 -08:00
|
|
|
// Start and polls the state of the joystick at the given interval.
|
|
|
|
//
|
|
|
|
// Emits the Events:
|
|
|
|
// Error error - On button error
|
|
|
|
// Events defined in the json button configuration file.
|
|
|
|
// They will have the format:
|
|
|
|
// [button]_press
|
|
|
|
// [button]_release
|
|
|
|
// [axis]
|
2016-11-07 19:32:12 +01:00
|
|
|
func (j *Driver) Start() (err error) {
|
2018-04-14 13:34:28 +02:00
|
|
|
switch j.configPath {
|
2022-10-21 18:57:27 +02:00
|
|
|
case Dualshock3:
|
2018-04-14 13:34:28 +02:00
|
|
|
j.config = dualshock3Config
|
2022-10-21 18:57:27 +02:00
|
|
|
case Dualshock4:
|
2018-04-14 13:34:28 +02:00
|
|
|
j.config = dualshock4Config
|
2022-10-21 18:57:27 +02:00
|
|
|
case TFlightHotasX:
|
2018-05-08 07:25:37 +01:00
|
|
|
j.config = tflightHotasXConfig
|
2022-10-21 18:57:27 +02:00
|
|
|
case Xbox360:
|
2018-04-14 13:34:28 +02:00
|
|
|
j.config = xbox360Config
|
2022-10-21 18:57:27 +02:00
|
|
|
case Xbox360RockBandDrums:
|
2018-05-07 23:46:17 +02:00
|
|
|
j.config = xbox360RockBandDrumsConfig
|
2022-10-21 18:57:27 +02:00
|
|
|
case XboxOne:
|
|
|
|
j.config = xboxOneConfig
|
2018-04-14 13:34:28 +02:00
|
|
|
default:
|
2018-08-04 23:07:02 -04:00
|
|
|
err := j.loadFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-19 15:45:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range j.config.Buttons {
|
|
|
|
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
|
|
|
|
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
|
|
|
|
}
|
|
|
|
for _, value := range j.config.Axis {
|
|
|
|
j.AddEvent(value.Name)
|
|
|
|
}
|
|
|
|
for _, value := range j.config.Hats {
|
2018-05-23 23:56:58 -07:00
|
|
|
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
|
|
|
|
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
|
2014-11-19 15:45:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2016-02-22 04:54:17 +05:30
|
|
|
for event := j.poll(); event != nil; event = j.poll() {
|
2016-11-07 19:32:12 +01:00
|
|
|
if errs := j.handleEvent(event); errs != nil {
|
|
|
|
j.Publish(j.Event("error"), errs)
|
2014-11-19 15:45:59 -08:00
|
|
|
}
|
|
|
|
}
|
2014-12-23 01:20:44 -08:00
|
|
|
select {
|
|
|
|
case <-time.After(j.interval):
|
|
|
|
case <-j.halt:
|
|
|
|
return
|
|
|
|
}
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
2014-11-19 15:45:59 -08:00
|
|
|
}()
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
// Halt stops joystick driver
|
2016-11-07 19:32:12 +01:00
|
|
|
func (j *Driver) Halt() (err error) {
|
2014-12-23 01:20:44 -08:00
|
|
|
j.halt <- true
|
|
|
|
return
|
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
|
2018-05-23 23:56:58 -07:00
|
|
|
var previousHat = ""
|
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// HandleEvent publishes an specific event according to data received
|
2016-09-25 21:07:36 +02:00
|
|
|
func (j *Driver) handleEvent(event sdl.Event) error {
|
2014-07-22 13:55:19 -07:00
|
|
|
switch data := event.(type) {
|
|
|
|
case *sdl.JoyAxisEvent:
|
|
|
|
if data.Which == j.adaptor().joystick.InstanceID() {
|
|
|
|
axis := j.findName(data.Axis, j.config.Axis)
|
|
|
|
if axis == "" {
|
2015-01-03 05:06:08 -08:00
|
|
|
return fmt.Errorf("Unknown Axis: %v", data.Axis)
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
2016-08-30 17:10:50 +02:00
|
|
|
j.Publish(j.Event(axis), data.Value)
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
|
|
|
case *sdl.JoyButtonEvent:
|
|
|
|
if data.Which == j.adaptor().joystick.InstanceID() {
|
|
|
|
button := j.findName(data.Button, j.config.Buttons)
|
|
|
|
if button == "" {
|
2015-01-03 05:06:08 -08:00
|
|
|
return fmt.Errorf("Unknown Button: %v", data.Button)
|
|
|
|
}
|
|
|
|
if data.State == 1 {
|
2016-08-30 17:10:50 +02:00
|
|
|
j.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
|
2018-04-14 12:33:38 +02:00
|
|
|
} else {
|
|
|
|
j.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 13:55:19 -07:00
|
|
|
case *sdl.JoyHatEvent:
|
|
|
|
if data.Which == j.adaptor().joystick.InstanceID() {
|
|
|
|
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
|
|
|
|
if hat == "" {
|
2015-01-03 05:06:08 -08:00
|
|
|
return fmt.Errorf("Unknown Hat: %v %v", data.Hat, data.Value)
|
2018-05-23 23:56:58 -07:00
|
|
|
} else if hat == "released" {
|
|
|
|
hat = previousHat
|
|
|
|
j.Publish(j.Event(fmt.Sprintf("%s_release", hat)), true)
|
|
|
|
} else {
|
|
|
|
previousHat = hat
|
|
|
|
j.Publish(j.Event(fmt.Sprintf("%s_press", hat)), true)
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-07-22 13:55:19 -07:00
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
func (j *Driver) findName(id uint8, list []pair) string {
|
2014-04-26 03:11:51 -07:00
|
|
|
for _, value := range list {
|
2014-06-10 15:16:11 -07:00
|
|
|
if int(id) == value.ID {
|
2014-04-26 03:11:51 -07:00
|
|
|
return value.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-10-20 10:46:03 -05:00
|
|
|
// findHatName returns name from hat found by id in provided list
|
2016-09-25 21:07:36 +02:00
|
|
|
func (j *Driver) findHatName(id uint8, hat uint8, list []hat) string {
|
2014-10-20 10:46:03 -05:00
|
|
|
for _, lHat := range list {
|
|
|
|
if int(id) == lHat.ID && int(hat) == lHat.Hat {
|
|
|
|
return lHat.Name
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-04-14 13:34:28 +02:00
|
|
|
|
|
|
|
// loadFile load the joystick config from a .json file
|
|
|
|
func (j *Driver) loadFile() error {
|
|
|
|
file, e := ioutil.ReadFile(j.configPath)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsontype joystickConfig
|
|
|
|
json.Unmarshal(file, &jsontype)
|
|
|
|
j.config = jsontype
|
|
|
|
return nil
|
|
|
|
}
|