QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: SMcNeill on November 26, 2020, 07:42:06 am

Title: _DEVICES and joystick glitched?
Post by: SMcNeill on November 26, 2020, 07:42:06 am
A little test code:

Code: QB64: [Select]
  1.     PRINT TIME$; " event -->";
  2.     Dev = _DEVICES
  3.     IF _DEVICEINPUT = 3 THEN
  4.         change = 0
  5.         IF _BUTTON(1) <> 0 THEN PRINT "BUTTON PUSHED": END
  6.         FOR i = 1 TO _LASTAXIS
  7.             IF _AXIS(i) <> 0 THEN
  8.                 change = SGN(_AXIS(i))
  9.                 PRINT "AXIS "; i; " changed"
  10.                 EXIT FOR
  11.             END IF
  12.         NEXT
  13.     ELSE
  14.         PRINT
  15.     END IF
  16.     _LIMIT 1

Now, without move any of the joysticks or directional pads, just click the buttons on your joystick.

The results I'm getting here are making this type routine worthless for my needs.  When I press the button, I get a result saying "AXIS 2 changed", rather than "BUTTON pushed". 

Anyone else able to duplicate this result, or should I look into investing in a new joystick, hoping that the one I have is glitching out?   It's older than heck, so I'm hoping the problem is with it and not with _DEVICES itself.  (Though I do know that _DEVICES is broken badly when it comes to keyboard responses, I thought it at least worked with joysticks for us??)
Title: Re: _DEVICES and joystick glitched?
Post by: Cobalt on November 26, 2020, 04:17:00 pm
While working on the controls for Dragon Warrior Fellippe introduced me to a joystick that used button input for directions rather than axis.

Not sure if thats relevant to your situation but thought I might share.
Title: Re: _DEVICES and joystick glitched?
Post by: TempodiBasic on November 27, 2020, 03:52:23 am
Hi Steve
here my two cents
I try the code on notebook Toshiba Windows 10 64 bits  QB64 1.4 64 bits  and as joystick a Geneic USB Wired Gamepad Joystick you can see the image here   https://www.amazon.it/Joystick-Vibrazione-Controller-Maniglia-Computer/dp/B08KHLF28S/ref=asc_df_B08KHLF28S/?tag=googshopit-21&linkCode=df0&hvadid=459286697270&hvpos=&hvnetw=g&hvrand=489339906717881003&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1008758&hvtargid=pla-1059099773167&psc=1 (https://www.amazon.it/Joystick-Vibrazione-Controller-Maniglia-Computer/dp/B08KHLF28S/ref=asc_df_B08KHLF28S/?tag=googshopit-21&linkCode=df0&hvadid=459286697270&hvpos=&hvnetw=g&hvrand=489339906717881003&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1008758&hvtargid=pla-1059099773167&psc=1)

and these are the results
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

So as you can see the code recognizes button 1 but the other 3 button have been seen both as axis 1 both as button

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
with this code
Code: QB64: [Select]
  1.     PRINT TIME$; " event -->";
  2.     Dev = _DEVICES
  3.     IF _DEVICEINPUT = 3 THEN
  4.         change = 0
  5.         IF _BUTTON(1) <> 0 THEN PRINT "BUTTON PUSHED": END
  6.         IF _BUTTON(2) <> 0 THEN PRINT "Button 2"
  7.         IF _BUTTON(3) <> 0 THEN PRINT "button 3"
  8.         IF _BUTTON(4) <> 0 THEN PRINT "Button 4"
  9.         FOR i = 1 TO _LASTAXIS
  10.             IF _AXIS(i) <> 0 THEN
  11.                 change = SGN(_AXIS(i))
  12.                 PRINT "AXIS "; i; " changed"
  13.                 EXIT FOR
  14.             END IF
  15.         NEXT
  16.     ELSE
  17.         PRINT
  18.     END IF
  19.     _LIMIT 1
  20.  

What kind of joystick are you using?
If you find interesting I can test the code from into Ubuntu on VM.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 04:26:45 am
You’re getting the same results I was, Richard.  Pressing a button registers as an axis change, which it shouldn’t.  (At least it shouldn’t, in my opinion.). Your axis is the left joystick (or D-pad, depending on settings), and have nothing to do with a button press.

The only solution I found for the issue is to introduce a tolerance level to rule out false axis updates.  Each time it registers, the program only reports a change of 0.001, or so, from the axis being off center.  To get rid of the false positives, I no longer check for values <> 0, but instead:

 IF ABS(_AXIS(i)) > 0.1 THEN...

You can change the line and test it again, if you want, and I believe those false positives will go away.

The end result, with this change, makes the controller a little less sensitive, requiring a heavier push in any direction on the joystick to count as an event, but it eliminates the false positives with the buttons, for us. 
Title: Re: _DEVICES and joystick glitched?
Post by: TempodiBasic on November 27, 2020, 10:28:40 am
a bit further experience

with this code I got these results:
Code: QB64: [Select]
  1.     PRINT TIME$; " event -->";
  2.     Dev = _DEVICES
  3.     IF _DEVICEINPUT = 3 THEN
  4.         change = 0
  5.         IF _BUTTON(1) <> 0 THEN PRINT "BUTTON PUSHED": END
  6.         IF _BUTTON(2) <> 0 THEN PRINT "Button 2"
  7.         IF _BUTTON(3) <> 0 THEN PRINT "button 3"
  8.         IF _BUTTON(4) <> 0 THEN PRINT "Button 4"
  9.         FOR i = 1 TO _LASTAXIS
  10.             IF _AXIS(i) <> 0.2 THEN
  11.                 change = SGN(_AXIS(i))
  12.                 PRINT "AXIS "; i; " changed"
  13.                 EXIT FOR
  14.             END IF
  15.         NEXT
  16.     ELSE
  17.         PRINT
  18.     END IF
  19.     _LIMIT 1
  20.  

results ...  [ This attachment cannot be displayed inline in 'Print Page' view ]  
moving the L knob I got axis 1 detection, moving the D knob I got the buttons and axis detection.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 10:37:10 am
So moving the axis also counts as a button push?  That makes _DEVICES almost worthless to add into a program.  I’ll do you some testing later, and if I can duplicate the results, I’ll end up stripping joystick support out of my projects.  We really need a complete input overhaul for QB64.  :(
Title: Re: _DEVICES and joystick glitched?
Post by: SpriggsySpriggs on November 27, 2020, 10:53:12 am
So moving the axis also counts as a button push?  That makes _DEVICES almost worthless to add into a program.  I’ll do you some testing later, and if I can duplicate the results, I’ll end up stripping joystick support out of my projects.  We really need a complete input overhaul for QB64.  :(
@SMcNeill
If the controller you are using uses XInput then you can use the library I made for XInput controllers in my API collection.
Title: Re: _DEVICES and joystick glitched?
Post by: FellippeHeitor on November 27, 2020, 11:24:37 am
The project I wrote to which I added joystick support (i) works as expected (ii) required no complete overhaul. Maybe it's the way you're writing your routines?

https://fellippeheitor.itch.io/spaceship
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 11:50:39 am
The project I wrote to which I added joystick support (i) works as expected (ii) required no complete overhaul. Maybe it's the way you're writing your routines?

https://fellippeheitor.itch.io/spaceship
DO
    Dev = _DEVICES
    IF _DEVICEINPUT = 3 THEN
        IF _BUTTON(1) <> 0 THEN PRINT "BUTTON PUSHED"
        FOR i = 1 TO _LASTAXIS
            IF _AXIS(i) <> 0 THEN PRINT "AXIS "; i; " changed"
        NEXT
    END IF
    WHILE _DEVICEINPUT: WEND
    _LIMIT 1
LOOP

What’s wrong with the above?  We check _DEVICEINPUT for any updates.  If the result is from the joystick (3), then we check to see if a button was pressed or if an axis changed.  Pressing the button shouldn’t change the axis, and the axis shouldn’t change button status.  The WHILE - WEND then clears the buffer, so we only deal with one event and no more.

If you have a suggestion for a different way to read things, without the false results, I’d love to hear it.  ;)
Title: Re: _DEVICES and joystick glitched?
Post by: FellippeHeitor on November 27, 2020, 11:54:29 am
Check my code too while I don't have a chance to fix yours. It might help. The download in the link I posted contains the source.
Title: Re: _DEVICES and joystick glitched?
Post by: FellippeHeitor on November 27, 2020, 01:38:44 pm
Try this:

Code: QB64: [Select]
  1.  
  2. CONST True = -1, False = 0
  3.  
  4. DIM startTime#, x AS LONG, found AS LONG, i AS LONG
  5.  
  6. 'Detection routine:
  7. PRINT "Detecting your controller. Press any button on it..";
  8. startTime# = TIMER
  9.     x = _DEVICEINPUT
  10.     IF x > 2 THEN
  11.         'Keyboard is 1, Mouse is 2. Anything after that could be a controller.
  12.         found = x
  13.         EXIT DO
  14.     END IF
  15.     PRINT ".";
  16.     _LIMIT 15
  17. LOOP UNTIL TIMER - startTime# > 10
  18.  
  19. IF found = 0 THEN PRINT "No joystick detected.": END
  20.  
  21.     CLS
  22.     COLOR
  23.     PRINT "Controller in use:"
  24.     COLOR 14
  25.     PRINT SPACE$(4); _DEVICE$(found)
  26.     x = CSRLIN
  27.     COLOR 0, 7
  28.     LOCATE x, 1: PRINT SPACE$(80);
  29.     LOCATE x, 1
  30.     PRINT "Buttons:             Axis:"
  31.     COLOR 7, 0
  32.  
  33.     WHILE _DEVICEINPUT(found): WEND
  34.     FOR i = 1 TO _LASTBUTTON(found)
  35.         LOCATE x + i, 1
  36.         PRINT USING "##: ##"; i; _BUTTON(i)
  37.     NEXT
  38.  
  39.     FOR i = 1 TO _LASTAXIS(found)
  40.         LOCATE x + i, 20
  41.         PRINT USING "##: +#.##"; i; _AXIS(i)
  42.         LOCATE x + i, 30: PRINT STRING$(41, 176);
  43.         LOCATE x + i, 50 + INT(map(_AXIS(i), -1, 1, -20, 20)): PRINT CHR$(219);
  44.     NEXT
  45.  
  46.     _DISPLAY
  47.     _LIMIT 60
  48.  
  49. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  50.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  51.  

Hopefully it yields the same results I get:


All buttons detected and all axis properly responsive, just as expected.
Title: Re: _DEVICES and joystick glitched?
Post by: Cobalt on November 27, 2020, 02:34:05 pm
Dragon Warrior had no issues with joypad input. Least not my joypad or Fellippe's. I wonder if its something with what your using Steve? You might try Dragon Warrior with your controller and see if it acts up in the same way.

I did have some weird results at one time, but it was just before my old controller went belly up.

When I run your code I get exactly what I would expect, save for my joypad doesn't use button 1 so I had to change that. Uses 3 as the lowest button value for some reason.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 05:15:04 pm
I get the same false results with your program Fellippe.

 [ This attachment cannot be displayed inline in 'Print Page' view ]  

I start with all values equal to 0, and then when I press a button, I generate a value on the _AXIS(2) and (4) of -0.0078 -- and I continue to generate this negative value from that point onwards.

Until a button is pressed, all my _AXIS read 0, as they should.  The moment I press a button, I have a permanent input stuck on my controller.

I'd be tempted to think it was just my controller, except Tempodi is generating the exact same style results, with the same style gamepad as mine.  Click a button, and we read really low level _AXIS inputs from that point on. 
Title: Re: _DEVICES and joystick glitched?
Post by: FellippeHeitor on November 27, 2020, 05:17:12 pm
Tempodi got good results with my code (we were just discussing it at Discord). Your controller sure is requiring some calibration. But do not underestimate the fact that you are doing some weird ordering of commands to get input from joysticks, as opposed to treating it more like we treat _MOUSEINPUT, which is what I've offered.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 05:30:19 pm
Tempodi got good results with my code (we were just discussing it at Discord). Your controller sure is requiring some calibration. But do not underestimate the fact that you are doing some weird ordering of commands to get input from joysticks, as opposed to treating it more like we treat _MOUSEINPUT, which is what I've offered.

It's not really that odd of an order.  What I'm doing is just basically a clearing of the buffer after reading it once, a lot like how we'd do a _KEYCLEAR to erase unwanted keystrokes.

Even if I change to a format which matches yours, I still generate the false results:

Code: QB64: [Select]
  1. Dev = _DEVICES
  2.     FOR i = 1 TO _LASTBUTTON(3)
  3.         IF _BUTTON(i) <> 0 THEN PRINT "Button"; i
  4.     NEXT
  5.     FOR i = 1 TO _LASTAXIS(3)
  6.         IF _AXIS(i) <> 0 THEN PRINT "AXIS "; i
  7.     NEXT
  8.     _LIMIT 1
  9.  

This completely follows your code:
Code: [Select]
    WHILE _DEVICEINPUT(found): WEND
    FOR i = 1 TO _LASTBUTTON(found)
        LOCATE x + i, 1: PRINT USING "##: ##"; i; _BUTTON(i)
    NEXT
 
    FOR i = 1 TO _LASTAXIS(found)
        LOCATE x + i, 20: PRINT USING "##: +#.##"; i; _AXIS(i)
        LOCATE x + i, 30: PRINT STRING$(41, 176);
        LOCATE x + i, 50 + INT(map(_AXIS(i), -1, 1, -20, 20)): PRINT CHR$(219);
    NEXT

And still, when I hit a button, I get an AXIS update rather than a button press.  Try the above little code of mine and see if it glitches on you, or if it works properly on your machine.  As it is, both programs are glitchy on my end.
Title: Re: _DEVICES and joystick glitched?
Post by: FellippeHeitor on November 27, 2020, 05:35:25 pm
No glitch on my end with the updated snipped you provided. I've only changed the _LIMIT line so it updates more often.
Title: Re: _DEVICES and joystick glitched?
Post by: SpriggsySpriggs on November 27, 2020, 05:36:40 pm
I don't have any glitches either. If any axes are off by a bit it is because my controller has a messed up deadzone. Same results with XInput WinAPI. It's probably just the controller.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 27, 2020, 05:38:09 pm
So it's either a Windows thing, a specific model joystick/driver thing, or just a Steve's old hardware thing...

I've ordered me a new joystick which should be here on the 2nd from Amazon.  Here's hoping that once it's hooked up, the glitch will disappear for good for me.  :)
Title: Re: _DEVICES and joystick glitched?
Post by: SpriggsySpriggs on November 27, 2020, 05:39:17 pm
Waiting for your update with great anticipation. I love the idea of using gamepads with QB64
Title: Re: _DEVICES and joystick glitched?
Post by: TempodiBasic on November 29, 2020, 05:24:48 am
Hi guys and gals
I love so much passion versus QB64 and its features...
Also this argument is very interesting for all those like to use Joystick in their applications.
I can apport my experience with the codes posted into this thread.

Using the beginning code posted by Steve  I find some issue.
In a second moment using the code posted by Fellippe I got it works fine!.
Running the second code of Steve that matches the features of that posted by Fellippe I got some strange results.
Seeing closer to it I found that we can get some erroneous input from Joystick if we go too slow to catch the events.

however the same last code of Steve gives good detection of event... the issue that I found  was about the output of these events. I try to be clearer : _AXIS gives as output a single precision number that  is -1/1 if the knob of joystick reaches the extreme inferior / superior point  of its range of movement, while when it stands on the middle he gives back a value very long in single precision digit.

The joystick that I have used is a generic joystick USB  similar to that of PS3

Adapting the output of the last code of Steve I can say that  it gives back tha same results of that of Fellippe.
Code: QB64: [Select]
  1. Dev = _DEVICES
  2.     FOR i = 1 TO _LASTBUTTON(3)
  3.         but = _BUTTON(i)
  4.         LOCATE i + 2, 5
  5.         IF but <> 0 THEN PRINT "Button"; i; but ELSE PRINT "Button "; i; SPACE$(20)
  6.     NEXT
  7.     FOR i = 1 TO _LASTAXIS(3)
  8.         LOCATE i + 2, 30
  9.         ax = _AXIS(i)
  10.         IF ax <> 0 THEN PRINT "AXIS "; i; ax ELSE PRINT "AXIS "; i; SPACE$(20)
  11.     NEXT
  12.     _LIMIT 10
  13.     CLS
  14.  
Thanks to read

PS: about the issue that moving the right knob I got button pressed sorry that is a my mistake because when is activate the switch on the joystick it works in that way Right knob and right side button are the same, so the left cursor buttons and left knob are the same. It is a feature of this kind of joystick, No issue for QB64!
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 29, 2020, 06:10:28 am
I try to be clearer : _AXIS gives as output a single precision number that  is -1/1 if the knob of joystick reaches the extreme inferior / superior point  of its range of movement, while when it stands on the middle he gives back a value very long in single precision digit.

The joystick that I have used is a generic joystick USB  similar to that of PS3

This is the same type results I’m producing, with the same style joystick.  At startup, everything I have reads zero properly.  All I need do is press a simple button, and then I generate the very long single precision digit.  (Which is, oddly enough, exactly 1/127 — which is the tolerance level STICK and STRIG tolerates without glitching out.)

Test the code here and see if that glitch doesn’t go away: https://www.qb64.org/forum/index.php?topic=3303.0

The level of off-centerness is less than 1 in 126 points of change allowed with the command. (1-126) is left/down, (127) is center, (128-254) is right/up.

STICK only reads 1/126th levels of precision.  The false reading is 1/127th of a click. It’s juuuuuuust enough of a difference that STICK ignores it and calls it 0, preventing the false positive.  (On my machine, at least.)  Test it out, and let me know if/how it works for you.  ;)
Title: Re: _DEVICES and joystick glitched?
Post by: TempodiBasic on November 29, 2020, 11:05:56 am
Hi Steve,
so I'm understanding that what you are calling glitch is a too much sensitivity of the _DEVICE that report a fixed result ( a single precision digit too long that is expressed in output as  digit elevated to power) near to 0 but not 0.
Wow  this appears to be like the  Limit towards 0 of a function!
So the user (coder) must manage manually this too precision of _DEVICE  to avoid false reading of events...
it needs just to tare the 0 state to that value that is always the same for the same state of my generic USB joystick (the fix long value is different for the two state of joystick:  Analogic ON / OFF   -1.5XXXXXXX ^-5 = -0,000015XXXXXX / -7,82XXXXXX^-3 = -0,00782XXXXXX ).

well my solution is taring the 0 position with tolerance as you suggest at the beginning of this thread
so adding this line of code into last test
Code: QB64: [Select]
  1.  IF ax < 0.01 AND ax > -0.01 THEN ax = 0  
we got a standard working of joystick
Code: QB64: [Select]
  1. Dev = _DEVICES
  2.     FOR i = 1 TO _LASTBUTTON(3)
  3.         but = _BUTTON(i)
  4.         LOCATE i + 2, 5
  5.         IF but <> 0 THEN PRINT "Button"; i; but ELSE PRINT "Button "; i; SPACE$(20)
  6.     NEXT
  7.     FOR i = 1 TO _LASTAXIS(3)
  8.         LOCATE i + 2, 30
  9.         ax = _AXIS(i)
  10.         IF ax < 0.01 AND ax > -0.01 THEN ax = 0
  11.         IF ax <> 0 THEN PRINT "AXIS "; i; ax ELSE PRINT "AXIS "; i; SPACE$(20)
  12.     NEXT
  13.     _LIMIT 10
  14.     CLS
  15.  
  16.  

another option is to use the scale of input for managing the input too precise
as Fellippe showed with his code....
as you can see he uses the function MAP to calculate the position of the cursor of the joystick along the axis.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 29, 2020, 11:43:16 am
That’s the solution I came up with as well to eliminate that false positive:

IF ABS(ax) > 0.01 THEN ‘Axis changed

For whatever reason, _DEVICES doesn’t reset all the way to 0 on my joystick, once I press a button.  The axis state stays around -0.0078.... (the same as your Analogic OFF)

With STICK, our scale of input is slightly larger than this minute amount off, so it reads 127 (dead center), as it should, rather than 126, which would be the next point off center.

My only concern with the tolerance limitation was when you posted the screenshot of moving the axis and getting false positives on the buttons.  Our buttons are only 0 or -1, so we can’t adjust a tolerance level for those!  Luckily, that’s a controller setting/option on your joystick, and not something we have to worry about.

I think a tolerance level on the axis, as above, can eliminate those false positives for us and have everything reading properly.  (Though I’d probably end up setting my stress test at around 0.1, instead of 0.001, just to rule out any problems with someone who might have a controller that glitches even a little more than ours do.)
Title: Re: _DEVICES and joystick glitched?
Post by: Cobalt on November 29, 2020, 12:05:58 pm
Analog Joysticks are just that Analog, so there is some "noise", this shows up even in my Windows Game Controllers properties panel. the little center cross hairs "vibrate" just a tiny bit when the stick is just siting on the desk, and tends to get a little "jiggle" if buttons are pressed. That of course would have nothing to do with QB64.
Thats where the programmer would have to deal with the noise from the device, I'm pretty sure if you could hack a console game for anything that uses one of those controllers, you would probably find some code in game to cancel that out or a special instruction set to cancel out controller noise before the game even sees it.

So, lets say, if you were to use a _BYTE to monitor joystick motion, you would want to ignore anything that returned, say, a value of +\- 3.
Title: Re: _DEVICES and joystick glitched?
Post by: STxAxTIC on November 29, 2020, 12:11:14 pm
I'm glad this is becoming resolved so publicly. Cobalt, you are on FIRE right now!
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 29, 2020, 12:24:13 pm
Analog Joysticks are just that Analog, so there is some "noise", this shows up even in my Windows Game Controllers properties panel. the little center cross hairs "vibrate" just a tiny bit when the stick is just siting on the desk, and tends to get a little "jiggle" if buttons are pressed. That of course would have nothing to do with QB64.
Thats where the programmer would have to deal with the noise from the device, I'm pretty sure if you could hack a console game for anything that uses one of those controllers, you would probably find some code in game to cancel that out or a special instruction set to cancel out controller noise before the game even sees it.

So, lets say, if you were to use a _BYTE to monitor joystick motion, you would want to ignore anything that returned, say, a value of +\- 3.

Aye.  That's what I mentioned up in Reply #3:  IF ABS(_AXIS(i)) > 0.1 THEN...

The only concern after that was when Tempodi was posting false positives with button readings when pressing an axis, but that was later determined to be a controller toggle setting.

_DEVICES needs to account for that minute variance, whereas STICK and STRIG, as I posted as alternatives, don't.  They were made for analog devices back in the day, and their level of tolerance isn't as great as _DEVICES is.  _DEVICE will read a joystick as being -0.0000005 points off center, whereas STICK will happily call that centered for you.  ;)
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on November 29, 2020, 10:38:04 pm
OK a signed byte is from -128 to + 127 decimal

If the axis has 256 increments per rotation, then the center = 128(unsigned)  -128(signed)

Say the axis detection hardware uses a signed byte to store position data.

Position data could be calculated as follows:

A signed byte value of -128 would be center position with a return value =
1/128 * (-128 +128) = 1/128 * 0

A signed byte value of -127 would be one increment past center position with a return value =   1/128 * (-128 +127) = 1/128 * -1 = -0.0078125

****
For the positive byte values:

A signed byte value of -128 would be center position with a return value =
1/128 * -(-128 +128) = 1/128 * 0 = 0

A signed byte value of 127 would be one increment past center position with a return value = 1/128 * -(-128 +127) = 1/128 * 1 = 0.0078125

A signed byte value of 1 would be 127 increment past center position with a return value = 1/128 * -(-128 +1) = 1/128 * 127 = .9921875
 
A signed byte value of  0 would be 128 increment past center position with a return value = 1/128 * -(-128 +0) = 1/128 * 128 = 1


****
OK was looking at the AXIS function and it returns -1, 1 with 0 at center.  In the code above you can see the calculation for 1 , 0 but  in order to get -1 there must be a weird value of -0 which does not really exist as there are only 256 states in a byte and to store the interpretation of -0 would require 257 states.

In the usual counting system 0 is not viewed as being the center but it seems that the AXIS outputs suggest the existence of -0.

For instance  the signed byte values usually count like -3, -2, -1,  0

If zero is at the center then
-0 would mean a count from -1 to 0 
 0 would mean a count from  1 to 0
 
OK lets try -0
A signed byte value of  -0 would be 128 increment past center position with a return value = 1/128 * (-128 +0) = 1/128 * -128 = -1

. Sorry for all the edits.
Title: Re: _DEVICES and joystick glitched?
Post by: johnno56 on November 30, 2020, 07:08:40 pm
A slightly related question:

With all this talk about joysticks and getting them to work properly, can I assume that somebody is planning on writing a flight-sim? Please say yes....I have a Logitech Extreme 3D Pro stick that has been sitting on my desk for years... The only time I pick it up is to remove dust... My trigger finger is rusting solid... lol

J
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on November 30, 2020, 08:59:18 pm
SPOK
what do you think about -0?

I'm trying to send joystick data to a computer. The data is a byte value where each value corresponds to a particular position.

The computer derives the position from the byte values as:

Position values:
      center = 0 
   max left = 1   
max right = -1   
 
A full 128 increments on either side of joystick center would require the byte plus another indicating bit of data that gives a total of  257 states. 

I wanted to send a single byte of data.
Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on November 30, 2020, 09:50:11 pm
SPOK
what do you think about -0?

I'm trying to send joystick data to a computer. The data is a byte value where each value corresponds to a particular position.

The computer derives the position from the byte values as:


      center = 0 
   max left = 1   
max right = -1   
 
A full 128 increments on either side of joystick center would require the byte plus another indicating bit of data that gives a total of  257 states. 

I wanted to send a single byte of data.

Which is why you use 127 increments on either side...

(-127 TO -1)  0    (1 TO 127)

Signed bytes go from -128 to + 127, so just use 127 step scale for amount off-center.
Title: Re: _DEVICES and joystick glitched?
Post by: SpriggsySpriggs on November 30, 2020, 09:51:41 pm
I'd use this stuff more often if I knew what the heck was going on
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on November 30, 2020, 10:15:02 pm
 SMcNeill

127 increments OK!!!!!!!

So with a signed byte the joystick center is still at -128

 127 increments on either side of center will arrive at byte values of 1 or -1, never arriving at zero. So:

byte value of 1 can be interpreted as
1/127 * -(-128 +1) = 1/127 * 127 = 1

byte value of -1 can be interpreted as
1/127 * (-128 +1) = 1/127 * -127 = -1

byte value of -128 can be interpreted as
1/127 * (-128 +128) = 1/127 * 0 = 0

all sent with a single byte!
Title: Re: _DEVICES and joystick glitched?
Post by: SpriggsySpriggs on November 30, 2020, 10:24:14 pm
So much simpler with XInput for determining position (but isn't cross platform with WinAPI, so....). So glad I have a modern controller:

Quote
bLeftTrigger

The current value of the left trigger analog control. The value is between 0 and 255.

bRightTrigger

The current value of the right trigger analog control. The value is between 0 and 255.

sThumbLX

Left thumbstick x-axis value. Each of the thumbstick axis members is a signed value between -32768 and 32767 describing the position of the thumbstick. A value of 0 is centered. Negative values signify down or to the left. Positive values signify up or to the right. The constants XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE or XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE can be used as a positive and negative value to filter a thumbstick input.

sThumbLY

Left thumbstick y-axis value. The value is between -32768 and 32767.

sThumbRX

Right thumbstick x-axis value. The value is between -32768 and 32767.

sThumbRY

Right thumbstick y-axis value. The value is between -32768 and 32767.
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on November 30, 2020, 11:10:09 pm
SpriggsySpriggs

Lots of resolution on the thumb stick !

So thumb stick center is actually zero for the signed (2 byte) value.

That is another way to do it.

Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on December 01, 2020, 12:22:15 am
SMcNeill

Quote
This is the same type results I’m producing, with the same style joystick.  At startup, everything I have reads zero properly.  All I need do is press a simple button, and then I generate the very long single precision digit.  (Which is, oddly enough, exactly 1/127 — which is the tolerance level STICK and STRIG tolerates without glitching out.)

The 1/127 makes sense if there are 127 increments  either side of joystick center

 byte value of -128 can be interpreted as
1/127 * (-128 +128) = 1/127 * 0 = 0  (center)

One increment either side of joystick center:

byte value of 127 can be interpreted as
1/127 * -(-128 +127) = 1/127 * 1 = 1/127 = 0.0078740157480315

byte value of -127 can be interpreted as
1/127 * (-128 +127) = 1/127 * -1 = -1/127  = -0.0078740157480315

1/127 is the value of a single increment.
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on December 01, 2020, 01:34:11 am
SMcNeill
From :  https://www.qb64.org/forum/index.php?topic=3303.0

Quote
Works with my USB gamepad, without any issues, which confuses the heck out of me.  The glitch my controller reports is always around a 0.0078... range of variance off center.  STICK offers a range from 1 to 254, with 127 being center, giving us 127 levels of precision in any direction.  1/127 is 0.0078... The variance _DEVICES reports is *exactly* the same amount as 1 level off on STICK — yet, it’s not going off center

 -128 is the signed byte value for joystick center

Quote
STICK offer a range of 1 to 254


I don't know why that is the range, it should be 1 to  255 because 1 is interpreted as 1 and 255 is interpreted as -1

There would be 127 increments on either side of joystick center * 2 = 254 increments with each increment using a state.

One state is zero (illegal)

The other state is used by the center value of -128 for a total of 256 states

Could STICK be thinking that 127 was center?

your controller  says 127 which is one increment past the actual center of -128.  So is STICK thinking that 127 is center and accepts the glitched value?

Whereas _DEVICES shows the error.

Title: Re: _DEVICES and joystick glitched?
Post by: SMcNeill on December 01, 2020, 05:19:49 am
https://www.qb64.org/wiki/STICK

STICK uses 127 as center, with values of 1 TO 254.

(1 TO 126)    127    (128 TO 254)

This gives 126 points of negative variance, and 127 points of positive variance off-center.  Now WHY these two scales aren’t the same, I have no clue, but that’s just the way they are.

_DEVICES returns SINGLE precision values from -1 TO +1, with 0 being centered, but it’s so sensitive it generates false positives if you just want to check for values <>0 as being off center.  You’ll need to code a tolerance level into your programs to avoid those false results.
Title: Re: _DEVICES and joystick glitched?
Post by: NOVARSEG on December 02, 2020, 12:35:38 am
Found some real STICK readouts on a you tube vid.  OK hope I'm allowed to post a vid here

at 7:36 into the video.  SORRY for posting a vid link but thought this was important.
****
For the right stick:
full up = 0
center  = 127
full down  = 255

full right = 255
center = 128
full left = 0
****
For the left stick:
full up = 0
center  = 127
full down  = 255

full right = 255
center = 128
full left = 0
****

The up - down axis has a center of 127 and the left - right axis has a center of 128

The left - right axis has 128 increments from center to full left and 127 increments from center to full right.   

The up - down axis has 128 increments from center to full down  and 127 increments from center to full up.   

Still not sure why both 127 and 128 are used as center values. Maybe there is no longer a true center because of the unequal increments. If 127 or 128 is used as center the increments are still 127 on one side of center and 128 on the other side.

If 127 is used as center : 
0 to 127 = 127 increments ,   127 to 255 = 128 increments

If 128 is used as center : 
0 to 128 = 128 increments ,   128 to 255 = 127 increments