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.

315 lines
9.6 KiB

  1. /* Copyright 2020 ZSA Technology Labs, Inc <@zsa>
  2. * Copyright 2020 Jack Humbert <jack.humb@gmail.com>
  3. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  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. dprintf("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. dprintf("error hori\n");
  74. } else {
  75. mcp23018_initd = is_launching = true;
  76. }
  77. }
  78. }
  79. void matrix_init(void) {
  80. dprintf("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. // Try to re-init right side
  106. if (!mcp23018_initd) {
  107. if (++mcp23018_reset_loop == 0) {
  108. // if (++mcp23018_reset_loop >= 1300) {
  109. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  110. // this will be approx bit more frequent than once per second
  111. print("trying to reset mcp23018\n");
  112. mcp23018_init();
  113. if (!mcp23018_initd) {
  114. print("left side not responding\n");
  115. } else {
  116. print("left side attached\n");
  117. #ifdef RGB_MATRIX_ENABLE
  118. rgb_matrix_init();
  119. #endif
  120. }
  121. }
  122. }
  123. matrix_row_t data = 0;
  124. // actual matrix
  125. for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) {
  126. // strobe row
  127. switch (row) {
  128. case 0: writePinHigh(B10); break;
  129. case 1: writePinHigh(B11); break;
  130. case 2: writePinHigh(B12); break;
  131. case 3: writePinHigh(B13); break;
  132. case 4: writePinHigh(B14); break;
  133. case 5: writePinHigh(B15); break;
  134. case 6: break; // Left hand has 6 rows
  135. }
  136. // right side
  137. if (mcp23018_initd) {
  138. // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
  139. // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
  140. // select row
  141. mcp23018_tx[0] = 0x12; // GPIOA
  142. mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row
  143. mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row
  144. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
  145. dprintf("error hori\n");
  146. mcp23018_initd = false;
  147. }
  148. // read col
  149. mcp23018_tx[0] = 0x13; // GPIOB
  150. if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, I2C_TIMEOUT)) {
  151. dprintf("error vert\n");
  152. mcp23018_initd = false;
  153. }
  154. data = ~(mcp23018_rx[0] & 0b00111111);
  155. // data = 0x01;
  156. if (matrix_debouncing_right[row] != data) {
  157. matrix_debouncing_right[row] = data;
  158. debouncing_right = true;
  159. debouncing_time_right = timer_read();
  160. changed = true;
  161. }
  162. }
  163. // left side
  164. if (row < ROWS_PER_HAND) {
  165. // i2c comm incur enough wait time
  166. if (!mcp23018_initd) {
  167. // need wait to settle pin state
  168. matrix_io_delay();
  169. }
  170. // read col data
  171. data = (
  172. (readPin(A0) << 0 ) |
  173. (readPin(A1) << 1 ) |
  174. (readPin(A2) << 2 ) |
  175. (readPin(A3) << 3 ) |
  176. (readPin(A6) << 4 ) |
  177. (readPin(A7) << 5 ) |
  178. (readPin(B0) << 6 )
  179. );
  180. // unstrobe row
  181. switch (row) {
  182. case 0: writePinLow(B10); break;
  183. case 1: writePinLow(B11); break;
  184. case 2: writePinLow(B12); break;
  185. case 3: writePinLow(B13); break;
  186. case 4: writePinLow(B14); break;
  187. case 5: writePinLow(B15); break;
  188. case 6: break;
  189. }
  190. if (matrix_debouncing[row] != data) {
  191. matrix_debouncing[row] = data;
  192. debouncing = true;
  193. debouncing_time = timer_read();
  194. changed = true;
  195. }
  196. }
  197. }
  198. // Debounce both hands
  199. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  200. for (int row = 0; row < ROWS_PER_HAND; row++) {
  201. matrix[row] = matrix_debouncing[row];
  202. }
  203. debouncing = false;
  204. }
  205. if (debouncing_right && timer_elapsed(debouncing_time_right) > DEBOUNCE && mcp23018_initd) {
  206. for (int row = 0; row < ROWS_PER_HAND; row++) {
  207. matrix[11 - row] = 0;
  208. for (int col = 0; col < MATRIX_COLS; col++) {
  209. matrix[11 - row] |= ((matrix_debouncing_right[6 - col] & (1 << row) ? 1 : 0) << col);
  210. }
  211. }
  212. debouncing_right = false;
  213. }
  214. matrix_scan_quantum();
  215. return (uint8_t)changed;
  216. }
  217. bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); }
  218. matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  219. void matrix_print(void) {
  220. dprintf("\nr/c 01234567\n");
  221. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  222. dprintf("%X0: ", row);
  223. matrix_row_t data = matrix_get_row(row);
  224. for (int col = 0; col < MATRIX_COLS; col++) {
  225. if (data & (1 << col))
  226. dprintf("1");
  227. else
  228. dprintf("0");
  229. }
  230. dprintf("\n");
  231. }
  232. }
  233. // DO NOT REMOVE
  234. // Needed for proper wake/sleep
  235. void matrix_power_up(void) {
  236. bool temp_launching = is_launching;
  237. // outputs
  238. setPinOutput(B10);
  239. setPinOutput(B11);
  240. setPinOutput(B12);
  241. setPinOutput(B13);
  242. setPinOutput(B14);
  243. setPinOutput(B15);
  244. // inputs
  245. setPinInputLow(A0);
  246. setPinInputLow(A1);
  247. setPinInputLow(A2);
  248. setPinInputLow(A3);
  249. setPinInputLow(A6);
  250. setPinInputLow(A7);
  251. setPinInputLow(B0);
  252. mcp23018_init();
  253. is_launching = temp_launching;
  254. if (!is_launching) {
  255. ML_LED_1(false);
  256. ML_LED_2(false);
  257. ML_LED_3(false);
  258. ML_LED_4(false);
  259. ML_LED_5(false);
  260. ML_LED_6(false);
  261. }
  262. // initialize matrix state: all keys off
  263. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  264. matrix[i] = 0;
  265. }
  266. }