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.

110 lines
3.8 KiB

  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. #include "debug.h"
  21. #include "timer.h"
  22. // clang-format off
  23. #define PIMORONI_TRACKBALL_REG_LED_RED 0x00
  24. #define PIMORONI_TRACKBALL_REG_LED_GRN 0x01
  25. #define PIMORONI_TRACKBALL_REG_LED_BLU 0x02
  26. #define PIMORONI_TRACKBALL_REG_LED_WHT 0x03
  27. #define PIMORONI_TRACKBALL_REG_LEFT 0x04
  28. #define PIMORONI_TRACKBALL_REG_RIGHT 0x05
  29. #define PIMORONI_TRACKBALL_REG_UP 0x06
  30. #define PIMORONI_TRACKBALL_REG_DOWN 0x07
  31. // clang-format on
  32. static uint16_t precision = 128;
  33. uint16_t pimoroni_trackball_get_cpi(void) {
  34. return (precision * 125);
  35. }
  36. /**
  37. * @brief Sets the scaling value for pimoroni trackball
  38. *
  39. * Sets a scaling value for pimoroni trackball to allow runtime adjustment. This isn't used by the sensor and is an
  40. * approximation so the functions are consistent across drivers.
  41. *
  42. * NOTE: This rounds down to the nearest number divisable by 125 that's a positive integer, values below 125 are clamped to 125.
  43. *
  44. * @param cpi uint16_t
  45. */
  46. void pimoroni_trackball_set_cpi(uint16_t cpi) {
  47. if (cpi < 249) {
  48. precision = 1;
  49. } else {
  50. precision = (cpi - (cpi % 125)) / 125;
  51. }
  52. }
  53. void pimoroni_trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  54. uint8_t data[4] = {r, g, b, w};
  55. __attribute__((unused)) i2c_status_t status = i2c_writeReg(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LED_RED, data, sizeof(data), PIMORONI_TRACKBALL_TIMEOUT);
  56. #ifdef CONSOLE_ENABLE
  57. if (debug_mouse) dprintf("Trackball RGBW i2c_status_t: %d\n", status);
  58. #endif
  59. }
  60. i2c_status_t read_pimoroni_trackball(pimoroni_data_t* data) {
  61. i2c_status_t status = i2c_readReg(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), PIMORONI_TRACKBALL_TIMEOUT);
  62. #ifdef CONSOLE_ENABLE
  63. if (debug_mouse) {
  64. static uint16_t d_timer;
  65. if (timer_elapsed(d_timer) > PIMORONI_TRACKBALL_DEBUG_INTERVAL) {
  66. dprintf("Trackball READ i2c_status_t: %d L: %d R: %d Up: %d D: %d SW: %d\n", status, data->left, data->right, data->up, data->down, data->click);
  67. d_timer = timer_read();
  68. }
  69. }
  70. #endif
  71. return status;
  72. }
  73. __attribute__((weak)) void pimoroni_trackball_device_init(void) {
  74. i2c_init();
  75. pimoroni_trackball_set_rgbw(0x00, 0x00, 0x00, 0x00);
  76. }
  77. int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) {
  78. uint8_t offset = 0;
  79. bool isnegative = false;
  80. if (negative_dir > positive_dir) {
  81. offset = negative_dir - positive_dir;
  82. isnegative = true;
  83. } else {
  84. offset = positive_dir - negative_dir;
  85. }
  86. uint16_t magnitude = (scale * offset * offset * precision) >> 7;
  87. return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude);
  88. }
  89. void pimoroni_trackball_adapt_values(int8_t* mouse, int16_t* offset) {
  90. if (*offset > 127) {
  91. *mouse = 127;
  92. *offset -= 127;
  93. } else if (*offset < -127) {
  94. *mouse = -127;
  95. *offset += 127;
  96. } else {
  97. *mouse = *offset;
  98. *offset = 0;
  99. }
  100. }