process_joystick.c 6.3 KB

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