process_joystick.c 6.4 KB

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