process_joystick.c 5.9 KB

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