]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/converter/sun_usb/matrix.c
fix LIB_SRC and QUANTUM_LIB_SRC for ARM (#5623)
[qmk_firmware.git] / keyboards / converter / sun_usb / matrix.c
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 QMK_KEYBOARD_H
19 #include "protocol/serial.h"
20
21 /*
22  * Matrix Array usage:
23  *
24  * ROW: 16(4bits)
25  * COL:  8(3bits)
26  *
27  *    8bit wide
28  *   +---------+
29  *  0|00 ... 07|
30  *  1|08 ... 0F|
31  *  :|   ...   |
32  *  :|   ...   |
33  *  E|70 ... 77|
34  *  F|78 ... 7F|
35  *   +---------+
36  */
37 static uint8_t matrix[MATRIX_ROWS];
38 #define ROW(code)      ((code>>3)&0xF)
39 #define COL(code)      (code&0x07)
40
41 static bool is_modified = false;
42
43 __attribute__ ((weak))
44 void matrix_init_kb(void) {
45     matrix_init_user();
46 }
47
48 __attribute__ ((weak))
49 void matrix_scan_kb(void) {
50     matrix_scan_user();
51 }
52
53 __attribute__ ((weak))
54 void matrix_init_user(void) {
55 }
56
57 __attribute__ ((weak))
58 void matrix_scan_user(void) {
59 }
60
61 inline
62 uint8_t matrix_rows(void)
63 {
64     return MATRIX_ROWS;
65 }
66
67 inline
68 uint8_t matrix_cols(void)
69 {
70     return MATRIX_COLS;
71 }
72
73 void matrix_init(void)
74 {
75     /* DDRD |= (1<<6); */
76     /* PORTD |= (1<<6); */
77     debug_enable = true;
78
79     serial_init();
80
81     // initialize matrix state: all keys off
82     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
83
84     /* // wait for keyboard coming up */
85     /* // otherwise LED status update fails */
86     /* print("Reseting "); */
87     /* while (1) { */
88     /*     print("."); */
89     /*     while (serial_recv()); */
90     /*     serial_send(0x01); */
91     /*     _delay_ms(500); */
92     /*     if (serial_recv() == 0xFF) { */
93     /*         _delay_ms(500); */
94     /*         if (serial_recv() == 0x04) */
95     /*             break; */
96     /*     } */
97     /* } */
98     /* print(" Done\n"); */
99
100     /* PORTD &= ~(1<<6); */
101
102     matrix_init_quantum();
103     return;
104 }
105
106 uint8_t matrix_scan(void)
107 {
108     uint8_t code;
109     code = serial_recv();
110     if (!code) return 0;
111
112     debug_hex(code); debug(" ");
113
114     switch (code) {
115         case 0xFF:  // reset success: FF 04
116             print("reset: ");
117             _delay_ms(500);
118             code = serial_recv();
119             xprintf("%02X\n", code);
120             if (code == 0x04) {
121                 // LED status
122                 led_set(host_keyboard_leds());
123             }
124             return 0;
125         case 0xFE:  // layout: FE <layout>
126             print("layout: ");
127             _delay_ms(500);
128             xprintf("%02X\n", serial_recv());
129             return 0;
130         case 0x7E:  // reset fail: 7E 01
131             print("reset fail: ");
132             _delay_ms(500);
133             xprintf("%02X\n", serial_recv());
134             return 0;
135         case 0x7F:
136             // all keys up
137             for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
138             return 0;
139     }
140
141     if (code&0x80) {
142         // break code
143         if (matrix_is_on(ROW(code), COL(code))) {
144             matrix[ROW(code)] &= ~(1<<COL(code));
145         }
146     } else {
147         // make code
148         if (!matrix_is_on(ROW(code), COL(code))) {
149             matrix[ROW(code)] |=  (1<<COL(code));
150         }
151     }
152
153     matrix_scan_quantum();
154     return code;
155 }
156
157 bool matrix_is_modified(void)
158 {
159     return is_modified;
160 }
161
162 inline
163 bool matrix_has_ghost(void)
164 {
165     return false;
166 }
167
168 inline
169 bool matrix_is_on(uint8_t row, uint8_t col)
170 {
171     return (matrix[row] & (1<<col));
172 }
173
174 inline
175 uint8_t matrix_get_row(uint8_t row)
176 {
177     return matrix[row];
178 }
179
180 void matrix_print(void)
181 {
182     print("\nr/c 01234567\n");
183     for (uint8_t row = 0; row < matrix_rows(); row++) {
184         phex(row); print(": ");
185         pbin_reverse(matrix_get_row(row));
186         print("\n");
187     }
188 }
189
190 uint8_t matrix_key_count(void)
191 {
192     uint8_t count = 0;
193     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
194         count += bitpop(matrix[i]);
195     }
196     return count;
197 }