Commit 85c08ad1 authored by simon's avatar simon
Browse files

add DeviceExists(index)

parent b04b7ccc
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -357,16 +357,6 @@ func (d HID) OnEdge(index uint8) chan Event {
//}


// see if Device exists.
func DeviceExists(index uint8) (ok bool) {
	for _, v := range d.Buttons {
		if v.number == button {
			return true
		}
	}
	return
}

// see if Button exists.
func (d HID) ButtonExists(index uint8) (ok bool) {
	for _, v := range d.Buttons {
+7 −0
Original line number Diff line number Diff line
@@ -20,8 +20,15 @@ type osEventRecord struct {

const maxValue = 1<<15 - 1

// common path root, so Connect and DeviceExists are not thread safe.
var inputPathSlice = []byte("/dev/input/js ")[0:13]

// see if Device exists.
func DeviceExists(index uint8) bool {
	_,err:=os.Stat(string(strconv.AppendUint(inputPathSlice, uint64(index-1), 10)))
	return err==nil
}

// Connect sets up a go routine that puts a joysticks events onto registered channels.
// to register channels use the returned HID object's On<xxx>(index) methods.
// Note: only one event, of each type '<xxx>', for each 'index', so re-registering, or deleting, an event stops events going on the old channel.
+34 −30
Original line number Diff line number Diff line
@@ -13,11 +13,44 @@ import (
)
import "math"

func TestHIDsMutipleCapture(t *testing.T) {
	if !DeviceExists(1) && !DeviceExists(2) && !DeviceExists(3) && !DeviceExists(4){
		panic("no HIDs")
	}

	buttonEvents := Capture(
		Channel{10, HID.OnLong}, // button #10 long pressed
		Channel{1, HID.OnClose}, 
	)
	hatEvents := Capture(
		Channel{1, HID.OnHat},  
		Channel{2, HID.OnSpeedX},
		Channel{2, HID.OnPanX},
	)

	var x float32 = .5
	var f time.Duration = time.Second / 440
	for {
		select {
		case <-buttonEvents[0]:
			return
		case <-buttonEvents[1]:
			play(NewSound(NewTone(f, float64(x)), time.Second/3))
		case h := <-hatEvents[0]:
			f = time.Duration(100*math.Pow(2, float64(h.(CoordsEvent).Y))) * time.Second / 44000
		case h := <-hatEvents[1]:
			fmt.Printf("hat 2 X speed %+v\n",h.(AxisEvent).V)
		case h := <-hatEvents[2]:
			fmt.Printf("hat 2 X pan %+v\n",h.(AxisEvent).V)
		}
	}
}

func TestHIDsAdvanced(t *testing.T) {
	js1 := Connect(1)

	if js1 == nil {
		panic("no HIDs")
		panic("no HID index 1")
	}
	if len(js1.Buttons) < 10 || len(js1.HatAxes) < 6 {
		t.Errorf("HID#1, available buttons %d, Hats %d\n", len(js1.Buttons), len(js1.HatAxes)/2)
@@ -94,35 +127,6 @@ func TestHIDsCapture(t *testing.T) {
	}
}

func TestHIDsMutipleCapture(t *testing.T) {
	buttonEvents := Capture(
		Channel{10, HID.OnLong}, // button #10 long pressed
		Channel{1, HID.OnClose}, 
	)
	hatEvents := Capture(
		Channel{1, HID.OnHat},  
		Channel{2, HID.OnSpeedX},
		Channel{2, HID.OnPanX},
	)

	var x float32 = .5
	var f time.Duration = time.Second / 440
	for {
		select {
		case <-buttonEvents[0]:
			return
		case <-buttonEvents[1]:
			play(NewSound(NewTone(f, float64(x)), time.Second/3))
		case h := <-hatEvents[0]:
			f = time.Duration(100*math.Pow(2, float64(h.(CoordsEvent).Y))) * time.Second / 44000
		case h := <-hatEvents[1]:
			fmt.Printf("hat 2 X speed %+v\n",h.(AxisEvent).V)
		case h := <-hatEvents[2]:
			fmt.Printf("hat 2 X pan %+v\n",h.(AxisEvent).V)
		}
	}
}

func play(s Sound) {
	out, in := io.Pipe()
	go func() {