Joystick position

From Rosetta Code
Revision as of 21:06, 27 June 2011 by rosettacode>Markhobley (Digital joysticks with no extent data, should produce full extent movement of the crosshair.)
Task
Joystick position
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to determine the joystick position and represent this on the display via a crosshair. For a centred joystick, the crosshair should appear in the centre of the screen. If the joystick is pushed left or right, then the cross hair should move left or right according to the extent that the joystick is pushed. If the joystick is pushed forward or pulled back, then the crosshair should move up or down according to the extent that that joystick is pushed or pulled. The edges of the display represent maximum extents for joystick movement. For example, a joystick pushed fully forward would raise the crosshair to the top centre of the screen. A joystick pulled backwards and to the right would move the crosshair to the bottom right of the screen (except for a small area reserved to show joystick status). Implementations can use a graphical display method to produce the crosshair, or alternatively represent the crosshair using a plus symbol on a terminal, and move the plus symbol position according to the joystick. The bottom part of the display can hide or show an alphanumeric sequence to represent the buttons pressed. For example, if pushbuttons 1,4 and 10 are depressed, we could display "1 4 A". The implemented code should continue to redraw the crosshair according to the joystick position and show the current pushbutton statuses until the task is terminated. Digital joysticks that produce no extent data, should have their position indicated as full extent movement of the crosshair.

For the purpose of this task, we assume that the joystick is calibrated and that the first joystick is being used. The task implementer could at their option provide a solution that includes a joystick selection facility, enabling the user to choose which joystick is to be used for this task.

PicoLisp

This is for the 64-bit version.

Note: The code is not yet tested with a real joystick (I don't have one), it was just simulated with dummy functions. Can somebody having a joystick please test it, and remove this message? <lang PicoLisp>(load "@lib/openGl.l")

(setq *JoyX 0.0 *JoyY 0.0)

(glutInit) (glutInitDisplayMode (| GLUT_RGBA GLUT_DOUBLE GLUT_ALPHA GLUT_DEPTH)) (glutInitWindowSize 400 400) (glutCreateWindow "Joystick")

(glClearColor 0.3 0.3 0.5 0)

(displayPrg

  (glClear GL_COLOR_BUFFER_BIT)
  (glBegin GL_LINES)
  (glVertex2f *JoyX (- *JoyY 0.1))  # Draw crosshair
  (glVertex2f *JoyX (+ *JoyY 0.1))
  (glVertex2f (- *JoyX 0.1) *JoyY)
  (glVertex2f (+ *JoyX 0.1) *JoyY)
  (glEnd)
  (glFlush)
  (glutSwapBuffers) )
  1. Track joystick position

(native `*GlutLib "glutJoystickFunc" NIL

  (lisp 'joystickFunc
     '((Btn X Y Z)
        (msg                          # Display buttons
           (make
              (for (B 1 (n0 Btn) (inc B))
                 (and (bit? 1 Btn) (link B))
                 (setq Btn (>> 1 Btn)) ) ) )
        (setq                         # Move crosshair
           *JoyX (*/ X 1.0 1000)
           *JoyY (*/ Y 1.0 1000) )
        (glutPostRedisplay) ) )
  100 )
  1. Exit upon mouse click

(mouseFunc '((Btn State X Y) (bye))) (glutMainLoop)</lang>