diff options
Diffstat (limited to 'keyboards/ymdk/sp64/matrix.c')
| -rw-r--r-- | keyboards/ymdk/sp64/matrix.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/keyboards/ymdk/sp64/matrix.c b/keyboards/ymdk/sp64/matrix.c new file mode 100644 index 000000000..8dd2bb8d7 --- /dev/null +++ b/keyboards/ymdk/sp64/matrix.c | |||
| @@ -0,0 +1,169 @@ | |||
| 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 | |||
| 15 | #include "matrix.h" | ||
| 16 | #include <stdint.h> | ||
| 17 | #include <stdbool.h> | ||
| 18 | #include <avr/io.h> | ||
| 19 | #include "wait.h" | ||
| 20 | #include "action_layer.h" | ||
| 21 | #include "print.h" | ||
| 22 | #include "debug.h" | ||
| 23 | #include "util.h" | ||
| 24 | #include "sp64.h" | ||
| 25 | #include "debounce.h" | ||
| 26 | |||
| 27 | /* matrix state(1:on, 0:off) */ | ||
| 28 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 29 | |||
| 30 | // Debouncing: store for each key the number of scans until it's eligible to | ||
| 31 | // change. When scanning the matrix, ignore any changes in keys that have | ||
| 32 | // already changed in the last DEBOUNCE scans. | ||
| 33 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 34 | |||
| 35 | static void matrix_select_row(uint8_t row); | ||
| 36 | |||
| 37 | #ifdef RIGHT_HALF | ||
| 38 | static uint8_t mcp23018_reset_loop = 0; | ||
| 39 | #endif | ||
| 40 | |||
| 41 | void matrix_init(void) | ||
| 42 | { | ||
| 43 | // all outputs for rows high | ||
| 44 | DDRB = 0xFF; | ||
| 45 | PORTB = 0xFF; | ||
| 46 | // all inputs for columns | ||
| 47 | DDRA = 0x00; | ||
| 48 | DDRC &= ~(0x111111<<2); | ||
| 49 | DDRD &= ~(1<<PIND7); | ||
| 50 | // all columns are pulled-up | ||
| 51 | PORTA = 0xFF; | ||
| 52 | PORTC |= (0b111111<<2); | ||
| 53 | PORTD |= (1<<PIND7); | ||
| 54 | |||
| 55 | #ifdef RIGHT_HALF | ||
| 56 | // initialize row and col | ||
| 57 | mcp23018_status = init_mcp23018(); | ||
| 58 | #endif | ||
| 59 | |||
| 60 | // initialize matrix state: all keys off | ||
| 61 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 62 | matrix[row] = 0; | ||
| 63 | matrix_debouncing[row] = 0; | ||
| 64 | } | ||
| 65 | debounce_init(MATRIX_ROWS); | ||
| 66 | matrix_init_quantum(); | ||
| 67 | } | ||
| 68 | |||
| 69 | uint8_t matrix_scan(void) | ||
| 70 | { | ||
| 71 | #ifdef RIGHT_HALF | ||
| 72 | // Then the keyboard | ||
| 73 | if (mcp23018_status != I2C_STATUS_SUCCESS) { | ||
| 74 | if (++mcp23018_reset_loop == 0) { | ||
| 75 | // if (++mcp23018_reset_loop >= 1300) { | ||
| 76 | // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans | ||
| 77 | // this will be approx bit more frequent than once per second | ||
| 78 | print("trying to reset mcp23018\n"); | ||
| 79 | mcp23018_status = init_mcp23018(); | ||
| 80 | if (mcp23018_status) { | ||
| 81 | print("left side not responding\n"); | ||
| 82 | } else { | ||
| 83 | print("left side attached\n"); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | #endif | ||
| 88 | bool changed = false; | ||
| 89 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) | ||
| 90 | { | ||
| 91 | matrix_row_t cols; | ||
| 92 | |||
| 93 | matrix_select_row(row); | ||
| 94 | #ifndef RIGHT_HALF | ||
| 95 | _delay_us(5); | ||
| 96 | #endif | ||
| 97 | |||
| 98 | cols = ( | ||
| 99 | // cols 0..7, PORTA 0 -> 7 | ||
| 100 | (~PINA) & 0xFF | ||
| 101 | ); | ||
| 102 | |||
| 103 | #ifdef RIGHT_HALF | ||
| 104 | uint8_t data = 0x7F; | ||
| 105 | // Receive the columns from right half | ||
| 106 | i2c_receive(I2C_ADDR_WRITE, &data, 1, MCP23018_I2C_TIMEOUT); | ||
| 107 | cols |= ((~(data) & 0x7F) << 7); | ||
| 108 | #endif | ||
| 109 | |||
| 110 | if (matrix_debouncing[row] != cols) { | ||
| 111 | matrix_debouncing[row] = cols; | ||
| 112 | //debouncing = DEBOUNCE; | ||
| 113 | changed = true; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | debounce(matrix_debouncing, matrix, MATRIX_ROWS, changed); | ||
| 118 | |||
| 119 | matrix_scan_quantum(); | ||
| 120 | |||
| 121 | #ifdef DEBUG_MATRIX | ||
| 122 | for (uint8_t c = 0; c < MATRIX_COLS; c++) | ||
| 123 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 124 | if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c); | ||
| 125 | #endif | ||
| 126 | |||
| 127 | return (uint8_t)changed; | ||
| 128 | } | ||
| 129 | |||
| 130 | inline | ||
| 131 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 132 | { | ||
| 133 | return matrix[row]; | ||
| 134 | } | ||
| 135 | |||
| 136 | void matrix_print(void) | ||
| 137 | { | ||
| 138 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 139 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 140 | phex(row); print(": "); | ||
| 141 | pbin_reverse16(matrix_get_row(row)); | ||
| 142 | print("\n"); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | uint8_t matrix_key_count(void) | ||
| 147 | { | ||
| 148 | uint8_t count = 0; | ||
| 149 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 150 | count += bitpop16(matrix[i]); | ||
| 151 | } | ||
| 152 | return count; | ||
| 153 | } | ||
| 154 | |||
| 155 | static void matrix_select_row(uint8_t row) | ||
| 156 | { | ||
| 157 | #ifdef RIGHT_HALF | ||
| 158 | uint8_t txdata[3]; | ||
| 159 | |||
| 160 | //Set the remote row on port A | ||
| 161 | txdata[0] = GPIOA; | ||
| 162 | txdata[1] = 0xFF & ~(1<<row); | ||
| 163 | mcp23018_status = i2c_transmit(I2C_ADDR_WRITE, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT); | ||
| 164 | #endif | ||
| 165 | |||
| 166 | // select other half | ||
| 167 | DDRB = (1 << row); | ||
| 168 | PORTB = ~(1 << row); | ||
| 169 | } | ||
