JOYTEST.CPP 959 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <conio.h>
  2. #include "textio.h"
  3. #define kBufSize 1024
  4. #define kJoyPort 0x201
  5. #define kNumAxes 4
  6. #define kNumButtons 4
  7. char data[kBufSize];
  8. int axis[kNumAxes];
  9. int button[kNumButtons];
  10. void ReadJoystick( void )
  11. {
  12. int i, j, mask;
  13. // write a dummy value to start resistive timing
  14. i = 0;
  15. outp(kJoyPort, 0xFF);
  16. while ( i < kBufSize )
  17. data[i++] = (char)inp(kJoyPort);
  18. for (j = 0, mask = 0x10; j < kNumButtons; j++, mask <<= 1)
  19. button[j] = (data[0] & mask) == 0;
  20. for (i = 0, mask = 1; i < kNumAxes; i++, mask <<= 1)
  21. {
  22. for (j = 0; j < kBufSize && (data[j] & mask) != 0; j++);
  23. axis[i] = j;
  24. }
  25. }
  26. void main( void )
  27. {
  28. int i;
  29. tioInit(1);
  30. while ( !kbhit() )
  31. {
  32. ReadJoystick();
  33. tioSetPos(0, 0);
  34. for ( i = 0; i < kNumAxes; i++ )
  35. tioPrint("Axis[%i]=%4d", i, axis[i]);
  36. for ( i = 0; i < kNumButtons; i++ )
  37. tioPrint("Button[%i]=%d", i, button[i]);
  38. }
  39. getch();
  40. }