gc_controller.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "gc_controller.h"
  2. #include "gc_read.h"
  3. #include "gamepad.h"
  4. #include <string.h>
  5. uint16_t gamecube_buttons = 0;
  6. uint8_t gamecube_joysticks[6] = {0};
  7. bool z_button = false;
  8. report_gamepad_t report = {
  9. .Button = 0,
  10. .LX = STICK_CENTER,
  11. .LY = STICK_CENTER,
  12. .RX = STICK_CENTER,
  13. .RY = STICK_CENTER,
  14. .HAT = HAT_CENTER
  15. };
  16. void board_init(void) {
  17. setPinOutput(C13);
  18. writePinLow(C13);
  19. gamecube_init();
  20. }
  21. void matrix_init_user(void) {
  22. }
  23. void matrix_scan_user(void) {
  24. gamecube_scan(&gamecube_buttons, gamecube_joysticks);
  25. z_button = gamecube_buttons & GAMECUBE_Z;
  26. // Home & Capture
  27. if (gamecube_buttons & GAMECUBE_START) {
  28. if (z_button && !(report.Button & SWITCH_HOME))
  29. report.Button |= SWITCH_CAPTURE;
  30. else
  31. report.Button |= SWITCH_HOME;
  32. } else {
  33. report.Button &= ~SWITCH_HOME;
  34. report.Button &= ~SWITCH_CAPTURE;
  35. }
  36. // Y and L
  37. if (gamecube_buttons & GAMECUBE_Y) {
  38. if (z_button && !(report.Button & SWITCH_Y))
  39. report.Button |= SWITCH_L;
  40. else
  41. report.Button |= SWITCH_Y;
  42. } else {
  43. report.Button &= ~SWITCH_Y;
  44. report.Button &= ~SWITCH_L;
  45. }
  46. // X and R
  47. if (gamecube_buttons & GAMECUBE_X) {
  48. if (z_button && !(report.Button & SWITCH_X))
  49. report.Button |= SWITCH_R;
  50. else
  51. report.Button |= SWITCH_X;
  52. } else {
  53. report.Button &= ~SWITCH_X;
  54. report.Button &= ~SWITCH_R;
  55. }
  56. // B and -
  57. if (gamecube_buttons & GAMECUBE_B) {
  58. if (z_button && !(report.Button & SWITCH_B))
  59. report.Button |= SWITCH_MINUS;
  60. else
  61. report.Button |= SWITCH_B;
  62. } else {
  63. report.Button &= ~SWITCH_B;
  64. report.Button &= ~SWITCH_MINUS;
  65. }
  66. // A and +
  67. if (gamecube_buttons & GAMECUBE_A) {
  68. if (z_button && !(report.Button & SWITCH_A))
  69. report.Button |= SWITCH_PLUS;
  70. else
  71. report.Button |= SWITCH_A;
  72. } else {
  73. report.Button &= ~SWITCH_A;
  74. report.Button &= ~SWITCH_PLUS;
  75. }
  76. if (gamecube_buttons & GAMECUBE_L) {
  77. report.Button |= SWITCH_ZL;
  78. } else {
  79. report.Button &= ~SWITCH_ZL;
  80. }
  81. if (gamecube_buttons & GAMECUBE_R) {
  82. report.Button |= SWITCH_ZR;
  83. } else {
  84. report.Button &= ~SWITCH_ZR;
  85. }
  86. if ((gamecube_buttons & GAMECUBE_UP) && (gamecube_buttons & GAMECUBE_RIGHT))
  87. report.HAT = HAT_TOP_RIGHT;
  88. else if ((gamecube_buttons & GAMECUBE_UP) && (gamecube_buttons & GAMECUBE_LEFT))
  89. report.HAT = HAT_TOP_LEFT;
  90. else if ((gamecube_buttons & GAMECUBE_DOWN) && (gamecube_buttons & GAMECUBE_RIGHT))
  91. report.HAT = HAT_BOTTOM_RIGHT;
  92. else if ((gamecube_buttons & GAMECUBE_DOWN) && (gamecube_buttons & GAMECUBE_LEFT))
  93. report.HAT = HAT_BOTTOM_LEFT;
  94. else if (gamecube_buttons & GAMECUBE_UP)
  95. report.HAT = HAT_TOP;
  96. else if (gamecube_buttons & GAMECUBE_DOWN)
  97. report.HAT = HAT_BOTTOM;
  98. else if (gamecube_buttons & GAMECUBE_RIGHT)
  99. report.HAT = HAT_RIGHT;
  100. else if (gamecube_buttons & GAMECUBE_LEFT)
  101. report.HAT = HAT_LEFT;
  102. else
  103. report.HAT = HAT_CENTER;
  104. if (report.Button || report.HAT != HAT_CENTER)
  105. writePinHigh(C13);
  106. else
  107. writePinLow(C13);
  108. // joystick calculations
  109. report.LX = gamecube_joysticks[0];
  110. report.LY = 255 - gamecube_joysticks[1];
  111. report.RX = gamecube_joysticks[2];
  112. report.RY = 255 - gamecube_joysticks[3];
  113. send_gamepad(&report);
  114. }