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.

366 lines
9.7 KiB

[Keyboard] Georgi Support (#5384) * Working on chording * Working on chording * Got layouts in order * Initial Georgi support * forgot to add keymaps * Updated readme * Update keyboards/georgi/keymaps/template/readme.md Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/template/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Disabled features, updated info * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Fixed info.json
5 years ago
[Keyboard] Georgi Support (#5384) * Working on chording * Working on chording * Got layouts in order * Initial Georgi support * forgot to add keymaps * Updated readme * Update keyboards/georgi/keymaps/template/readme.md Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/template/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Disabled features, updated info * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Fixed info.json
5 years ago
[Keyboard] Georgi Support (#5384) * Working on chording * Working on chording * Got layouts in order * Initial Georgi support * forgot to add keymaps * Updated readme * Update keyboards/georgi/keymaps/template/readme.md Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/georgi.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/keymaps/template/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/matrix.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Disabled features, updated info * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/georgi/config.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Fixed info.json
5 years ago
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "matrix.h"
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "action_layer.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "keymap_steno.h"
  24. #include QMK_KEYBOARD_H
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. // MCP Pin Defs
  29. #define RROW1 (1<<3)
  30. #define RROW2 (1<<2)
  31. #define RROW3 (1<<1)
  32. #define RROW4 (1<<0)
  33. #define COL0 (1<<0)
  34. #define COL1 (1<<1)
  35. #define COL2 (1<<2)
  36. #define COL3 (1<<3)
  37. #define COL4 (1<<4)
  38. #define COL5 (1<<5)
  39. #define COL6 (1<<6)
  40. // ATmega pin defs
  41. #define ROW1 (1<<6)
  42. #define ROW2 (1<<5)
  43. #define ROW3 (1<<4)
  44. #define ROW4 (1<<1)
  45. #define COL7 (1<<0)
  46. #define COL8 (1<<1)
  47. #define COL9 (1<<2)
  48. #define COL10 (1<<3)
  49. #define COL11 (1<<2)
  50. #define COL12 (1<<3)
  51. #define COL13 (1<<6)
  52. // bit masks
  53. #define BMASK (COL7 | COL8 | COL9 | COL10)
  54. #define CMASK (COL13)
  55. #define DMASK (COL11 | COL12)
  56. #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
  57. #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
  58. #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
  59. /* matrix state(1:on, 0:off) */
  60. static matrix_row_t matrix[MATRIX_ROWS];
  61. /*
  62. * matrix state(1:on, 0:off)
  63. * contains the raw values without debounce filtering of the last read cycle.
  64. */
  65. static matrix_row_t raw_matrix[MATRIX_ROWS];
  66. // Debouncing: store for each key the number of scans until it's eligible to
  67. // change. When scanning the matrix, ignore any changes in keys that have
  68. // already changed in the last DEBOUNCE scans.
  69. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  70. static matrix_row_t read_cols(uint8_t row);
  71. static void init_cols(void);
  72. static void unselect_rows(void);
  73. static void select_row(uint8_t row);
  74. static uint8_t mcp23018_reset_loop;
  75. // static uint16_t mcp23018_reset_loop;
  76. __attribute__ ((weak))
  77. void matrix_init_user(void) {}
  78. __attribute__ ((weak))
  79. void matrix_scan_user(void) {}
  80. __attribute__ ((weak))
  81. void matrix_init_kb(void) {
  82. matrix_init_user();
  83. }
  84. __attribute__ ((weak))
  85. void matrix_scan_kb(void) {
  86. matrix_scan_user();
  87. }
  88. inline
  89. uint8_t matrix_rows(void)
  90. {
  91. return MATRIX_ROWS;
  92. }
  93. inline
  94. uint8_t matrix_cols(void)
  95. {
  96. return MATRIX_COLS;
  97. }
  98. void matrix_init(void)
  99. {
  100. // initialize row and col
  101. mcp23018_status = init_mcp23018();
  102. unselect_rows();
  103. init_cols();
  104. // initialize matrix state: all keys off
  105. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  106. matrix[i] = 0;
  107. raw_matrix[i] = 0;
  108. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  109. debounce_matrix[i * MATRIX_COLS + j] = 0;
  110. }
  111. }
  112. matrix_init_quantum();
  113. }
  114. void matrix_power_up(void) {
  115. mcp23018_status = init_mcp23018();
  116. unselect_rows();
  117. init_cols();
  118. // initialize matrix state: all keys off
  119. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  120. matrix[i] = 0;
  121. }
  122. }
  123. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  124. // eligible to change in this scan.
  125. matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
  126. matrix_row_t result = 0;
  127. matrix_row_t change = rawcols ^ raw_matrix[row];
  128. raw_matrix[row] = rawcols;
  129. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  130. if (debounce_matrix[row * MATRIX_COLS + i]) {
  131. --debounce_matrix[row * MATRIX_COLS + i];
  132. } else {
  133. result |= (1 << i);
  134. }
  135. if (change & (1 << i)) {
  136. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  137. }
  138. }
  139. return result;
  140. }
  141. matrix_row_t debounce_read_cols(uint8_t row) {
  142. // Read the row without debouncing filtering and store it for later usage.
  143. matrix_row_t cols = read_cols(row);
  144. // Get the Debounce mask.
  145. matrix_row_t mask = debounce_mask(cols, row);
  146. // debounce the row and return the result.
  147. return (cols & mask) | (matrix[row] & ~mask);;
  148. }
  149. uint8_t matrix_scan(void)
  150. {
  151. // Then the keyboard
  152. if (mcp23018_status) { // if there was an error
  153. if (++mcp23018_reset_loop == 0) {
  154. // if (++mcp23018_reset_loop >= 1300) {
  155. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  156. // this will be approx bit more frequent than once per second
  157. print("trying to reset mcp23018\n");
  158. mcp23018_status = init_mcp23018();
  159. if (mcp23018_status) {
  160. print("left side not responding\n");
  161. } else {
  162. print("left side attached\n");
  163. }
  164. }
  165. }
  166. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  167. select_row(i);
  168. // and select on left hand
  169. select_row(i + MATRIX_ROWS_PER_SIDE);
  170. // we don't need a 30us delay anymore, because selecting a
  171. // left-hand row requires more than 30us for i2c.
  172. // grab cols from left hand
  173. matrix[i] = debounce_read_cols(i);
  174. // grab cols from right hand
  175. matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
  176. unselect_rows();
  177. }
  178. matrix_scan_quantum();
  179. #ifdef DEBUG_MATRIX
  180. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  181. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  182. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  183. #endif
  184. return 1;
  185. }
  186. bool matrix_is_modified(void) // deprecated and evidently not called.
  187. {
  188. return true;
  189. }
  190. inline
  191. bool matrix_is_on(uint8_t row, uint8_t col)
  192. {
  193. return (matrix[row] & ((matrix_row_t)1<<col));
  194. }
  195. inline
  196. matrix_row_t matrix_get_row(uint8_t row)
  197. {
  198. return matrix[row];
  199. }
  200. void matrix_print(void)
  201. {
  202. print("\nr/c 0123456789ABCDEF\n");
  203. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  204. phex(row); print(": ");
  205. pbin_reverse16(matrix_get_row(row));
  206. print("\n");
  207. }
  208. }
  209. uint8_t matrix_key_count(void)
  210. {
  211. uint8_t count = 0;
  212. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  213. count += bitpop16(matrix[i]);
  214. }
  215. return count;
  216. }
  217. // Remember this means ROWS
  218. static void init_cols(void)
  219. {
  220. // init on mcp23018
  221. // not needed, already done as part of init_mcp23018()
  222. // Input with pull-up(DDR:0, PORT:1)
  223. DDRF &= ~FMASK;
  224. PORTF |= FMASK;
  225. }
  226. static matrix_row_t read_cols(uint8_t row)
  227. {
  228. if (row < 7) {
  229. if (mcp23018_status) { // if there was an error
  230. return 0;
  231. } else {
  232. uint8_t data = 0;
  233. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  234. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  235. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  236. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  237. data = ~((uint8_t)mcp23018_status);
  238. mcp23018_status = I2C_STATUS_SUCCESS;
  239. out:
  240. i2c_stop();
  241. #ifdef DEBUG_MATRIX
  242. if (data != 0x00) xprintf("I2C: %d\n", data);
  243. #endif
  244. return data;
  245. }
  246. } else {
  247. /* read from teensy
  248. * bitmask is 0b0111001, but we want the lower four
  249. * we'll return 1s for the top two, but that's harmless.
  250. */
  251. // So I need to confuckulate all this
  252. //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
  253. //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  254. return ~(
  255. (((PINF & ROW4) >> 1)
  256. | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
  257. & 0xF);
  258. }
  259. }
  260. // Row pin configuration
  261. static void unselect_rows(void)
  262. {
  263. // no need to unselect on mcp23018, because the select step sets all
  264. // the other row bits high, and it's not changing to a different
  265. // direction
  266. // Hi-Z(DDR:0, PORT:0) to unselect
  267. DDRB &= ~(BMASK);
  268. PORTB &= ~(BMASK);
  269. DDRC &= ~CMASK;
  270. PORTC &= ~CMASK;
  271. DDRD &= ~DMASK;
  272. PORTD &= ~DMASK;
  273. }
  274. static void select_row(uint8_t row)
  275. {
  276. if (row < 7) {
  277. // select on mcp23018
  278. if (mcp23018_status) { // do nothing on error
  279. } else { // set active row low : 0 // set other rows hi-Z : 1
  280. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  281. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  282. mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  283. out:
  284. i2c_stop();
  285. }
  286. } else {
  287. // Output low(DDR:1, PORT:0) to select
  288. switch (row) {
  289. case 7:
  290. DDRB |= COL7;
  291. PORTB &= ~COL7;
  292. break;
  293. case 8:
  294. DDRB |= COL8;
  295. PORTB &= ~COL8;
  296. break;
  297. case 9:
  298. DDRB |= COL9;
  299. PORTB &= ~COL9;
  300. break;
  301. case 10:
  302. DDRB |= COL10;
  303. PORTB &= ~COL10;
  304. break;
  305. case 11:
  306. DDRD |= COL11;
  307. PORTD &= ~COL11;
  308. break;
  309. case 12:
  310. DDRD |= COL12;
  311. PORTD &= ~COL12;
  312. break;
  313. case 13:
  314. DDRC |= COL13;
  315. PORTC &= ~COL13;
  316. break;
  317. }
  318. }
  319. }