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.

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