Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clearing _DEVICES
#1
Is there a way to force controller _DEVICES to be cleared and re-detected?

For instance, when _DEVICES is first used in a running program the detected number of controllers will be returned. If one or more of the detected controllers is then disconnected the _DEVICE$ for the disconnected controllers will add "[DISCONNECTED]" to the string returned but still occupy a place in _DEVICES. If a user were to start plugging in random controllers after program startup the _DEVICES value will just keep growing with each new unique controller connected. The program listed below will show this in action. I would like to clear the _DEVICES list when a controller is listed as "[DISCONNECTED]" so _DEVICES can recount the actual number of controllers still plugged in if this is possible while a program is running. Any thoughts?

Code: (Select All)
DIM Devices AS INTEGER
DIM Fcount AS INTEGER
DIM d AS INTEGER
DIM DeviceName AS STRING

Devices = _DEVICES
Fcount = 0

DO
    CLS
    _LIMIT 30
    Fcount = Fcount + 1
    IF Fcount = 30 THEN '                                        check for new devices once per second
        Fcount = 1
        IF _DEVICES <> Devices THEN Devices = _DEVICES '         if number of devices changes get new count
    END IF
    PRINT
    FOR d = 1 TO Devices '                                       print found devices
        COLOR 14, 1
        DeviceName = _DEVICE$(d)
        IF INSTR(DeviceName, "[DISCONNECTED]") THEN COLOR 7, 0 ' change color if disconnected
        PRINT " Found: "; _DEVICE$(d)
        COLOR 7, 0
    NEXT d
    _DISPLAY
LOOP UNTIL _KEYDOWN(27) '                                        press ESC to exit
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#2
I added this to the top of the loop: 

Code: (Select All)
While _DeviceInput(1): Wend

and your code seems to be working (full program below). 

step 1: no game controller plugged in yet
[Image: clearing-devices-01.png]

Step 2: after plugging in a game controller
[Image: clearing-devices-02.png]

Step 3: after unplugging game controller
[Image: clearing-devices-03.png]

Step 4: after plugging the controller back in
[Image: clearing-devices-04.png]

I only tried it on one computer so far (old MS Surface Pro 3 w/Windows 10) so if it's still not working for you, maybe try it on a different machine? 

Code: (Select All)
' Clearing _DEVICES
' https://qb64phoenix.com/forum/showthread.php?tid=1685

' TerryRitchie, 8-bit Enthusiast
' 05-14-2023, 01:12 AM
' Is there a way to force controller _DEVICES to be cleared and re-detected?
' For instance, when _DEVICES is first used in a running program the detected
' number of controllers will be returned. If one or more of the detected
' controllers is then disconnected the _DEVICE$ for the disconnected
' controllers will add "[DISCONNECTED]" to the string returned but still
' occupy a place in _DEVICES. If a user were to start plugging in random
' controllers after program startup the _DEVICES value will just keep
' growing with each new unique controller connected. The program listed
' below will show this in action. I would like to clear the _DEVICES list
' when a controller is listed as "[DISCONNECTED]" so _DEVICES can recount
' the actual number of controllers still plugged in if this is possible
' while a program is running. Any thoughts?

Dim Devices As Integer
Dim Fcount As Integer
Dim d As Integer
Dim DeviceName As String

Devices = _Devices
Fcount = 0

Do
    Cls
    _Limit 30
    While _DeviceInput(1): Wend ' <----- I ADDED THIS LINE TO REFRESH

    Fcount = Fcount + 1
    If Fcount = 30 Then '                                        check for new devices once per second
        Fcount = 1
        If _Devices <> Devices Then Devices = _Devices '         if number of devices changes get new count
    End If
    Print
    For d = 1 To Devices '                                       print found devices
        Color 14, 1
        DeviceName = _Device$(d)
        If InStr(DeviceName, "[DISCONNECTED]") Then Color 7, 0 ' change color if disconnected
        Print " Found: "; _Device$(d)
        Color 7, 0
    Next d
    _Display
Loop Until _KeyDown(27) '                                        press ESC to exit
Reply
#3
Correct, you're seeing the _DEVICE$ change from having [DISCONNECTED] when the device is unplugged to having that string removed when plugged back in. I was wondering if you could have the device disappear from the list completely when disconnected. The only way to to this now is to end the program and restart.

In effect what I would like to do is have _DEVICES clear the list each time it is called and redetect ONLY currently connected controllers while the program is running, refreshing the list.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#4
I hate to say this but you'd probably have to find the C code that runs the _DEVICES keyword and find whatever is used for freeing that in your OS. You may or may not be able to find the actual handle so you can free it but it might be worth checking if you really need this functionality.
Schuwatch!
Yes, it's me. Now shut up.
Reply
#5
(05-19-2023, 11:03 AM)Ultraman Wrote: I hate to say this but you'd probably have to find the C code that runs the _DEVICES keyword and find whatever is used for freeing that in your OS. You may or may not be able to find the actual handle so you can free it but it might be worth checking if you really need this functionality.

Yeah, I figured this may be the answer as well. I've tried every conceivable way of using the controller statements to see if I could somehow get a fresh list. I've worked around the issue in the latest version the controller library I posted. I'm not nearly comfortable enough with C to go snooping around in the code. This may be something the developers could consider in a future update if it's possible.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#6
Hi Terry
reading now the new posts about game controller I have found this  
Quote:// Unique device identifier for application session, starting at 0 for the first device attached and
// incrementing by 1 for each additional device. If a device is removed and subsequently reattached
// during the same application session, it will have a new deviceID.


in this path of our QB64pe folder....

Quote:qb64pe 3_7\qb64pe\internal\c\parts\input\game_controller\src\gamepad


it may bring the solution...
sorry I do not go on now for 2 reasons: my dusted C (I take much time to follow C code because at this time my knowledge is more theorical and less practical, because I often write something in QB64pe) and no time to use now for coding or studing the code already written.
See again on this fantastic forum friends.
Reply
#7
One thing I had to do on my PC was use the Xinput library in Windows. My controller did not work properly with _DEVICES so I had to interface with Xinput and call all the different functions there. The nice thing about it was that I was able to activate the vibration mode on the controller as well.
Schuwatch!
Yes, it's me. Now shut up.
Reply
#8
@Ultraman
Yeah I know of its existence but I do not know where is this wonderful library!
Reply




Users browsing this thread: 1 Guest(s)