process_joystick.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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() { return process_joystick_analogread_quantum(); }
  66. bool process_joystick_analogread_quantum() {
  67. #if JOYSTICK_AXES_COUNT > 0
  68. for (int axis_index = 0; axis_index < JOYSTICK_AXES_COUNT; ++axis_index) {
  69. if (joystick_axes[axis_index].input_pin == JS_VIRTUAL_AXIS) {
  70. continue;
  71. }
  72. // save previous input pin status as well
  73. uint16_t inputSavedState = savePinState(joystick_axes[axis_index].input_pin);
  74. // disable pull-up resistor
  75. writePinLow(joystick_axes[axis_index].input_pin);
  76. // if pin was a pull-up input, we need to uncharge it by turning it low
  77. // before making it a low input
  78. setPinOutput(joystick_axes[axis_index].input_pin);
  79. wait_us(10);
  80. // save and apply output pin status
  81. uint16_t outputSavedState = 0;
  82. if (joystick_axes[axis_index].output_pin != JS_VIRTUAL_AXIS) {
  83. // save previous output pin status
  84. outputSavedState = savePinState(joystick_axes[axis_index].output_pin);
  85. setPinOutput(joystick_axes[axis_index].output_pin);
  86. writePinHigh(joystick_axes[axis_index].output_pin);
  87. }
  88. uint16_t groundSavedState = 0;
  89. if (joystick_axes[axis_index].ground_pin != JS_VIRTUAL_AXIS) {
  90. // save previous output pin status
  91. groundSavedState = savePinState(joystick_axes[axis_index].ground_pin);
  92. setPinOutput(joystick_axes[axis_index].ground_pin);
  93. writePinLow(joystick_axes[axis_index].ground_pin);
  94. }
  95. wait_us(10);
  96. setPinInput(joystick_axes[axis_index].input_pin);
  97. wait_us(10);
  98. # if defined(__AVR__) || defined(PROTOCOL_CHIBIOS)
  99. int16_t axis_val = analogReadPin(joystick_axes[axis_index].input_pin);
  100. # else
  101. // default to resting position
  102. int16_t axis_val = joystick_axes[axis_index].mid_digit;
  103. # endif
  104. // test the converted value against the lower range
  105. int32_t ref = joystick_axes[axis_index].mid_digit;
  106. int32_t range = joystick_axes[axis_index].min_digit;
  107. int32_t ranged_val = ((axis_val - ref) * -JOYSTICK_RESOLUTION) / (range - ref);
  108. if (ranged_val > 0) {
  109. // the value is in the higher range
  110. range = joystick_axes[axis_index].max_digit;
  111. ranged_val = ((axis_val - ref) * JOYSTICK_RESOLUTION) / (range - ref);
  112. }
  113. // clamp the result in the valid range
  114. ranged_val = ranged_val < -JOYSTICK_RESOLUTION ? -JOYSTICK_RESOLUTION : ranged_val;
  115. ranged_val = ranged_val > JOYSTICK_RESOLUTION ? JOYSTICK_RESOLUTION : ranged_val;
  116. if (ranged_val != joystick_status.axes[axis_index]) {
  117. joystick_status.axes[axis_index] = ranged_val;
  118. joystick_status.status |= JS_UPDATED;
  119. }
  120. // restore output, ground and input status
  121. if (joystick_axes[axis_index].output_pin != JS_VIRTUAL_AXIS) {
  122. restorePinState(joystick_axes[axis_index].output_pin, outputSavedState);
  123. }
  124. if (joystick_axes[axis_index].ground_pin != JS_VIRTUAL_AXIS) {
  125. restorePinState(joystick_axes[axis_index].ground_pin, groundSavedState);
  126. }
  127. restorePinState(joystick_axes[axis_index].input_pin, inputSavedState);
  128. }
  129. #endif
  130. return true;
  131. }