diff options
Diffstat (limited to 'users/drashna/pimoroni_trackball.c')
| -rw-r--r-- | users/drashna/pimoroni_trackball.c | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/users/drashna/pimoroni_trackball.c b/users/drashna/pimoroni_trackball.c index c5bb560b3..9ae094c05 100644 --- a/users/drashna/pimoroni_trackball.c +++ b/users/drashna/pimoroni_trackball.c | |||
| @@ -1,3 +1,19 @@ | |||
| 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 | |||
| 1 | #include "pimoroni_trackball.h" | 17 | #include "pimoroni_trackball.h" |
| 2 | #include "i2c_master.h" | 18 | #include "i2c_master.h" |
| 3 | 19 | ||
| @@ -6,7 +22,7 @@ static int16_t x_offset = 0; | |||
| 6 | static int16_t y_offset = 0; | 22 | static int16_t y_offset = 0; |
| 7 | static int16_t h_offset = 0; | 23 | static int16_t h_offset = 0; |
| 8 | static int16_t v_offset = 0; | 24 | static int16_t v_offset = 0; |
| 9 | static float precisionSpeed = 1; | 25 | static float precisionSpeed = 1; |
| 10 | 26 | ||
| 11 | #ifndef I2C_TIMEOUT | 27 | #ifndef I2C_TIMEOUT |
| 12 | # define I2C_TIMEOUT 100 | 28 | # define I2C_TIMEOUT 100 |
| @@ -47,17 +63,37 @@ __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* m | |||
| 47 | } | 63 | } |
| 48 | } | 64 | } |
| 49 | 65 | ||
| 66 | void trackball_register_button(bool pressed, enum mouse_buttons button) { | ||
| 67 | report_mouse_t currentReport = pointing_device_get_report(); | ||
| 68 | if (pressed) { | ||
| 69 | currentReport.buttons |= button; | ||
| 70 | } else { | ||
| 71 | currentReport.buttons &= ~button; | ||
| 72 | } | ||
| 73 | pointing_device_set_report(currentReport); | ||
| 74 | } | ||
| 75 | |||
| 50 | float trackball_get_precision(void) { return precisionSpeed; } | 76 | float trackball_get_precision(void) { return precisionSpeed; } |
| 51 | void trackball_set_precision(float precision) { precisionSpeed = precision; } | 77 | void trackball_set_precision(float precision) { precisionSpeed = precision; } |
| 52 | bool trackball_is_scrolling(void) { return scrolling; } | 78 | bool trackball_is_scrolling(void) { return scrolling; } |
| 53 | void trackball_set_scrolling(bool scroll) { scrolling = scroll; } | 79 | void trackball_set_scrolling(bool scroll) { scrolling = scroll; } |
| 54 | 80 | ||
| 55 | __attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00,0x00,0x00,0x4F); } | 81 | bool has_report_changed (report_mouse_t first, report_mouse_t second) { |
| 82 | return !( | ||
| 83 | (!first.buttons && first.buttons == second.buttons) && | ||
| 84 | (!first.x && first.x == second.x) && | ||
| 85 | (!first.y && first.y == second.y) && | ||
| 86 | (!first.h && first.h == second.h) && | ||
| 87 | (!first.v && first.v == second.v) ); | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | __attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00, 0x00, 0x00, 0x4F); } | ||
| 56 | 92 | ||
| 57 | void pointing_device_task(void) { | 93 | void pointing_device_task(void) { |
| 58 | static bool debounce; | 94 | static bool debounce; |
| 59 | static uint16_t debounce_timer; | 95 | static uint16_t debounce_timer; |
| 60 | uint8_t state[5] = {}; | 96 | uint8_t state[5] = {}; |
| 61 | if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { | 97 | if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { |
| 62 | if (!state[4] && !debounce) { | 98 | if (!state[4] && !debounce) { |
| 63 | if (scrolling) { | 99 | if (scrolling) { |
| @@ -85,7 +121,7 @@ void pointing_device_task(void) { | |||
| 85 | } | 121 | } |
| 86 | } else { | 122 | } else { |
| 87 | if (state[4]) { | 123 | if (state[4]) { |
| 88 | debounce = true; | 124 | debounce = true; |
| 89 | debounce_timer = timer_read(); | 125 | debounce_timer = timer_read(); |
| 90 | } | 126 | } |
| 91 | } | 127 | } |
| @@ -97,7 +133,6 @@ void pointing_device_task(void) { | |||
| 97 | 133 | ||
| 98 | trackball_check_click(state[4] & (1 << 7), &mouse); | 134 | trackball_check_click(state[4] & (1 << 7), &mouse); |
| 99 | 135 | ||
| 100 | |||
| 101 | #ifndef PIMORONI_TRACKBALL_ROTATE | 136 | #ifndef PIMORONI_TRACKBALL_ROTATE |
| 102 | update_member(&mouse.x, &x_offset); | 137 | update_member(&mouse.x, &x_offset); |
| 103 | update_member(&mouse.y, &y_offset); | 138 | update_member(&mouse.y, &y_offset); |
| @@ -110,5 +145,7 @@ void pointing_device_task(void) { | |||
| 110 | update_member(&mouse.v, &h_offset); | 145 | update_member(&mouse.v, &h_offset); |
| 111 | #endif | 146 | #endif |
| 112 | pointing_device_set_report(mouse); | 147 | pointing_device_set_report(mouse); |
| 113 | pointing_device_send(); | 148 | if (has_report_changed(mouse, pointing_device_get_report())) { |
| 149 | pointing_device_send(); | ||
| 150 | } | ||
| 114 | } | 151 | } |
