]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/sun_usb/matrix.c
Merge branch 'matrix-fix'
[max/tmk_keyboard.git] / 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 <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "util.h"
24 #include "matrix.h"
25 #include "debug.h"
26 #include "protocol/serial.h"
27 #include "led.h"
28 #include "host.h"
29
30
31 /*
32  * Matrix Array usage:
33  *
34  * ROW: 16(4bits)
35  * COL:  8(3bits)
36  *
37  *    8bit wide
38  *   +---------+
39  *  0|00 ... 07|
40  *  1|08 ... 0F|
41  *  :|   ...   |
42  *  :|   ...   |
43  *  E|70 ... 77|
44  *  F|78 ... 7F|
45  *   +---------+
46  */
47 static uint8_t matrix[MATRIX_ROWS];
48 #define ROW(code)      ((code>>3)&0xF)
49 #define COL(code)      (code&0x07)
50
51
52 void matrix_init(void)
53 {
54     DDRD |= (1<<6);
55     PORTD |= (1<<6);
56     //debug_enable = true;
57
58     serial_init();
59
60     // initialize matrix state: all keys off
61     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
62
63     // wait for keyboard coming up
64     // otherwise LED status update fails
65     print("Reseting ");
66     while (1) {
67         print(".");
68         while (serial_recv());
69         serial_send(0x01);
70         _delay_ms(500);
71         if (serial_recv() == 0xFF) {
72             _delay_ms(500);
73             if (serial_recv() == 0x04)
74                 break;
75         }
76     }
77     print(" Done\n");
78     return;
79 }
80
81 uint8_t matrix_scan(void)
82 {
83     uint8_t code;
84     code = serial_recv();
85     if (!code) return 0;
86
87     debug_hex(code); debug(" ");
88
89     switch (code) {
90         case 0xFF:  // reset success: FF 04
91             print("reset: ");
92             _delay_ms(500);
93             code = serial_recv();
94             xprintf("%02X\n", code);
95             if (code == 0x04) {
96                 // LED status
97                 led_set(host_keyboard_leds());
98             }
99             return 0;
100         case 0xFE:  // layout: FE <layout>
101             print("layout: ");
102             _delay_ms(500);
103             xprintf("%02X\n", serial_recv());
104             return 0;
105         case 0x7E:  // reset fail: 7E 01
106             print("reset fail: ");
107             _delay_ms(500);
108             xprintf("%02X\n", serial_recv());
109             return 0;
110         case 0x7F:
111             // all keys up
112             for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
113             return 0;
114     }
115
116     if (code&0x80) {
117         // break code
118         if (matrix_is_on(ROW(code), COL(code))) {
119             matrix[ROW(code)] &= ~(1<<COL(code));
120         }
121     } else {
122         // make code
123         if (!matrix_is_on(ROW(code), COL(code))) {
124             matrix[ROW(code)] |=  (1<<COL(code));
125         }
126     }
127     return code;
128 }
129
130 inline
131 uint8_t matrix_get_row(uint8_t row)
132 {
133     return matrix[row];
134 }