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.

110 lines
2.8 KiB

  1. #include "drashna.h"
  2. #include "analog.h"
  3. #include "pointing_device.h"
  4. #define KC_X0 LT(_FN, KC_ESC)
  5. // clang-format off
  6. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  7. [_QWERTY] = LAYOUT(
  8. KC_VOLU, KC_MPLY, KC_MPRV, RESET,
  9. KC_VOLD, KC_MUTE, KC_MNXT, RESET
  10. ),
  11. };
  12. // clang-format on
  13. // Joystick
  14. // Set Pins
  15. // uint8_t xPin = 8; // VRx / /B4
  16. // uint8_t yPin = 7; // VRy // B5
  17. uint8_t swPin = E6; // SW
  18. // Set Parameters
  19. uint16_t minAxisValue = 0;
  20. uint16_t maxAxisValue = 1023;
  21. uint8_t maxCursorSpeed = 2;
  22. uint8_t precisionSpeed = 1;
  23. uint8_t speedRegulator = 20; // Lower Values Create Faster Movement
  24. int8_t xPolarity = 1;
  25. int8_t yPolarity = 1;
  26. uint8_t cursorTimeout = 10;
  27. int16_t xOrigin, yOrigin;
  28. uint16_t lastCursor = 0;
  29. int16_t axisCoordinate(uint8_t pin, uint16_t origin) {
  30. int8_t direction;
  31. int16_t distanceFromOrigin;
  32. int16_t range;
  33. int16_t position = analogReadPin(pin);
  34. if (origin == position) {
  35. return 0;
  36. } else if (origin > position) {
  37. distanceFromOrigin = origin - position;
  38. range = origin - minAxisValue;
  39. direction = -1;
  40. } else {
  41. distanceFromOrigin = position - origin;
  42. range = maxAxisValue - origin;
  43. direction = 1;
  44. }
  45. float percent = (float)distanceFromOrigin / range;
  46. int16_t coordinate = (int16_t)(percent * 100);
  47. if (coordinate < 0) {
  48. return 0;
  49. } else if (coordinate > 100) {
  50. return 100 * direction;
  51. } else {
  52. return coordinate * direction;
  53. }
  54. }
  55. int8_t axisToMouseComponent(uint8_t pin, int16_t origin, uint8_t maxSpeed, int8_t polarity) {
  56. int coordinate = axisCoordinate(pin, origin);
  57. if (coordinate == 0) {
  58. return 0;
  59. } else {
  60. float percent = (float)coordinate / 100;
  61. if (keyboard_report->mods & MOD_BIT(KC_LSFT)) {
  62. return percent * precisionSpeed * polarity * (abs(coordinate) / speedRegulator);
  63. } else {
  64. return percent * maxCursorSpeed * polarity * (abs(coordinate) / speedRegulator);
  65. }
  66. }
  67. }
  68. void pointing_device_task(void) {
  69. report_mouse_t report = pointing_device_get_report();
  70. // todo read as one vector
  71. if (timer_elapsed(lastCursor) > cursorTimeout) {
  72. lastCursor = timer_read();
  73. report.x = axisToMouseComponent(B4, xOrigin, maxCursorSpeed, xPolarity);
  74. report.y = axisToMouseComponent(B5, yOrigin, maxCursorSpeed, yPolarity);
  75. }
  76. //
  77. if (!readPin(E6)) {
  78. report.buttons |= MOUSE_BTN1;
  79. } else {
  80. report.buttons &= ~MOUSE_BTN1;
  81. }
  82. pointing_device_set_report(report);
  83. pointing_device_send();
  84. }
  85. void matrix_init_keymap(void) {
  86. // init pin? Is needed?
  87. setPinInputHigh(E6);
  88. // Account for drift
  89. xOrigin = analogReadPin(B4);
  90. yOrigin = analogReadPin(B5);
  91. }