logo

qmk_firmware

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

matrix.c (7688B)


  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 "print.h"
  24. #include <string.h>
  25. #define VIBRATE_LENGTH 50 //Defines number of interrupts motor will vibrate for, must be bigger than 8 for correct operation
  26. volatile uint8_t vibrate = 0; //Trigger vibration in interrupt
  27. static matrix_row_t matrix[MATRIX_ROWS];
  28. const uint8_t SENr[6] = {1, 2, 3, 5, 6, 7};//Maps capacitive pads to pins
  29. const uint8_t SENc[6] = {0, 4, 8, 9, 10, 11};
  30. volatile uint8_t LEDs[6][6] = {{0}};//Stores current LED values
  31. //Read data from the cap touch IC
  32. uint8_t readDataFromTS(uint8_t reg) {
  33. uint8_t rx[1] = { 0 };
  34. if (i2c_read_register(0x1C << 1, reg, rx, 1, 100) == 0) {
  35. return rx[0];
  36. }
  37. return 0;
  38. }
  39. //Write data to cap touch IC
  40. uint8_t writeDataToTS(uint8_t reg, uint8_t data) {
  41. uint8_t tx[2] = { reg, data };
  42. if (i2c_transmit(0x1C << 1, tx, 2, 100) == 0) {
  43. return 1;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. uint8_t checkTSPres(void) {
  49. return (readDataFromTS(0x00) == 0x3E);
  50. }
  51. uint8_t capSetup(void) {
  52. uint8_t temp_return = checkTSPres();
  53. if (temp_return == 1) {
  54. // Perform measurements every 16ms
  55. writeDataToTS(0x08, 1);
  56. // Increase detection integrator value
  57. writeDataToTS(0x0B, 1);
  58. // Oversample to gain two bits for columns
  59. writeDataToTS(0x28, 0x42);
  60. writeDataToTS(0x29, 0x00);
  61. writeDataToTS(0x2A, 0x00);
  62. writeDataToTS(0x2B, 0x00);
  63. writeDataToTS(0x2C, 0x42);
  64. writeDataToTS(0x2D, 0x00);
  65. writeDataToTS(0x2E, 0x00);
  66. writeDataToTS(0x2F, 0x00);
  67. writeDataToTS(0x30, 0x42);
  68. writeDataToTS(0x31, 0x42);
  69. writeDataToTS(0x32, 0x42);
  70. writeDataToTS(0x33, 0x42);
  71. // Recalibration if touch detected for more than 8 seconds n*0.16s
  72. writeDataToTS(0x0C, 50);
  73. // Enable keys and set key groups
  74. writeDataToTS(0x1C, 0x00 | 0x04);
  75. writeDataToTS(0x1D, 0x00 | 0x08);
  76. writeDataToTS(0x1E, 0x00 | 0x08);
  77. writeDataToTS(0x1F, 0x00 | 0x08);
  78. writeDataToTS(0x20, 0x00 | 0x04);
  79. writeDataToTS(0x21, 0x00 | 0x08);
  80. writeDataToTS(0x22, 0x00 | 0x08);
  81. writeDataToTS(0x23, 0x00 | 0x08);
  82. writeDataToTS(0x24, 0x00 | 0x04);
  83. writeDataToTS(0x25, 0x00 | 0x04);
  84. writeDataToTS(0x26, 0x00 | 0x04);
  85. writeDataToTS(0x27, 0x00 | 0x04);
  86. }
  87. return temp_return;
  88. }
  89. __attribute__ ((weak))
  90. void matrix_init_user(void) {}
  91. __attribute__ ((weak))
  92. void matrix_scan_user(void) {}
  93. __attribute__ ((weak))
  94. void matrix_init_kb(void) {
  95. matrix_init_user();
  96. }
  97. __attribute__ ((weak))
  98. void matrix_scan_kb(void) {
  99. matrix_scan_user();
  100. }
  101. void matrix_init(void) {
  102. i2c_init();
  103. //Motor enable
  104. gpio_set_pin_output(E6);
  105. //Motor PWM
  106. gpio_set_pin_output(D7);
  107. //Power LED
  108. gpio_set_pin_output(B7);
  109. gpio_write_pin_high(B7);
  110. //LEDs Columns
  111. gpio_set_pin_output(F7);
  112. gpio_set_pin_output(F6);
  113. gpio_set_pin_output(F5);
  114. gpio_set_pin_output(F4);
  115. gpio_set_pin_output(F1);
  116. gpio_set_pin_output(F0);
  117. //LEDs Rows
  118. gpio_set_pin_output(D6);
  119. gpio_set_pin_output(B4);
  120. gpio_set_pin_output(B5);
  121. gpio_set_pin_output(B6);
  122. gpio_set_pin_output(C6);
  123. gpio_set_pin_output(C7);
  124. //Capacitive Interrupt
  125. gpio_set_pin_input(D2);
  126. capSetup();
  127. writeDataToTS(0x06, 0x12); //Calibrate capacitive touch IC
  128. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  129. matrix_init_kb();
  130. }
  131. uint16_t touchDetectionRoutine(void) {
  132. uint16_t data;
  133. uint8_t temp1, temp2;
  134. temp1 = readDataFromTS(0x04);
  135. temp2 = readDataFromTS(0x03);
  136. data = temp1;
  137. data = (data << 8) | temp2;
  138. return data;
  139. }
  140. //Process raw capacitive data, map pins to rows and columns
  141. void decodeArray(uint16_t dataIn, uint8_t *column, uint8_t *row) {
  142. uint8_t i1 = 20, i2 = 20;
  143. for (uint8_t i = 0; i < 12; i++) {
  144. if ((dataIn & 0b1) == 1) {
  145. if (i1 == 20) {
  146. i1 = i;
  147. } else if (i2 == 20) {
  148. i2 = i;
  149. }
  150. }
  151. dataIn = dataIn >> 1;
  152. }
  153. for (uint8_t j = 0; j < 6; j++) {
  154. if (SENr[j] == i1 || SENr[j] == i2) {
  155. *row = j;
  156. }
  157. if (SENc[j] == i1 || SENc[j] == i2) {
  158. *column = j;
  159. }
  160. }
  161. }
  162. void touchClearCurrentDetections(void) {
  163. readDataFromTS(0x05);
  164. readDataFromTS(0x02);
  165. readDataFromTS(0x03);
  166. readDataFromTS(0x04);
  167. }
  168. //Check interrupt pin
  169. uint8_t isTouchChangeDetected(void) {
  170. return !gpio_read_pin(D2);
  171. }
  172. uint8_t matrix_scan(void) {
  173. if (isTouchChangeDetected()) {
  174. uint16_t dataIn = touchDetectionRoutine();
  175. if ((dataIn & 0b111100010001) > 0 && (dataIn & 0b000011101110) > 0) {
  176. uint8_t column = 10, row = 10;
  177. decodeArray(dataIn, &column, &row);
  178. if (column != 10 && row != 10) {
  179. vibrate = VIBRATE_LENGTH; //Trigger vibration
  180. matrix[row] = _BV(column);
  181. } else {
  182. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  183. }
  184. } else {
  185. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  186. }
  187. touchClearCurrentDetections();
  188. }
  189. for (uint8_t c = 0; c < 6; c++) {
  190. for (uint8_t r = 0; r < 6; r++) {
  191. switch (r) {
  192. case 0: gpio_write_pin(D6, matrix_is_on(r, c)); break;
  193. case 1: gpio_write_pin(B4, matrix_is_on(r, c)); break;
  194. case 2: gpio_write_pin(B5, matrix_is_on(r, c)); break;
  195. case 3: gpio_write_pin(B6, matrix_is_on(r, c)); break;
  196. case 4: gpio_write_pin(C6, matrix_is_on(r, c)); break;
  197. case 5: gpio_write_pin(C7, matrix_is_on(r, c)); break;
  198. }
  199. switch (c) {
  200. case 0: gpio_write_pin(F5, !matrix_is_on(r, c)); break;
  201. case 1: gpio_write_pin(F4, !matrix_is_on(r, c)); break;
  202. case 2: gpio_write_pin(F1, !matrix_is_on(r, c)); break;
  203. case 3: gpio_write_pin(F0, !matrix_is_on(r, c)); break;
  204. case 4: gpio_write_pin(F6, !matrix_is_on(r, c)); break;
  205. case 5: gpio_write_pin(F7, !matrix_is_on(r, c)); break;
  206. }
  207. }
  208. }
  209. if (vibrate == VIBRATE_LENGTH) {
  210. gpio_write_pin_high(E6);
  211. gpio_write_pin_high(D7);
  212. vibrate--;
  213. } else if (vibrate > 0) {
  214. vibrate--;
  215. } else if (vibrate == 0) {
  216. gpio_write_pin_low(D7);
  217. gpio_write_pin_low(E6);
  218. }
  219. matrix_scan_kb();
  220. return 1;
  221. }
  222. bool matrix_is_on(uint8_t row, uint8_t col) {
  223. return (matrix[row] & (1<<col));
  224. }
  225. matrix_row_t matrix_get_row(uint8_t row) {
  226. return matrix[row];
  227. }
  228. void matrix_print(void) {
  229. xprintf("\nr/c 01234567\n");
  230. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  231. xprintf("%X0: ", row);
  232. matrix_row_t data = matrix_get_row(row);
  233. for (int col = 0; col < MATRIX_COLS; col++) {
  234. if (data & (1<<col))
  235. xprintf("1");
  236. else
  237. xprintf("0");
  238. }
  239. xprintf("\n");
  240. }
  241. }