pimoroni_trackball.c 4.9 KB

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