aboutsummaryrefslogtreecommitdiff
path: root/keyboard/phantom/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/phantom/matrix.c')
-rw-r--r--keyboard/phantom/matrix.c249
1 files changed, 249 insertions, 0 deletions
diff --git a/keyboard/phantom/matrix.c b/keyboard/phantom/matrix.c
new file mode 100644
index 000000000..07f3f4289
--- /dev/null
+++ b/keyboard/phantom/matrix.c
@@ -0,0 +1,249 @@
1/* Copyright 2012 Jun Wako <wakojun@gmail.com>
2 *
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
5 *
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
8 *
9 * As for liscensing consult with the original files or its author.
10 */
11#include <stdint.h>
12#include <stdbool.h>
13#include <avr/io.h>
14#include <util/delay.h>
15#include "print.h"
16#include "debug.h"
17#include "util.h"
18#include "matrix.h"
19
20
21#ifndef DEBOUNCE
22# define DEBOUNCE 0
23#endif
24static uint8_t debouncing = DEBOUNCE;
25
26// bit array of key state(1:on, 0:off)
27static matrix_row_t *matrix;
28static matrix_row_t *matrix_debounced;
29static matrix_row_t _matrix0[MATRIX_ROWS];
30static matrix_row_t _matrix1[MATRIX_ROWS];
31
32
33#define _DDRA (uint8_t *const)&DDRA
34#define _DDRB (uint8_t *const)&DDRB
35#define _DDRC (uint8_t *const)&DDRC
36#define _DDRD (uint8_t *const)&DDRD
37#define _DDRE (uint8_t *const)&DDRE
38#define _DDRF (uint8_t *const)&DDRF
39
40#define _PINA (uint8_t *const)&PINA
41#define _PINB (uint8_t *const)&PINB
42#define _PINC (uint8_t *const)&PINC
43#define _PIND (uint8_t *const)&PIND
44#define _PINE (uint8_t *const)&PINE
45#define _PINF (uint8_t *const)&PINF
46
47#define _PORTA (uint8_t *const)&PORTA
48#define _PORTB (uint8_t *const)&PORTB
49#define _PORTC (uint8_t *const)&PORTC
50#define _PORTD (uint8_t *const)&PORTD
51#define _PORTE (uint8_t *const)&PORTE
52#define _PORTF (uint8_t *const)&PORTF
53
54#define _BIT0 0x01
55#define _BIT1 0x02
56#define _BIT2 0x04
57#define _BIT3 0x08
58#define _BIT4 0x10
59#define _BIT5 0x20
60#define _BIT6 0x40
61#define _BIT7 0x80
62
63/* Specifies the ports and pin numbers for the rows */
64static
65uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB};
66
67static
68uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
69
70static
71uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB};
72
73static
74const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5};
75
76/* Specifies the ports and pin numbers for the columns */
77static
78uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE,
79 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF,
80 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD};
81
82static
83uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE,
84 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF,
85 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD};
86
87static
88const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6,
89 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7,
90 _BIT7, _BIT6, _BIT1, _BIT2, _BIT3};
91
92static
93inline void pull_column(int col) {
94 *col_port[col] &= ~col_bit[col];
95}
96
97static
98inline void release_column(int col) {
99 *col_port[col] |= col_bit[col];
100}
101
102/* PORTB is set as input with pull-up resistors
103 PORTC,D,E,F are set to high output */
104
105static
106void setup_io_pins(void) {
107 uint8_t row, col;
108 for(row = 0; row < MATRIX_ROWS; row++) {
109 *row_ddr[row] &= ~row_bit[row];
110 *row_port[row] |= row_bit[row];
111 }
112 for(col = 0; col < MATRIX_COLS; col++) {
113 *col_ddr[col] |= col_bit[col];
114 *col_port[col] |= col_bit[col];
115 }
116}
117
118/* LEDs are on output compare pins OC1B OC1C
119 This activates fast PWM mode on them.
120 Prescaler 256 and 8-bit counter results in
121 16000000/256/256 = 244 Hz blink frequency.
122 LED_A: Caps Lock
123 LED_B: Scroll Lock */
124/* Output on PWM pins are turned off when the timer
125 reaches the value in the output compare register,
126 and are turned on when it reaches TOP (=256). */
127static
128void setup_leds(void) {
129 TCCR1A |= // Timer control register 1A
130 (1<<WGM10) | // Fast PWM 8-bit
131 (1<<COM1B1)| // Clear OC1B on match, set at TOP
132 (1<<COM1C1); // Clear OC1C on match, set at TOP
133 TCCR1B |= // Timer control register 1B
134 (1<<WGM12) | // Fast PWM 8-bit
135 (1<<CS12); // Prescaler 256
136 OCR1B = 250; // Output compare register 1B
137 OCR1C = 250; // Output compare register 1C
138 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
139 DDRB &= 0x3F;
140 PORTB &= 0x3F;
141}
142
143
144inline
145uint8_t matrix_rows(void)
146{
147 return MATRIX_ROWS;
148}
149
150inline
151uint8_t matrix_cols(void)
152{
153 return MATRIX_COLS;
154}
155
156void matrix_init(void)
157{
158 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
159 MCUCR |= (1<<JTD);
160 MCUCR |= (1<<JTD);
161
162 // initialize row and col
163 setup_io_pins();
164 setup_leds();
165
166 // initialize matrix state: all keys off
167 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
168 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
169 matrix = _matrix0;
170 matrix_debounced = _matrix1;
171}
172
173uint8_t matrix_scan(void)
174{
175 if (!debouncing) {
176 matrix_row_t *tmp = matrix_debounced;
177 matrix_debounced = matrix;
178 matrix = tmp;
179 }
180
181 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
182 pull_column(col); // output hi on theline
183 _delay_us(3); // without this wait it won't read stable value.
184 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
185 bool prev_bit = matrix[row] & ((matrix_row_t)1<<col);
186 bool curr_bit = !(*row_pin[row] & row_bit[row]);
187 if (prev_bit != curr_bit) {
188 matrix[row] ^= ((matrix_row_t)1<<col);
189 if (debouncing) {
190 debug("bounce!: "); debug_hex(debouncing); print("\n");
191 }
192 debouncing = DEBOUNCE;
193 }
194 }
195 release_column(col);
196 }
197
198 if (debouncing) {
199 debouncing--;
200 }
201
202 return 1;
203}
204
205bool matrix_is_modified(void)
206{
207 // NOTE: no longer used
208 return true;
209}
210
211inline
212bool matrix_has_ghost(void)
213{
214 return false;
215}
216
217inline
218bool matrix_is_on(uint8_t row, uint8_t col)
219{
220 return (matrix_debounced[row] & ((matrix_row_t)1<<col));
221}
222
223inline
224matrix_row_t matrix_get_row(uint8_t row)
225{
226 return matrix_debounced[row];
227}
228
229void matrix_print(void)
230{
231 print("\nr/c 01234567\n");
232 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
233 phex(row); print(": ");
234 pbin_reverse(matrix_get_row(row));
235 print("\n");
236 }
237}
238
239uint8_t matrix_key_count(void)
240{
241 uint8_t count = 0;
242 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
243 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
244 if (matrix_is_on(i, j))
245 count++;
246 }
247 }
248 return count;
249}