logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

pmw33xx_common.h (6582B)


  1. // Copyright 2022 Pablo Martinez (@elpekenin)
  2. // Copyright 2022 Daniel Kao (dkao)
  3. // Copyright 2022 Stefan Kerkmann (KarlK90)
  4. // Copyright 2022 Ulrich Spörlein (@uqs)
  5. // Copyright 2021 Alabastard (@Alabastard-64)
  6. // Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  7. // Copyright 2019 Sunjun Kim
  8. // Copyright 2020 Ploopy Corporation
  9. // SPDX-License-Identifier: GPL-2.0-or-later
  10. #pragma once
  11. #include "compiler_support.h"
  12. #include "keyboard.h"
  13. #include <stdint.h>
  14. #include "spi_master.h"
  15. #include "util.h"
  16. #include "pointing_device.h"
  17. #if defined(POINTING_DEVICE_DRIVER_pmw3360)
  18. # include "pmw3360.h"
  19. #elif defined(POINTING_DEVICE_DRIVER_pmw3389)
  20. # include "pmw3389.h"
  21. #endif
  22. typedef struct __attribute__((packed)) {
  23. union {
  24. struct {
  25. bool capture_from_raw_data : 1; // FRAME_RData_1st
  26. uint8_t operation_mode : 2; // OP_MODE
  27. bool is_lifted : 1; // Lift_stat
  28. bool raw_data_grab_is_raw_data : 1; // RData_1st
  29. uint8_t _reserved : 2; // 1 + Reserved
  30. bool is_motion : 1; // MOT
  31. } b;
  32. uint8_t w;
  33. } motion;
  34. uint8_t observation;
  35. int16_t delta_x; // displacement on x directions. Unit: Count. (CPI * Count = Inch value)
  36. int16_t delta_y; // displacement on y directions.
  37. } pmw33xx_report_t;
  38. STATIC_ASSERT(sizeof(pmw33xx_report_t) == 6, "pmw33xx_report_t must be 6 bytes in size");
  39. STATIC_ASSERT(sizeof((pmw33xx_report_t){0}.motion) == 1, "pmw33xx_report_t.motion must be 1 byte in size");
  40. #if !defined(PMW33XX_CLOCK_SPEED)
  41. # define PMW33XX_CLOCK_SPEED 2000000
  42. #endif
  43. #if !defined(PMW33XX_SPI_DIVISOR)
  44. # ifdef __AVR__
  45. # define PMW33XX_SPI_DIVISOR (F_CPU / PMW33XX_CLOCK_SPEED)
  46. # else
  47. # define PMW33XX_SPI_DIVISOR 64
  48. # endif
  49. #endif
  50. #if !defined(PMW33XX_LIFTOFF_DISTANCE)
  51. # define PMW33XX_LIFTOFF_DISTANCE 0x02
  52. #endif
  53. #if !defined(ROTATIONAL_TRANSFORM_ANGLE)
  54. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  55. #endif
  56. #if ROTATIONAL_TRANSFORM_ANGLE > 127 || ROTATIONAL_TRANSFORM_ANGLE < (-127)
  57. # error ROTATIONAL_TRANSFORM_ANGLE has to be in the range of +/- 127 for all PMW33XX sensors.
  58. #endif
  59. // Support single and plural spellings
  60. #ifndef PMW33XX_CS_PINS
  61. # ifndef PMW33XX_CS_PIN
  62. # ifdef POINTING_DEVICE_CS_PIN
  63. # define PMW33XX_CS_PIN POINTING_DEVICE_CS_PIN
  64. # define PMW33XX_CS_PINS \
  65. { PMW33XX_CS_PIN }
  66. # else
  67. # error "No chip select pin defined -- missing PMW33XX_CS_PIN or PMW33XX_CS_PINS"
  68. # endif
  69. # else
  70. # define PMW33XX_CS_PINS \
  71. { PMW33XX_CS_PIN }
  72. # endif
  73. #endif
  74. // Support single spelling and default to be the same as left side
  75. #if !defined(PMW33XX_CS_PINS_RIGHT)
  76. # if !defined(PMW33XX_CS_PIN_RIGHT)
  77. # define PMW33XX_CS_PIN_RIGHT PMW33XX_CS_PIN
  78. # endif
  79. # define PMW33XX_CS_PINS_RIGHT \
  80. { PMW33XX_CS_PIN_RIGHT }
  81. #endif
  82. // Defines so the old variable names are swapped by the appropiate value on each half
  83. #define cs_pins (is_keyboard_left() ? cs_pins_left : cs_pins_right)
  84. #define in_burst (is_keyboard_left() ? in_burst_left : in_burst_right)
  85. #define pmw33xx_number_of_sensors (is_keyboard_left() ? ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS) : ARRAY_SIZE((pin_t[])PMW33XX_CS_PINS_RIGHT))
  86. #if PMW33XX_CPI > PMW33XX_CPI_MAX || PMW33XX_CPI < PMW33XX_CPI_MIN || (PMW33XX_CPI % PMW33XX_CPI_STEP) != 0U
  87. # pragma message "PMW33XX_CPI has to be in the range of " STR(PMW33XX_CPI_MAX) "-" STR(PMW33XX_CPI_MIN) " in increments of " STR(PMW33XX_CPI_STEP) ". But it is " STR(PMW33XX_CPI) "."
  88. # error Use correct PMW33XX_CPI value.
  89. #endif
  90. #define CONSTRAIN(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  91. #define pmw3360_pointing_device_driver pmw33xx_pointing_device_driver;
  92. #define pmw3389_pointing_device_driver pmw33xx_pointing_device_driver;
  93. const pointing_device_driver_t pmw33xx_pointing_device_driver;
  94. /**
  95. * @brief Initializes the given sensor so it is in a working state and ready to
  96. * be polled for data.
  97. *
  98. * @param sensor Index of the sensors chip select pin
  99. * @return true Initialization was a success
  100. * @return false Initialization failed, do not proceed operation
  101. */
  102. bool __attribute__((cold)) pmw33xx_init(uint8_t sensor);
  103. /**
  104. * @brief Gets the currently set CPI value from the sensor. CPI is often
  105. * refereed to as the sensors sensitivity.
  106. *
  107. * @param sensor Index of the sensors chip select pin
  108. * @return uint16_t Current CPI value of the sensor
  109. */
  110. uint16_t pmw33xx_get_cpi(uint8_t sensor);
  111. /**
  112. * @brief Sets the given CPI value for the given PMW33XX sensor. CIP is often
  113. * refereed to as the sensors sensitivity. Values outside of the allow range are
  114. * constrained into legal values.
  115. *
  116. * @param sensor Index of the sensors chip select pin
  117. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  118. */
  119. void pmw33xx_set_cpi(uint8_t sensor, uint16_t cpi);
  120. /**
  121. * @brief Sets the given CPI value to all registered PMW33XX sensors. CPI is
  122. * often refereed to as the sensors sensitivity. Values outside of the allow
  123. * range are constrained into legal values.
  124. *
  125. * @param sensor Index of the sensors chip select pin
  126. * @param cpi CPI value to set, legal range depends on the PMW sensor type
  127. */
  128. void pmw33xx_set_cpi_all_sensors(uint16_t cpi);
  129. /**
  130. * @brief Reads and clears the current delta, and motion register values on the
  131. * given sensor.
  132. *
  133. * @param sensor Index of the sensors chip select pin
  134. * @return pmw33xx_report_t Current values of the sensor, if errors occurred all
  135. * fields are set to zero
  136. */
  137. pmw33xx_report_t pmw33xx_read_burst(uint8_t sensor);
  138. /**
  139. * @brief Read one byte of data from the given register on the sensor
  140. *
  141. * @param sensor Index of the sensors chip select pin
  142. * @param reg_addr Register address to read from
  143. * @return uint8_t
  144. */
  145. uint8_t pmw33xx_read(uint8_t sensor, uint8_t reg_addr);
  146. /**
  147. * @brief Writes one byte of data to the given register on the sensor
  148. *
  149. * @param sensor Index of the sensors chip select pin
  150. * @param reg_addr Registers address to write to
  151. * @param data Data to write to the register
  152. * @return true Write was a success
  153. * @return false Write failed, do not proceed operation
  154. */
  155. bool pmw33xx_write(uint8_t sensor, uint8_t reg_addr, uint8_t data);
  156. void pmw33xx_init_wrapper(void);
  157. void pmw33xx_set_cpi_wrapper(uint16_t cpi);
  158. uint16_t pmw33xx_get_cpi_wrapper(void);
  159. report_mouse_t pmw33xx_get_report(report_mouse_t mouse_report);