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

Avoid unreferenced Gamepad instances to be created by the compiler #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SukkoPera
Copy link
Contributor

This seems to fix #266 upstream.

Since now only referenced Gamepad instances are actually created by the compiler, I took the liberty of adding Gamepad5 and 6.



extern SingleGamepad_ Gamepad5;
extern SingleGamepad_ Gamepad6;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correct, only 3 will ever work, as the 32u4 only has 6 usb endpoints, of which 3 are used by the serial?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but they turn up handy if the CDC serial gets disabled. Since with this modification their presence won't cause any overhead, I thought I'd throw them in.

@NicoHood
Copy link
Owner

Does it seem to fix, or does it really fix the issue?

@SukkoPera
Copy link
Contributor Author

SukkoPera commented Aug 29, 2021

It fixes the issue on a project of mine. I need to test on other projects before I can say it definitely does with confidence. Just thought I'd share the patch in the meantime, so that maybe other people could test on their own.

@SukkoPera
Copy link
Contributor Author

SukkoPera commented Aug 29, 2021

Just noticed an odd side-effect: Gamepad1 no longer corresponds to the first gamepad detected by the system, sigh. Other gamepads might be out of order as well. Controllers seem to be detected in the exact reverse order. Any idea why?

Apart from this problem, I am using 6 controllers together with no issues (of course the CDC Serial was disabled).

@NicoHood
Copy link
Owner

I think the order of Gamepad creation could be related to:

  • First use in the code
  • Compiler sorting, possibly by name

So you could try to change the name of the cpp files maybe. Another idea would be to add special attributes to the class to change its construction order. I am not sure if that is possible, but I remember that there could be options like that.

Is it even important to have them in the correct order?

@SukkoPera
Copy link
Contributor Author

SukkoPera commented Aug 29, 2021

I thought the same, but the first use in my code is in an array which is populated in order.

Files and instances are named in ascending order so I see no way that could lead to the reverse order.

I think users would expect Gamepad1 to be the first controller, I think we should preserve this behavior.

@NicoHood
Copy link
Owner

I dont know, but I bet you will find it out.

@SukkoPera
Copy link
Contributor Author

More testing confirms that this solves the initial issue, but still I can't understand why the order of controllers gets reversed.

@RevHarryPowell
Copy link

Sorry to revive this old conversation, but I’m having trouble implementing SukkoPera’s fix in my code, which currently has only 2 HID gamepads. The only way I can get rid of the third is by deleting the extra SingleGamepadX.cpp files and removing the lines in SingleGamepad.h. I’d like to be able to dynamically change the number of HID gamepads generated. Is there a way to accomplish this?

@fermino
Copy link

fermino commented Sep 23, 2023

Hi! First of all, @NicoHood I know that maintaining a project isn't easy, so thanks a lot for this library!

Is there any chance to have this PR merged? I am experiencing the same issues as in #266, and although creating instances in the lib files is common practice in the arduino environment, it get's kind of annoying when you try to dive a bit deeper.

For what I've been reading the order of compilation is not defined by the C standard, but if there's a GCC behavior we could try to hack around that to make it work for most of the people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce number of pre-defined Gamepad instances
4 participants