Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectInput Joystick Button problem

Started by jgilbs Sep 21, 2005 at 9:47 AM 0 replies 350+ views
Original Post
jgilbs
jgilbs
Hey guys, I need some help with DirectInput and I searched around and didn't find an answer. I am writing an app that interfaces with a magstripe reader via the gameport(since its easy to use and already has +5V, eliminating the need for a power supply) and I am using DirectInput to read the port and get the data. All the signals output by the reader happen to be active-low, which works out since thats how the joystick port works, "active-low" is translated to "Active high" for me. Whenever the reader sense a magstripe(ie creditcard) is in the reader, it outputs a "Card Present" signal. As the card is swiped, it then sends the data. Since data speed is a function of how fast the user swipes the card, obviously the transmission rate is variable. This is ompensated for by "strobe" output, which outputs a strobe signal when a valid bit of data is present on the data line. I have been using immediate input mode to read the port continously. I have a "time critical" priority thread that is in an infinite loop, constantly using the functions poll and getdevicestate to read the port. The last statement in the loop is "SwitchToThread" so that the loop does not consume an excessive amount of CPU time by allowing other processes to run as needed. I have another while loop inside the main one that loops while the "card present" pin is active. Instead of doing any logic and thereby wasting CPU time and possibly missing data, all that is done in that loop is read the port and append the bit to the dataset. the loop only has 4 lines, which are as follows: while(state.rgbButtons[3] == 128) //"Button 4"(pin 14) is my "card present" pin //128 is used since high-order bit is when button is active { pJoy->Poll(); pJoy->GetDeviceState(sizeof(DIJOYSTATE), &state); sData += (char)(state.rgbButtons[0]); sStrobe += (char)(state.rgbButtons[1]); } sData and sStrobe are CString variables that I used since I want to easily output them to a messagebox and file during debugging. After this loop exits, I use CString's replace function to replace "0" and "128" with "0" and "1" When the final product is done, these may be BYTE variables or something in order to conserve memory. What I expect to end up with are very long strings of "1"s and "0"s in both data and strobe. (Something like the following:) 0000001111111111111100000000000111111111111111000000000001111111111000000000000 1111111111000000000000011111111110000000000111111111100000000111111111000000000 01111111111110000000000000 This is "expanded form" since the loop might have run several times without the input changing. As a result, for each consecutive string of 1s, I want to find the "midpoint" of the stream of 1s in the strobe dataset, and read the corresponding value in the data dataset. I can use this method to reconstruct the bitstream, as it will allow me to see the value of the data while strobe is high. This method works in that I am getting data from the ports successfully. But for some reason, the "card present" loop always returns prematurely. I only get about 20 or so strobes. I should have somewhere on the order of 90 to 400. Also, the results dialog box pops up while the card is still halfway in the reader as i am swiping it. Does anyone have any insight into whats going on here? Could this be caused by noise on the CP pin? Or when a device is polled, does DirectX allows other threads to run, possibly disrupting mine?The port has exclusive background access so its not losing focus or anything. and i think the port is fast enough to read the data, because i am seeing sequences of 3-400 consecutive numbers, so its going through that many loops before the input ever changes. What can I do to make this work?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.