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