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.

82 lines
2.5 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 "spi_master.h"
  20. #ifndef PMW3360_CPI
  21. # define PMW3360_CPI 1600
  22. #endif
  23. #ifndef PMW3360_CLOCK_SPEED
  24. # define PMW3360_CLOCK_SPEED 70000000
  25. #endif
  26. #ifndef PMW3360_SPI_LSBFIRST
  27. # define PMW3360_SPI_LSBFIRST false
  28. #endif
  29. #ifndef PMW3360_SPI_MODE
  30. # define PMW3360_SPI_MODE 3
  31. #endif
  32. #ifndef PMW3360_SPI_DIVISOR
  33. # ifdef __AVR__
  34. # define PMW3360_SPI_DIVISOR (F_CPU / PMW3360_CLOCK_SPEED)
  35. # else
  36. # define PMW3360_SPI_DIVISOR 64
  37. # endif
  38. #endif
  39. #ifndef ROTATIONAL_TRANSFORM_ANGLE
  40. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  41. #endif
  42. #ifndef PMW3360_CS_PIN
  43. # error "No chip select pin defined -- missing PMW3360_CS_PIN"
  44. #endif
  45. #ifdef CONSOLE_ENABLE
  46. void print_byte(uint8_t byte);
  47. #endif
  48. typedef struct {
  49. int8_t motion;
  50. bool isMotion; // True if a motion is detected.
  51. bool isOnSurface; // True when a chip is on a surface
  52. int16_t dx; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  53. int8_t mdx;
  54. int16_t dy; // displacement on y directions.
  55. int8_t mdy;
  56. } report_pmw_t;
  57. bool spi_start_adv(void);
  58. void spi_stop_adv(void);
  59. spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data);
  60. uint8_t spi_read_adv(uint8_t reg_addr);
  61. bool pmw_spi_init(void);
  62. void pmw_set_cpi(uint16_t cpi);
  63. uint16_t pmw_get_cpi(void);
  64. void pmw_upload_firmware(void);
  65. bool pmw_check_signature(void);
  66. report_pmw_t pmw_read_burst(void);
  67. #define degToRad(angleInDegrees) ((angleInDegrees)*M_PI / 180.0)
  68. #define radToDeg(angleInRadians) ((angleInRadians)*180.0 / M_PI)
  69. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))