diff options
Diffstat (limited to 'keyboards/dichotomy/matrix.c')
| -rwxr-xr-x | keyboards/dichotomy/matrix.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/keyboards/dichotomy/matrix.c b/keyboards/dichotomy/matrix.c new file mode 100755 index 000000000..14c3f0d8e --- /dev/null +++ b/keyboards/dichotomy/matrix.c | |||
| @@ -0,0 +1,228 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako | ||
| 3 | Copyright 2014 Jack Humbert | ||
| 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 | #if defined(__AVR__) | ||
| 21 | #include <avr/io.h> | ||
| 22 | #endif | ||
| 23 | #include "wait.h" | ||
| 24 | #include "print.h" | ||
| 25 | #include "debug.h" | ||
| 26 | #include "util.h" | ||
| 27 | #include "matrix.h" | ||
| 28 | #include "timer.h" | ||
| 29 | #include "dichotomy.h" | ||
| 30 | #include "pointing_device.h" | ||
| 31 | #include "report.h" | ||
| 32 | |||
| 33 | #if (MATRIX_COLS <= 8) | ||
| 34 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
| 35 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
| 36 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
| 37 | # define ROW_SHIFTER ((uint8_t)1) | ||
| 38 | #elif (MATRIX_COLS <= 16) | ||
| 39 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
| 40 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
| 41 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
| 42 | # define ROW_SHIFTER ((uint16_t)1) | ||
| 43 | #elif (MATRIX_COLS <= 32) | ||
| 44 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
| 45 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
| 46 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
| 47 | # define ROW_SHIFTER ((uint32_t)1) | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #define MAIN_ROWMASK 0xFFF0; | ||
| 51 | #define LOWER_ROWMASK 0x3FC0; | ||
| 52 | |||
| 53 | /* matrix state(1:on, 0:off) */ | ||
| 54 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 55 | |||
| 56 | __attribute__ ((weak)) | ||
| 57 | void matrix_init_quantum(void) { | ||
| 58 | matrix_init_kb(); | ||
| 59 | } | ||
| 60 | |||
| 61 | __attribute__ ((weak)) | ||
| 62 | void matrix_scan_quantum(void) { | ||
| 63 | matrix_scan_kb(); | ||
| 64 | } | ||
| 65 | |||
| 66 | __attribute__ ((weak)) | ||
| 67 | void matrix_init_kb(void) { | ||
| 68 | matrix_init_user(); | ||
| 69 | } | ||
| 70 | |||
| 71 | __attribute__ ((weak)) | ||
| 72 | void matrix_scan_kb(void) { | ||
| 73 | matrix_scan_user(); | ||
| 74 | } | ||
| 75 | |||
| 76 | __attribute__ ((weak)) | ||
| 77 | void matrix_init_user(void) { | ||
| 78 | } | ||
| 79 | |||
| 80 | __attribute__ ((weak)) | ||
| 81 | void matrix_scan_user(void) { | ||
| 82 | } | ||
| 83 | |||
| 84 | inline | ||
| 85 | uint8_t matrix_rows(void) { | ||
| 86 | return MATRIX_ROWS; | ||
| 87 | } | ||
| 88 | |||
| 89 | inline | ||
| 90 | uint8_t matrix_cols(void) { | ||
| 91 | return MATRIX_COLS; | ||
| 92 | } | ||
| 93 | |||
| 94 | void matrix_init(void) { | ||
| 95 | DDRF |= (1<<6); | ||
| 96 | DDRF |= (1<<5); | ||
| 97 | DDRD |= (1<<1); | ||
| 98 | matrix_init_quantum(); | ||
| 99 | } | ||
| 100 | |||
| 101 | uint8_t matrix_scan(void) | ||
| 102 | { | ||
| 103 | SERIAL_UART_INIT(); | ||
| 104 | //xprintf("\r\nTRYING TO SCAN"); | ||
| 105 | |||
| 106 | uint32_t timeout = 0; | ||
| 107 | |||
| 108 | //the s character requests the RF slave to send the matrix | ||
| 109 | SERIAL_UART_DATA = 's'; | ||
| 110 | |||
| 111 | //trust the external keystates entirely, erase the last data | ||
| 112 | uint8_t uart_data[11] = {0}; | ||
| 113 | |||
| 114 | //there are 10 bytes corresponding to 10 columns, and an end byte | ||
| 115 | for (uint8_t i = 0; i < 11; i++) { | ||
| 116 | //wait for the serial data, timeout if it's been too long | ||
| 117 | //this only happened in testing with a loose wire, but does no | ||
| 118 | //harm to leave it in here | ||
| 119 | while(!SERIAL_UART_RXD_PRESENT){ | ||
| 120 | timeout++; | ||
| 121 | if (timeout > 10000){ | ||
| 122 | xprintf("\r\nTime out in keyboard."); | ||
| 123 | break; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | uart_data[i] = SERIAL_UART_DATA; | ||
| 127 | } | ||
| 128 | |||
| 129 | //check for the end packet, the key state bytes use the LSBs, so 0xE0 | ||
| 130 | //will only show up here if the correct bytes were recieved | ||
| 131 | uint8_t checksum = 0x00; | ||
| 132 | for (uint8_t z=0; z<10; z++){ | ||
| 133 | checksum = checksum^uart_data[z]; | ||
| 134 | } | ||
| 135 | checksum = checksum ^ (uart_data[10] & 0xF0); | ||
| 136 | // Smash the checksum from 1 byte into 4 bits | ||
| 137 | checksum = (checksum ^ ((checksum & 0xF0)>>4)) & 0x0F; | ||
| 138 | //xprintf("\r\nGOT RAW PACKET: \r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d",uart_data[0],uart_data[1],uart_data[2],uart_data[3],uart_data[4],uart_data[5],uart_data[6],uart_data[7],uart_data[8],uart_data[9],uart_data[10],checksum); | ||
| 139 | if ((uart_data[10] & 0x0F) == checksum) { //this is an arbitrary binary checksum (1001) (that would be 0x9.) | ||
| 140 | //xprintf("\r\nGOT PACKET: \r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d",uart_data[0],uart_data[1],uart_data[2],uart_data[3],uart_data[4],uart_data[5]); | ||
| 141 | //shifting and transferring the keystates to the QMK matrix variable | ||
| 142 | //bits 1-12 are row 1, 13-24 are row 2, 25-36 are row 3, | ||
| 143 | //bits 37-42 are row 4 (only 6 wide, 1-3 are 0, and 10-12 are 0) | ||
| 144 | //bits 43-48 are row 5 (same as row 4) | ||
| 145 | /* ASSUMING MSB FIRST */ | ||
| 146 | matrix[0] = (((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1])) & MAIN_ROWMASK; | ||
| 147 | matrix[1] = ((uint16_t) uart_data[1] << 12) | ((uint16_t) uart_data[2] << 4); | ||
| 148 | matrix[2] = (((uint16_t) uart_data[3] << 8) | ((uint16_t) uart_data[4])) & MAIN_ROWMASK; | ||
| 149 | matrix[3] = (((uint16_t) uart_data[4] << 9) | ((uint16_t) uart_data[5] << 1)) & LOWER_ROWMASK; | ||
| 150 | matrix[4] = (((uint16_t) uart_data[5] << 7) | ((uart_data[10] & 1<<7) ? 1:0) << 13 | ((uart_data[10] & 1<<6) ? 1:0) << 6) & LOWER_ROWMASK; | ||
| 151 | /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */ | ||
| 152 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 153 | //I've unpacked these into the mirror image of what QMK expects them to be, so... | ||
| 154 | /*uint8_t halfOne = (matrix[i]>>8); | ||
| 155 | uint8_t halfTwo = (matrix[i] & 0xFF); | ||
| 156 | halfOne = ((halfOne * 0x0802LU & 0x22110LU) | (halfOne * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; | ||
| 157 | halfTwo = ((halfTwo * 0x0802LU & 0x22110LU) | (halfTwo * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; | ||
| 158 | matrix[i] = ((halfTwo<<8) & halfOne);*/ | ||
| 159 | //matrix[i] = ((matrix[i] * 0x0802LU & 0x22110LU) | (matrix[i] * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; | ||
| 160 | matrix[i] = bitrev16(matrix[i]); | ||
| 161 | //bithack mirror! Doesn't make any sense, but works - and efficiently. | ||
| 162 | } | ||
| 163 | //if (uart_data[6]!=0 || uart_data[7]!=0){ | ||
| 164 | //if (maxCount<101){ | ||
| 165 | // xprintf("\r\nMouse data: x=%d, y=%d",(int8_t)uart_data[6],(int8_t)uart_data[7]); | ||
| 166 | //} | ||
| 167 | report_mouse_t currentReport = {}; | ||
| 168 | //check for the end packet, bytes 1-4 are movement and scroll | ||
| 169 | //but byte 5 has bits 0-3 for the scroll button state | ||
| 170 | //(1000 if pressed, 0000 if not) and bits 4-7 are always 1 | ||
| 171 | //We can use this to verify the report sent properly. | ||
| 172 | |||
| 173 | currentReport = pointing_device_get_report(); | ||
| 174 | //shifting and transferring the info to the mouse report varaible | ||
| 175 | //mouseReport.x = 127 max -127 min | ||
| 176 | currentReport.x = (int8_t) uart_data[6]; | ||
| 177 | //mouseReport.y = 127 max -127 min | ||
| 178 | currentReport.y = (int8_t) uart_data[7]; | ||
| 179 | //mouseReport.v = 127 max -127 min (scroll vertical) | ||
| 180 | currentReport.v = (int8_t) uart_data[8]; | ||
| 181 | //mouseReport.h = 127 max -127 min (scroll horizontal) | ||
| 182 | currentReport.h = (int8_t) uart_data[9]; | ||
| 183 | /* | ||
| 184 | currentReport.x = 0; | ||
| 185 | currentReport.y = 0; | ||
| 186 | currentReport.v = 0; | ||
| 187 | currentReport.h = 0;*/ | ||
| 188 | pointing_device_set_report(currentReport); | ||
| 189 | } else { | ||
| 190 | //xprintf("\r\nRequested packet, data 10 was %d but checksum was %d",(uart_data[10] & 0x0F), (checksum & 0x0F)); | ||
| 191 | } | ||
| 192 | //matrix_print(); | ||
| 193 | |||
| 194 | matrix_scan_quantum(); | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | inline | ||
| 199 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 200 | { | ||
| 201 | return (matrix[row] & ((matrix_row_t)1<col)); | ||
| 202 | } | ||
| 203 | |||
| 204 | inline | ||
| 205 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 206 | { | ||
| 207 | return matrix[row]; | ||
| 208 | } | ||
| 209 | |||
| 210 | void matrix_print(void) | ||
| 211 | { | ||
| 212 | print_matrix_header(); | ||
| 213 | |||
| 214 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 215 | phex(row); print(": "); | ||
| 216 | print_matrix_row(row); | ||
| 217 | print("\n"); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | uint8_t matrix_key_count(void) | ||
| 222 | { | ||
| 223 | uint8_t count = 0; | ||
| 224 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 225 | count += matrix_bitpop(i); | ||
| 226 | } | ||
| 227 | return count; | ||
| 228 | } | ||
