Commit 0559f8e3 authored by simon's avatar simon
Browse files

fix: axis swap bug

parent 5fe797d1
Loading
Loading
Loading
Loading
+13 −14
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@ package joysticks
import (
	"math"
	"time"
	//	"fmt"
)

// TODO drag event
@@ -113,9 +112,9 @@ type RadiusEvent struct {
	Radius float32
}

// ParcelOutEvents waits on the HID.OSEvent channel (so blocks until OSEvent is closed), then puts the required event(s), on any registered channel(s).
// ParcelOutEvents waits on the HID.OSEvent channel (so is blocking), then puts the required event(s), on any registered channel(s).
func (d HID) ParcelOutEvents() {
	for evt := range(d.OSEvents){
	for evt := range d.OSEvents {
		switch evt.Type {
		case 1:
			b := d.Buttons[evt.Index]
@@ -171,25 +170,25 @@ func (d HID) ParcelOutEvents() {
			if c, ok := d.Events[eventSignature{hatPosition, h.number}]; ok {
				switch h.axis {
				case 2:
					c <- CoordsEvent{when{toDuration(evt.Time)}, v, d.HatAxes[evt.Index+1].value}
					c <- CoordsEvent{when{toDuration(evt.Time)}, v, d.HatAxes[evt.Index-1].value}
				case 1:
					c <- CoordsEvent{when{toDuration(evt.Time)}, d.HatAxes[evt.Index-1].value, v}
					c <- CoordsEvent{when{toDuration(evt.Time)}, d.HatAxes[evt.Index+1].value, v}
				}
			}
			if c, ok := d.Events[eventSignature{hatAngle, h.number}]; ok {
				switch h.axis {
				case 2:
					c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index+1].value)))}
					c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index-1].value)))}
				case 1:
					c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))}
					c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index+1].value), float64(v)))}
				}
			}
			if c, ok := d.Events[eventSignature{hatRadius, h.number}]; ok {
				switch h.axis {
				case 2:
					c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(v)*float64(v) + float64(d.HatAxes[evt.Index+1].value)*float64(d.HatAxes[evt.Index+1].value)))}
					c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(v)*float64(v) + float64(d.HatAxes[evt.Index-1].value)*float64(d.HatAxes[evt.Index-1].value)))}
				case 1:
					c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(d.HatAxes[evt.Index-1].value)*float64(d.HatAxes[evt.Index-1].value) + float64(v)*float64(v)))}
					c <- RadiusEvent{when{toDuration(evt.Time)}, float32(math.Sqrt(float64(d.HatAxes[evt.Index+1].value)*float64(d.HatAxes[evt.Index+1].value) + float64(v)*float64(v)))}
				}
			}
			if c, ok := d.Events[eventSignature{hatEdge, h.number}]; ok {
@@ -197,9 +196,9 @@ func (d HID) ParcelOutEvents() {
				if (v == 1 || v == -1) && h.value != 1 && h.value != -1 {
					switch h.axis {
					case 2:
						c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index+1].value)))}
						c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(v), float64(d.HatAxes[evt.Index-1].value)))}
					case 1:
						c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index-1].value), float64(v)))}
						c <- AngleEvent{when{toDuration(evt.Time)}, float32(math.Atan2(float64(d.HatAxes[evt.Index+1].value), float64(v)))}
					}
				}
			}
@@ -207,11 +206,11 @@ func (d HID) ParcelOutEvents() {
				if v == 0 && h.value != 0 {
					switch h.axis {
					case 2:
						if d.HatAxes[evt.Index+1].value == 0 {
						if d.HatAxes[evt.Index-1].value == 0 {
							c <- when{toDuration(evt.Time)}
						}
					case 1:
						if d.HatAxes[evt.Index-1].value == 0 {
						if d.HatAxes[evt.Index+1].value == 0 {
							c <- when{toDuration(evt.Time)}
						}
					}
@@ -221,7 +220,6 @@ func (d HID) ParcelOutEvents() {
		default:
			// log.Println("unknown input type. ",evt.Type & 0x7f)
		}

	}
}

@@ -390,3 +388,4 @@ func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) {
}


+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ func (d HID) populate() {
				hatNumber += 1
			}
		default:
			go func() { d.OSEvents <- evt }() // have to consume a real event to know we reached the end of the synthetic burst, so hand it on.
			go func() { d.OSEvents <- evt }() // have to consume a real event to know we reached the end of the synthetic burst, so refire it.
			return
		}
	}
+4 −4
Original line number Diff line number Diff line
@@ -29,11 +29,11 @@ func TestHIDsAdvanced(t *testing.T) {
	b4 := js1.OnClose(4)
	b5 := js1.OnClose(5)
	quit := js1.OnOpen(10)
	h3 := js1.OnMove(3)
	h3 := js1.OnMove(1)
	h4 := js1.OnPanX(2)
	h5 := js1.OnPanY(2)
	h6 := js1.OnEdge(1)
	h7 := js1.OnCenter(1)
	h7 := js1.OnCenter(3)
	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

@@ -54,7 +54,7 @@ func TestHIDsAdvanced(t *testing.T) {
			js1.HatCoords(1, coord)
			fmt.Println(coord)
		case h := <-h3:
			fmt.Printf("hat 3 moved %+v\n", h)
			fmt.Printf("hat 1 moved %+v\n", h)
		case h := <-h4:
			fmt.Println("hat 2 X moved", h.(AxisEvent).V)
		case h := <-h5:
@@ -62,7 +62,7 @@ func TestHIDsAdvanced(t *testing.T) {
		case h := <-h6:
			fmt.Println("hat 1 edged", h.(AngleEvent).Angle)
		case <-h7:
			fmt.Println("hat 1 centered")
			fmt.Println("hat 3 centered")
		}
	}
}