process_joystick.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "joystick.h"
  2. #include "process_joystick.h"
  3. #include "analog.h"
  4. #include <string.h>
  5. #include <math.h>
  6. bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record);
  7. bool process_joystick(uint16_t keycode, keyrecord_t *record) {
  8. if (process_joystick_buttons(keycode, record) && (joystick_status.status & JS_UPDATED) > 0) {
  9. send_joystick_packet(&joystick_status);
  10. joystick_status.status &= ~JS_UPDATED;
  11. }
  12. return true;
  13. }
  14. __attribute__((weak)) void joystick_task(void) {
  15. if (process_joystick_analogread() && (joystick_status.status & JS_UPDATED)) {
  16. send_joystick_packet(&joystick_status);
  17. joystick_status.status &= ~JS_UPDATED;
  18. }
  19. }
  20. bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record) {
  21. if (keycode < JS_BUTTON0 || keycode > JS_BUTTON_MAX) {
  22. return true;
  23. } else {
  24. if (record->event.pressed) {
  25. joystick_status.buttons[(keycode - JS_BUTTON0) / 8] |= 1 << (keycode % 8);
  26. } else {
  27. joystick_status.buttons[(keycode - JS_BUTTON0) / 8] &= ~(1 << (keycode % 8));
  28. }
  29. joystick_status.status |= JS_UPDATED;
  30. }
  31. return true;
  32. }
  33. uint16_t savePinState(pin_t pin) {
  34. #ifdef __AVR__
  35. uint8_t pinNumber = pin & 0xF;
  36. return ((PORTx_ADDRESS(pin) >> pinNumber) & 0x1) << 1 | ((DDRx_ADDRESS(pin) >> pinNumber) & 0x1);
  37. #elif defined(PROTOCOL_CHIBIOS)
  38. /*
  39. The pin configuration is backed up in the following format :
  40. bit 15 9 8 7 6 5 4 3 2 1 0
  41. |unused|ODR|IDR|PUPDR|OSPEEDR|OTYPER|MODER|
  42. */
  43. return ((PAL_PORT(pin)->MODER >> (2 * PAL_PAD(pin))) & 0x3) | (((PAL_PORT(pin)->OTYPER >> (1 * PAL_PAD(pin))) & 0x1) << 2) | (((PAL_PORT(pin)->OSPEEDR >> (2 * PAL_PAD(pin))) & 0x3) << 3) | (((PAL_PORT(pin)->PUPDR >> (2 * PAL_PAD(pin))) & 0x3) << 5) | (((PAL_PORT(pin)->IDR >> (1 * PAL_PAD(pin))) & 0x1) << 7) | (((PAL_PORT(pin)->ODR >> (1 * PAL_PAD(pin))) & 0x1) << 8);
  44. #else
  45. return 0;
  46. #endif
  47. }
  48. void restorePinState(pin_t pin, uint16_t restoreState) {
  49. #if defined(PROTOCOL_LUFA)
  50. uint8_t pinNumber = pin & 0xF;
  51. PORTx_ADDRESS(pin) = (PORTx_ADDRESS(pin) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber);
  52. DDRx_ADDRESS(pin) = (DDRx_ADDRESS(pin) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber);
  53. #elif defined(PROTOCOL_CHIBIOS)
  54. PAL_PORT(pin)->MODER = (PAL_PORT(pin)->MODER & ~(0x3 << (2 * PAL_PAD(pin)))) | (restoreState & 0x3) << (2 * PAL_PAD(pin));
  55. PAL_PORT(pin)->OTYPER = (PAL_PORT(pin)->OTYPER & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 2) & 0x1) << (1 * PAL_PAD(pin));
  56. PAL_PORT(pin)->OSPEEDR = (PAL_PORT(pin)->OSPEEDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 3) & 0x3) << (2 * PAL_PAD(pin));
  57. PAL_PORT(pin)->PUPDR = (PAL_PORT(pin)->PUPDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 5) & 0x3) << (2 * PAL_PAD(pin));
  58. PAL_PORT(pin)->IDR = (PAL_PORT(pin)->IDR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 7) & 0x1) << (1 * PAL_PAD(pin));
  59. PAL_PORT(pin)->ODR = (PAL_PORT(pin)->ODR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 8) & 0x1) << (1 * PAL_PAD(pin));
  60. #else
  61. return;
  62. #endif
  63. }
  64. __attribute__((weak)) bool process_joystick_analogread() { return process_joystick_analogread_quantum(); }
  65. bool process_joystick_analogread_quantum() {
  66. #if JOYSTICK_AXES_COUNT > 0
  67. for (int axis_index = 0; axis_index < JOYSTICK_AXES_COUNT; ++axis_index) {
  68. if (joystick_axes[axis_index].input_pin == JS_VIRTUAL_AXIS) {
  69. continue;
  70. }
  71. // save previous input pin status as well
  72. uint16_t inputSavedState = savePinState(joystick_axes[axis_index].input_pin);
  73. // disable pull-up resistor
  74. writePinLow(joystick_axes[axis_index].input_pin);
  75. // if pin was a pull-up input, we need to uncharge it by turning it low
  76. // before making it a low input
  77. setPinOutput(joystick_axes[axis_index].input_pin);
  78. wait_us(10);
  79. // save and apply output pin status
  80. uint16_t outputSavedState = 0;
  81. if (joystick_axes[axis_index].output_pin != JS_VIRTUAL_AXIS) {
  82. // save previous output pin status
  83. outputSavedState = savePinState(joystick_axes[axis_index].output_pin);
  84. setPinOutput(joystick_axes[axis_index].output_pin);
  85. writePinHigh(joystick_axes[axis_index].output_pin);
  86. }
  87. uint16_t groundSavedState = 0;
  88. if (joystick_axes[axis_index].ground_pin != JS_VIRTUAL_AXIS) {
  89. // save previous output pin status
  90. groundSavedState = savePinState(joystick_axes[axis_index].ground_pin);
  91. setPinOutput(joystick_axes[axis_index].ground_pin);
  92. writePinLow(joystick_axes[axis_index].ground_pin);
  93. }
  94. wait_us(10);
  95. setPinInput(joystick_axes[axis_index].input_pin);
  96. wait_us(10);
  97. # if defined(__AVR__) || defined(PROTOCOL_CHIBIOS)
  98. int16_t axis_val = analogReadPin(joystick_axes[axis_index].input_pin);
  99. # else
  100. // default to resting position
  101. int16_t axis_val = joystick_axes[axis_index].mid_digit;
  102. # endif
  103. // test the converted value against the lower range
  104. int32_t ref = joystick_axes[axis_index].mid_digit;
  105. int32_t range = joystick_axes[axis_index].min_digit;
  106. int32_t ranged_val = ((axis_val - ref) * -JOYSTICK_RESOLUTION) / (range - ref);
  107. if (ranged_val > 0) {
  108. // the value is in the higher range
  109. range = joystick_axes[axis_index].max_digit;
  110. ranged_val = ((axis_val - ref) * JOYSTICK_RESOLUTION) / (range - ref);
  111. }
  112. // clamp the result in the valid range
  113. ranged_val = ranged_val < -JOYSTICK_RESOLUTION ? -JOYSTICK_RESOLUTION : ranged_val;
  114. ranged_val = ranged_val > JOYSTICK_RESOLUTION ? JOYSTICK_RESOLUTION : ranged_val;
  115. if (ranged_val != joystick_status.axes[axis_index]) {
  116. joystick_status.axes[axis_index] = ranged_val;
  117. joystick_status.status |= JS_UPDATED;
  118. }
  119. // restore output, ground and input status
  120. if (joystick_axes[axis_index].output_pin != JS_VIRTUAL_AXIS) {
  121. restorePinState(joystick_axes[axis_index].output_pin, outputSavedState);
  122. }
  123. if (joystick_axes[axis_index].ground_pin != JS_VIRTUAL_AXIS) {
  124. restorePinState(joystick_axes[axis_index].ground_pin, groundSavedState);
  125. }
  126. restorePinState(joystick_axes[axis_index].input_pin, inputSavedState);
  127. }
  128. #endif
  129. return true;
  130. }