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