Skip to content

Commit

Permalink
platform/joystick: add bin/scanner to handle OS differences
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Sep 9, 2023
1 parent 8e434f2 commit 2891173
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 47 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/muka/go-bluetooth v0.0.0-20221213043340-85dc80edc4e1 // indirect
github.com/nats-io/nats-server/v2 v2.7.4 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/saltosystems/winrt-go v0.0.0-20230613063811-c792451fa808 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/muka/go-bluetooth v0.0.0-20220830075246-0746e3a1ea53/go.mod h1:dMCjicU6vRBk34dqOmIZm0aod6gUwZXOXzBROqGous0=
github.com/muka/go-bluetooth v0.0.0-20221213043340-85dc80edc4e1 h1:BuVRHr4HHJbk1DHyWkArJ7E8J/VA8ncCr/VLnQFazBo=
Expand All @@ -69,6 +71,8 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf/go.mod h1:+AwQL2mK3Pd3S+TUwg0tYQjid0q1txyNUJuuSmz8Kdk=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
Expand Down
135 changes: 88 additions & 47 deletions platforms/joystick/bin/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,106 @@
// Based on original code from Jacky Boen
// https://github.com/veandco/go-sdl2/blob/master/examples/events/events.go

// Simple program that displays the state of the specified joystick
//
// go run joysticktest.go 2
// displays state of joystick id 2
package main

import (
"fmt"
"github.com/nsf/termbox-go"
"github.com/0xcafed00d/joystick"
"os"

"github.com/veandco/go-sdl2/sdl"
"strconv"
"time"
)

var joysticks [16]*sdl.Joystick

func run() int {
var event sdl.Event
var running bool

sdl.Init(sdl.INIT_JOYSTICK)
defer sdl.Quit()

sdl.JoystickEventState(sdl.ENABLE)

running = true
for running {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.JoyAxisEvent:
fmt.Printf("[%d ms] Axis: %d\tvalue:%d\n",
t.Timestamp, t.Axis, t.Value)
case *sdl.JoyBallEvent:
fmt.Printf("[%d ms] Ball:%d\txrel:%d\tyrel:%d\n",
t.Timestamp, t.Ball, t.XRel, t.YRel)
case *sdl.JoyButtonEvent:
fmt.Printf("[%d ms] Button:%d\tstate:%d\n",
t.Timestamp, t.Button, t.State)
case *sdl.JoyHatEvent:
fmt.Printf("[%d ms] Hat:%d\tvalue:%d\n",
t.Timestamp, t.Hat, t.Value)
case *sdl.JoyDeviceAddedEvent:
joysticks[int(t.Which)] = sdl.JoystickOpen(int(t.Which))
if joysticks[int(t.Which)] != nil {
fmt.Printf("Joystick %d connected\n", t.Which)
}
case *sdl.JoyDeviceRemovedEvent:
if joystick := joysticks[int(t.Which)]; joystick != nil {
joystick.Close()
}
fmt.Printf("Joystick %d disconnected\n", t.Which)
default:
fmt.Printf("Unknown event\n")
}
func printAt(x, y int, s string) {
for _, r := range s {
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x++
}
}

func readJoystick(js joystick.Joystick) {
jinfo, err := js.Read()

if err != nil {
printAt(1, 5, "Error: "+err.Error())
return
}

printAt(1, 5, "Buttons:")
for button := 0; button < js.ButtonCount(); button++ {
if jinfo.Buttons&(1<<uint32(button)) != 0 {
printAt(10+button, 5, "X")
printAt(1, 6, fmt.Sprintf("Button %2d Pressed", button))
} else {
printAt(10+button, 5, ".")
}
}

sdl.Delay(16)
for axis := 0; axis < js.AxisCount(); axis++ {
printAt(1, axis+8, fmt.Sprintf("Axis %2d Value: %7d", axis, jinfo.AxisData[axis]))
}

return 0
return
}

func main() {
os.Exit(run())

jsid := 0
if len(os.Args) > 1 {
i, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println(err)
return
}
jsid = i
}

js, jserr := joystick.Open(jsid)

if jserr != nil {
fmt.Println(jserr)
return
}

err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()

eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()

ticker := time.NewTicker(time.Millisecond * 40)

for doQuit := false; !doQuit; {
select {
case ev := <-eventQueue:
if ev.Type == termbox.EventKey {
if ev.Ch == 'q' {
doQuit = true
}
}
if ev.Type == termbox.EventResize {
termbox.Flush()
}

case <-ticker.C:
printAt(1, 0, "-- Press 'q' to Exit --")
printAt(1, 1, fmt.Sprintf("Joystick Name: %s", js.Name()))
printAt(1, 2, fmt.Sprintf(" Axis Count: %d", js.AxisCount()))
printAt(1, 3, fmt.Sprintf(" Button Count: %d", js.ButtonCount()))
readJoystick(js)
termbox.Flush()
}
}
}

0 comments on commit 2891173

Please sign in to comment.