Main Menu

Analogue Joysticks

Started by monkeybot, February 15, 2014, 12:14:21 PM

Previous topic - Next topic

monkeybot

the call function exists but it just crashes when trying to query it
I don't really get the whole DLL interface thing...


PlayBASIC Code: [Select]
; PROJECT : joy
; AUTHOR : PC
; CREATED : 15/02/2014
; EDITED : 15/02/2014

Constant JOY_BUTTON1 = 1
Constant JOY_BUTTON2 = 2
Constant JOY_BUTTON3 = 4
Constant JOY_BUTTON4 = 8
Constant JOYERR_BASE = 160
Constant JOYERR_NOERROR = (0)
Constant JOYERR_NOCANDO = (JOYERR_BASE + 6)
Constant JOYERR_PARMS = (JOYERR_BASE + 5)
Constant JOYERR_UNPLUGGED = (JOYERR_BASE + 7)
Constant MAXPNAMELEN = 32
Constant JOYSTICKID1 = 0
Constant JOYSTICKID2 = 1

Type JOYINFO
X; As Long
Y; As Long
Z ;As Long
Buttons; As Long
EndType
;Private Type JOYCAPS
; wMid As Integer
; wPid As Integer
; szPname As String * MAXPNAMELEN
; wXmin As Long
; wXmax As Long
; wYmin As Long
; wYmax As Long
; wZmin As Long
; wZmax As Long
; wNumButtons As Long
; wPeriodMin As Long
; wPeriodMax As Long
;End Type

;Private Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As Long
;Private Declare Function joyGetNumDevs Lib "winmm.dll" () As Long
;Private Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Long, pji As JOYINFO) As Long
;Private Function GetJoystick(ByVal joy As Integer, JI As JOYINFO) As Boolean
; If joyGetPos(joy, JI) <> JOYERR_NOERROR Then
; GetJoystick = False
; Else
; GetJoystick = True
; End If
;End Function
;' If IsConnected is False then it returns the number of
;' joysticks the driver supports. (But may not be connected)
;'
;' If IsConnected is True the it returns the number of
;' joysticks present and connected.
;'
;'; IsConnected is true by default.
;Private Function IsJoyPresent(Optional IsConnected As Variant) As Long
; Dim ic As Boolean
; Dim i As Long
; Dim j As Long
;; Dim ret As Long
; Dim JI As JOYINFO

; ic = IIf(IsMissing(IsConnected), True, CBool(IsConnected))

; i = joyGetNumDevs

; If ic Then
; j = 0
; Do While i > 0
; i = i - 1 'Joysticks id's are 0 and 1
; If joyGetPos(i, JI) = JOYERR_NOERROR Then
; j = j + 1
; End If
; Loop

; IsJoyPresent = j
; Else
; IsJoyPresent = i
; End If

;End Function
;' Fills the ji structure with the minimum x, y, and z
;' coordinates. Buttons is filled with the number of
;' buttons.
;Private Function GetJoyMin(ByVal joy As Integer, JI As JOYINFO) As Boolean
; Dim jc As JOYCAPS

; If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
; GetJoyMin = False

; Else
; JI.X = jc.wXmin
; JI.Y = jc.wYmin
; JI.Z = jc.wZmin
; JI.Buttons = jc.wNumButtons
;
; GetJoyMin = True
; End If
;End Function
;' Fills the ji structure with the maximum x, y, and z
;'; coordinates. Buttons is filled with the number of
;' buttons.
;Private Function GetJoyMax(ByVal joy As Integer, JI As JOYINFO) As Boolean
; Dim jc As JOYCAPS
; If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
; GetJoyMax = False
; Else
; JI.X = jc.wXmax
; JI.Y = jc.wYmax
; JI.Z = jc.wZmax
; JI.Buttons = jc.wNumButtons
; GetJoyMax = True
; End If
;End Function
;Private Sub Form_Paint()
;'KPD-Team 1999
; 'URL: http://www.allapi.net/
; 'E-Mail: KPDTeam@Allapi.net
; Dim JInfo As JOYINFO
;; 'Clear the form
; Me.Cls
; 'Print the information to the form
; Me.Print "Number of joysticks the driver supports:" + Str$(IsJoyPresent(False))
; Me.Print "Number of connected joysticks:" + Str$(IsJoyPresent(True))
; GetJoystick JOYSTICKID1, JInfo
; Me.Print "Number of buttons:" + Str$(JInfo.Buttons)
; GetJoyMax JOYSTICKID1, JInfo
;; Me.Print "Max X:" + Str$(JInfo.X)
; Me.Print "Max Y:" + Str$(JInfo.Y)
; Me.Print "Max Z:" + Str$(JInfo.Z)
; ;GetJoyMin JOYSTICKID1, JInfo
; Me.Print "Min X:" + Str$(JInfo.X)
; Me.Print "Min Y:" + Str$(JInfo.Y)
; Me.Print "Min Z:" + Str$(JInfo.Z)
;End Sub


;Read more: http://www.answers.com/topic/joystick-win-api-example#ixzz2tPkBraRS











Login required to view complete source code



kevin


  If you pass a function something it's not expecting then, it will absolute crash.

  MSDN has info on the function and what the parameters are
  http://msdn.microsoft.com/en-us/library/windows/desktop/dd743591%28v=vs.85%29.aspx

monkeybot

#2
right,this is driving me mad!!
i now understand that the call returns a status  code
I have tried returning the  joyinfo to a type via pointer and i have also tried to return the joyinfo to a memory bank via pointer!!!

Can you show me how to do this as i am obviously making a basic error and i just can't work out what it is!!!!!

print *intptr(j2)   ;i thought this should return the contents of the bank @ that pointer location
   







PlayBASIC Code: [Select]
; PROJECT : joy
; AUTHOR : PC
; CREATED : 15/02/2014
; EDITED : 16/02/2014

Constant JOY_BUTTON1 = 1
Constant JOY_BUTTON2 = 2
Constant JOY_BUTTON3 = 4
Constant JOY_BUTTON4 = 8
Constant JOYERR_BASE = 160
Constant JOYERR_NOERROR = (0)
Constant JOYERR_NOCANDO = (JOYERR_BASE + 6)
Constant JOYERR_PARMS = (JOYERR_BASE + 5)
Constant JOYERR_UNPLUGGED = (JOYERR_BASE + 7)
Constant MAXPNAMELEN = 32
Constant JOYSTICKID1 = 0
Constant JOYSTICKID2 = 1
PositionScreen 1000,0

Type JOYINFO
X; As Long
Y; As Long
Z ;As Long
Buttons; As Long
EndType
;Private Type JOYCAPS
; wMid As Integer
; wPid As Integer
; szPname As String * MAXPNAMELEN
; wXmin As Long
; wXmax As Long
; wYmin As Long
; wYmax As Long
; wZmin As Long
; wZmax As Long
; wNumButtons As Long
; wPeriodMin As Long
; wPeriodMax As Long
;End Type

;Private Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As Long
;Private Declare Function joyGetNumDevs Lib "winmm.dll" () As Long
;Private Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Long, pji As JOYINFO) As Long
;Private Function GetJoystick(ByVal joy As Integer, JI As JOYINFO) As Boolean
; If joyGetPos(joy, JI) <> JOYERR_NOERROR Then
; GetJoystick = False
; Else
; GetJoystick = True
; End If
;End Function
;' If IsConnected is False then it returns the number of
;' joysticks the driver supports. (But may not be connected)
;'
;' If IsConnected is True the it returns the number of
;' joysticks present and connected.
;'
;'; IsConnected is true by default.
;Private Function IsJoyPresent(Optional IsConnected As Variant) As Long
; Dim ic As Boolean
; Dim i As Long
; Dim j As Long
;; Dim ret As Long
; Dim JI As JOYINFO

; ic = IIf(IsMissing(IsConnected), True, CBool(IsConnected))

; i = joyGetNumDevs

; If ic Then
; j = 0
; Do While i > 0
; i = i - 1 'Joysticks id's are 0 and 1
; If joyGetPos(i, JI) = JOYERR_NOERROR Then
; j = j + 1
; End If
; Loop

; IsJoyPresent = j
; Else
; IsJoyPresent = i
; End If

;End Function
;' Fills the ji structure with the minimum x, y, and z
;' coordinates. Buttons is filled with the number of
;' buttons.
;Private Function GetJoyMin(ByVal joy As Integer, JI As JOYINFO) As Boolean
; Dim jc As JOYCAPS

; If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
; GetJoyMin = False

; Else
; JI.X = jc.wXmin
; JI.Y = jc.wYmin
; JI.Z = jc.wZmin
; JI.Buttons = jc.wNumButtons
;
; GetJoyMin = True
; End If
;End Function
;' Fills the ji structure with the maximum x, y, and z
;'; coordinates. Buttons is filled with the number of
;' buttons.
;Private Function GetJoyMax(ByVal joy As Integer, JI As JOYINFO) As Boolean
; Dim jc As JOYCAPS
; If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
; GetJoyMax = False
; Else
; JI.X = jc.wXmax
; JI.Y = jc.wYmax
; JI.Z = jc.wZmax
; JI.Buttons = jc.wNumButtons
; GetJoyMax = True
; End If
;End Function
;Private Sub Form_Paint()
;'KPD-Team 1999
; 'URL: http://www.allapi.net/
; 'E-Mail: KPDTeam@Allapi.net
; Dim JInfo As JOYINFO
;; 'Clear the form
; Me.Cls
; 'Print the information to the form
; Me.Print "Number of joysticks the driver supports:" + Str$(IsJoyPresent(False))
; Me.Print "Number of connected joysticks:" + Str$(IsJoyPresent(True))
; GetJoystick JOYSTICKID1, JInfo
; Me.Print "Number of buttons:" + Str$(JInfo.Buttons)
; GetJoyMax JOYSTICKID1, JInfo
;; Me.Print "Max X:" + Str$(JInfo.X)
; Me.Print "Max Y:" + Str$(JInfo.Y)
; Me.Print "Max Z:" + Str$(JInfo.Z)
; ;GetJoyMin JOYSTICKID1, JInfo
; Me.Print "Min X:" + Str$(JInfo.X)
; Me.Print "Min Y:" + Str$(JInfo.Y)
; Me.Print "Min Z:" + Str$(JInfo.Z)
;End Sub


;Read more: http://www.answers.com/topic/joystick-win-api-example#ixzz2tPkBraRS










Login required to view complete source code

kevin


   The joyGetPos function seems to work here, although the joyGetDevCaps functions seems somewhat fussy.   

PlayBASIC Code: [Select]
Constant JOY_BUTTON1 = $1
Constant JOY_BUTTON2 = $2
Constant JOY_BUTTON3 = $4
Constant JOY_BUTTON4 = $8
Constant JOYERR_BASE = 160
Constant JOYERR_NOERROR = 0
Constant JOYERR_NOCANDO = (JOYERR_BASE + 6)
Constant JOYERR_PARMS = (JOYERR_BASE + 5)
Constant JOYERR_UNPLUGGED = (JOYERR_BASE + 7)
Constant MAXPNAMELEN = 32
Constant JOYSTICKID1 = 0
Constant JOYSTICKID2 = 1


Type tJOYINFO
X ;As Long
Y ;As Long
Z ;As Long
Buttons; As Long
EndType




linkDll "winmm.dll"

; Private Declare Function joyGetNumDevs Lib "winmm.dll" () As Long
joyGetNumDevs() alias "joyGetNumDevs" as integer

; Private Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As Long
joyGetDevCaps(JoyID, JoyCaps_Pointer, JoyCapStructureSize) Alias "joyGetDevCapsA" As integer

;Private Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Long, pji As JOYINFO) As Long
joyGetPos(uJoyID, JoyInfo_Pointer) alias "joyGetPos" as integer

EndLInkDlL




setfps 60
do
cls

print "JoyStick Driver Supports #"+str$(JoyGetNumDevs())+" joysticks"

Dim JoyInfo as tJOYINFO pointer


if JoyGetNumDevs()
For id=0 to JoyGetNumDevs()-1

JoyInfo= New tJoyInfo

StatusCode=JoyGetPos(id ,int(JOyInfo))
if StateCode=JOYERR_NOERROR
print StateCode
print digits$(JoyInfo.x,8)+" "+ digits$(JoyInfo.y,8)+" "+ digits$(JoyInfo.z,8)+" "+bin$(JoyInfo.Buttons)
endif

free JoyInfo

next
endif

sync

loop



monkeybot

thanks for that i will scour and digest