matrix.c (6908B)
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 19 #include "moonlander.h" 20 #include "mcp23018.h" 21 22 #pragma GCC push_options 23 #pragma GCC optimize("-O3") 24 /* 25 #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs 26 #define MATRIX_COL_PINS { A0, A1, A2, A3, A6, A7, B0 } inputs 27 #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs 28 #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs 29 30 */ 31 /* matrix state(1:on, 0:off) */ 32 extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values 33 extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 34 static matrix_row_t raw_matrix_right[MATRIX_ROWS]; 35 36 #define MCP_ROWS_PER_HAND (MATRIX_ROWS / 2) 37 38 extern bool mcp23018_leds[3]; 39 extern bool is_launching; 40 41 static uint16_t mcp23018_reset_loop; 42 uint8_t mcp23018_errors; 43 44 bool io_expander_ready(void) { 45 uint8_t tx; 46 return mcp23018_read_pins(MCP23018_DEFAULT_ADDRESS, mcp23018_PORTA, &tx); 47 } 48 49 void matrix_init_custom(void) { 50 // outputs 51 gpio_set_pin_output(B10); 52 gpio_set_pin_output(B11); 53 gpio_set_pin_output(B12); 54 gpio_set_pin_output(B13); 55 gpio_set_pin_output(B14); 56 gpio_set_pin_output(B15); 57 58 // inputs 59 gpio_set_pin_input_low(A0); 60 gpio_set_pin_input_low(A1); 61 gpio_set_pin_input_low(A2); 62 gpio_set_pin_input_low(A3); 63 gpio_set_pin_input_low(A6); 64 gpio_set_pin_input_low(A7); 65 gpio_set_pin_input_low(B0); 66 67 mcp23018_init(MCP23018_DEFAULT_ADDRESS); 68 mcp23018_errors += !mcp23018_set_config(MCP23018_DEFAULT_ADDRESS, mcp23018_PORTA, 0b00000000); 69 mcp23018_errors += !mcp23018_set_config(MCP23018_DEFAULT_ADDRESS, mcp23018_PORTB, 0b00111111); 70 71 if (!mcp23018_errors) { 72 is_launching = true; 73 } 74 } 75 76 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 77 bool changed = false; 78 // Attempt to reset the mcp23018 if it's not initialized 79 if (mcp23018_errors) { 80 if (++mcp23018_reset_loop > 0x1FFF) { 81 if (io_expander_ready()) { 82 // If we managed to initialize the mcp23018 - we need to reinitialize the matrix / layer state. During an electric discharge the i2c peripherals might be in a weird state. Giving a delay and resetting the MCU allows to recover from this. 83 wait_ms(200); 84 mcu_reset(); 85 } 86 } 87 } 88 89 matrix_row_t data = 0; 90 // actual matrix 91 for (uint8_t row = 0; row <= MCP_ROWS_PER_HAND; row++) { 92 // strobe row 93 switch (row) { 94 case 0: 95 gpio_write_pin_high(B10); 96 break; 97 case 1: 98 gpio_write_pin_high(B11); 99 break; 100 case 2: 101 gpio_write_pin_high(B12); 102 break; 103 case 3: 104 gpio_write_pin_high(B13); 105 break; 106 case 4: 107 gpio_write_pin_high(B14); 108 break; 109 case 5: 110 gpio_write_pin_high(B15); 111 break; 112 case 6: 113 break; // Left hand has 6 rows 114 } 115 116 // Selecting the row on the right side of the keyboard. 117 if (!mcp23018_errors) { 118 // select row 119 mcp23018_errors += !mcp23018_set_output_all(MCP23018_DEFAULT_ADDRESS, (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7), ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7)); 120 } 121 122 // Reading the left side of the keyboard. 123 if (row < MCP_ROWS_PER_HAND) { 124 // i2c comm incur enough wait time 125 if (mcp23018_errors) { 126 // need wait to settle pin state 127 matrix_io_delay(); 128 } 129 // read col data 130 data = ((gpio_read_pin(A0) << 0) | (gpio_read_pin(A1) << 1) | (gpio_read_pin(A2) << 2) | (gpio_read_pin(A3) << 3) | (gpio_read_pin(A6) << 4) | (gpio_read_pin(A7) << 5) | (gpio_read_pin(B0) << 6)); 131 // unstrobe row 132 switch (row) { 133 case 0: 134 gpio_write_pin_low(B10); 135 break; 136 case 1: 137 gpio_write_pin_low(B11); 138 break; 139 case 2: 140 gpio_write_pin_low(B12); 141 break; 142 case 3: 143 gpio_write_pin_low(B13); 144 break; 145 case 4: 146 gpio_write_pin_low(B14); 147 break; 148 case 5: 149 gpio_write_pin_low(B15); 150 break; 151 case 6: 152 break; 153 } 154 155 if (current_matrix[row] != data) { 156 current_matrix[row] = data; 157 changed = true; 158 } 159 } 160 161 // Reading the right side of the keyboard. 162 if (!mcp23018_errors) { 163 uint8_t rx; 164 mcp23018_errors += !mcp23018_read_pins(MCP23018_DEFAULT_ADDRESS, mcp23018_PORTB, &rx); 165 data = ~(rx & 0b00111111); 166 } else { 167 data = 0; 168 } 169 170 if (raw_matrix_right[row] != data) { 171 raw_matrix_right[row] = data; 172 changed = true; 173 } 174 } 175 176 for (uint8_t row = 0; row < MCP_ROWS_PER_HAND; row++) { 177 current_matrix[11 - row] = 0; 178 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 179 current_matrix[11 - row] |= ((raw_matrix_right[6 - col] & (1 << row) ? 1 : 0) << col); 180 } 181 } 182 return changed; 183 } 184 185 // DO NOT REMOVE 186 // Needed for proper wake/sleep 187 void matrix_power_up(void) { 188 bool temp_launching = is_launching; 189 190 matrix_init_custom(); 191 192 is_launching = temp_launching; 193 if (!is_launching) { 194 ML_LED_1(false); 195 ML_LED_2(false); 196 ML_LED_3(false); 197 ML_LED_4(false); 198 ML_LED_5(false); 199 ML_LED_6(false); 200 } 201 202 // initialize matrix state: all keys off 203 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 204 matrix[i] = 0; 205 } 206 } 207 208 bool is_transport_connected(void) { 209 return (bool)(mcp23018_errors == 0); 210 } 211 #pragma GCC pop_options