qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

matrix.c (7254B)


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