This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,6 @@
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.

View file

@ -0,0 +1,2 @@
---
note: Joystick

View file

@ -0,0 +1,23 @@
VDU 23,22,512;512;8,16,16,0
VDU 5
GCOL 4,15
REPEAT
B% = ADVAL(0)
X% = ADVAL(1) / 64
Y% = 1023 - ADVAL(2) / 64
PROCjoy(B%,X%,Y%)
WAIT 4
PROCjoy(B%,X%,Y%)
UNTIL FALSE
END
DEF PROCjoy(B%,X%,Y%)
LOCAL I%
LINE X%-32,Y%,X%+32,Y% : LINE X%,Y%-32,X%,Y%+32
VDU 30
FOR I% = 0 TO 15
IF B% AND 1<<I% THEN PRINT "Button "; I%+1 " pressed";
VDU 10,13
NEXT
ENDPROC

View file

@ -0,0 +1,2 @@
Start,Control Panel, Game Controllers, List:installed controllers,Click:Joystick,
Button:Properties,Button:Test

View file

@ -0,0 +1,72 @@
'disable text window
nomainwin
'set window size
WindowWidth = 308
WindowHeight = 331
'center window on screen
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)
'open graphics window
open "Joystick Position" for graphics_nf_nsb as #m
'trap window closing
#m "trapclose [quit]"
'put pen down
#m "down"
'get center of graphics window
#m "home"
#m "posxy CenterX CenterY"
'draw sprite for crosshair
#m "backcolor black; color black"
#m "place 0 20;boxfilled 20 40"
#m "line 0 10 20 10"
#m "line 10 0 10 20"
#m "place 10 10; circle 10"
#m "backcolor white; color red"
#m "line 0 30 20 30"
#m "line 10 20 10 40"
#m "place 10 30; circle 10"
#m "flush"
'get sprite image
#m "getbmp plus 0 0 20 40"
#m "cls"
'create sprite from image
#m "addsprite crosshair plus"
#m "centersprite crosshair"
#m "spritexy crosshair "; CenterX; " "; CenterY
#m "drawsprites"
'check joystick every 100 milliseconds
timer 100, [CheckJoystick]
wait
[CheckJoystick]
readjoystick 1
'calculate crosshair position
PosX = int(CenterX*Joy1x/32767)
PosY = int(CenterY*Joy1y/32767)
'update crosshair position
#m "spritexy crosshair "; PosX; " "; PosY
#m "drawsprites"
'display button information
if Joy1button1 > 0 then #m "place 0 0;\\Button 1 pressed"
if Joy1button2 > 0 then #m "place 0 0;\\\Button 2 pressed"
wait
[quit]
timer 0
close #m
unloadbmp "plus"
end

View file

@ -0,0 +1,13 @@
10 MODE 1:BORDER 14:x=320:y=200:d=1
20 a=JOY(0) ' read state of first joystick
30 IF d THEN q$="*" ELSE q$=" "
40 IF a THEN MOVE x-8,y+8:TAG:PRINT q$;:TAGOFF
50 IF (a AND 1) AND y<380 THEN y=y+10
60 IF (a AND 2) AND y>20 THEN y=y-10
70 IF (a AND 4) AND x>20 THEN x=x-10
80 IF (a AND 8) AND x<620 THEN x=x+10
90 IF a AND 16 THEN LOCATE 1,1:PRINT "Fire1 pressed":d=1
100 IF a AND 32 THEN LOCATE 1,2:PRINT "Fire2 pressed":d=0
110 IF a<16 THEN LOCATE 1,1:PRINT " ":PRINT " "
120 MOVE x-8,y+8:TAG:PRINT "X";:TAGOFF
130 GOTO 20

View file

@ -0,0 +1 @@
Slider2D[Dynamic[ControllerState[{"X", "Y"}]], ImageSize -> {500, 500}]

View file

@ -0,0 +1,40 @@
(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) )
# 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 )
# Exit upon mouse click
(mouseFunc '((Btn State X Y) (bye)))
(glutMainLoop)

View file

@ -0,0 +1,73 @@
import sys
import pygame
pygame.init()
# Create a clock (for framerating)
clk = pygame.time.Clock()
# Grab joystick 0
if pygame.joystick.get_count() == 0:
raise IOError("No joystick detected")
joy = pygame.joystick.Joystick(0)
joy.init()
# Create display
size = width, height = 600, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Joystick Tester")
# Frame XHair zone
frameRect = pygame.Rect((45, 45), (510, 510))
# Generate crosshair
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()
# Generate button surfaces
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(),
# Get co-ords: ((width*slot)+offset, offset). Offsets chosen
# to match frames.
((15*b)+45, 560)
]
while True:
# Pump and check the events queue
pygame.event.pump()
for events in pygame.event.get():
if events.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Black the screen
screen.fill(pygame.Color("black"))
# Get joystick axes
x = joy.get_axis(0)
y = joy.get_axis(1)
# Blit to the needed coords:
# x*amplitude+(centre offset (window size/2))-(xhair offset (xh size/2))
screen.blit(crosshair, ((x*250)+300-5, (y*250)+300-5))
pygame.draw.rect(screen, pygame.Color("red"), frameRect, 1)
# Get and display the joystick buttons
for b in range(joy.get_numbuttons()):
if joy.get_button(b):
screen.blit(buttons[b][0], buttons[b][1])
# Write the display
pygame.display.flip()
clk.tick(40) # Limit to <=40 FPS

View file

@ -0,0 +1,43 @@
package require Tk 8.6
package require mkLibsdl
# This code assumes we're dealing with the first pair of axes on the first
# joystick; modern joysticks are complex...
# Callback for all joystick activity
proc display {joyDict} {
global x y buttons message
set axis -1
dict with joyDict {
if {$joystick != 0} return
if {[info exist button]} {
# Handle button presses...
set buttons($button) $value
set message "Buttons:"
foreach b [lsort -integer [array names buttons]] {
if {$buttons($b)} {
lappend message $b
}
}
} else {
# Handle joystick movement...
if {$axis == -1} return
set value [expr {$value / 32768.0 * 100 + 120}]
if {$axis == 0} {
set x $value
} elseif {$axis == 1} {
set y $value
}
.c coords xhairV $x [expr {$y-5}] $x [expr {$y+5}]
.c coords xhairH [expr {$x-5}] $y [expr {$x+5}] $y
}
}
}
# Make a GUI
set message "Buttons:"
pack [canvas .c -width 240 -height 240] [label .l -textvariable message]
set x [set y 120]
.c create line {120 115 120 125} -tags xhairV
.c create line {115 120 125 120} -tags xhairH
joystick event eval {display [joystick event peek]}