Loading joysticks.go +20 −0 Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ type HID struct { hatPanYEvents map[uint8]chan event hatPositionEvents map[uint8]chan event hatAngleEvents map[uint8]chan event hatEdgeEvents map[uint8]chan event } type event interface { Loading Loading @@ -131,6 +132,16 @@ func (d HID) ParcelOutEvents() { c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))} } } if c, ok := d.hatEdgeEvents[h.number]; ok { if (v==1 || v==-1) && h.value != 1 && h.value !=-1 { switch h.axis { case 1: c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index+1].value)))} case 2: c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))} } } } d.HatAxes[evt.Index] = hatAxis{h.number, h.axis, h.reversed, toDuration(evt.Time), v} default: // log.Println("unknown input type. ",evt.Type & 0x7f) Loading Loading @@ -196,6 +207,15 @@ func (d HID) OnRotate(hat uint8) chan event { return c } // hat moved to edge func (d HID) OnEdge(hat uint8) chan event { c := make(chan event) d.hatEdgeEvents[hat] = c return c } // see if Button exists. func (d HID) ButtonExists(button uint8) (ok bool) { for _, v := range d.Buttons { Loading joysticks_linux.go +1 −1 Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ func Connect(index int) (d *HID) { if e != nil { return nil } d = &HID{make(chan osEventRecord), make(map[uint8]button), make(map[uint8]hatAxis), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event)} d = &HID{make(chan osEventRecord), make(map[uint8]button), make(map[uint8]hatAxis), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event)} // start thread to read joystick events to the joystick.state osEvent channel go eventPipe(r, d.OSEvents) d.populate() Loading joysticks_test.go +3 −0 Original line number Diff line number Diff line Loading @@ -91,6 +91,7 @@ func TestHIDsAdvanced(t *testing.T) { h4 := js1.OnPanX(2) h5 := js1.OnPanY(2) h3 := js1.OnMove(3) h6:=js1.OnEdge(1) go js1.ParcelOutEvents() time.AfterFunc(time.Second*10, func() { js1.InsertSyntheticEvent(1, 1, 1) }) // value=1 (close),type=1 (button), index=1, so fires b1 after 10 seconds Loading @@ -115,6 +116,8 @@ func TestHIDsAdvanced(t *testing.T) { fmt.Println("hat 2 X moved", h.(HatPanXEvent).V) case h := <-h5: fmt.Println("hat 2 Y moved", h) case h := <-h6: fmt.Println("hat 1 edged", h.(HatAngleEvent).Angle) } } } Loading Loading
joysticks.go +20 −0 Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ type HID struct { hatPanYEvents map[uint8]chan event hatPositionEvents map[uint8]chan event hatAngleEvents map[uint8]chan event hatEdgeEvents map[uint8]chan event } type event interface { Loading Loading @@ -131,6 +132,16 @@ func (d HID) ParcelOutEvents() { c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))} } } if c, ok := d.hatEdgeEvents[h.number]; ok { if (v==1 || v==-1) && h.value != 1 && h.value !=-1 { switch h.axis { case 1: c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index+1].value)))} case 2: c <- HatAngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))} } } } d.HatAxes[evt.Index] = hatAxis{h.number, h.axis, h.reversed, toDuration(evt.Time), v} default: // log.Println("unknown input type. ",evt.Type & 0x7f) Loading Loading @@ -196,6 +207,15 @@ func (d HID) OnRotate(hat uint8) chan event { return c } // hat moved to edge func (d HID) OnEdge(hat uint8) chan event { c := make(chan event) d.hatEdgeEvents[hat] = c return c } // see if Button exists. func (d HID) ButtonExists(button uint8) (ok bool) { for _, v := range d.Buttons { Loading
joysticks_linux.go +1 −1 Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ func Connect(index int) (d *HID) { if e != nil { return nil } d = &HID{make(chan osEventRecord), make(map[uint8]button), make(map[uint8]hatAxis), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event)} d = &HID{make(chan osEventRecord), make(map[uint8]button), make(map[uint8]hatAxis), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event), make(map[uint8]chan event)} // start thread to read joystick events to the joystick.state osEvent channel go eventPipe(r, d.OSEvents) d.populate() Loading
joysticks_test.go +3 −0 Original line number Diff line number Diff line Loading @@ -91,6 +91,7 @@ func TestHIDsAdvanced(t *testing.T) { h4 := js1.OnPanX(2) h5 := js1.OnPanY(2) h3 := js1.OnMove(3) h6:=js1.OnEdge(1) go js1.ParcelOutEvents() time.AfterFunc(time.Second*10, func() { js1.InsertSyntheticEvent(1, 1, 1) }) // value=1 (close),type=1 (button), index=1, so fires b1 after 10 seconds Loading @@ -115,6 +116,8 @@ func TestHIDsAdvanced(t *testing.T) { fmt.Println("hat 2 X moved", h.(HatPanXEvent).V) case h := <-h5: fmt.Println("hat 2 Y moved", h) case h := <-h6: fmt.Println("hat 1 edged", h.(HatAngleEvent).Angle) } } } Loading