Further modifications by enumerating all joysticks first and then querying them in a separate loop yielded even better results with no need to sleep at all. (modified code below). So our guess is now that the microcontroller has a few ms to get ready to deliver the signals over usb. We will check this next week more in depth, but for now the solution is satisfying for our needs, even without being 100% sure of the cause. Again, thanks for all the help and input from you guys.
JOYINFOEX structtmp;
structtmp.dwSize = sizeof(JOYINFOEX);
structtmp.dwFlags = JOY_RETURNALL;
std::vector<int> connectedJoysticks;
// first enumerate all joysticks
for (int i = JOYSTICKID1; i < (JOYSTICKID1 +16); i++)
{
if (joyGetPosEx(i, &structtmp) == JOYERR_NOERROR) {
connectedJoysticks.push_back(i);
}
}
// then read the buttons only for the connected ones
for(unsigned int i = 0; i < connectedJoysticks.size(); i++)
{
JOYINFOEX actualPos;
actualPos.dwSize = sizeof(JOYINFOEX);
actualPos.dwFlags = JOY_RETURNALL;
int type = 0;
while(type == 0)
{
//get button states
if (joyGetPosEx(connectedJoysticks, &actualPos) != JOYERR_NOERROR) {
std::cout << "Error reading joystick " << i << std::endl;
continue;;
}
DWORD jb = actualPos.dwButtons;
int button[32];
//get button bit of the 32 buttons
for (unsigned int i = 0; i < 32; i++) {
button = (DWORD)powf(2.0f, i);
if (jb & button) button=1; else button=0;
}
type = button[31] + button[30] * 2 + button[29] * 4 + button[28] * 8 + button[27] * 16 + button[26] * 32 + button[25] * 64 + button[24] * 128;
}
std::cout << "Got Joystick: " << i << " of type " << type << std::endl;
}