Reading wrong values from Joystick for the first few milliseconds

Started by
12 comments, last by jbadams 11 years, 6 months ago
Thank you all for the help and the hints. We've ruled bad soldering-connections almost out, by creating a few more devices of the same type and the all behave the exact same. On the average we're now down to waiting 20ms per joystick (even with multiple connected joysticks) until we get useful signals, by modifiying the code in a way that it only waits until we get something other than 0.

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;
}

Advertisement
you use std::cout at the end, thus you output memory to a very place, some chip, driver away, maybe you do not know definition for the buttons constants only. See joystick buttons constants somehow

you use std::cout at the end, thus you output memory to a very place, some chip, driver away,

Huh? cout is a handle to the process' standard output (stdout). It's meant to print out stuff on the screen, and that's exactly what it's doing here...

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


you use std::cout at the end, thus you output memory to a very place, some chip, driver away, maybe you do not know definition for the buttons constants only. See joystick buttons constants somehow

What do you think you just said? Could you try rephrasing that?

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement