Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hid joystick interface #4226

Merged
merged 23 commits into from
Jul 25, 2020
Merged

Hid joystick interface #4226

merged 23 commits into from
Jul 25, 2020

Conversation

a-chol
Copy link
Contributor

@a-chol a-chol commented Oct 24, 2018

This implements a joystick feature, including a joystick_task function called from tmk, specific keycodes for joystick buttons and a usb hid interface for v-usb and lufa.
I tested the v-usb backend, as I have a compatible board, but only provide a compilable-but-untested implementation for lufa. Runtime tests of this backend is appreciated.
In order to test, you have to add JOYSTICK_ENABLE = yes to your rules.mk and

#define JOYSTICK_BUTTON_COUNT 8
#define JOYSTICK_AXES_COUNT 2

in your config.h

Any feedback is appreciated.


closes #3903

docs/feature_joystick.md Outdated Show resolved Hide resolved
quantum/joystick.h Outdated Show resolved Hide resolved
quantum/quantum_keycodes.h Show resolved Hide resolved
tmk_core/protocol/lufa/lufa.c Show resolved Hide resolved
@drashna
Copy link
Member

drashna commented Oct 24, 2018

It looks like this is added to VUSB and LUFA but not chibios, or arm_atsam. Is there a reason why, other than a lack of hardware?

@a-chol
Copy link
Contributor Author

a-chol commented Oct 25, 2018

I'm AFK for a few days so I'll apply your suggestions when I come back.
I only implemented VUSB and Lufa mostly for hardware reasons. Giving Lufa a shot was a way to bring in more feedback and there is no reason to spare the others.

@a-chol
Copy link
Contributor Author

a-chol commented Oct 25, 2018

By the way, if anyone else has the hardware and feels like giving it a try, please.do. I'm not sure of the process of collaborating on a pull request on github though, maybe submitting a PR to my fork?

@drashna
Copy link
Member

drashna commented Oct 25, 2018

Also, don't forget that if you're adding a feature page, you'll need to link it in /docs/_summary.md and /docs/_sidebar.md

@a-chol a-chol mentioned this pull request Nov 3, 2018
@a-chol
Copy link
Contributor Author

a-chol commented Nov 30, 2018

I just pushed an untested implementation of the hid joystick interface for chibios. It builds with the chibios_test keyboard, but lacks the analog read function. I couldn't find a simple function like the analogRead from the drivers/avr folder. It can still be usefull as is though.
So far, there is an implementation for AVR v-usb (tested), AVR LUFA (untested) and ARM chibios (untested).
If anyone can give this a try on any of those untested platforms, it would be of great help.

@drashna
Copy link
Member

drashna commented Dec 2, 2018

What about ATSAM? (massdrop boards)

@a-chol
Copy link
Contributor Author

a-chol commented Dec 2, 2018

I can give it a try but without the hardware to test, it's really hit-or-miss. I'm afraid that pushing incomplete implementation in this PR requires it to be validated before merging, which in the end postpones the merge for other protocols.
Unless we manage to have a tester for each protocol, I don't see that PR being merged anytime soon.

@drashna
Copy link
Member

drashna commented Dec 6, 2018

Also, it looks like you have a merge conflict here.

@drashna
Copy link
Member

drashna commented Jan 25, 2019

It looks like there are some merge conflicts that need to be resolved

@a-chol
Copy link
Contributor Author

a-chol commented Jan 30, 2019

The merge conflicts are resolved.
The feature is reaching completion. I've validated the adc reading code with my own keyboard, properly reading and mapping the inputs to the int8 value range.
Axis configuration now supports defining the input, output and ground pins to configure. When configured, the pin configuration is saved, then restored so that the qmk matrix scan, relying on input pins not changing, works properly.
Axis configuration also support defining calibration values using a low, resting and high raw adc reference values. Sometimes the resting position doesn't return the middle value of the extreme positions, but it is essential that the resting position return a value of 0 once mapped to the hid range, so a piecewise transfer function between raw adc values and the hid output range was required.
Beside these changes, I implemented the reading code in a function called from a weak linkage function, so that any keyboard can redefine the adc reading procedure and still call the original quantum code (for reading a qwic device for instance).

ADC reading is supported for AVR only for now. I think it would be more valuable for the project to merge it as is and rely on subsequent pull requests to implement the missing parts for other controllers.

edit: I've completed the documentation as well, but I could go a little deeper in the circuitry segment. As far as I understand, it should be possible to connect an analog device to any keyboard, but my experience and knowledge is limited to my own keyboard, so validation from an experienced electronician would be required.

@drashna
Copy link
Member

drashna commented Feb 5, 2019

This is ready to go, correct?

@a-chol
Copy link
Contributor Author

a-chol commented Feb 5, 2019

I just pushed some documentation fixes, I confirm it's ready to go!

@drashna
Copy link
Member

drashna commented Mar 27, 2019

There are some merge conflicts here. If you could resolve these?

@a-chol
Copy link
Contributor Author

a-chol commented Apr 4, 2019

Conflicts are resolved. It's pretty cool to be able to do so from github directly!

@fauxpark fauxpark mentioned this pull request Aug 6, 2020
13 tasks
noroadsleft pushed a commit that referenced this pull request Aug 11, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
noroadsleft pushed a commit that referenced this pull request Aug 27, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
noroadsleft pushed a commit that referenced this pull request Aug 29, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
nicocesar pushed a commit to nicocesar/qmk_firmware that referenced this pull request Sep 6, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
@4sStylZ
Copy link
Contributor

4sStylZ commented Sep 14, 2020

Hi there, I just want to say thank's to everyone involved in this PR !
That's a very cool feature. I will test it soon !

@seb-pau seb-pau mentioned this pull request Sep 25, 2020
14 tasks
drashna pushed a commit to zsa/qmk_firmware that referenced this pull request Sep 30, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
kjganz pushed a commit to kjganz/qmk_firmware that referenced this pull request Oct 28, 2020
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
amrsoll added a commit to amrsoll/qmk_firmware that referenced this pull request May 1, 2022
The analog joystick feature does not compile accross all platforms (see qmk#4226 (review))

All joystick related code is put between #if JOYSTICK_AXES_COUNT > 0

This also reduces the size of the compiled code when you only want to use joystick buttons.

\### How to reproduce bug:

1. `qmk new-keyboard`
2. select the MCU `GD32VF103` (other options don't matter)
3. `rules.mk` Add `JOYSTICK_ENABLE = yes`
4. `config.h` : Add
    ```c
    #define JOYSTICK_BUTTON_COUNT 1
    #define JOYSTICK_AXES_COUNT 0
    ```
5. (optional) Change a key to `JS_BUTTON0` in keymap
6. run `qmk compile` against new keyboard

Errors will show related to the ADC.

\### Disclaimer

1. I do not have the proper HW to debug on all keyboards, but it does work from the RP2040 branch which has a similar problem
   See qmk#14877 (comment)
2. I guess `ifeq ($(shell expr $(JOYSTICK_AXES_COUNT) \> 0), 1)` ain't so pretty,
   but it didn't work with `#ifneq ($(JOYSTICK_AXES_COUNT), 0) ??
BorisTestov pushed a commit to BorisTestov/qmk_firmware that referenced this pull request May 23, 2024
* add support for hid gamepad interface
add documentation for HID joystick
Add joystick_task to read analog axes values even when no key is pressed or release. update doc
Update docs/feature_joystick.md
Manage pin setup and read to maintain matrix scan after analog read

* Incorporates patches and changes to HID reporting

There are some patches provided by @a-chol incorporated on this commit,
and also some changes I made to the HID Report structure.

The most interesting is the one dealing with number of buttons: Linux
doesn't seem to care, but Windows requires the HID structure to be byte
aligned (that's in the spec). So if one declares 8/16/32... buttons they
should not have any issues, but this is what happens when you have 9
buttons:

```
 bits |0|1|2|3|4|5|6|7|
      |*|*|*|*|*|*|*|*| axis 0 (report size 8)
      |*|*|*|*|*|*|*|*| ...
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*|
      |*|*|*|*|*|*|*|*| axis 6
      |*|*|*|*|*|*|*|*| first 8 buttons (report size 1)
      |*| | | | | | | | last of 9 buttons, not aligned
```

So for that I added a conditonal that will add a number of reports with
size 1 to make sure it aligns to the next multiple of 8. Those reports
send dummy inputs that don't do anything aside from aligning the data.

Tested on Linux, Windows 10 and Street Fighter (where the joystick is
recognized as direct-input)

* Add save and restore of each pin used in reading joystick (AVR).
Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc
instead of an output pin from the MCU.

Fix joystick report id

Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes)

Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis.

Documentation fixes

* Fix port addressing for joystick analog read

* The other required set of changes
As per the PR, the changes still holding it up.
Add onekey for testing.
Fix ARM builds.
Fix device descriptor when either axes or buttons is zero.
Add compile-time check for at least one axis or button.
Move definition to try to fix conflict.
PR review comments.
qmk cformat

* avoid float functions to compute range mapping for axis adc reading

* Remove V-USB support for now. Updated docs accordingly.

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/usb_descriptor.c

Co-Authored-By: Ryan <[email protected]>

* Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios

* Fix HID joystick report sending for ChibiOS.
Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.

* Update tmk_core/protocol/chibios/usb_main.c

Co-Authored-By: Ryan <[email protected]>

* Update tmk_core/protocol/lufa/lufa.c

Co-Authored-By: Ryan <[email protected]>

* Add missing mcuconf.h and halconf.h to onekey:joystick keymap.
Add suggested fixes from PR.

* Switch saveState and restoreState signature to use pin_t type.
onekey:joystick : add a second axis, virtual and programmatically animated.

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Update docs/feature_joystick.md

Co-Authored-By: Ryan <[email protected]>

* Add PR corrections

* Remove halconf.h and mcuconf.h from onekey keymaps

* Change ADC_PIN to A0

Co-authored-by: achol <[email protected]>
Co-authored-by: José Júnior <[email protected]>
Co-authored-by: a-chol <[email protected]>
Co-authored-by: Nick Brassel <[email protected]>
Co-authored-by: Ryan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HID Gamepad support