Commit 424b00e5 authored by simon's avatar simon
Browse files

simple example

parent 9881f08e
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -18,9 +18,23 @@ Overview/docs: [![GoDoc](https://godoc.org/github.com/splace/joysticks?status.sv

     go get github.com/splace/joysticks

# Example: 
# Examples: 

print out event info when pressing button #1 or moving hat#1.(with 10sec timeout.) 
highlevel: block until button one pressed.

	package main

	import . "github.com/splace/joysticks"

	func main() {
		evts := Capture(
			Channel{1, HID.OnClose},  // event[0] chan set to receive button #1 closes events
		)
		<-evts[0]
	}


print out description of event when pressing button #1 or moving hat#1.(with 10sec timeout.) 

	package main

@@ -34,6 +48,8 @@ print out event info when pressing button #1 or moving hat#1.(with 10sec timeout
		if device == nil {
			panic("no HIDs")
		}

		// using Connect allows a device to be interrogated
		fmt.Printf("HID#1:- Buttons:%d, Hats:%d\n", len(device.Buttons), len(device.HatAxes)/2)

		// make channels for specific events
+2 −62
Original line number Diff line number Diff line
package main

import (
	"io"
	"os/exec"
	"time"
	"math"
	"os"
	"os/signal"
	"log"
)

import . "github.com/splace/joysticks"

import . "github.com/splace/sounds"


func main() {
	stopChan := make(chan os.Signal)
	signal.Notify(stopChan, os.Interrupt)

	events := Capture(
		Channel{10, HID.OnLong},  // event[0] button #10 long pressed
		Channel{1, HID.OnClose},  // event[1] button #1 closes
		Channel{7, HID.OnOpen},  // event[2] button #7 opens
		Channel{8, HID.OnOpen},  // event[3] button #8 opens
		Channel{2, HID.OnRotate}, // event[4] hat #1 rotates
		Channel{1, HID.OnEdge}, // event[5] hat #2 rotates
		Channel{1, HID.OnClose},  // event[0] is button #1 closes
	)
	var volume float32 = .5
	var octave =5
	var note int =1
	for {
		select {
		case <-stopChan: // wait for SIGINT
			log.Println("Interrupted")
			return
		case <-events[0]:
			return
		case <-events[1]:
			play(NewSound(NewTone(Period(octave,note), float64(volume)), time.Second/3))
		case <-events[2]:
			octave++
		case <-events[3]:
			octave--
		case h := <-events[4]:
			volume = h.(AngleEvent).Angle/6.28 + .5
		case h := <-events[5]:
			//f = time.Duration(100*math.Pow(2, float64(h.(HatAngleEvent).Angle)/6.28)) * time.Second / 44000
			note = int(h.(AngleEvent).Angle*6 / math.Pi)+6
			play(NewSound(NewTone(Period(octave,note), float64(volume)), time.Second/3))
		}
	<-events[0]
}
}

func play(s Sound) {
	cmd := exec.Command("aplay")
	out, in := io.Pipe()
	go func() {
		Encode(in, 2, 44100, s)
		in.Close()
	}()
	cmd.Stdin = out
	err := cmd.Run()
	if err != nil {
		panic(err)
	}
	log.Println("Playing:",s)
}