Task: Joystick position




Go: package main

import (
    "fmt"
    "github.com/nsf/termbox-go"
    "github.com/simulatedsimian/joystick"
    "log"
    "os"
    "strconv"
    "time"
)

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, hidden bool) {
    jinfo, err := js.Read()
    check(err)

    w, h := termbox.Size()
    tbcd := termbox.ColorDefault
    termbox.Clear(tbcd, tbcd)
    printAt(1, h-1, "q - quit")
    if hidden {
        printAt(11, h-1, "s - show buttons:")
    } else {
        bs := ""
        printAt(11, h-1, "h - hide buttons:")
        for button := 0; button < js.ButtonCount(); button++ {
            if jinfo.Buttons&(1<<uint32(button)) != 0 {
                
                bs += fmt.Sprintf(" %X", button+1)
            }
        }
        printAt(28, h-1, bs)
    }

    
    x := int(float64((jinfo.AxisData[0]+32767)*(w-1)) / 65535)
    y := int(float64((jinfo.AxisData[1]+32767)*(h-2)) / 65535)
    printAt(x, y, "+") 
    termbox.Flush()
}

func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    
    
    
    jsid := 0
    
    if len(os.Args) > 1 {
        i, err := strconv.Atoi(os.Args[1])
        check(err)
        jsid = i
    }

    js, jserr := joystick.Open(jsid)
    check(jserr)
 
    err := termbox.Init()
    check(err)
    defer termbox.Close()

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

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

    for doQuit := false; !doQuit; {
        select {
        case ev := <-eventQueue:
            if ev.Type == termbox.EventKey {
                if ev.Ch == 'q' {
                    doQuit = true
                } else if ev.Ch == 'h' {
                    hidden = true
                } else if ev.Ch == 's' {
                    hidden = false
                }
            }
            if ev.Type == termbox.EventResize {
                termbox.Flush()
            }
        case <-ticker.C:
            readJoystick(js, hidden)
        }
    }
}




Python: import sys
import pygame

pygame.init()


clk = pygame.time.Clock()


if pygame.joystick.get_count() == 0:
    raise IOError("No joystick detected")
joy = pygame.joystick.Joystick(0)
joy.init()


size = width, height = 600, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Joystick Tester")


frameRect = pygame.Rect((45, 45), (510, 510))


crosshair = pygame.surface.Surface((10, 10))
crosshair.fill(pygame.Color("magenta"))
pygame.draw.circle(crosshair, pygame.Color("blue"), (5,5), 5, 0)
crosshair.set_colorkey(pygame.Color("magenta"), pygame.RLEACCEL)
crosshair = crosshair.convert()


writer = pygame.font.Font(pygame.font.get_default_font(), 15)
buttons = {}
for b in range(joy.get_numbuttons()):
    buttons[b] = [
        writer.render(
            hex(b)[2:].upper(),
            1,
            pygame.Color("red"),
            pygame.Color("black")
        ).convert(),
        
        
        ((15*b)+45, 560)
    ]

while True:
    
    pygame.event.pump()
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    
    screen.fill(pygame.Color("black"))

    
    x = joy.get_axis(0)
    y = joy.get_axis(1)

    
    
    screen.blit(crosshair, ((x*250)+300-5, (y*250)+300-5))
    pygame.draw.rect(screen, pygame.Color("red"), frameRect, 1)

    
    for b in range(joy.get_numbuttons()):
        if joy.get_button(b):
            screen.blit(buttons[b][0], buttons[b][1])

    
    pygame.display.flip()
    clk.tick(40)