diff options
| author | Drashna Jaelre <drashna@live.com> | 2021-11-14 22:03:24 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-14 22:03:24 -0800 |
| commit | 56e3f06a26851976e559aacf7a096c61403304be (patch) | |
| tree | 1e9ec98ad239fdd241e77ac4c4822fc2721a9cea /drivers/sensors/analog_joystick.c | |
| parent | 462c3a615113e84ac3ca837a5caeb928c0ec8505 (diff) | |
| download | qmk_firmware-56e3f06a26851976e559aacf7a096c61403304be.tar.gz qmk_firmware-56e3f06a26851976e559aacf7a096c61403304be.zip | |
Rework and expand Pointing Device support (#14343)
Co-authored-by: Dasky <32983009+daskygit@users.noreply.github.com>
Diffstat (limited to 'drivers/sensors/analog_joystick.c')
| -rw-r--r-- | drivers/sensors/analog_joystick.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/drivers/sensors/analog_joystick.c b/drivers/sensors/analog_joystick.c new file mode 100644 index 000000000..0f4d1d7a4 --- /dev/null +++ b/drivers/sensors/analog_joystick.c | |||
| @@ -0,0 +1,94 @@ | |||
| 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 | |||
| 17 | #include "analog_joystick.h" | ||
| 18 | #include "analog.h" | ||
| 19 | #include "gpio.h" | ||
| 20 | #include "wait.h" | ||
| 21 | |||
| 22 | // Set Parameters | ||
| 23 | uint16_t minAxisValue = ANALOG_JOYSTICK_AXIS_MIN; | ||
| 24 | uint16_t maxAxisValue = ANALOG_JOYSTICK_AXIS_MAX; | ||
| 25 | |||
| 26 | uint8_t maxCursorSpeed = ANALOG_JOYSTICK_SPEED_MAX; | ||
| 27 | uint8_t speedRegulator = ANALOG_JOYSTICK_SPEED_REGULATOR; // Lower Values Create Faster Movement | ||
| 28 | |||
| 29 | int16_t xOrigin, yOrigin; | ||
| 30 | |||
| 31 | uint16_t lastCursor = 0; | ||
| 32 | |||
| 33 | int16_t axisCoordinate(uint8_t pin, uint16_t origin) { | ||
| 34 | int8_t direction; | ||
| 35 | int16_t distanceFromOrigin; | ||
| 36 | int16_t range; | ||
| 37 | |||
| 38 | int16_t position = analogReadPin(pin); | ||
| 39 | |||
| 40 | if (origin == position) { | ||
| 41 | return 0; | ||
| 42 | } else if (origin > position) { | ||
| 43 | distanceFromOrigin = origin - position; | ||
| 44 | range = origin - minAxisValue; | ||
| 45 | direction = -1; | ||
| 46 | } else { | ||
| 47 | distanceFromOrigin = position - origin; | ||
| 48 | range = maxAxisValue - origin; | ||
| 49 | direction = 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | float percent = (float)distanceFromOrigin / range; | ||
| 53 | int16_t coordinate = (int16_t)(percent * 100); | ||
| 54 | if (coordinate < 0) { | ||
| 55 | return 0; | ||
| 56 | } else if (coordinate > 100) { | ||
| 57 | return 100 * direction; | ||
| 58 | } else { | ||
| 59 | return coordinate * direction; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | int8_t axisToMouseComponent(uint8_t pin, int16_t origin, uint8_t maxSpeed) { | ||
| 64 | int16_t coordinate = axisCoordinate(pin, origin); | ||
| 65 | if (coordinate != 0) { | ||
| 66 | float percent = (float)coordinate / 100; | ||
| 67 | return percent * maxCursorSpeed * (abs(coordinate) / speedRegulator); | ||
| 68 | } else { | ||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | report_analog_joystick_t analog_joystick_read(void) { | ||
| 74 | report_analog_joystick_t report = {0}; | ||
| 75 | |||
| 76 | if (timer_elapsed(lastCursor) > ANALOG_JOYSTICK_READ_INTERVAL) { | ||
| 77 | lastCursor = timer_read(); | ||
| 78 | report.x = axisToMouseComponent(ANALOG_JOYSTICK_X_AXIS_PIN, xOrigin, maxCursorSpeed); | ||
| 79 | report.y = axisToMouseComponent(ANALOG_JOYSTICK_Y_AXIS_PIN, yOrigin, maxCursorSpeed); | ||
| 80 | } | ||
| 81 | #ifdef ANALOG_JOYSTICK_CLICK_PIN | ||
| 82 | report.button = !readPin(ANALOG_JOYSTICK_CLICK_PIN); | ||
| 83 | #endif | ||
| 84 | return report; | ||
| 85 | } | ||
| 86 | |||
| 87 | void analog_joystick_init(void) { | ||
| 88 | #ifdef ANALOG_JOYSTICK_CLICK_PIN | ||
| 89 | setPinInputHigh(ANALOG_JOYSTICK_CLICK_PIN); | ||
| 90 | #endif | ||
| 91 | // Account for drift | ||
| 92 | xOrigin = analogReadPin(ANALOG_JOYSTICK_X_AXIS_PIN); | ||
| 93 | yOrigin = analogReadPin(ANALOG_JOYSTICK_Y_AXIS_PIN); | ||
| 94 | } | ||
