qmk_firmware

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

matrix.c (2410B)


      1 /*
      2 Copyright 2011,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 
     19 Ported to QMK by Techsock <info@techsock.com>
     20 */
     21 
     22 #include <stdint.h>
     23 #include <avr/io.h>
     24 #include <util/delay.h>
     25 #include "print.h"
     26 #include "util.h"
     27 #include "debug.h"
     28 #include "host.h"
     29 #include "m0110.h"
     30 #include "matrix.h"
     31 #include "report.h"
     32 #include "timer.h"
     33 
     34 
     35 #define CAPS        0x39
     36 #define CAPS_BREAK  (CAPS | 0x80)
     37 #define ROW(key)    ((key)>>3&0x0F)
     38 #define COL(key)    ((key)&0x07)
     39 
     40 
     41 static bool is_modified = false;
     42 
     43 // matrix state buffer(1:on, 0:off)
     44 static uint8_t *matrix;
     45 static uint8_t _matrix0[MATRIX_ROWS];
     46 
     47 static void register_key(uint8_t key);
     48 
     49 
     50 __attribute__ ((weak))
     51 void matrix_init_kb(void) {
     52     matrix_init_user();
     53 }
     54 
     55 __attribute__ ((weak))
     56 void matrix_scan_kb(void) {
     57     matrix_scan_user();
     58 }
     59 
     60 __attribute__ ((weak))
     61 void matrix_init_user(void) {
     62 }
     63 
     64 __attribute__ ((weak))
     65 void matrix_scan_user(void) {
     66 }
     67 
     68 void matrix_init(void)
     69 {
     70     m0110_init();
     71     // initialize matrix state: all keys off
     72     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
     73     matrix = _matrix0;
     74 
     75 	matrix_init_kb();
     76     return;
     77 }
     78 
     79 uint8_t matrix_scan(void)
     80 {
     81     uint8_t key;
     82 
     83     is_modified = false;
     84     key = m0110_recv_key();
     85 
     86     if (key == M0110_NULL) {
     87         return 0;
     88     } else if (key == M0110_ERROR) {
     89         return 0;
     90     } else {
     91         is_modified = true;
     92         register_key(key);
     93     }
     94 
     95     if (debug_enable) {
     96         print("["); print_hex8(key); print("]\n");
     97     }
     98     
     99     matrix_scan_kb();
    100     return 1;
    101 }
    102 
    103 void matrix_print(void){
    104 
    105 }
    106 
    107 inline
    108 uint8_t matrix_get_row(uint8_t row)
    109 {
    110     return matrix[row];
    111 }
    112 
    113 inline
    114 static void register_key(uint8_t key)
    115 {
    116     if (key&0x80) {
    117         matrix[ROW(key)] &= ~(1<<COL(key));
    118     } else {
    119         matrix[ROW(key)] |=  (1<<COL(key));
    120     }
    121 }