Fix matrix.c to use new default impl.
[tmk_keyboard.git] / converter / sun_usb / matrix.c
CommitLineData
97ee1be5 1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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"
97ee1be5 24#include "matrix.h"
25#include "debug.h"
b56717b8 26#include "protocol/serial.h"
1ad31539 27#include "led.h"
28#include "host.h"
97ee1be5 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 * +---------+
97ee1be5 46 */
47static uint8_t matrix[MATRIX_ROWS];
48#define ROW(code) ((code>>3)&0xF)
49#define COL(code) (code&0x07)
50
97ee1be5 51
52void matrix_init(void)
53{
62117e7e 54 DDRD |= (1<<6);
55 PORTD |= (1<<6);
523cc6aa 56 //debug_enable = true;
97ee1be5 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
49b0c7e5 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");
97ee1be5 78 return;
79}
80
81uint8_t matrix_scan(void)
82{
97ee1be5 83 uint8_t code;
84 code = serial_recv();
b56717b8 85 if (!code) return 0;
86
87 debug_hex(code); debug(" ");
88
89 switch (code) {
a5516088 90 case 0xFF: // reset success: FF 04
91 print("reset: ");
b56717b8 92 _delay_ms(500);
a5516088 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;
b56717b8 110 case 0x7F:
111 // all keys up
112 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
113 return 0;
97ee1be5 114 }
115
97ee1be5 116 if (code&0x80) {
117 // break code
118 if (matrix_is_on(ROW(code), COL(code))) {
119 matrix[ROW(code)] &= ~(1<<COL(code));
97ee1be5 120 }
121 } else {
122 // make code
123 if (!matrix_is_on(ROW(code), COL(code))) {
124 matrix[ROW(code)] |= (1<<COL(code));
97ee1be5 125 }
126 }
127 return code;
128}
129
97ee1be5 130inline
131uint8_t matrix_get_row(uint8_t row)
132{
133 return matrix[row];
134}
Imprint / Impressum