diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index 224d37f9..04df57ae 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -120,39 +120,11 @@ func (j *Driver) adaptor() *Adaptor { // [button]_release // [axis] func (j *Driver) Start() (err error) { - switch j.configPath { - case Dualshock3: - j.config = dualshock3Config - case Dualshock4: - j.config = dualshock4Config - case Dualsense: - j.config = dualsenseConfig - case TFlightHotasX: - j.config = tflightHotasXConfig - case Xbox360: - j.config = xbox360Config - case Xbox360RockBandDrums: - j.config = xbox360RockBandDrumsConfig - case XboxOne: - j.config = xboxOneConfig - case Shield: - j.config = shieldConfig - case NintendoSwitchPair: - j.config = joyconPairConfig - default: - err := j.loadFile() - if err != nil { - return err - } + if err := j.initConfig(); err != nil { + return err } - 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) - } + j.initEvents() go func() { for { @@ -182,6 +154,46 @@ func (j *Driver) Start() (err error) { return } +func (j *Driver) initConfig() error { + switch j.configPath { + case Dualshock3: + j.config = dualshock3Config + case Dualshock4: + j.config = dualshock4Config + case Dualsense: + j.config = dualsenseConfig + case TFlightHotasX: + j.config = tflightHotasXConfig + case Xbox360: + j.config = xbox360Config + case Xbox360RockBandDrums: + j.config = xbox360RockBandDrumsConfig + case XboxOne: + j.config = xboxOneConfig + case Shield: + j.config = shieldConfig + case NintendoSwitchPair: + j.config = joyconPairConfig + default: + err := j.loadFile() + if err != nil { + return err + } + } + + return nil +} + +func (j *Driver) initEvents() { + 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) + } +} + // Halt stops joystick driver func (j *Driver) Halt() (err error) { j.halt <- true diff --git a/platforms/joystick/joystick_driver_test.go b/platforms/joystick/joystick_driver_test.go index a98d1016..b442d2ca 100644 --- a/platforms/joystick/joystick_driver_test.go +++ b/platforms/joystick/joystick_driver_test.go @@ -7,136 +7,104 @@ import ( "gobot.io/x/gobot/v2" "gobot.io/x/gobot/v2/gobottest" + + js "github.com/0xcafed00d/joystick" ) var _ gobot.Driver = (*Driver)(nil) -func initTestDriver(config string) *Driver { +func initTestDriver(config string) (*Driver, *testJoystick) { a := NewAdaptor(6) + tj := &testJoystick{} a.connect = func(j *Adaptor) (err error) { - j.joystick = &testJoystick{} + j.joystick = tj return nil } _ = a.Connect() d := NewDriver(a, config) - return d + return d, tj } func TestJoystickDriverName(t *testing.T) { - d := initTestDriver("./configs/xbox360_power_a_mini_proex.json") + d, _ := initTestDriver("./configs/dualshock3.json") gobottest.Assert(t, strings.HasPrefix(d.Name(), "Joystick"), true) d.SetName("NewName") gobottest.Assert(t, d.Name(), "NewName") } func TestDriverStart(t *testing.T) { - d := initTestDriver("./configs/xbox360_power_a_mini_proex.json") + d, _ := initTestDriver("./configs/dualshock3.json") d.interval = 1 * time.Millisecond gobottest.Assert(t, d.Start(), nil) time.Sleep(2 * time.Millisecond) } func TestDriverHalt(t *testing.T) { - d := initTestDriver("./configs/xbox360_power_a_mini_proex.json") + d, _ := initTestDriver("./configs/dualshock3.json") go func() { <-d.halt }() gobottest.Assert(t, d.Halt(), nil) } -// func TestDriverHandleEvent(t *testing.T) { -// sem := make(chan bool) -// d := initTestDriver("./configs/xbox360_power_a_mini_proex.json") -// _ = d.Start() +func TestDriverHandleEvent(t *testing.T) { + sem := make(chan bool) + d, tj := initTestDriver("./configs/dualshock3.json") + tj.axisCount = 6 + tj.buttonCount = 17 -// // left x stick -// _ = d.On(d.Event("left_x"), func(data interface{}) { -// gobottest.Assert(t, int16(100), data.(int16)) -// sem <- true -// }) -// _ = d.handleEvent(&sdl.JoyAxisEvent{ -// Which: 0, -// Axis: 0, -// Value: 100, -// }) -// select { -// case <-sem: -// case <-time.After(10 * time.Second): -// t.Errorf("Button Event \"left_x\" was not published") -// } + if err := d.initConfig(); err != nil { + t.Errorf("initConfig() error: %v", err) + } + + d.initEvents() -// // x button press -// _ = d.On(d.Event("x_press"), func(data interface{}) { -// sem <- true -// }) -// _ = d.handleEvent(&sdl.JoyButtonEvent{ -// Which: 0, -// Button: 2, -// State: 1, -// }) -// select { -// case <-sem: -// case <-time.After(10 * time.Second): -// t.Errorf("Button Event \"x_press\" was not published") -// } + // left x stick + _ = d.On(d.Event("left_x"), func(data interface{}) { + gobottest.Assert(t, int(255), data.(int)) + sem <- true + }) + _ = d.handleAxes(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 0, + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"left_x\" was not published") + } -// // x button release -// _ = d.On(d.Event("x_release"), func(data interface{}) { -// sem <- true -// }) -// _ = d.handleEvent(&sdl.JoyButtonEvent{ -// Which: 0, -// Button: 2, -// State: 0, -// }) -// select { -// case <-sem: -// case <-time.After(10 * time.Second): -// t.Errorf("Button Event \"x_release\" was not published") -// } + // x button press + _ = d.On(d.Event("x_press"), func(data interface{}) { + sem <- true + }) + _ = d.handleButtons(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 1 << 14, + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"x_press\" was not published") + } -// // down button press -// _ = d.On(d.Event("down_press"), func(data interface{}) { -// sem <- true -// }) -// _ = d.handleEvent(&sdl.JoyHatEvent{ -// Which: 0, -// Hat: 0, -// Value: 4, -// }) -// select { -// case <-sem: -// case <-time.After(10 * time.Second): -// t.Errorf("Hat Event \"down\" was not published") -// } - -// err := d.handleEvent(&sdl.JoyHatEvent{ -// Which: 0, -// Hat: 99, -// Value: 4, -// }) - -// gobottest.Assert(t, err.Error(), "Unknown Hat: 99 4") - -// err = d.handleEvent(&sdl.JoyAxisEvent{ -// Which: 0, -// Axis: 99, -// Value: 100, -// }) - -// gobottest.Assert(t, err.Error(), "Unknown Axis: 99") - -// err = d.handleEvent(&sdl.JoyButtonEvent{ -// Which: 0, -// Button: 99, -// State: 0, -// }) - -// gobottest.Assert(t, err.Error(), "Unknown Button: 99") -// } + // x button release + _ = d.On(d.Event("x_release"), func(data interface{}) { + sem <- true + }) + _ = d.handleButtons(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 0, + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"x_release\" was not published") + } +} func TestDriverInvalidConfig(t *testing.T) { - d := initTestDriver("./configs/doesnotexist") + d, _ := initTestDriver("./configs/doesnotexist") err := d.Start() gobottest.Assert(t, strings.Contains(err.Error(), "open ./configs/doesnotexist: no such file or directory"), true) } diff --git a/platforms/joystick/test_helper.go b/platforms/joystick/test_helper.go index 4fa17da1..b3873825 100644 --- a/platforms/joystick/test_helper.go +++ b/platforms/joystick/test_helper.go @@ -2,11 +2,14 @@ package joystick import js "github.com/0xcafed00d/joystick" -type testJoystick struct{} +type testJoystick struct { + axisCount int + buttonCount int +} func (t *testJoystick) Close() {} func (t *testJoystick) ID() int { return 0 } -func (t *testJoystick) ButtonCount() int { return 0 } -func (t *testJoystick) AxisCount() int { return 0 } +func (t *testJoystick) ButtonCount() int { return t.buttonCount } +func (t *testJoystick) AxisCount() int { return t.axisCount } func (t *testJoystick) Name() string { return "test-joy" } func (t *testJoystick) Read() (js.State, error) { return js.State{}, nil }