Fix matrix.c to use new default impl.
[tmk_keyboard.git] / converter / m0110_usb / matrix.c
CommitLineData
3f0289e6 1/*
805ce3c1 2Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3f0289e6 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/*
19 * scan matrix
20 */
21#include <stdint.h>
22#include <stdbool.h>
23#include <avr/io.h>
24#include <util/delay.h>
25#include "print.h"
26#include "util.h"
27#include "debug.h"
28#include "host.h"
29#include "led.h"
30#include "m0110.h"
31#include "matrix.h"
32
33
34#define CAPS 0x39
f5f48c2a 35#define CAPS_BREAK (CAPS | 0x80)
3f0289e6 36#define ROW(key) ((key)>>3&0x0F)
37#define COL(key) ((key)&0x07)
38
39
40static bool is_modified = false;
41
42// matrix state buffer(1:on, 0:off)
43static uint8_t *matrix;
44static uint8_t _matrix0[MATRIX_ROWS];
45
3f0289e6 46static void register_key(uint8_t key);
47
48
3f0289e6 49void matrix_init(void)
50{
3f0289e6 51 m0110_init();
52 // initialize matrix state: all keys off
53 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
54 matrix = _matrix0;
7d621aca 55
56 // LED flash
57 DDRD |= (1<<6); PORTD |= (1<<6);
58 _delay_ms(500);
59 DDRD |= (1<<6); PORTD &= ~(1<<6);
60
3f0289e6 61 return;
62}
63
64uint8_t matrix_scan(void)
65{
66 uint8_t key;
67
68 is_modified = false;
69 key = m0110_recv_key();
70
3f0289e6 71 if (key == M0110_NULL) {
72 return 0;
12f6e9ff 73 } else if (key == M0110_ERROR) {
12f6e9ff 74 return 0;
3f0289e6 75 } else {
3f0289e6 76 is_modified = true;
77 register_key(key);
78 }
79
80 if (debug_enable) {
805ce3c1 81 print("["); phex(key); print("]\n");
3f0289e6 82 }
83 return 1;
84}
85
3f0289e6 86inline
87uint8_t matrix_get_row(uint8_t row)
88{
89 return matrix[row];
90}
91
3f0289e6 92inline
93static void register_key(uint8_t key)
94{
95 if (key&0x80) {
96 matrix[ROW(key)] &= ~(1<<COL(key));
97 } else {
98 matrix[ROW(key)] |= (1<<COL(key));
99 }
100}
Imprint / Impressum