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.

94 lines
3.0 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 "analog_joystick.h"
  17. #include "analog.h"
  18. #include "gpio.h"
  19. #include "wait.h"
  20. // Set Parameters
  21. uint16_t minAxisValue = ANALOG_JOYSTICK_AXIS_MIN;
  22. uint16_t maxAxisValue = ANALOG_JOYSTICK_AXIS_MAX;
  23. uint8_t maxCursorSpeed = ANALOG_JOYSTICK_SPEED_MAX;
  24. uint8_t speedRegulator = ANALOG_JOYSTICK_SPEED_REGULATOR; // Lower Values Create Faster Movement
  25. int16_t xOrigin, yOrigin;
  26. uint16_t lastCursor = 0;
  27. int16_t axisCoordinate(pin_t pin, uint16_t origin) {
  28. int8_t direction;
  29. int16_t distanceFromOrigin;
  30. int16_t range;
  31. int16_t position = analogReadPin(pin);
  32. if (origin == position) {
  33. return 0;
  34. } else if (origin > position) {
  35. distanceFromOrigin = origin - position;
  36. range = origin - minAxisValue;
  37. direction = -1;
  38. } else {
  39. distanceFromOrigin = position - origin;
  40. range = maxAxisValue - origin;
  41. direction = 1;
  42. }
  43. float percent = (float)distanceFromOrigin / range;
  44. int16_t coordinate = (int16_t)(percent * 100);
  45. if (coordinate < 0) {
  46. return 0;
  47. } else if (coordinate > 100) {
  48. return 100 * direction;
  49. } else {
  50. return coordinate * direction;
  51. }
  52. }
  53. int8_t axisToMouseComponent(pin_t pin, int16_t origin, uint8_t maxSpeed) {
  54. int16_t coordinate = axisCoordinate(pin, origin);
  55. if (coordinate != 0) {
  56. float percent = (float)coordinate / 100;
  57. return percent * maxCursorSpeed * (abs(coordinate) / speedRegulator);
  58. } else {
  59. return 0;
  60. }
  61. }
  62. report_analog_joystick_t analog_joystick_read(void) {
  63. report_analog_joystick_t report = {0};
  64. if (timer_elapsed(lastCursor) > ANALOG_JOYSTICK_READ_INTERVAL) {
  65. lastCursor = timer_read();
  66. report.x = axisToMouseComponent(ANALOG_JOYSTICK_X_AXIS_PIN, xOrigin, maxCursorSpeed);
  67. report.y = axisToMouseComponent(ANALOG_JOYSTICK_Y_AXIS_PIN, yOrigin, maxCursorSpeed);
  68. }
  69. #ifdef ANALOG_JOYSTICK_CLICK_PIN
  70. report.button = !readPin(ANALOG_JOYSTICK_CLICK_PIN);
  71. #endif
  72. return report;
  73. }
  74. void analog_joystick_init(void) {
  75. #ifdef ANALOG_JOYSTICK_CLICK_PIN
  76. setPinInputHigh(ANALOG_JOYSTICK_CLICK_PIN);
  77. #endif
  78. // Account for drift
  79. xOrigin = analogReadPin(ANALOG_JOYSTICK_X_AXIS_PIN);
  80. yOrigin = analogReadPin(ANALOG_JOYSTICK_Y_AXIS_PIN);
  81. }