joy.pbi
Code: Select all
;*** Joystick-Routines
;*** GPI 06.02.2003
;some notes:
;The Axis - procedures have a range from -100 to 100
;because the my joystick doesn't center right, it is possible to set
;a death zone. Try in the example a high death-zone (90%)
;Also import: My joystick can't turn complete to left. I become
;only 99. so don't use fixed number and to small steps. Use x>90 x>70 x>50 for example.
;A very powerful function is the joy_change - procedures.
;With this can be easy test, if a button/axis/pov of all joysticks has changed.
;the Init_Joy() procedure returns the maximum number of joysticks.
;When it return 2, the joyid can be 0 or 1 (the joyid start with 0, the maximum number starts with 1)
;The first button is the button 0.
;I found no structure in the poc-Returns, so use the constants
;#pov_upleft, #pov_up, #pov_upright, #pov_left, #pov_center, #pov_right, #pov_downleft, #pov_down and #pov_downright
;to get the position of the POV
;see the example to understand the routines.
;when the program set the constant #joy_debugmessage=1, the joysticks-routines will
;return the errormessages to the debug-window (for example:joyid out of range)
Structure joy_caps ; structure joycaps is too short!
wMid.w
wPid.w
szPname.b[#MAXPNAMELEN]
wXmin.l
wXmax.l
wYmin.l
wYmax.l
wZmin.l
wZmax.l
wNumButtons.l
wPeriodMin.l
wPeriodMax.l
wRmin.l
wRmax.l
wUmin.l
wUmax.l
wVmin.l
wVmax.l
wCaps.l
wMaxAxes.l
wNumAxes.l
wMaxButtons.l
szRegKey.b[#MAXPNAMELEN]
szOEMVxD.b[32]; den genauen wert kenn ich nicht
EndStructure
Structure joy_infoex ; sizeof(JOYINFOEX) does not work!
size.l
flags.l
xpos.l
ypos.l
zpos.l
rpos.l
upos.l
vpos.l
buttons.l
buttonNumber.l
pov.l
reserved1.l
reserved2.l
EndStructure
Structure joy_info
x_center.l
x_minus.l
x_min.l
x_max.l
x_death.l
x_death_pro.l
y_center.l
y_minus.l
y_min.l
y_max.l
y_death.l
y_death_pro.l
z_center.l
z_minus.l
z_min.l
z_max.l
z_death.l
z_death_pro.l
r_center.l
r_minus.l
r_min.l
r_max.l
r_death.l
r_death_pro.l
u_center.l
u_minus.l
u_min.l
u_max.l
u_death.l
u_death_pro.l
v_center.l
v_minus.l
v_min.l
v_max.l
v_death.l
v_death_pro.l
pov.l
EndStructure
Structure joy_old_status
joyid.l
axisx.l
axisy.l
axisz.l
axisr.l
axisu.l
axisv.l
axisz.l
button.l
pov.l
EndStructure
Structure joy_changed_info
joyid.l
axis.l
button.l
pov.l
EndStructure
#JOYCAPS_HASZ=1
#JOYCAPS_HASR=2
#JOYCAPS_HASU=4
#JOYCAPS_HASV=8
#JOYCAPS_HASPOV=16
#JOYCAPS_POV4DIR=32
#JOYCAPS_POVCTS=64
#pov_upleft=$7b0c
#pov_up=$0000
#pov_upright=$1194
#pov_left=$6978
#pov_center=$ffff
#pov_right=$2328
#pov_downleft=$57e4
#pov_down=$4650
#pov_downright=$34bc
Dim joy_information.joy_info(0)
Dim joy_status.joy_infoex(0)
Dim joy_oldstatus.joy_old_status(0)
max_nr_of_joy=-1
Global joy_information
Global joy_status
Global max_nr_of_joy
Global joy_changed_status.joy_changed_info
Procedure Init_Joy()
i=-1
joy_info.joy_caps
Repeat
i+1
ret=joyGetDevCaps_(i,@joy_info,SizeOf(joy_caps))
Until ret0
max_nr_of_joy=i-1
If max_nr_of_joy>=0
Dim joy_information.joy_info(max_nr_of_joy)
Dim joy_status.joy_infoex(max_nr_of_joy)
Dim joy_oldstatus.joy_old_status(max_nr_of_joy)
For a=0 To max_nr_of_joy
joyGetDevCaps_(a,@joy_info,SizeOf(joy_caps))
joy_information(a)\x_min=joy_info\wxmin
joy_information(a)\x_max=joy_info\wxmax
joy_information(a)\x_center=(joy_info\wxmin+joy_info\wxmax) /2
joy_information(a)\x_death_pro=0
joy_information(a)\x_death=0
joy_information(a)\x_minus=joy_info\wxmax-joy_info\wxmin
joy_information(a)\y_min=joy_info\wymin
joy_information(a)\y_max=joy_info\wymax
joy_information(a)\y_center=(joy_info\wymin+joy_info\wymax) /2
joy_information(a)\y_death_pro=0
joy_information(a)\y_death=0
joy_information(a)\y_minus=joy_info\wymax-joy_info\wymin
joy_information(a)\z_min=joy_info\wzmin
joy_information(a)\z_max=joy_info\wzmax
joy_information(a)\z_center=(joy_info\wzmin+joy_info\wzmax) /2
joy_information(a)\z_death_pro=0
joy_information(a)\z_death=0
joy_information(a)\z_minus=joy_info\wzmax-joy_info\wzmin
joy_information(a)\r_min=joy_info\wrmin
joy_information(a)\r_max=joy_info\wrmax
joy_information(a)\r_center=(joy_info\wrmin+joy_info\wrmax) /2
joy_information(a)\r_death_pro=0
joy_information(a)\r_death=0
joy_information(a)\r_minus=joy_info\wrmax-joy_info\wrmin
joy_information(a)\u_min=joy_info\wumin
joy_information(a)\u_max=joy_info\wumax
joy_information(a)\u_center=(joy_info\wumin+joy_info\wumax) /2
joy_information(a)\u_death_pro=0
joy_information(a)\u_death=0
joy_information(a)\u_minus=joy_info\wumax-joy_info\wumin
joy_information(a)\v_min=joy_info\wvmin
joy_information(a)\v_max=joy_info\wvmax
joy_information(a)\v_center=(joy_info\wvmin+joy_info\wvmax) /2
joy_information(a)\v_death_pro=0
joy_information(a)\v_death=0
joy_information(a)\v_minus=joy_info\wvmax-joy_info\wvmin
joy_information(a)\pov=joy_info\wcaps
joy_status(a)\size=SizeOf(joy_InfoEX)
joy_status(a)\flags=#JOY_RETURNALL
Next
EndIf
ProcedureReturn i
EndProcedure
Procedure Exit_Joy()
Dim joy_information.joy_info(0)
Dim joy_status.joy_infoex(0)
max_nr_of_joy=-1
EndProcedure
Procedure Examine_Joy(joyid)
If joyid-1 And joyid-1 And buttonnumber-1 And joyid-1 And joyid-1 And joyid-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
x=joy_status(joyid)\xpos - joy_information(joyid)\x_center
If x-joy_information(joyid)\x_death>0
ProcedureReturn Round((x-joy_information(joyid)\x_death)*200/joy_information(joyid)\x_minus,0)
ElseIf x+joy_information(joyid)\x_death-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
Y=joy_status(joyid)\Ypos - joy_information(joyid)\Y_center
If Y-joy_information(joyid)\Y_death>0
ProcedureReturn Round((Y-joy_information(joyid)\Y_death)*200/joy_information(joyid)\Y_minus,0)
ElseIf Y+joy_information(joyid)\Y_death-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
Z=joy_status(joyid)\Zpos - joy_information(joyid)\Z_center
If Z-joy_information(joyid)\Z_death>0
ProcedureReturn Round((Z-joy_information(joyid)\Z_death)*200/joy_information(joyid)\Z_minus,0)
ElseIf Z+joy_information(joyid)\Z_death-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
R=joy_status(joyid)\Rpos - joy_information(joyid)\R_center
If R-joy_information(joyid)\R_death>0
ProcedureReturn Round((R-joy_information(joyid)\R_death)*200/joy_information(joyid)\R_minus,0)
ElseIf R+joy_information(joyid)\R_death-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
U=joy_status(joyid)\Upos - joy_information(joyid)\U_center
If U-joy_information(joyid)\U_death>0
ProcedureReturn Round((U-joy_information(joyid)\U_death)*200/joy_information(joyid)\U_minus,0)
ElseIf U+joy_information(joyid)\U_death-1 And joyid-1 And joyid=0 And death-1 And joyid-1 And joyid0
V=joy_status(joyid)\Vpos - joy_information(joyid)\V_center
If V-joy_information(joyid)\V_death>0
ProcedureReturn Round((V-joy_information(joyid)\V_death)*200/joy_information(joyid)\V_minus,0)
ElseIf V+joy_information(joyid)\V_deathjoy_axisX(i)
ok=i
If joy_oldstatus(i)\axisx>joy_axisX(i)
joy_changed_status\axis=-1
Else
joy_changed_status\axis=1
EndIf
EndIf
If joy_oldstatus(i)\axisyjoy_axisY(i)
ok=i
If joy_oldstatus(i)\axisy>joy_axisy(i)
joy_changed_status\axis=-2
Else
joy_changed_status\axis=2
EndIf
EndIf
If joy_oldstatus(i)\axiszjoy_axisZ(i)
ok=i
If joy_oldstatus(i)\axisz>joy_axisz(i)
joy_changed_status\axis=-3
Else
joy_changed_status\axis=3
EndIf
EndIf
If joy_oldstatus(i)\axisrjoy_axisR(i)
ok=i
If joy_oldstatus(i)\axisr>joy_axisr(i)
joy_changed_status\axis=-4
Else
joy_changed_status\axis=4
EndIf
EndIf
If joy_oldstatus(i)\axisujoy_axisU(i)
ok=i
If joy_oldstatus(i)\axisu>joy_axisu(i)
joy_changed_status\axis=-5
Else
joy_changed_status\axis=5
EndIf
EndIf
If joy_oldstatus(i)\axisvjoy_axisV
ok=i
If joy_oldstatus(i)\axisv>joy_axisv(i)
joy_changed_status\axis=-6
Else
joy_changed_status\axis=6
EndIf
EndIf
If joy_oldstatus(i)\buttonjoy_buttonflags(i)
ok=i
joy_changed_status\button=joy_oldstatus(i)\button!joy_buttonflags(i)
EndIf
If joy_oldstatus(i)\povjoy_pov(i)
ok=i
joy_changed_status\pov=joy_pov(i)
EndIf
Next
joy_changed_status\joyid=ok
ProcedureReturn ok
EndProcedure
Procedure Joy_Changed_Joystick()
ProcedureReturn joy_changed_status\joyid
EndProcedure
Procedure Joy_Changed_Axis()
ProcedureReturn joy_changed_status\axis
EndProcedure
Procedure Joy_Changed_Button()
ok=-1
For i=0 To 31
If joy_changed_status\button & (10
a$+Chr(a)
EndIf
Until a=0 Or an=0
ProcedureReturn a$
EndProcedure
;anzahl der Joystick bestimmen
;-main
If OpenWindow(0, 0,0, 500,500, #PB_Window_ScreenCentered | #PB_Window_SystemMenu ,"Joysticktest by GPI")
If CreateGadgetList(WindowID())
;init joy procedures
joy_an=init_joy()
If joy_an=-1
MessageRequester("JoystickTest","Sorry, no Joysticks found.",0)
End
EndIf
;output tree
TreeGadget(0, 10, 10, 480,300, #PB_Tree_AlwaysShowSelection | #PB_Tree_NoButtons)
;combobox for joystick-choice
ComboBoxGadget(1, 10,320, 480, 100)
;axis info
TextGadget ( 2, 10,345, 70,20,"X:",#PB_Text_Border )
TextGadget (12, 10,370, 70,20, "DeathX",#PB_Text_Border)
StringGadget(22, 50,370, 30,20, "20",#PB_String_Numeric)
TextGadget ( 3, 92,345, 70,20,"Y:",#PB_Text_Border )
TextGadget (13, 92,370, 70,20, "DeathY",#PB_Text_Border)
StringGadget(23,132,370, 30,20, "20",#PB_String_Numeric)
TextGadget ( 4,174,345, 70,20,"Z:",#PB_Text_Border )
TextGadget (14,174,370, 70,20, "DeathZ",#PB_Text_Border)
StringGadget(24,214,370, 30,20, "20",#PB_String_Numeric)
TextGadget ( 5,256,345, 70,20,"R:",#PB_text_border )
TextGadget (15,256,370, 70,20, "DeathR",#PB_Text_Border)
StringGadget(25,296,370, 30,20, "20",#PB_String_Numeric)
TextGadget ( 6,338,345, 70,20,"U:",#PB_Text_Border )
TextGadget (16,338,370, 70,20, "DeathU",#PB_Text_Border)
StringGadget(26,378,370, 30,20, "20",#PB_String_Numeric)
TextGadget ( 7,420,345, 70,20,"V:",#PB_Text_Border )
TextGadget (17,420,370, 70,20, "DeathV",#PB_Text_Border)
StringGadget(27,460,370, 30,20, "20",#PB_String_Numeric)
;pov
TextGadget(8, 10,395, 150,20,"POV:",#PB_Text_Border )
;button
TextGadget(9, 175,395, 315,20,"Buttons:",#PB_Text_Border )
For i=0 To 31
y=(I/16):x=i-16*y
ButtonGadget(40+i, 10+(460*x/15),420+y*25, 20, 20,Str(i),#PB_Button_Toggle)
DisableGadget(40+i, 1)
Next
;"change Test"-button
ButtonGadget(80, 10,470, 70,20,"Change Test")
;build the tree
;also the items for the joy-choice-combobox insert here!
add_node("Info")
add_mess("Number of joy:"+Str(joy_an))
end_node()
joy_info.joy_caps
For i=0 To joy_an-1
ret=joyGetDevCaps_([url]mailto:i,@joy_info.joy_caps,SizeOf[/url](joy_caps))
If ret
;add a item in a combobox!
AddGadgetItem(1, -1, "Joystick "+Str(i)+ "(not present)")
add_node("Joystick "+Str(i))
add_mess("not present ("+Str(ret)+")")
end_node()
Else
;add a item in a combobox!
AddGadgetItem(1, -1, "Joystick "+Str(i))
add_node("Joystick "+Str(i))
add_node("Driver Information")
add_mess("Joystick produkt name:"+create_str (@joy_info\szpname[0],#maxpnamelen))
add_mess("Manufacture and Product identifier:"+Str(joy_info\wmid)+" / "+Str(joy_info\wpid))
add_mess("registry key:"+create_str (@joy_info\szregkey[0],#maxpnamelen))
add_mess("driver oem:"+create_str (@joy_info\szoemvxd[0],#maxpnamelen))
end_node()
Add_node("Axis")
add_mess("Number of axes supported:"+Str(joy_info\wmaxaxes))
add_mess("Number of axes in use:"+Str(joy_info\wnumaxes))
add_mess("X-Axis:"+Str(joy_info\wxmin)+" to "+Str(joy_info\wxmax))
add_mess("Y-Axis:"+Str(joy_info\wymin)+" to "+Str(joy_info\wymax))
If joy_info\wcaps & #joycaps_hasz
add_mess("Z-Axis:"+Str(joy_info\wzmin)+" to "+Str(joy_info\wzmax))
EndIf
If joy_info\wcaps & #joycaps_hasr
add_mess("R-Axis:"+Str(joy_info\wrmin)+" to "+Str(joy_info\wrmax))
EndIf
If joy_info\wcaps & #joycaps_hasu
add_mess("U-Axis:"+Str(joy_info\wumin)+" to "+Str(joy_info\wumax))
EndIf
If joy_info\wcaps & #joycaps_hasv
add_mess("V-Axis:"+Str(joy_info\wvmin)+" to "+Str(joy_info\wvmax))
EndIf
End_node()
If joy_info\wcaps & #joycaps_haspov
add_node("POV (point-of-view)")
If joy_info\wcaps & #joycaps_pov4dir
add_mess("Support of discrete values (centered,forward,backward,left,right)")
EndIf
If joy_info\wcaps & #joycaps_povcts
add_mess("Support of continuous degree bearings")
EndIf
end_node()
EndIf
add_node("Buttons")
add_mess("Supported buttons:"+Str(joy_info\wmaxbuttons))
add_mess("Number of buttons:"+Str(joy_info\wNumButtons))
end_node()
add_node("Additional Information")
add_mess("Polling Frequency:"+Str(joy_info\wperiodmin)+" to "+Str(joy_info\wperiodmax))
end_node()
end_node()
EndIf
Next
SetGadgetState(1,0) ; Combobox set to joystick 0
For i=0 To CountGadgetItems(0)
SetGadgetItemState(0, i, #PB_Tree_Expanded) ; Expand the complete tree
Next
Repeat
Event = WindowEvent()
If event=#PB_Event_Gadget And EventGadgetID() =80 ; the changed-test presed
HideWindow(0,1) ; hide the old window
; Open a new window
If OpenWindow(1,0,0,200,50,#PB_Window_ScreenCentered,"JoystickTest - Changed")
If CreateGadgetList(WindowID())
; with a text and a button
TextGadget(81,10,0,180,20,"Press a button of a joystick",#PB_Text_Center)
ButtonGadget(82,10,25,180,20,"abort")
a$=""
joy_change_start(); start change
Repeat
joy=joy_changed(); -1= no change or the joyid will return
If joy>-1
a$="Change of joystick "+Str(joy)+Chr(13)
x=joy_changed_axis() ; 0=no axis has changed
If x=-1:a$+"Axis X:lower"+Chr(13):EndIf
If x= 1:a$+"Axis X:higher"+Chr(13):EndIf
If x=-2:a$+"Axis Y:lower"+Chr(13):EndIf
If x= 2:a$+"Axis Y:higher"+Chr(13):EndIf
If x=-3:a$+"Axis Z:lower"+Chr(13):EndIf
If x= 3:a$+"Axis Z:higher"+Chr(13):EndIf
If x=-4:a$+"Axis R:lower"+Chr(13):EndIf
If x= 4:a$+"Axis R:higher"+Chr(13):EndIf
If x=-5:a$+"Axis U:lower"+Chr(13):EndIf
If x= 5:a$+"Axis U:higher"+Chr(13):EndIf
If x=-6:a$+"Axis V:lower"+Chr(13):EndIf
If x= 6:a$+"Axis V:higher"+Chr(13):EndIf
x=joy_changed_button(); -1=no button has changed
If x>-1:a$+"Button "+Str(x):EndIf
x=joy_changed_pov(); -1=no button has changed
If x-1:a$+"POV "+Str(x):EndIf
EndIf
If WindowEvent()=#pb_event_gadget And EventGadgetID()=82:a$="abort":EndIf
Until a$""
Else
a$="Can't create Gadgetlist"
EndIf
;close window and show the result.
CloseWindow(1)
MessageRequester("Joytest",a$, 0)
EndIf
;show and activate the main window
HideWindow(0,0)
UseWindow(0)
ActivateWindow()
UseGadgetList(WindowID(0))
;change the joy-choice-combobox to the joystick, which has changed
SetGadgetState(1,joy)
EndIf
joy=GetGadgetState(1); which joy has choiced.
If joy >=0
ret=examine_joy(joy) ; the the joystick
If ret=0
;test the axis
If Joy_ExistAxisX(joy)
joy_setAxisXDeathzone(joy,Val(GetGadgetText(22)))
SetGadgetText(2,"X:"+Str(joy_axisX(joy)))
Else
SetGadgetText(2,"X:-")
EndIf
If joy_ExistAxisY(joy)
joy_setAxisyDeathzone(joy,Val(GetGadgetText(23)))
SetGadgetText(3,"Y:"+Str(joy_axisY(joy)))
Else
SetGadgetText(3,"Y:-")
EndIf
If joy_existaxisz(joy)
joy_setAxiszDeathzone(joy,Val(GetGadgetText(24)))
SetGadgetText(4,"Z:"+Str(joy_axisz(joy)))
Else
SetGadgetText(4,"Z:-")
EndIf
If joy_existaxisr(joy)
joy_setAxisrDeathzone(joy,Val(GetGadgetText(25)))
SetGadgetText(5,"R:"+Str(joy_axisr(joy)))
Else
SetGadgetText(5,"R:-")
EndIf
If joy_existaxisu(joy)
joy_setAxisuDeathzone(joy,Val(GetGadgetText(26)))
SetGadgetText(6,"U:"+Str(joy_axisu(joy)))
Else
SetGadgetText(6,"U:-")
EndIf
If joy_existaxisv(joy)
joy_setAxisvDeathzone(joy,Val(GetGadgetText(27)))
SetGadgetText(7,"V:"+Str(joy_axisv(joy)))
Else
SetGadgetText(7,"V:-")
EndIf
;test the pov
If joy_existpov(joy)
a$=Hex(joy_pov(joy)): While Len(a$)<4: a$="0"+a$: Wend
a$+" "
If joy_pov(joy)=#pov_upleft
a$+"upleft"
EndIf
If joy_pov(joy)=#pov_up
a$+"up"
EndIf
If joy_pov(joy)=#pov_upright
a$+"upright"
EndIf
If joy_pov(joy)=#pov_left
a$+"left"
EndIf
If joy_pov(joy)=#pov_center
a$+"center"
EndIf
If joy_pov(joy)=#pov_right
a$+"right"
EndIf
If joy_pov(joy)=#pov_downleft
a$+"downleft"
EndIf
If joy_pov(joy)=#pov_down
a$+"down"
EndIf
If joy_pov(joy)=#pov_downright
a$+"downright"
EndIf
SetGadgetText(8,"POV:$"+a$+":"+Str(joy_pov(joy)))
Else
SetGadgetText(8,"POV:-")
EndIf
;test the buttons
a$=Bin(joy_buttonflags(joy)): While Len(a$)<32: a$="0"+a$: Wend
SetGadgetText(9,"ButtonFlags (bin):"+a$+":"+Str(joy_buttonflags(joy)))
For i=0 To 31
If joy_button(joy,i)
;DisableGadget(40+i, 0)
SetGadgetState(40+i,0)
Else
;DisableGadget(40+i, 1)
SetGadgetState(40+i,1)
EndIf
Next
Else
;joystick not ready!
SetGadgetText(2,"ERROR!"):SetGadgetText(3,"ERROR!"):SetGadgetText(4,"ERROR!")
SetGadgetText(5,"ERROR!"):SetGadgetText(6,"ERROR!"):SetGadgetText(7,"ERROR!")
SetGadgetText(8,"ERROR!"):SetGadgetText(9,"ERROR!")
EndIf
Delay(10)
EndIf
Until Event = #PB_EventCloseWindow
EndIf
EndIf
End