fc660c: Change pin for Insert LED
[tmk_keyboard.git] / keyboard / fc660c / fc660c.c
CommitLineData
36a29a58 1/*
503837cd 2Copyright 2017 Jun Wako <wakojun@gmail.com>
36a29a58
JW
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 <util/delay.h>
24#include "print.h"
25#include "debug.h"
26#include "util.h"
27#include "timer.h"
28#include "matrix.h"
36a29a58
JW
29#include <avr/wdt.h>
30#include "suspend.h"
31#include "lufa.h"
503837cd
JW
32#include "led.h"
33#include "fc660c.h"
36a29a58
JW
34
35
36a29a58
JW
36static uint32_t matrix_last_modified = 0;
37
38// matrix state buffer(1:on, 0:off)
39static matrix_row_t *matrix;
40static matrix_row_t *matrix_prev;
41static matrix_row_t _matrix0[MATRIX_ROWS];
42static matrix_row_t _matrix1[MATRIX_ROWS];
43
44
45void matrix_init(void)
46{
472a60c4 47#if 0
36a29a58
JW
48 debug_enable = true;
49 debug_keyboard = true;
503837cd 50 debug_matrix = true;
36a29a58
JW
51#endif
52
53 KEY_INIT();
54
472a60c4 55 // LEDs on CapsLock and Insert
2ed3abb6 56 DDRB |= (1<<5) | (1<<6);
57 PORTB |= (1<<5) | (1<<6);
472a60c4 58
36a29a58
JW
59 // initialize matrix state: all keys off
60 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
61 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
62 matrix = _matrix0;
63 matrix_prev = _matrix1;
64}
65
66uint8_t matrix_scan(void)
67{
503837cd 68 matrix_row_t *tmp;
36a29a58
JW
69
70 tmp = matrix_prev;
71 matrix_prev = matrix;
72 matrix = tmp;
73
503837cd
JW
74 uint8_t row, col;
75 for (col = 0; col < MATRIX_COLS; col++) {
76 SET_COL(col);
77 for (row = 0; row < MATRIX_ROWS; row++) {
78 //KEY_SELECT(row, col);
79 SET_ROW(row);
80 _delay_us(2);
36a29a58
JW
81
82 // Not sure this is needed. This just emulates HHKB controller's behaviour.
83 if (matrix_prev[row] & (1<<col)) {
503837cd 84 KEY_HYS_ON();
36a29a58
JW
85 }
86 _delay_us(10);
87
88 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
89 // If V-USB interrupts in this section we could lose 40us or so
90 // and would read invalid value from KEY_STATE.
91 uint8_t last = TIMER_RAW;
92
93 KEY_ENABLE();
94
95 // Wait for KEY_STATE outputs its value.
503837cd 96 _delay_us(2);
36a29a58
JW
97
98 if (KEY_STATE()) {
99 matrix[row] &= ~(1<<col);
100 } else {
101 matrix[row] |= (1<<col);
102 }
103
104 // Ignore if this code region execution time elapses more than 20us.
105 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
106 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
107 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
108 matrix[row] = matrix_prev[row];
109 }
110
111 _delay_us(5);
503837cd 112 KEY_HYS_OFF();
36a29a58
JW
113 KEY_UNABLE();
114
115 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
116 // This takes 25us or more to make sure KEY_STATE returns to idle state.
36a29a58 117 _delay_us(75);
36a29a58 118 }
503837cd
JW
119 if (matrix[row] ^ matrix_prev[row]) {
120 matrix_last_modified = timer_read32();
121 }
36a29a58
JW
122 }
123 return 1;
124}
125
126inline
127matrix_row_t matrix_get_row(uint8_t row)
128{
129 return matrix[row];
130}
131
503837cd
JW
132void led_set(uint8_t usb_led)
133{
134 if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
472a60c4 135 PORTB &= ~(1<<6);
503837cd 136 } else {
472a60c4 137 PORTB |= (1<<6);
503837cd 138 }
36a29a58 139}
Imprint / Impressum