qmk_firmware

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

custom_matrix.cpp (6863B)


      1 /*
      2 Copyright 2016 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 <stdint.h>
     19 #include <stdbool.h>
     20 
     21 // USB HID host
     22 #include "Usb.h"
     23 #include "usbhub.h"
     24 #include "hid.h"
     25 #include "hidboot.h"
     26 #include "parser.h"
     27 
     28 #include "keycode.h"
     29 #include "util.h"
     30 #include "print.h"
     31 #include "debug.h"
     32 #include "timer.h"
     33 #include "matrix.h"
     34 #include "led.h"
     35 #include "host.h"
     36 #include "keyboard.h"
     37 
     38 /* KEY CODE to Matrix
     39  *
     40  * HID keycode(1 byte):
     41  * Higher 5 bits indicates ROW and lower 3 bits COL.
     42  *
     43  *  7 6 5 4 3 2 1 0
     44  * +---------------+
     45  * |  ROW  |  COL  |
     46  * +---------------+
     47  *
     48  * Matrix space(16 * 16):
     49  *   r\c0123456789ABCDEF
     50  *   0 +----------------+
     51  *   : |                |
     52  *   : |                |
     53  *  16 +----------------+
     54  */
     55 #define ROW_MASK 0xF0
     56 #define COL_MASK 0x0F
     57 #define CODE(row, col)  (((row) << 4) | (col))
     58 #define ROW(code)       (((code) & ROW_MASK) >> 4)
     59 #define COL(code)       ((code) & COL_MASK)
     60 #define ROW_BITS(code)  (1 << COL(code))
     61 
     62 
     63 // Integrated key state of all keyboards
     64 static report_keyboard_t local_keyboard_report;
     65 
     66 static bool matrix_is_mod = false;
     67 
     68 /*
     69  * USB Host Shield HID keyboards
     70  * This supports two cascaded hubs and four keyboards
     71  */
     72 USB usb_host;
     73 USBHub hub1(&usb_host);
     74 USBHub hub2(&usb_host);
     75 HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd1(&usb_host);
     76 HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd2(&usb_host);
     77 HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd3(&usb_host);
     78 HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd4(&usb_host);
     79 KBDReportParser kbd_parser1;
     80 KBDReportParser kbd_parser2;
     81 KBDReportParser kbd_parser3;
     82 KBDReportParser kbd_parser4;
     83 
     84 
     85 extern "C"
     86 {
     87     uint8_t matrix_rows(void) { return MATRIX_ROWS; }
     88     uint8_t matrix_cols(void) { return MATRIX_COLS; }
     89     bool matrix_has_ghost(void) { return false; }
     90     void matrix_init(void) {
     91         // USB Host Shield setup
     92         usb_host.Init();
     93         kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
     94         kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
     95         kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
     96         kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
     97     }
     98 
     99     static void or_report(report_keyboard_t report) {
    100         // integrate reports into local_keyboard_report
    101         local_keyboard_report.mods |= report.mods;
    102         for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
    103             if (IS_ANY(report.keys[i])) {
    104                 for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
    105                     if (! local_keyboard_report.keys[j]) {
    106                         local_keyboard_report.keys[j] = report.keys[i];
    107                         break;
    108                     }
    109                 }
    110             }
    111         }
    112     }
    113 
    114     uint8_t matrix_scan(void) {
    115         static uint16_t last_time_stamp1 = 0;
    116         static uint16_t last_time_stamp2 = 0;
    117         static uint16_t last_time_stamp3 = 0;
    118         static uint16_t last_time_stamp4 = 0;
    119 
    120         // check report came from keyboards
    121         if (kbd_parser1.time_stamp != last_time_stamp1 ||
    122             kbd_parser2.time_stamp != last_time_stamp2 ||
    123             kbd_parser3.time_stamp != last_time_stamp3 ||
    124             kbd_parser4.time_stamp != last_time_stamp4) {
    125 
    126             last_time_stamp1 = kbd_parser1.time_stamp;
    127             last_time_stamp2 = kbd_parser2.time_stamp;
    128             last_time_stamp3 = kbd_parser3.time_stamp;
    129             last_time_stamp4 = kbd_parser4.time_stamp;
    130 
    131             // clear and integrate all reports
    132             local_keyboard_report = {};
    133             or_report(kbd_parser1.report);
    134             or_report(kbd_parser2.report);
    135             or_report(kbd_parser3.report);
    136             or_report(kbd_parser4.report);
    137 
    138             matrix_is_mod = true;
    139 
    140             dprintf("state:  %02X %02X", local_keyboard_report.mods, local_keyboard_report.reserved);
    141             for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
    142                 dprintf(" %02X", local_keyboard_report.keys[i]);
    143             }
    144             dprint("\r\n");
    145         } else {
    146             matrix_is_mod = false;
    147         }
    148 
    149         uint16_t timer;
    150         timer = timer_read();
    151         usb_host.Task();
    152         timer = timer_elapsed(timer);
    153         if (timer > 100) {
    154             dprintf("host.Task: %d\n", timer);
    155         }
    156 
    157         static uint8_t usb_state = 0;
    158         if (usb_state != usb_host.getUsbTaskState()) {
    159             usb_state = usb_host.getUsbTaskState();
    160             dprintf("usb_state: %02X\n", usb_state);
    161 
    162             // restore LED state when keyboard comes up
    163             if (usb_state == USB_STATE_RUNNING) {
    164                 dprintf("speed: %s\n", usb_host.getVbusState()==FSHOST ? "full" : "low");
    165                 led_set(host_keyboard_leds());
    166             }
    167         }
    168         return 1;
    169     }
    170 
    171     bool matrix_is_on(uint8_t row, uint8_t col) {
    172         uint8_t code = CODE(row, col);
    173 
    174         if (IS_MODIFIER_KEYCODE(code)) {
    175             if (local_keyboard_report.mods & ROW_BITS(code)) {
    176                 return true;
    177             }
    178         }
    179         for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
    180             if (local_keyboard_report.keys[i] == code) {
    181                 return true;
    182             }
    183         }
    184         return false;
    185     }
    186 
    187     matrix_row_t matrix_get_row(uint8_t row) {
    188         uint16_t row_bits = 0;
    189 
    190         if (IS_MODIFIER_KEYCODE(CODE(row, 0)) && local_keyboard_report.mods) {
    191             row_bits |= local_keyboard_report.mods;
    192         }
    193 
    194         for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
    195             if (IS_ANY(local_keyboard_report.keys[i])) {
    196                 if (row == ROW(local_keyboard_report.keys[i])) {
    197                     row_bits |= ROW_BITS(local_keyboard_report.keys[i]);
    198                 }
    199             }
    200         }
    201         return row_bits;
    202     }
    203 
    204     void matrix_print(void) {
    205         print("\nr/c 0123456789ABCDEF\n");
    206         for (uint8_t row = 0; row < matrix_rows(); row++) {
    207             xprintf("%02d: ", row);
    208             print_bin_reverse16(matrix_get_row(row));
    209             print("\n");
    210         }
    211     }
    212 
    213     void led_set(uint8_t usb_led)
    214     {
    215         kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
    216         kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
    217         kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
    218         kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);
    219         led_update_kb((led_t){.raw = usb_led});
    220     }
    221 
    222 };