pimoroni_trackball.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. float pimoroni_trackball_get_precision(void) { return ((float)precision / 128); }
  34. void pimoroni_trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); }
  35. void pimoroni_trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  36. uint8_t data[4] = {r, g, b, w};
  37. __attribute__((unused)) i2c_status_t status = i2c_writeReg(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LED_RED, data, sizeof(data), PIMORONI_TRACKBALL_TIMEOUT);
  38. #ifdef CONSOLE_ENABLE
  39. if (debug_mouse) dprintf("Trackball RGBW i2c_status_t: %d\n", status);
  40. #endif
  41. }
  42. i2c_status_t read_pimoroni_trackball(pimoroni_data_t* data) {
  43. i2c_status_t status = i2c_readReg(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), PIMORONI_TRACKBALL_TIMEOUT);
  44. #ifdef CONSOLE_ENABLE
  45. if (debug_mouse) {
  46. static uint16_t d_timer;
  47. if (timer_elapsed(d_timer) > PIMORONI_TRACKBALL_DEBUG_INTERVAL) {
  48. 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);
  49. d_timer = timer_read();
  50. }
  51. }
  52. #endif
  53. return status;
  54. }
  55. __attribute__((weak)) void pimironi_trackball_device_init(void) {
  56. i2c_init();
  57. pimoroni_trackball_set_rgbw(0x00, 0x00, 0x00, 0x00);
  58. }
  59. int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) {
  60. uint8_t offset = 0;
  61. bool isnegative = false;
  62. if (negative_dir > positive_dir) {
  63. offset = negative_dir - positive_dir;
  64. isnegative = true;
  65. } else {
  66. offset = positive_dir - negative_dir;
  67. }
  68. uint16_t magnitude = (scale * offset * offset * precision) >> 7;
  69. return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude);
  70. }
  71. void pimoroni_trackball_adapt_values(int8_t* mouse, int16_t* offset) {
  72. if (*offset > 127) {
  73. *mouse = 127;
  74. *offset -= 127;
  75. } else if (*offset < -127) {
  76. *mouse = -127;
  77. *offset += 127;
  78. } else {
  79. *mouse = *offset;
  80. *offset = 0;
  81. }
  82. }