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
2.9 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2019 Sunjun Kim
  3. * Copyright 2020 Ploopy Corporation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include <stdint.h>
  20. #include "report.h"
  21. #include "spi_master.h"
  22. #ifndef PMW3360_CPI
  23. # define PMW3360_CPI 1600
  24. #endif
  25. #ifndef PMW3360_CLOCK_SPEED
  26. # define PMW3360_CLOCK_SPEED 2000000
  27. #endif
  28. #ifndef PMW3360_SPI_LSBFIRST
  29. # define PMW3360_SPI_LSBFIRST false
  30. #endif
  31. #ifndef PMW3360_SPI_MODE
  32. # define PMW3360_SPI_MODE 3
  33. #endif
  34. #ifndef PMW3360_SPI_DIVISOR
  35. # ifdef __AVR__
  36. # define PMW3360_SPI_DIVISOR (F_CPU / PMW3360_CLOCK_SPEED)
  37. # else
  38. # define PMW3360_SPI_DIVISOR 64
  39. # endif
  40. #endif
  41. #ifndef ROTATIONAL_TRANSFORM_ANGLE
  42. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  43. #endif
  44. #ifndef PMW3360_CS_PIN
  45. # error "No chip select pin defined -- missing PMW3360_CS_PIN"
  46. #endif
  47. /*
  48. The pmw33660 and pmw3389 use the same registers and timing and such.
  49. The only differences between the two is the firmware used, and the
  50. range for the DPI. So add a semi-secret hack to allow use of the
  51. pmw3389's firmware blob. Also, can set the max cpi range too.
  52. This should work for the 3390 and 3391 too, in theory.
  53. */
  54. #ifndef PMW3360_FIRMWARE_H
  55. # define PMW3360_FIRMWARE_H "pmw3360_firmware.h"
  56. #endif
  57. #ifdef CONSOLE_ENABLE
  58. void print_byte(uint8_t byte);
  59. #endif
  60. typedef struct {
  61. int8_t motion;
  62. bool isMotion; // True if a motion is detected.
  63. bool isOnSurface; // True when a chip is on a surface
  64. int16_t dx; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  65. int8_t mdx;
  66. int16_t dy; // displacement on y directions.
  67. int8_t mdy;
  68. } report_pmw3360_t;
  69. bool spi_start_adv(void);
  70. void spi_stop_adv(void);
  71. spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data);
  72. uint8_t spi_read_adv(uint8_t reg_addr);
  73. bool pmw3360_init(void);
  74. void pmw3360_set_cpi(uint16_t cpi);
  75. uint16_t pmw3360_get_cpi(void);
  76. void pmw3360_upload_firmware(void);
  77. bool pmw3360_check_signature(void);
  78. report_pmw3360_t pmw3360_read_burst(void);
  79. #define degToRad(angleInDegrees) ((angleInDegrees)*M_PI / 180.0)
  80. #define radToDeg(angleInRadians) ((angleInRadians)*180.0 / M_PI)