You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.7 KiB

  1. /**
  2. * Gamecube controller to Nintendo 64 adapter
  3. * by Andrew Brown
  4. * Rewritten for N64 to HID by Peter Den Hartog
  5. * Modified to be a library by Kai Lüke
  6. */
  7. /**
  8. * To use, hook up the following to the Arduino:
  9. * Digital I/O 2: N64 serial line
  10. * All appropriate grounding and power lines, i.e.
  11. * GND to left N64 controller PIN, Dig.PIN2 to middle Serial/Signal,
  12. * 3.3V to right N64 PIN
  13. * /------------\
  14. * / O O O \
  15. * | GND Signl 3.3V |
  16. * |________________|
  17. * Maybe: connect PIN X with external 1K pull-up resistor to the 3.3V rail
  18. * Default and fallback PIN is 2
  19. */
  20. #ifndef N64Controller_h
  21. #define N64Controller_h
  22. #include "N64Interface.h"
  23. #define A_IDX 0
  24. #define B_IDX 1
  25. #define Z_IDX 2
  26. #define START_IDX 3
  27. #define D_UP_IDX 4
  28. #define D_DOWN_IDX 5
  29. #define D_LEFT_IDX 6
  30. #define D_RIGHT_IDX 7
  31. #define L_IDX 10
  32. #define R_IDX 11
  33. #define C_UP_IDX 12
  34. #define C_DOWN_IDX 13
  35. #define C_LEFT_IDX 14
  36. #define C_RIGHT_IDX 15
  37. #define X_IDX 16
  38. #define Y_IDX 24
  39. class N64Controller {
  40. public:
  41. N64Controller(unsigned char serialPin); // first thing to call
  42. void begin(); // second thing to call
  43. void update(); // then update always and get button info
  44. // consider to have a delay instead of
  45. // calling update all the time in a loop
  46. inline bool D_up() { return (interface->raw_dump[D_UP_IDX]) > 0; };
  47. inline bool D_down() { return (interface->raw_dump[D_DOWN_IDX]) > 0; };
  48. inline bool D_left() { return (interface->raw_dump[D_LEFT_IDX]) > 0; };
  49. inline bool D_right() { return (interface->raw_dump[D_RIGHT_IDX]) > 0; };
  50. inline bool Start() { return (interface->raw_dump[START_IDX]) > 0; };
  51. inline bool A() { return (interface->raw_dump[A_IDX]) > 0; };
  52. inline bool B() { return (interface->raw_dump[B_IDX]) > 0; };
  53. inline bool Z() { return (interface->raw_dump[Z_IDX]) > 0; };
  54. inline bool L() { return (interface->raw_dump[L_IDX]) > 0; };
  55. inline bool R() { return (interface->raw_dump[R_IDX]) > 0; };
  56. inline bool C_up() { return (interface->raw_dump[C_UP_IDX]) > 0; };
  57. inline bool C_down() { return (interface->raw_dump[C_DOWN_IDX]) > 0; };
  58. inline bool C_left() { return (interface->raw_dump[C_LEFT_IDX]) > 0; };
  59. inline bool C_right() { return (interface->raw_dump[C_RIGHT_IDX]) > 0; };
  60. inline char axis_x() { return axis(X_IDX); };
  61. inline char axis_y() { return axis(Y_IDX); };
  62. void print_N64_status();
  63. private:
  64. N64Interface * interface;
  65. inline char axis(int index) {
  66. char value = 0;
  67. for (char i=0; i<8; i++) {
  68. value |= interface->raw_dump[index+i] ? (0x80 >> i) : 0;
  69. }
  70. return value;
  71. }
  72. };
  73. #endif