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.

195 lines
6.3 KiB

adding Hadron v3 keyboard, QWIIC devices support, haptic feedback support (#4462) * add initial support for hadron ver3 * add initial support for hadron ver3 * pull qwiic support for micro_led to be modified for use in hadron's 64x24 ssd1306 oled display * initial work on OLED using qwiic driver * early work to get 128x32 oled working by redefining qwiic micro oled parameters. Currently working, but would affect qwiic's micro oled functionality * moved oled defines to config.h and added ifndef to micro_oled driver * WORKING :D - note, still work in progress to get the start location correct on the 128x32 display. * added equation to automatically calculate display offset based on screen width * adding time-out timer to oled display * changed read lock staus via read_led_state * lock indications fixes * Added scroll lock indication to oled * add support for DRV2605 haptic driver * Improve readabiity of DRV2605 driver. -added typedef for waveform library -added unions for registers * Update keyboards/hadron/ver2/keymaps/default/config.h Co-Authored-By: ishtob <ishtob@gmail.com> * Update keyboards/hadron/ver2/keymaps/default/config.h Co-Authored-By: ishtob <ishtob@gmail.com> * Update keyboards/hadron/ver2/keymaps/default/config.h Co-Authored-By: ishtob <ishtob@gmail.com> * Update keyboards/hadron/ver2/keymaps/default/config.h Co-Authored-By: ishtob <ishtob@gmail.com> * Fixes for PR * PR fixes * fix old persistent layer function to use new set_single_persistent_default_layer * fix issues with changing makefile defines that broken per-key haptic pulse * Comment fixes * Add definable parameter and auto-calibration based on motor choice
5 years ago
  1. #include <string.h>
  2. #include "hal.h"
  3. #include "timer.h"
  4. #include "wait.h"
  5. #include "printf.h"
  6. #include "backlight.h"
  7. #include "matrix.h"
  8. #include "action.h"
  9. #include "keycode.h"
  10. /* matrix state(1:on, 0:off) */
  11. static matrix_row_t matrix[MATRIX_ROWS];
  12. static matrix_row_t matrix_debouncing[MATRIX_COLS];
  13. static bool debouncing = false;
  14. static uint16_t debouncing_time = 0;
  15. static uint8_t encoder_state = 0;
  16. static int8_t encoder_value = 0;
  17. static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
  18. __attribute__ ((weak))
  19. void matrix_init_user(void) {}
  20. __attribute__ ((weak))
  21. void matrix_scan_user(void) {}
  22. __attribute__ ((weak))
  23. void matrix_init_kb(void) {
  24. matrix_init_user();
  25. }
  26. __attribute__ ((weak))
  27. void matrix_scan_kb(void) {
  28. matrix_scan_user();
  29. }
  30. void matrix_init(void) {
  31. printf("matrix init\n");
  32. //debug_matrix = true;
  33. // encoder setup
  34. palSetPadMode(GPIOB, 13, PAL_MODE_INPUT_PULLUP);
  35. palSetPadMode(GPIOB, 14, PAL_MODE_INPUT_PULLUP);
  36. encoder_state = (palReadPad(GPIOB, 13) << 0) | (palReadPad(GPIOB, 14) << 1);
  37. // actual matrix setup
  38. palSetPadMode(GPIOB, 8, PAL_MODE_OUTPUT_PUSHPULL);
  39. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  40. palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
  41. palSetPadMode(GPIOA, 0, PAL_MODE_OUTPUT_PUSHPULL);
  42. palSetPadMode(GPIOA, 1, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOA, 2, PAL_MODE_OUTPUT_PUSHPULL);
  44. palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
  45. palSetPadMode(GPIOA, 3, PAL_MODE_OUTPUT_PUSHPULL);
  46. palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
  47. palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL);
  48. palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL);
  49. palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL);
  50. palSetPadMode(GPIOC, 13, PAL_MODE_OUTPUT_PUSHPULL);
  51. palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
  52. palSetPadMode(GPIOB, 9, PAL_MODE_OUTPUT_PUSHPULL);
  53. palSetPadMode(GPIOC, 15, PAL_MODE_INPUT_PULLDOWN);
  54. palSetPadMode(GPIOC, 14, PAL_MODE_INPUT_PULLDOWN);
  55. palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLDOWN);
  56. palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLDOWN);
  57. palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLDOWN);
  58. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  59. memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t));
  60. matrix_init_quantum();
  61. }
  62. __attribute__ ((weak))
  63. void encoder_update(bool clockwise) { }
  64. #ifndef ENCODER_RESOLUTION
  65. #define ENCODER_RESOLUTION 4
  66. #endif
  67. uint8_t matrix_scan(void) {
  68. // encoder on B13 and B14
  69. encoder_state <<= 2;
  70. encoder_state |= (palReadPad(GPIOB, 13) << 0) | (palReadPad(GPIOB, 14) << 1);
  71. encoder_value += encoder_LUT[encoder_state & 0xF];
  72. if (encoder_value >= ENCODER_RESOLUTION) {
  73. encoder_update(0);
  74. }
  75. if (encoder_value <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  76. encoder_update(1);
  77. }
  78. encoder_value %= ENCODER_RESOLUTION;
  79. // actual matrix
  80. for (int col = 0; col < MATRIX_COLS; col++) {
  81. matrix_row_t data = 0;
  82. // strobe col { PB8, PB2, PB10, PA0, PA1, PA2, PB0, PA3, PB1, PA6, PA7, PB1, PA6, PA7, PB12, PC3, PB11, }
  83. switch (col) {
  84. case 0: palSetPad(GPIOB, 8); break;
  85. case 1: palSetPad(GPIOB, 2); break;
  86. case 2: palSetPad(GPIOB, 10); break;
  87. case 3: palSetPad(GPIOA, 0); break;
  88. case 4: palSetPad(GPIOA, 1); break;
  89. case 5: palSetPad(GPIOA, 2); break;
  90. case 6: palSetPad(GPIOB, 0); break;
  91. case 7: palSetPad(GPIOA, 3); break;
  92. case 8: palSetPad(GPIOB, 1); break;
  93. case 9: palSetPad(GPIOA, 6); break;
  94. case 10: palSetPad(GPIOA, 7); break;
  95. case 11: palSetPad(GPIOB, 12); break;
  96. case 12: palSetPad(GPIOC, 13); break;
  97. case 13: palSetPad(GPIOB, 11); break;
  98. case 14: palSetPad(GPIOB, 9); break;
  99. }
  100. // need wait to settle pin state
  101. wait_us(20);
  102. // read row data { PC15, PC14, PA10, PA9, PA8 }
  103. data = (
  104. (palReadPad(GPIOC, 15) << 0 ) |
  105. (palReadPad(GPIOC, 14) << 1 ) |
  106. (palReadPad(GPIOA, 10) << 2 ) |
  107. (palReadPad(GPIOA, 9) << 3 ) |
  108. (palReadPad(GPIOA, 8) << 4 )
  109. );
  110. // unstrobe col { PB8, PB2, PB10, PA0, PA1, PA2, PB0, PA3, PB1, PA6, PA7, PB1, PA6, PA7, PB12, PC3, PB11, }
  111. switch (col) {
  112. case 0: palClearPad(GPIOB, 8); break;
  113. case 1: palClearPad(GPIOB, 2); break;
  114. case 2: palClearPad(GPIOB, 10); break;
  115. case 3: palClearPad(GPIOA, 0); break;
  116. case 4: palClearPad(GPIOA, 1); break;
  117. case 5: palClearPad(GPIOA, 2); break;
  118. case 6: palClearPad(GPIOB, 0); break;
  119. case 7: palClearPad(GPIOA, 3); break;
  120. case 8: palClearPad(GPIOB, 1); break;
  121. case 9: palClearPad(GPIOA, 6); break;
  122. case 10: palClearPad(GPIOA, 7); break;
  123. case 11: palClearPad(GPIOB, 12); break;
  124. case 12: palClearPad(GPIOC, 13); break;
  125. case 13: palClearPad(GPIOB, 11); break;
  126. case 14: palClearPad(GPIOB, 9); break;
  127. }
  128. if (matrix_debouncing[col] != data) {
  129. matrix_debouncing[col] = data;
  130. debouncing = true;
  131. debouncing_time = timer_read();
  132. }
  133. }
  134. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  135. for (int row = 0; row < MATRIX_ROWS; row++) {
  136. matrix[row] = 0;
  137. for (int col = 0; col < MATRIX_COLS; col++) {
  138. matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col);
  139. }
  140. }
  141. debouncing = false;
  142. }
  143. matrix_scan_quantum();
  144. return 1;
  145. }
  146. bool matrix_is_on(uint8_t row, uint8_t col) {
  147. return (matrix[row] & (1<<col));
  148. }
  149. matrix_row_t matrix_get_row(uint8_t row) {
  150. return matrix[row];
  151. }
  152. void matrix_print(void) {
  153. printf("\nr/c 01234567\n");
  154. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  155. printf("%X0: ", row);
  156. matrix_row_t data = matrix_get_row(row);
  157. for (int col = 0; col < MATRIX_COLS; col++) {
  158. if (data & (1<<col))
  159. printf("1");
  160. else
  161. printf("0");
  162. }
  163. printf("\n");
  164. }
  165. }