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.

309 lines
9.1 KiB

  1. /* Copyright 2020 ZSA Technology Labs, Inc <@zsa>
  2. * Copyright 2020 Jack Humbert <jack.humb@gmail.com>
  3. * Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <hal.h>
  22. #include "timer.h"
  23. #include "wait.h"
  24. #include "print.h"
  25. #include "matrix.h"
  26. #include "action.h"
  27. #include "keycode.h"
  28. #include <string.h>
  29. #include "moonlander.h"
  30. #include "i2c_master.h"
  31. #include "debounce.h"
  32. /*
  33. #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs
  34. #define MATRIX_COL_PINS { A0, A1, A2, A3, A6, A7, B0 } inputs
  35. */
  36. /* matrix state(1:on, 0:off) */
  37. static matrix_row_t matrix[MATRIX_ROWS];
  38. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  39. static matrix_row_t matrix_debouncing_right[MATRIX_COLS];
  40. static bool debouncing = false;
  41. static uint16_t debouncing_time = 0;
  42. static bool debouncing_right = false;
  43. static uint16_t debouncing_time_right = 0;
  44. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  45. #ifndef MATRIX_IO_DELAY
  46. # define MATRIX_IO_DELAY 20
  47. #endif
  48. extern bool mcp23018_leds[3];
  49. extern bool is_launching;
  50. __attribute__((weak)) void matrix_init_user(void) {}
  51. __attribute__((weak)) void matrix_scan_user(void) {}
  52. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  53. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  54. __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); }
  55. bool mcp23018_initd = false;
  56. static uint8_t mcp23018_reset_loop;
  57. uint8_t mcp23018_tx[3];
  58. uint8_t mcp23018_rx[1];
  59. void mcp23018_init(void) {
  60. i2c_init();
  61. // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
  62. // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
  63. mcp23018_tx[0] = 0x00; // IODIRA
  64. mcp23018_tx[1] = 0b00000000; // A is output
  65. mcp23018_tx[2] = 0b00111111; // B is inputs
  66. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
  67. printf("error hori\n");
  68. } else {
  69. mcp23018_tx[0] = 0x0C; // GPPUA
  70. mcp23018_tx[1] = 0b10000000; // A is not pulled-up
  71. mcp23018_tx[2] = 0b11111111; // B is pulled-up
  72. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
  73. printf("error hori\n");
  74. } else {
  75. mcp23018_initd = is_launching = true;
  76. }
  77. }
  78. }
  79. void matrix_init(void) {
  80. printf("matrix init\n");
  81. // debug_matrix = true;
  82. // outputs
  83. setPinOutput(B10);
  84. setPinOutput(B11);
  85. setPinOutput(B12);
  86. setPinOutput(B13);
  87. setPinOutput(B14);
  88. setPinOutput(B15);
  89. // inputs
  90. setPinInputLow(A0);
  91. setPinInputLow(A1);
  92. setPinInputLow(A2);
  93. setPinInputLow(A3);
  94. setPinInputLow(A6);
  95. setPinInputLow(A7);
  96. setPinInputLow(B0);
  97. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  98. memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  99. memset(matrix_debouncing_right, 0, MATRIX_COLS * sizeof(matrix_row_t));
  100. mcp23018_init();
  101. matrix_init_quantum();
  102. }
  103. uint8_t matrix_scan(void) {
  104. bool changed = false;
  105. matrix_row_t data = 0;
  106. // actual matrix
  107. for (uint8_t row = 0; row < ROWS_PER_HAND; row++) {
  108. // strobe row
  109. switch (row) {
  110. case 0: writePinHigh(B10); break;
  111. case 1: writePinHigh(B11); break;
  112. case 2: writePinHigh(B12); break;
  113. case 3: writePinHigh(B13); break;
  114. case 4: writePinHigh(B14); break;
  115. case 5: writePinHigh(B15); break;
  116. }
  117. // need wait to settle pin state
  118. matrix_io_delay();
  119. // read col data
  120. data = (
  121. (readPin(A0) << 0 ) |
  122. (readPin(A1) << 1 ) |
  123. (readPin(A2) << 2 ) |
  124. (readPin(A3) << 3 ) |
  125. (readPin(A6) << 4 ) |
  126. (readPin(A7) << 5 ) |
  127. (readPin(B0) << 6 )
  128. );
  129. // unstrobe row
  130. switch (row) {
  131. case 0: writePinLow(B10); break;
  132. case 1: writePinLow(B11); break;
  133. case 2: writePinLow(B12); break;
  134. case 3: writePinLow(B13); break;
  135. case 4: writePinLow(B14); break;
  136. case 5: writePinLow(B15); break;
  137. }
  138. if (matrix_debouncing[row] != data) {
  139. matrix_debouncing[row] = data;
  140. debouncing = true;
  141. debouncing_time = timer_read();
  142. changed = true;
  143. }
  144. }
  145. for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) {
  146. // right side
  147. if (!mcp23018_initd) {
  148. if (++mcp23018_reset_loop == 0) {
  149. // if (++mcp23018_reset_loop >= 1300) {
  150. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  151. // this will be approx bit more frequent than once per second
  152. print("trying to reset mcp23018\n");
  153. mcp23018_init();
  154. if (!mcp23018_initd) {
  155. print("left side not responding\n");
  156. } else {
  157. print("left side attached\n");
  158. #ifdef RGB_MATRIX_ENABLE
  159. rgb_matrix_init();
  160. #endif
  161. }
  162. }
  163. }
  164. // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
  165. // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
  166. // select row
  167. mcp23018_tx[0] = 0x12; // GPIOA
  168. mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row
  169. mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row
  170. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
  171. printf("error hori\n");
  172. mcp23018_initd = false;
  173. }
  174. // read col
  175. mcp23018_tx[0] = 0x13; // GPIOB
  176. if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, I2C_TIMEOUT)) {
  177. printf("error vert\n");
  178. mcp23018_initd = false;
  179. }
  180. data = ~(mcp23018_rx[0] & 0b00111111);
  181. // data = 0x01;
  182. if (matrix_debouncing_right[row] != data) {
  183. matrix_debouncing_right[row] = data;
  184. debouncing_right = true;
  185. debouncing_time_right = timer_read();
  186. changed = true;
  187. }
  188. }
  189. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  190. for (int row = 0; row < ROWS_PER_HAND; row++) {
  191. matrix[row] = matrix_debouncing[row];
  192. }
  193. debouncing = false;
  194. }
  195. if (debouncing_right && timer_elapsed(debouncing_time_right) > DEBOUNCE && mcp23018_initd) {
  196. for (int row = 0; row < ROWS_PER_HAND; row++) {
  197. matrix[11 - row] = 0;
  198. for (int col = 0; col < MATRIX_COLS; col++) {
  199. matrix[11 - row] |= ((matrix_debouncing_right[6 - col] & (1 << row) ? 1 : 0) << col);
  200. }
  201. }
  202. debouncing_right = false;
  203. }
  204. matrix_scan_quantum();
  205. return (uint8_t)changed;
  206. }
  207. bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); }
  208. matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  209. void matrix_print(void) {
  210. printf("\nr/c 01234567\n");
  211. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  212. printf("%X0: ", row);
  213. matrix_row_t data = matrix_get_row(row);
  214. for (int col = 0; col < MATRIX_COLS; col++) {
  215. if (data & (1 << col))
  216. printf("1");
  217. else
  218. printf("0");
  219. }
  220. printf("\n");
  221. }
  222. }
  223. // DO NOT REMOVE
  224. // Needed for proper wake/sleep
  225. void matrix_power_up(void) {
  226. bool temp_launching = is_launching;
  227. // outputs
  228. setPinOutput(B10);
  229. setPinOutput(B11);
  230. setPinOutput(B12);
  231. setPinOutput(B13);
  232. setPinOutput(B14);
  233. setPinOutput(B15);
  234. // inputs
  235. setPinInputLow(A0);
  236. setPinInputLow(A1);
  237. setPinInputLow(A2);
  238. setPinInputLow(A3);
  239. setPinInputLow(A6);
  240. setPinInputLow(A7);
  241. setPinInputLow(B0);
  242. mcp23018_init();
  243. is_launching = temp_launching;
  244. if (!is_launching) {
  245. ML_LED_1(false);
  246. ML_LED_2(false);
  247. ML_LED_3(false);
  248. ML_LED_4(false);
  249. ML_LED_5(false);
  250. ML_LED_6(false);
  251. }
  252. // initialize matrix state: all keys off
  253. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  254. matrix[i] = 0;
  255. }
  256. }