You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.7 KiB

  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. static uint16_t i2c_timeout_timer;
  25. #ifndef I2C_TIMEOUT
  26. # define I2C_TIMEOUT 100
  27. #endif
  28. #ifndef I2C_WAITCHECK
  29. # define I2C_WAITCHECK 1000
  30. #endif
  31. #ifndef MOUSE_DEBOUNCE
  32. # define MOUSE_DEBOUNCE 5
  33. #endif
  34. void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  35. uint8_t data[] = {0x00, red, green, blue, white};
  36. i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), I2C_TIMEOUT);
  37. }
  38. int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) {
  39. int16_t offset = (int16_t)positive - (int16_t)negative;
  40. int16_t magnitude = (int16_t)(scale * offset * offset * precisionSpeed);
  41. return offset < 0 ? -magnitude : magnitude;
  42. }
  43. void update_member(int8_t* member, int16_t* offset) {
  44. if (*offset > 127) {
  45. *member = 127;
  46. *offset -= 127;
  47. } else if (*offset < -127) {
  48. *member = -127;
  49. *offset += 127;
  50. } else {
  51. *member = *offset;
  52. *offset = 0;
  53. }
  54. }
  55. __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* mouse) {
  56. if (pressed) {
  57. mouse->buttons |= MOUSE_BTN1;
  58. } else {
  59. mouse->buttons &= ~MOUSE_BTN1;
  60. }
  61. }
  62. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  63. if (true) {
  64. xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed);
  65. }
  66. if (!process_record_user(keycode, record)) { return false; }
  67. /* If Mousekeys is disabled, then use handle the mouse button
  68. * keycodes. This makes things simpler, and allows usage of
  69. * the keycodes in a consistent manner. But only do this if
  70. * Mousekeys is not enable, so it's not handled twice.
  71. */
  72. #ifndef MOUSEKEY_ENABLE
  73. if (IS_MOUSEKEY_BUTTON(keycode)) {
  74. report_mouse_t currentReport = pointing_device_get_report();
  75. if (record->event.pressed) {
  76. currentReport.buttons |= 1 << (keycode - KC_MS_BTN1);
  77. } else {
  78. currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1));
  79. }
  80. pointing_device_set_report(currentReport);
  81. pointing_device_send();
  82. }
  83. #endif
  84. return true;
  85. }
  86. void trackball_register_button(bool pressed, enum mouse_buttons button) {
  87. report_mouse_t currentReport = pointing_device_get_report();
  88. if (pressed) {
  89. currentReport.buttons |= button;
  90. } else {
  91. currentReport.buttons &= ~button;
  92. }
  93. pointing_device_set_report(currentReport);
  94. }
  95. float trackball_get_precision(void) { return precisionSpeed; }
  96. void trackball_set_precision(float precision) { precisionSpeed = precision; }
  97. bool trackball_is_scrolling(void) { return scrolling; }
  98. void trackball_set_scrolling(bool scroll) { scrolling = scroll; }
  99. __attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x80, 0x00, 0x00, 0x00); }
  100. void pointing_device_task(void) {
  101. static bool debounce;
  102. static uint16_t debounce_timer;
  103. uint8_t state[5] = {};
  104. if (timer_elapsed(i2c_timeout_timer) > I2C_WAITCHECK) {
  105. if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) {
  106. if (!state[4] && !debounce) {
  107. if (scrolling) {
  108. #ifdef PIMORONI_TRACKBALL_INVERT_X
  109. h_offset += mouse_offset(state[2], state[3], 1);
  110. #else
  111. h_offset -= mouse_offset(state[2], state[3], 1);
  112. #endif
  113. #ifdef PIMORONI_TRACKBALL_INVERT_Y
  114. v_offset += mouse_offset(state[1], state[0], 1);
  115. #else
  116. v_offset -= mouse_offset(state[1], state[0], 1);
  117. #endif
  118. } else {
  119. #ifdef PIMORONI_TRACKBALL_INVERT_X
  120. x_offset -= mouse_offset(state[2], state[3], 5);
  121. #else
  122. x_offset += mouse_offset(state[2], state[3], 5);
  123. #endif
  124. #ifdef PIMORONI_TRACKBALL_INVERT_Y
  125. y_offset -= mouse_offset(state[1], state[0], 5);
  126. #else
  127. y_offset += mouse_offset(state[1], state[0], 5);
  128. #endif
  129. }
  130. } else {
  131. if (state[4]) {
  132. debounce = true;
  133. debounce_timer = timer_read();
  134. }
  135. }
  136. } else {
  137. i2c_timeout_timer = timer_read();
  138. }
  139. }
  140. if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false;
  141. report_mouse_t mouse = pointing_device_get_report();
  142. // trackball_check_click(state[4] & (1 << 7), &mouse);
  143. #ifndef PIMORONI_TRACKBALL_ROTATE
  144. update_member(&mouse.x, &x_offset);
  145. update_member(&mouse.y, &y_offset);
  146. update_member(&mouse.h, &h_offset);
  147. update_member(&mouse.v, &v_offset);
  148. #else
  149. update_member(&mouse.x, &y_offset);
  150. update_member(&mouse.y, &x_offset);
  151. update_member(&mouse.h, &v_offset);
  152. update_member(&mouse.v, &h_offset);
  153. #endif
  154. pointing_device_set_report(mouse);
  155. pointing_device_send();
  156. }