Merge branch 'unimap'
[tmk_keyboard.git] / converter / x68k_usb / matrix.c
CommitLineData
38d61ee1 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"
f45f4193 24#include "serial.h"
38d61ee1 25#include "matrix.h"
f45f4193 26#include "debug.h"
38d61ee1 27
28
29/*
30 * Matrix Array usage:
31 *
32 * ROW: 16(4bits)
33 * COL: 8(3bits)
34 *
35 * 8bit wide
36 * +---------+
37 * 0|00 ... 07|
38 * 1|08 ... 0F|
39 * :| ... |
40 * :| ... |
41 * E|70 ... 77|
42 * F|78 ... 7F|
43 * +---------+
44 *
45 */
46static uint8_t matrix[MATRIX_ROWS];
47#define ROW(code) ((code>>3)&0xF)
48#define COL(code) (code&0x07)
49
50static bool is_modified = false;
51
52
53inline
54uint8_t matrix_rows(void)
55{
56 return MATRIX_ROWS;
57}
58
59inline
60uint8_t matrix_cols(void)
61{
62 return MATRIX_COLS;
63}
64
65void matrix_init(void)
66{
f45f4193 67 serial_init();
38d61ee1 68
69 // initialize matrix state: all keys off
70 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
71
72 return;
73}
74
75uint8_t matrix_scan(void)
76{
77 is_modified = false;
78
f45f4193 79 uint16_t code;
80 code = serial_recv2();
81 if (code == -1) {
38d61ee1 82 return 0;
83 }
84
f45f4193 85 dprintf("%02X\n", code);
38d61ee1 86 if (code&0x80) {
87 // break code
88 if (matrix_is_on(ROW(code), COL(code))) {
89 matrix[ROW(code)] &= ~(1<<COL(code));
90 is_modified = true;
91 }
92 } else {
93 // make code
94 if (!matrix_is_on(ROW(code), COL(code))) {
95 matrix[ROW(code)] |= (1<<COL(code));
96 is_modified = true;
97 }
98 }
99 return code;
100}
101
102bool matrix_is_modified(void)
103{
104 return is_modified;
105}
106
107inline
108bool matrix_has_ghost(void)
109{
110 return false;
111}
112
113inline
114bool matrix_is_on(uint8_t row, uint8_t col)
115{
116 return (matrix[row] & (1<<col));
117}
118
119inline
120uint8_t matrix_get_row(uint8_t row)
121{
122 return matrix[row];
123}
124
125void matrix_print(void)
126{
127 print("\nr/c 01234567\n");
128 for (uint8_t row = 0; row < matrix_rows(); row++) {
129 phex(row); print(": ");
130 pbin_reverse(matrix_get_row(row));
131 print("\n");
132 }
133}
134
135uint8_t matrix_key_count(void)
136{
137 uint8_t count = 0;
138 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
139 count += bitpop(matrix[i]);
140 }
141 return count;
142}
Imprint / Impressum