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.

291 lines
7.3 KiB

  1. /*
  2. MIT License
  3. Copyright (c) 2018, JacoBurge
  4. Adapted for QMK by Jack Humbert in 2018
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #include "matrix.h"
  22. #include "i2c_master.h"
  23. #include "quantum.h"
  24. #define VIBRATE_LENGTH 50 //Defines number of interrupts motor will vibrate for, must be bigger than 8 for correct operation
  25. volatile uint8_t vibrate = 0; //Trigger vibration in interrupt
  26. static matrix_row_t matrix[MATRIX_ROWS];
  27. const uint8_t SENr[6] = {1, 2, 3, 5, 6, 7};//Maps capacitive pads to pins
  28. const uint8_t SENc[6] = {0, 4, 8, 9, 10, 11};
  29. volatile uint8_t LEDs[6][6] = {{0}};//Stores current LED values
  30. //Read data from the cap touch IC
  31. uint8_t readDataFromTS(uint8_t reg) {
  32. uint8_t rx[1] = { 0 };
  33. if (i2c_readReg(0x1C << 1, reg, rx, 1, 100) == 0) {
  34. return rx[0];
  35. }
  36. return 0;
  37. }
  38. //Write data to cap touch IC
  39. uint8_t writeDataToTS(uint8_t reg, uint8_t data) {
  40. uint8_t tx[2] = { reg, data };
  41. if (i2c_transmit(0x1C << 1, tx, 2, 100) == 0) {
  42. return 1;
  43. } else {
  44. return 0;
  45. }
  46. }
  47. uint8_t checkTSPres(void) {
  48. return (readDataFromTS(0x00) == 0x3E);
  49. }
  50. uint8_t capSetup(void) {
  51. uint8_t temp_return = checkTSPres();
  52. if (temp_return == 1) {
  53. // Perform measurements every 16ms
  54. writeDataToTS(0x08, 1);
  55. // Increase detection integrator value
  56. writeDataToTS(0x0B, 1);
  57. // Oversample to gain two bits for columns
  58. writeDataToTS(0x28, 0x42);
  59. writeDataToTS(0x29, 0x00);
  60. writeDataToTS(0x2A, 0x00);
  61. writeDataToTS(0x2B, 0x00);
  62. writeDataToTS(0x2C, 0x42);
  63. writeDataToTS(0x2D, 0x00);
  64. writeDataToTS(0x2E, 0x00);
  65. writeDataToTS(0x2F, 0x00);
  66. writeDataToTS(0x30, 0x42);
  67. writeDataToTS(0x31, 0x42);
  68. writeDataToTS(0x32, 0x42);
  69. writeDataToTS(0x33, 0x42);
  70. // Recalibration if touch detected for more than 8 seconds n*0.16s
  71. writeDataToTS(0x0C, 50);
  72. // Enable keys and set key groups
  73. writeDataToTS(0x1C, 0x00 | 0x04);
  74. writeDataToTS(0x1D, 0x00 | 0x08);
  75. writeDataToTS(0x1E, 0x00 | 0x08);
  76. writeDataToTS(0x1F, 0x00 | 0x08);
  77. writeDataToTS(0x20, 0x00 | 0x04);
  78. writeDataToTS(0x21, 0x00 | 0x08);
  79. writeDataToTS(0x22, 0x00 | 0x08);
  80. writeDataToTS(0x23, 0x00 | 0x08);
  81. writeDataToTS(0x24, 0x00 | 0x04);
  82. writeDataToTS(0x25, 0x00 | 0x04);
  83. writeDataToTS(0x26, 0x00 | 0x04);
  84. writeDataToTS(0x27, 0x00 | 0x04);
  85. }
  86. return temp_return;
  87. }
  88. __attribute__ ((weak))
  89. void matrix_init_user(void) {}
  90. __attribute__ ((weak))
  91. void matrix_scan_user(void) {}
  92. __attribute__ ((weak))
  93. void matrix_init_kb(void) {
  94. matrix_init_user();
  95. }
  96. __attribute__ ((weak))
  97. void matrix_scan_kb(void) {
  98. matrix_scan_user();
  99. }
  100. void matrix_init(void) {
  101. i2c_init();
  102. //Motor enable
  103. setPinOutput(E6);
  104. //Motor PWM
  105. setPinOutput(D7);
  106. //Power LED
  107. setPinOutput(B7);
  108. writePinHigh(B7);
  109. //LEDs Columns
  110. setPinOutput(F7);
  111. setPinOutput(F6);
  112. setPinOutput(F5);
  113. setPinOutput(F4);
  114. setPinOutput(F1);
  115. setPinOutput(F0);
  116. //LEDs Rows
  117. setPinOutput(D6);
  118. setPinOutput(B4);
  119. setPinOutput(B5);
  120. setPinOutput(B6);
  121. setPinOutput(C6);
  122. setPinOutput(C7);
  123. //Capacitive Interrupt
  124. setPinInput(D2);
  125. capSetup();
  126. writeDataToTS(0x06, 0x12); //Calibrate capacitive touch IC
  127. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  128. matrix_init_quantum();
  129. }
  130. uint16_t touchDetectionRoutine(void) {
  131. uint16_t data;
  132. uint8_t temp1, temp2;
  133. temp1 = readDataFromTS(0x04);
  134. temp2 = readDataFromTS(0x03);
  135. data = temp1;
  136. data = (data << 8) | temp2;
  137. return data;
  138. }
  139. //Process raw capacitive data, map pins to rows and columns
  140. void decodeArray(uint16_t dataIn, uint8_t *column, uint8_t *row) {
  141. uint8_t i1 = 20, i2 = 20;
  142. for (uint8_t i = 0; i < 12; i++) {
  143. if ((dataIn & 0b1) == 1) {
  144. if (i1 == 20) {
  145. i1 = i;
  146. } else if (i2 == 20) {
  147. i2 = i;
  148. }
  149. }
  150. dataIn = dataIn >> 1;
  151. }
  152. for (uint8_t j = 0; j < 6; j++) {
  153. if (SENr[j] == i1 || SENr[j] == i2) {
  154. *row = j;
  155. }
  156. if (SENc[j] == i1 || SENc[j] == i2) {
  157. *column = j;
  158. }
  159. }
  160. }
  161. void touchClearCurrentDetections(void) {
  162. readDataFromTS(0x05);
  163. readDataFromTS(0x02);
  164. readDataFromTS(0x03);
  165. readDataFromTS(0x04);
  166. }
  167. //Check interrupt pin
  168. uint8_t isTouchChangeDetected(void) {
  169. return !readPin(D2);
  170. }
  171. uint8_t matrix_scan(void) {
  172. if (isTouchChangeDetected()) {
  173. uint16_t dataIn = touchDetectionRoutine();
  174. if ((dataIn & 0b111100010001) > 0 && (dataIn & 0b000011101110) > 0) {
  175. uint8_t column = 10, row = 10;
  176. decodeArray(dataIn, &column, &row);
  177. if (column != 10 && row != 10) {
  178. vibrate = VIBRATE_LENGTH; //Trigger vibration
  179. matrix[row] = _BV(column);
  180. } else {
  181. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  182. }
  183. } else {
  184. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  185. }
  186. touchClearCurrentDetections();
  187. }
  188. for (uint8_t c = 0; c < 6; c++) {
  189. for (uint8_t r = 0; r < 6; r++) {
  190. switch (r) {
  191. case 0: writePin(D6, matrix_is_on(r, c)); break;
  192. case 1: writePin(B4, matrix_is_on(r, c)); break;
  193. case 2: writePin(B5, matrix_is_on(r, c)); break;
  194. case 3: writePin(B6, matrix_is_on(r, c)); break;
  195. case 4: writePin(C6, matrix_is_on(r, c)); break;
  196. case 5: writePin(C7, matrix_is_on(r, c)); break;
  197. }
  198. switch (c) {
  199. case 0: writePin(F5, !matrix_is_on(r, c)); break;
  200. case 1: writePin(F4, !matrix_is_on(r, c)); break;
  201. case 2: writePin(F1, !matrix_is_on(r, c)); break;
  202. case 3: writePin(F0, !matrix_is_on(r, c)); break;
  203. case 4: writePin(F6, !matrix_is_on(r, c)); break;
  204. case 5: writePin(F7, !matrix_is_on(r, c)); break;
  205. }
  206. }
  207. }
  208. if (vibrate == VIBRATE_LENGTH) {
  209. writePinHigh(E6);
  210. writePinHigh(D7);
  211. vibrate--;
  212. } else if (vibrate > 0) {
  213. vibrate--;
  214. } else if (vibrate == 0) {
  215. writePinLow(D7);
  216. writePinLow(E6);
  217. }
  218. matrix_scan_quantum();
  219. return 1;
  220. }
  221. bool matrix_is_on(uint8_t row, uint8_t col) {
  222. return (matrix[row] & (1<<col));
  223. }
  224. matrix_row_t matrix_get_row(uint8_t row) {
  225. return matrix[row];
  226. }
  227. void matrix_print(void) {
  228. printf("\nr/c 01234567\n");
  229. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  230. printf("%X0: ", row);
  231. matrix_row_t data = matrix_get_row(row);
  232. for (int col = 0; col < MATRIX_COLS; col++) {
  233. if (data & (1<<col))
  234. printf("1");
  235. else
  236. printf("0");
  237. }
  238. printf("\n");
  239. }
  240. }