Fix matrix.c to use new default impl.
[tmk_keyboard.git] / converter / ibm4704_usb / matrix.c
CommitLineData
ebe951a4 1/*
2Copyright 2014 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 "action.h"
23#include "print.h"
24#include "debug.h"
25#include "util.h"
26#include "ibm4704.h"
27#include "matrix.h"
28
29
30static void matrix_make(uint8_t code);
31static void matrix_break(uint8_t code);
ebe951a4 32
33
34/*
35 * Matrix Array usage:
36 * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
37 *
38 * 8bit wide
39 * +---------+
40 * 0| |
41 * :| XX | 00-7F
42 * f| |
43 * +---------+
44 *
45 * Exceptions:
46 */
47static uint8_t matrix[MATRIX_ROWS];
48
49// scan code bits 7654 3210
50// R:row/C:column -RRR RCCC
51#define ROW(code) ((code>>3)&0x0f)
52#define COL(code) (code&0x07)
53
54
ebe951a4 55static void enable_break(void)
56{
c4efadf6 57 print("Enable break: ");
455fd51a 58 while (ibm4704_send(0xFC)) { _delay_ms(10); }
d42aa478 59 // valid scancode: 00-79h
455fd51a 60 for (uint8_t code = 0; code < 0x7F; code++) {
80fd3b0b 61 while (ibm4704_send(0x80|code)) _delay_ms(10);
62 _delay_ms(5); // wait for response
63 // No response(FF) when ok, FD when out of bound
64 xprintf("s%02X:r%02X ", code, ibm4704_recv());
ebe951a4 65 }
80fd3b0b 66 while (ibm4704_send(0xFF)) { _delay_ms(10); } // End
ebe951a4 67 print("End\n");
ebe951a4 68}
69
ebe951a4 70
80fd3b0b 71void matrix_setup(void)
72{
ebe951a4 73 ibm4704_init();
80fd3b0b 74}
ebe951a4 75
80fd3b0b 76void matrix_init(void)
77{
78 debug_enable = true;
c4efadf6 79
c4efadf6 80 print("IBM 4704 converter\n");
80fd3b0b 81 matrix_clear();
82 _delay_ms(2000); // wait for keyboard starting up
d42aa478 83 xprintf("Keyboard ID: %02X\n", ibm4704_recv());
ebe951a4 84 enable_break();
ebe951a4 85}
86
87/*
88 * IBM 4704 Scan Code
89 */
90uint8_t matrix_scan(void)
91{
92 uint8_t code = ibm4704_recv();
93 if (code==0xFF) {
94 // Not receivd
95 return 0;
75ca495c 96 } else if ((code&0x7F) >= 0x7C) {
97 // 0xFF-FC and 0x7F-7C is not scancode
d42aa478 98 xprintf("Error: %02X\n", code);
ebe951a4 99 matrix_clear();
100 return 0;
101 } else if (code&0x80) {
d42aa478 102 dprintf("%02X\n", code);
ebe951a4 103 matrix_make(code);
104 } else {
d42aa478 105 dprintf("%02X\n", code);
ebe951a4 106 matrix_break(code);
107 }
108 return 1;
109}
110
ebe951a4 111inline
112uint8_t matrix_get_row(uint8_t row)
113{
114 return matrix[row];
115}
116
ebe951a4 117inline
118static void matrix_make(uint8_t code)
119{
120 matrix[ROW(code)] |= 1<<COL(code);
121}
122
123inline
124static void matrix_break(uint8_t code)
125{
126 matrix[ROW(code)] &= ~(1<<COL(code));
127}
128
124bafe9 129void matrix_clear(void)
ebe951a4 130{
131 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
132}
Imprint / Impressum