logo

qmk_firmware

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

paw3204.c (5118B)


  1. /* Copyright 2021 Gompa (@Gompa)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // https://github.com/shinoaliceKabocha/choco60_track/tree/master/keymaps/default
  17. #include "paw3204.h"
  18. #include "wait.h"
  19. #include "debug.h"
  20. #include "gpio.h"
  21. #include "pointing_device_internal.h"
  22. #define REG_PID1 0x00
  23. #define REG_PID2 0x01
  24. #define REG_STAT 0x02
  25. #define REG_X 0x03
  26. #define REG_Y 0x04
  27. #define REG_SETUP 0x06
  28. #define REG_IMGQUAL 0x07
  29. #define REG_IMGREC 0x0E
  30. #define REG_IMGTRASH 0x0D
  31. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  32. // CPI values
  33. enum cpi_values {
  34. CPI400, // 0b000
  35. CPI500, // 0b001
  36. CPI600, // 0b010
  37. CPI800, // 0b011
  38. CPI1000, // 0b100
  39. CPI1200, // 0b101
  40. CPI1600, // 0b110
  41. };
  42. uint8_t paw3204_serial_read(void);
  43. void paw3204_serial_write(uint8_t reg_addr);
  44. uint8_t paw3204_read_reg(uint8_t reg_addr);
  45. void paw3204_write_reg(uint8_t reg_addr, uint8_t data);
  46. const pointing_device_driver_t paw3204_pointing_device_driver = {
  47. .init = paw3204_init,
  48. .get_report = paw3204_get_report,
  49. .set_cpi = paw3204_set_cpi,
  50. .get_cpi = paw3204_get_cpi,
  51. };
  52. void paw3204_init(void) {
  53. gpio_set_pin_output(PAW3204_SCLK_PIN); // setclockpin to output
  54. gpio_set_pin_input_high(PAW3204_SDIO_PIN); // set datapin input high
  55. paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi
  56. wait_us(5);
  57. paw3204_read_reg(0x00); // read id
  58. paw3204_read_reg(0x01); // read id2
  59. // PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600
  60. paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold
  61. }
  62. uint8_t paw3204_serial_read(void) {
  63. gpio_set_pin_input(PAW3204_SDIO_PIN);
  64. uint8_t byte = 0;
  65. for (uint8_t i = 0; i < 8; ++i) {
  66. gpio_write_pin_low(PAW3204_SCLK_PIN);
  67. wait_us(1);
  68. byte = (byte << 1) | gpio_read_pin(PAW3204_SDIO_PIN);
  69. gpio_write_pin_high(PAW3204_SCLK_PIN);
  70. wait_us(1);
  71. }
  72. return byte;
  73. }
  74. void paw3204_serial_write(uint8_t data) {
  75. gpio_write_pin_low(PAW3204_SDIO_PIN);
  76. gpio_set_pin_output(PAW3204_SDIO_PIN);
  77. for (int8_t b = 7; b >= 0; b--) {
  78. gpio_write_pin_low(PAW3204_SCLK_PIN);
  79. if (data & (1 << b)) {
  80. gpio_write_pin_high(PAW3204_SDIO_PIN);
  81. } else {
  82. gpio_write_pin_low(PAW3204_SDIO_PIN);
  83. }
  84. gpio_write_pin_high(PAW3204_SCLK_PIN);
  85. }
  86. wait_us(4);
  87. }
  88. report_paw3204_t paw3204_read(void) {
  89. report_paw3204_t data = {0};
  90. data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field)
  91. data.x = (int8_t)paw3204_read_reg(REG_X);
  92. data.y = (int8_t)paw3204_read_reg(REG_Y);
  93. return data;
  94. }
  95. void paw3204_write_reg(uint8_t reg_addr, uint8_t data) {
  96. paw3204_serial_write(0b10000000 | reg_addr);
  97. paw3204_serial_write(data);
  98. }
  99. uint8_t paw3204_read_reg(uint8_t reg_addr) {
  100. paw3204_serial_write(reg_addr);
  101. wait_us(5);
  102. return paw3204_serial_read();
  103. }
  104. void paw3204_set_cpi(uint16_t cpi) {
  105. uint8_t cpival = CPI1000;
  106. if (cpi <= 450) {
  107. cpival = CPI400;
  108. } else if (cpi <= 550) {
  109. cpival = CPI500;
  110. } else if (cpi <= 700) {
  111. cpival = CPI600;
  112. } else if (cpi <= 900) {
  113. cpival = CPI800;
  114. } else if (cpi <= 1100) {
  115. cpival = CPI1000;
  116. } else if (cpi <= 1400) {
  117. cpival = CPI1200;
  118. } else if (cpi > 1400) {
  119. cpival = CPI1600;
  120. }
  121. paw3204_write_reg(REG_SETUP, cpival);
  122. }
  123. uint16_t paw3204_get_cpi(void) {
  124. uint16_t cpival = 1000;
  125. switch (paw3204_read_reg(REG_SETUP) & 0b111) {
  126. case CPI400:
  127. cpival = 400;
  128. break;
  129. case CPI500:
  130. cpival = 500;
  131. break;
  132. case CPI600:
  133. cpival = 600;
  134. break;
  135. case CPI800:
  136. cpival = 800;
  137. break;
  138. case CPI1000:
  139. cpival = 1000;
  140. break;
  141. case CPI1200:
  142. cpival = 1200;
  143. break;
  144. case CPI1600:
  145. cpival = 1600;
  146. break;
  147. }
  148. return cpival;
  149. }
  150. uint8_t read_pid_paw3204(void) {
  151. return paw3204_read_reg(REG_PID1);
  152. }
  153. report_mouse_t paw3204_get_report(report_mouse_t mouse_report) {
  154. report_paw3204_t data = paw3204_read();
  155. if (data.isMotion) {
  156. pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
  157. mouse_report.x = data.x;
  158. mouse_report.y = data.y;
  159. }
  160. return mouse_report;
  161. }