You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.5 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "drashna.h"
  17. #include "analog.h"
  18. #include "pointing_device.h"
  19. #define KC_X0 LT(_FN, KC_ESC)
  20. // clang-format off
  21. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  22. [_QWERTY] = LAYOUT(
  23. KC_VOLU, KC_MPLY, KC_MPRV, RESET,
  24. KC_VOLD, KC_MUTE, KC_MNXT, RESET
  25. ),
  26. };
  27. // clang-format on
  28. // Joystick
  29. // Set Pins
  30. // uint8_t xPin = 8; // VRx / /B4
  31. // uint8_t yPin = 7; // VRy // B5
  32. uint8_t swPin = E6; // SW
  33. // Set Parameters
  34. uint16_t minAxisValue = 0;
  35. uint16_t maxAxisValue = 1023;
  36. uint8_t maxCursorSpeed = 2;
  37. uint8_t precisionSpeed = 1;
  38. uint8_t speedRegulator = 20; // Lower Values Create Faster Movement
  39. int8_t xPolarity = 1;
  40. int8_t yPolarity = 1;
  41. uint8_t cursorTimeout = 10;
  42. int16_t xOrigin, yOrigin;
  43. uint16_t lastCursor = 0;
  44. int16_t axisCoordinate(uint8_t pin, uint16_t origin) {
  45. int8_t direction;
  46. int16_t distanceFromOrigin;
  47. int16_t range;
  48. int16_t position = analogReadPin(pin);
  49. if (origin == position) {
  50. return 0;
  51. } else if (origin > position) {
  52. distanceFromOrigin = origin - position;
  53. range = origin - minAxisValue;
  54. direction = -1;
  55. } else {
  56. distanceFromOrigin = position - origin;
  57. range = maxAxisValue - origin;
  58. direction = 1;
  59. }
  60. float percent = (float)distanceFromOrigin / range;
  61. int16_t coordinate = (int16_t)(percent * 100);
  62. if (coordinate < 0) {
  63. return 0;
  64. } else if (coordinate > 100) {
  65. return 100 * direction;
  66. } else {
  67. return coordinate * direction;
  68. }
  69. }
  70. int8_t axisToMouseComponent(uint8_t pin, int16_t origin, uint8_t maxSpeed, int8_t polarity) {
  71. int coordinate = axisCoordinate(pin, origin);
  72. if (coordinate == 0) {
  73. return 0;
  74. } else {
  75. float percent = (float)coordinate / 100;
  76. if (keyboard_report->mods & MOD_BIT(KC_LSFT)) {
  77. return percent * precisionSpeed * polarity * (abs(coordinate) / speedRegulator);
  78. } else {
  79. return percent * maxCursorSpeed * polarity * (abs(coordinate) / speedRegulator);
  80. }
  81. }
  82. }
  83. void pointing_device_task(void) {
  84. report_mouse_t report = pointing_device_get_report();
  85. // todo read as one vector
  86. if (timer_elapsed(lastCursor) > cursorTimeout) {
  87. lastCursor = timer_read();
  88. report.x = axisToMouseComponent(B4, xOrigin, maxCursorSpeed, xPolarity);
  89. report.y = axisToMouseComponent(B5, yOrigin, maxCursorSpeed, yPolarity);
  90. }
  91. //
  92. if (!readPin(E6)) {
  93. report.buttons |= MOUSE_BTN1;
  94. } else {
  95. report.buttons &= ~MOUSE_BTN1;
  96. }
  97. pointing_device_set_report(report);
  98. pointing_device_send();
  99. }
  100. void matrix_init_keymap(void) {
  101. // init pin? Is needed?
  102. setPinInputHigh(E6);
  103. // Account for drift
  104. xOrigin = analogReadPin(B4);
  105. yOrigin = analogReadPin(B5);
  106. }