15 * Column pins are input with internal pull-down. Row pins are output and strobe with high.
16 * Key is high or 1 when it turns on.
18 * col: { PTD1, PTD2, PTD3, PTD4, PTD5, PTD6, PTD7 }
19 * row: { PTB0, PTB1, PTB2, PTB3, PTB16, PTB17, PTC4, PTC5, PTD0 }
21 static gpio_t col[MATRIX_COLS];
22 static gpio_t row[MATRIX_ROWS];
24 /* matrix state(1:on, 0:off) */
25 static matrix_row_t matrix[MATRIX_ROWS];
26 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
27 static bool debouncing = false;
28 static uint16_t debouncing_time = 0;
31 void matrix_init(void)
34 gpio_init_in_ex(&col[0], PTD1, PullDown);
35 gpio_init_in_ex(&col[1], PTD2, PullDown);
36 gpio_init_in_ex(&col[2], PTD3, PullDown);
37 gpio_init_in_ex(&col[3], PTD4, PullDown);
38 gpio_init_in_ex(&col[4], PTD5, PullDown);
39 gpio_init_in_ex(&col[5], PTD6, PullDown);
40 gpio_init_in_ex(&col[6], PTD7, PullDown);
44 gpio_init_out_ex(&row[0], PTB0, 0);
45 gpio_init_out_ex(&row[1], PTB1, 0);
46 gpio_init_out_ex(&row[2], PTB2, 0);
47 gpio_init_out_ex(&row[3], PTB3, 0);
48 gpio_init_out_ex(&row[4], PTB16, 0);
49 gpio_init_out_ex(&row[5], PTB17, 0);
50 gpio_init_out_ex(&row[6], PTC4, 0);
51 gpio_init_out_ex(&row[7], PTC5, 0);
52 gpio_init_out_ex(&row[8], PTD0, 0);
54 gpio_init_out_ex(&row[0], PTC0, 0);
55 gpio_init_out_ex(&row[1], PTC1, 0);
56 gpio_init_out_ex(&row[2], PTC2, 0);
57 gpio_init_out_ex(&row[3], PTC3, 0);
58 gpio_init_out_ex(&row[4], PTC4, 0);
59 gpio_init_out_ex(&row[5], PTC5, 0);
60 gpio_init_out_ex(&row[6], PTC6, 0);
61 gpio_init_out_ex(&row[7], PTC7, 0);
62 gpio_init_out_ex(&row[8], PTD0, 0);
67 uint8_t matrix_scan(void)
69 for (int i = 0; i < MATRIX_ROWS; i++) {
72 gpio_write(&row[i], 1);
73 wait_us(1); // need wait to settle pin state
74 for (int j = 0; j < MATRIX_COLS; j++) {
75 if (gpio_read(&col[j])) {
79 gpio_write(&row[i], 0);
81 if (matrix_debouncing[i] != r) {
82 matrix_debouncing[i] = r;
84 debouncing_time = timer_read();
88 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
89 for (int i = 0; i < MATRIX_ROWS; i++) {
90 matrix[i] = matrix_debouncing[i];
99 for (int i = 0; i < MATRIX_ROWS; i++) {
100 matrix[i] = matrix_debouncing[i];
108 bool matrix_is_on(uint8_t row, uint8_t col)
110 return (matrix[row] & (1<<col));
113 matrix_row_t matrix_get_row(uint8_t row)
118 void matrix_print(void)