diff options
Diffstat (limited to 'drivers/sensors/pimoroni_trackball.c')
| -rw-r--r-- | drivers/sensors/pimoroni_trackball.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c new file mode 100644 index 000000000..c0ac644f7 --- /dev/null +++ b/drivers/sensors/pimoroni_trackball.c | |||
| @@ -0,0 +1,140 @@ | |||
| 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 "pimoroni_trackball.h" | ||
| 18 | #include "i2c_master.h" | ||
| 19 | |||
| 20 | static uint8_t scrolling = 0; | ||
| 21 | static int16_t x_offset = 0; | ||
| 22 | static int16_t y_offset = 0; | ||
| 23 | static int16_t h_offset = 0; | ||
| 24 | static int16_t v_offset = 0; | ||
| 25 | static float precisionSpeed = 1; | ||
| 26 | |||
| 27 | static uint16_t i2c_timeout_timer; | ||
| 28 | |||
| 29 | #ifndef PIMORONI_I2C_TIMEOUT | ||
| 30 | # define PIMORONI_I2C_TIMEOUT 100 | ||
| 31 | #endif | ||
| 32 | #ifndef I2C_WAITCHECK | ||
| 33 | # define I2C_WAITCHECK 1000 | ||
| 34 | #endif | ||
| 35 | #ifndef MOUSE_DEBOUNCE | ||
| 36 | # define MOUSE_DEBOUNCE 5 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) { | ||
| 40 | uint8_t data[] = {0x00, red, green, blue, white}; | ||
| 41 | i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), PIMORONI_I2C_TIMEOUT); | ||
| 42 | } | ||
| 43 | |||
| 44 | int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) { | ||
| 45 | int16_t offset = (int16_t)positive - (int16_t)negative; | ||
| 46 | int16_t magnitude = (int16_t)(scale * offset * offset * precisionSpeed); | ||
| 47 | return offset < 0 ? -magnitude : magnitude; | ||
| 48 | } | ||
| 49 | |||
| 50 | void update_member(int8_t* member, int16_t* offset) { | ||
| 51 | if (*offset > 127) { | ||
| 52 | *member = 127; | ||
| 53 | *offset -= 127; | ||
| 54 | } else if (*offset < -127) { | ||
| 55 | *member = -127; | ||
| 56 | *offset += 127; | ||
| 57 | } else { | ||
| 58 | *member = *offset; | ||
| 59 | *offset = 0; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* mouse) { | ||
| 64 | if (pressed) { | ||
| 65 | mouse->buttons |= MOUSE_BTN1; | ||
| 66 | } else { | ||
| 67 | mouse->buttons &= ~MOUSE_BTN1; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | float trackball_get_precision(void) { return precisionSpeed; } | ||
| 72 | void trackball_set_precision(float precision) { precisionSpeed = precision; } | ||
| 73 | bool trackball_is_scrolling(void) { return scrolling; } | ||
| 74 | void trackball_set_scrolling(bool scroll) { scrolling = scroll; } | ||
| 75 | |||
| 76 | __attribute__((weak)) void pointing_device_init(void) { i2c_init(); trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } | ||
| 77 | |||
| 78 | void pointing_device_task(void) { | ||
| 79 | static bool debounce; | ||
| 80 | static uint16_t debounce_timer; | ||
| 81 | uint8_t state[5] = {}; | ||
| 82 | if (timer_elapsed(i2c_timeout_timer) > I2C_WAITCHECK) { | ||
| 83 | if (i2c_readReg(TRACKBALL_READ, 0x04, state, 5, PIMORONI_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { | ||
| 84 | if (!state[4] && !debounce) { | ||
| 85 | if (scrolling) { | ||
| 86 | #ifdef PIMORONI_TRACKBALL_INVERT_X | ||
| 87 | h_offset += mouse_offset(state[2], state[3], 1); | ||
| 88 | #else | ||
| 89 | h_offset -= mouse_offset(state[2], state[3], 1); | ||
| 90 | #endif | ||
| 91 | #ifdef PIMORONI_TRACKBALL_INVERT_Y | ||
| 92 | v_offset += mouse_offset(state[1], state[0], 1); | ||
| 93 | #else | ||
| 94 | v_offset -= mouse_offset(state[1], state[0], 1); | ||
| 95 | #endif | ||
| 96 | } else { | ||
| 97 | #ifdef PIMORONI_TRACKBALL_INVERT_X | ||
| 98 | x_offset -= mouse_offset(state[2], state[3], 5); | ||
| 99 | #else | ||
| 100 | x_offset += mouse_offset(state[2], state[3], 5); | ||
| 101 | #endif | ||
| 102 | #ifdef PIMORONI_TRACKBALL_INVERT_Y | ||
| 103 | y_offset -= mouse_offset(state[1], state[0], 5); | ||
| 104 | #else | ||
| 105 | y_offset += mouse_offset(state[1], state[0], 5); | ||
| 106 | #endif | ||
| 107 | } | ||
| 108 | } else { | ||
| 109 | if (state[4]) { | ||
| 110 | debounce = true; | ||
| 111 | debounce_timer = timer_read(); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } else { | ||
| 115 | i2c_timeout_timer = timer_read(); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false; | ||
| 120 | |||
| 121 | report_mouse_t mouse = pointing_device_get_report(); | ||
| 122 | |||
| 123 | #ifdef PIMORONI_TRACKBALL_CLICK | ||
| 124 | trackball_check_click(state[4] & (1 << 7), &mouse); | ||
| 125 | #endif | ||
| 126 | |||
| 127 | #ifndef PIMORONI_TRACKBALL_ROTATE | ||
| 128 | update_member(&mouse.x, &x_offset); | ||
| 129 | update_member(&mouse.y, &y_offset); | ||
| 130 | update_member(&mouse.h, &h_offset); | ||
| 131 | update_member(&mouse.v, &v_offset); | ||
| 132 | #else | ||
| 133 | update_member(&mouse.x, &y_offset); | ||
| 134 | update_member(&mouse.y, &x_offset); | ||
| 135 | update_member(&mouse.h, &v_offset); | ||
| 136 | update_member(&mouse.v, &h_offset); | ||
| 137 | #endif | ||
| 138 | pointing_device_set_report(mouse); | ||
| 139 | pointing_device_send(); | ||
| 140 | } | ||
