pimoroni_trackball.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2021 Dasky (@daskygit)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "pimoroni_trackball.h"
  18. #include "i2c_master.h"
  19. #include "print.h"
  20. #ifndef PIMORONI_TRACKBALL_ADDRESS
  21. # define PIMORONI_TRACKBALL_ADDRESS 0x0A
  22. #endif
  23. #ifndef PIMORONI_TRACKBALL_INTERVAL_MS
  24. # define PIMORONI_TRACKBALL_INTERVAL_MS 8
  25. #endif
  26. #ifndef PIMORONI_TRACKBALL_MOUSE_SCALE
  27. # define PIMORONI_TRACKBALL_MOUSE_SCALE 5
  28. #endif
  29. #ifndef PIMORONI_TRACKBALL_SCROLL_SCALE
  30. # define PIMORONI_TRACKBALL_SCROLL_SCALE 1
  31. #endif
  32. #ifndef PIMORONI_TRACKBALL_DEBOUNCE_CYCLES
  33. # define PIMORONI_TRACKBALL_DEBOUNCE_CYCLES 20
  34. #endif
  35. #ifndef PIMORONI_TRACKBALL_ERROR_COUNT
  36. # define PIMORONI_TRACKBALL_ERROR_COUNT 10
  37. #endif
  38. #define TRACKBALL_TIMEOUT 100
  39. #define TRACKBALL_REG_LED_RED 0x00
  40. #define TRACKBALL_REG_LED_GRN 0x01
  41. #define TRACKBALL_REG_LED_BLU 0x02
  42. #define TRACKBALL_REG_LED_WHT 0x03
  43. #define TRACKBALL_REG_LEFT 0x04
  44. #define TRACKBALL_REG_RIGHT 0x05
  45. #define TRACKBALL_REG_UP 0x06
  46. #define TRACKBALL_REG_DOWN 0x07
  47. static pimoroni_data current_pimoroni_data;
  48. static report_mouse_t mouse_report;
  49. static bool scrolling = false;
  50. static int16_t x_offset = 0;
  51. static int16_t y_offset = 0;
  52. static int16_t h_offset = 0;
  53. static int16_t v_offset = 0;
  54. static uint16_t precision = 128;
  55. static uint8_t error_count = 0;
  56. float trackball_get_precision(void) { return ((float)precision / 128); }
  57. void trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); }
  58. bool trackball_is_scrolling(void) { return scrolling; }
  59. void trackball_set_scrolling(bool scroll) { scrolling = scroll; }
  60. void trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  61. uint8_t data[4] = {r, g, b, w};
  62. __attribute__((unused)) i2c_status_t status = i2c_writeReg(PIMORONI_TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LED_RED, data, sizeof(data), TRACKBALL_TIMEOUT);
  63. #ifdef TRACKBALL_DEBUG
  64. dprintf("Trackball RGBW i2c_status_t: %d\n", status);
  65. #endif
  66. }
  67. i2c_status_t read_pimoroni_trackball(pimoroni_data* data) {
  68. i2c_status_t status = i2c_readReg(PIMORONI_TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), TRACKBALL_TIMEOUT);
  69. #ifdef TRACKBALL_DEBUG
  70. dprintf("Trackball READ i2c_status_t: %d\nLeft: %d\nRight: %d\nUp: %d\nDown: %d\nSwtich: %d\n", status, data->left, data->right, data->up, data->down, data->click);
  71. #endif
  72. return status;
  73. }
  74. __attribute__((weak)) void pointing_device_init(void) {
  75. i2c_init();
  76. trackball_set_rgbw(0x00, 0x00, 0x00, 0x00);
  77. }
  78. int16_t trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) {
  79. uint8_t offset = 0;
  80. bool isnegative = false;
  81. if (negative_dir > positive_dir) {
  82. offset = negative_dir - positive_dir;
  83. isnegative = true;
  84. } else {
  85. offset = positive_dir - negative_dir;
  86. }
  87. uint16_t magnitude = (scale * offset * offset * precision) >> 7;
  88. return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude);
  89. }
  90. void trackball_adapt_values(int8_t* mouse, int16_t* offset) {
  91. if (*offset > 127) {
  92. *mouse = 127;
  93. *offset -= 127;
  94. } else if (*offset < -127) {
  95. *mouse = -127;
  96. *offset += 127;
  97. } else {
  98. *mouse = *offset;
  99. *offset = 0;
  100. }
  101. }
  102. __attribute__((weak)) void trackball_click(bool pressed, report_mouse_t* mouse) {
  103. #ifdef PIMORONI_TRACKBALL_CLICK
  104. if (pressed) {
  105. mouse->buttons |= MOUSE_BTN1;
  106. } else {
  107. mouse->buttons &= ~MOUSE_BTN1;
  108. }
  109. #endif
  110. }
  111. __attribute__((weak)) bool pointing_device_task_user(pimoroni_data* trackball_data) { return true; };
  112. __attribute__((weak)) void pointing_device_task() {
  113. static fast_timer_t throttle = 0;
  114. static uint16_t debounce = 0;
  115. if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT && timer_elapsed_fast(throttle) >= PIMORONI_TRACKBALL_INTERVAL_MS) {
  116. i2c_status_t status = read_pimoroni_trackball(&current_pimoroni_data);
  117. if (status == I2C_STATUS_SUCCESS) {
  118. error_count = 0;
  119. if (pointing_device_task_user(&current_pimoroni_data)) {
  120. mouse_report = pointing_device_get_report();
  121. if (!(current_pimoroni_data.click & 128)) {
  122. trackball_click(false, &mouse_report);
  123. if (!debounce) {
  124. if (scrolling) {
  125. #ifdef PIMORONI_TRACKBALL_INVERT_X
  126. h_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE);
  127. #else
  128. h_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE);
  129. #endif
  130. #ifdef PIMORONI_TRACKBALL_INVERT_Y
  131. v_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE);
  132. #else
  133. v_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE);
  134. #endif
  135. } else {
  136. #ifdef PIMORONI_TRACKBALL_INVERT_X
  137. x_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE);
  138. #else
  139. x_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE);
  140. #endif
  141. #ifdef PIMORONI_TRACKBALL_INVERT_Y
  142. y_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE);
  143. #else
  144. y_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE);
  145. #endif
  146. }
  147. if (scrolling) {
  148. #ifndef PIMORONI_TRACKBALL_ROTATE
  149. trackball_adapt_values(&mouse_report.h, &h_offset);
  150. trackball_adapt_values(&mouse_report.v, &v_offset);
  151. #else
  152. trackball_adapt_values(&mouse_report.h, &v_offset);
  153. trackball_adapt_values(&mouse_report.v, &h_offset);
  154. #endif
  155. mouse_report.x = 0;
  156. mouse_report.y = 0;
  157. } else {
  158. #ifndef PIMORONI_TRACKBALL_ROTATE
  159. trackball_adapt_values(&mouse_report.x, &x_offset);
  160. trackball_adapt_values(&mouse_report.y, &y_offset);
  161. #else
  162. trackball_adapt_values(&mouse_report.x, &y_offset);
  163. trackball_adapt_values(&mouse_report.y, &x_offset);
  164. #endif
  165. mouse_report.h = 0;
  166. mouse_report.v = 0;
  167. }
  168. } else {
  169. debounce--;
  170. }
  171. } else {
  172. trackball_click(true, &mouse_report);
  173. debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES;
  174. }
  175. }
  176. } else {
  177. error_count++;
  178. }
  179. pointing_device_set_report(mouse_report);
  180. pointing_device_send();
  181. throttle = timer_read_fast();
  182. }
  183. }