qmk_firmware

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

matrix.c (4024B)


      1 /*
      2 Copyright 2012 Jun Wako <wakojun@gmail.com>
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 #include "matrix.h"
     19 #include "host.h"
     20 #include "led.h"
     21 #include "debug.h"
     22 #include "wait.h"
     23 #include "uart.h"
     24 
     25 /*
     26  * Matrix Array usage:
     27  *
     28  * ROW: 16(4bits)
     29  * COL:  8(3bits)
     30  *
     31  *    8bit wide
     32  *   +---------+
     33  *  0|00 ... 07|
     34  *  1|08 ... 0F|
     35  *  :|   ...   |
     36  *  :|   ...   |
     37  *  E|70 ... 77|
     38  *  F|78 ... 7F|
     39  *   +---------+
     40  */
     41 static uint8_t matrix[MATRIX_ROWS];
     42 #define ROW(code)      ((code>>3)&0xF)
     43 #define COL(code)      (code&0x07)
     44 
     45 __attribute__ ((weak))
     46 void matrix_init_kb(void) {
     47     matrix_init_user();
     48 }
     49 
     50 __attribute__ ((weak))
     51 void matrix_scan_kb(void) {
     52     matrix_scan_user();
     53 }
     54 
     55 __attribute__ ((weak))
     56 void matrix_init_user(void) {
     57 }
     58 
     59 __attribute__ ((weak))
     60 void matrix_scan_user(void) {
     61 }
     62 
     63 inline
     64 uint8_t matrix_rows(void)
     65 {
     66     return MATRIX_ROWS;
     67 }
     68 
     69 inline
     70 uint8_t matrix_cols(void)
     71 {
     72     return MATRIX_COLS;
     73 }
     74 
     75 void matrix_init(void)
     76 {
     77     /* gpio_set_pin_output(D6); */
     78     /* gpio_write_pin_high(D6); */
     79     debug_enable = true;
     80 
     81     uart_init(1200);
     82 
     83     // initialize matrix state: all keys off
     84     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
     85 
     86     /* // wait for keyboard coming up */
     87     /* // otherwise LED status update fails */
     88     /* print("Reseting "); */
     89     /* while (1) { */
     90     /*     print("."); */
     91     /*     while (uart_read()); */
     92     /*     uart_write(0x01); */
     93     /*     wait_ms(500); */
     94     /*     if (uart_read() == 0xFF) { */
     95     /*         wait_ms(500); */
     96     /*         if (uart_read() == 0x04) */
     97     /*             break; */
     98     /*     } */
     99     /* } */
    100     /* print(" Done\n"); */
    101 
    102     /* gpio_write_pin_low(D6) */
    103 
    104     matrix_init_kb();
    105     return;
    106 }
    107 
    108 uint8_t matrix_scan(void)
    109 {
    110     uint8_t code;
    111     code = uart_read();
    112     if (!code) return 0;
    113 
    114     dprintf("%02X ", code);
    115 
    116     switch (code) {
    117         case 0xFF:  // reset success: FF 04
    118             print("reset: ");
    119             wait_ms(500);
    120             code = uart_read();
    121             xprintf("%02X\n", code);
    122             if (code == 0x04) {
    123                 // LED status
    124                 led_set(host_keyboard_leds());
    125             }
    126             return 0;
    127         case 0xFE:  // layout: FE <layout>
    128             print("layout: ");
    129             wait_ms(500);
    130             xprintf("%02X\n", uart_read());
    131             return 0;
    132         case 0x7E:  // reset fail: 7E 01
    133             print("reset fail: ");
    134             wait_ms(500);
    135             xprintf("%02X\n", uart_read());
    136             return 0;
    137         case 0x7F:
    138             // all keys up
    139             for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
    140             return 0;
    141     }
    142 
    143     if (code&0x80) {
    144         // break code
    145         if (matrix_is_on(ROW(code), COL(code))) {
    146             matrix[ROW(code)] &= ~(1<<COL(code));
    147         }
    148     } else {
    149         // make code
    150         if (!matrix_is_on(ROW(code), COL(code))) {
    151             matrix[ROW(code)] |=  (1<<COL(code));
    152         }
    153     }
    154 
    155     matrix_scan_kb();
    156     return code;
    157 }
    158 
    159 inline
    160 bool matrix_has_ghost(void)
    161 {
    162     return false;
    163 }
    164 
    165 inline
    166 bool matrix_is_on(uint8_t row, uint8_t col)
    167 {
    168     return (matrix[row] & (1<<col));
    169 }
    170 
    171 inline
    172 uint8_t matrix_get_row(uint8_t row)
    173 {
    174     return matrix[row];
    175 }
    176 
    177 void matrix_print(void)
    178 {
    179     print("\nr/c 01234567\n");
    180     for (uint8_t row = 0; row < matrix_rows(); row++) {
    181         print_hex8(row); print(": ");
    182         print_bin_reverse8(matrix_get_row(row));
    183         print("\n");
    184     }
    185 }