Commit 7f258388 authored by simon's avatar simon
Browse files

simple example

parent b828a66a
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
only needs 2 buttons and one hat.

prints button press and hat move event.

also prints current hat#1 position when pressing button #2.

output:

	HID#1:- Buttons:12, Hats:3
	Timeout in 10 secs.
	button #1 pressed
	button #2 pressed
	current hat #1 position: [0 0]
	hat #1 moved too: -0.07217628 0
	hat #1 moved too: -0.1340373 0
	hat #1 moved too: -0.16498306 0
	hat #1 moved too: -0.1752678 0
	hat #1 moved too: -0.18558306 0
	hat #1 moved too: -0.19589831 0
	hat #1 moved too: -0.18558306 0
	hat #1 moved too: -0.19589831 0
	hat #1 moved too: -0.16498306 0
	hat #1 moved too: -0.15466781 0
	hat #1 moved too: -0.1340373 0
	hat #1 moved too: -0.12372204 0
	hat #1 moved too: -0.113406785 0
	hat #1 moved too: -0.12372204 0
	hat #1 moved too: -0.113406785 0
	hat #1 moved too: -0.12372204 0
	hat #1 moved too: -0.113406785 0
	hat #1 moved too: -0.12372204 0
	hat #1 moved too: -0.113406785 0
	button #2 pressed
	current hat #1 position: [-0.113406785 0]
+34 −6
Original line number Diff line number Diff line
package main

//import "log"
//import "time"
import "os"
import . "github.com/splace/joysticks"
import "fmt"
import  "time"

func main() {
	deviceReader, err := os.OpenFile("/dev/input/js1", os.O_RDWR, 0)
	var OSEvents chan osEventRecord
	go eventPipe(deviceReader, OSEvents)
	device := Connect(1)

	if device == nil {
		panic("no HIDs")
	}
	fmt.Printf("HID#1:- Buttons:%d, Hats:%d\n", len(device.Buttons), len(device.HatAxes)/2)

	b1press := device.OnClose(1)
	b2press := device.OnClose(2)
	h1move := device.OnMove(1)

	go device.ParcelOutEvents()

	fmt.Println("Timeout in 10 secs.")
	
loop:
	for{
		select {
	    case <-time.After(time.Second*10):
			fmt.Println("Shutting down due to timeout.")
			break loop
		case <-b1press:
			fmt.Println("button #1 pressed")
		case <-b2press:
			fmt.Println("button #2 pressed")
			coords := make([]float32, 2)
			device.ReadHatPosition(1,coords)
			fmt.Println("current hat #1 position:",coords)
		
		case h := <-h1move:
			hpos:=h.(HatPositionEvent)
			fmt.Println("hat #1 moved too:", hpos.X,hpos.Y)
		}
	}	
}