]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/phantom/matrix.c
Add initial files for Phantom from Tranquilite@GH.
[tmk_firmware.git] / keyboard / phantom / matrix.c
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
24 static uint8_t debouncing = DEBOUNCE;
25
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t *matrix;
28 static matrix_row_t *matrix_debounced;
29 static matrix_row_t _matrix0[MATRIX_ROWS];
30 static 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 */
64 static
65 uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB,  _DDRB,  _DDRB,  _DDRB,  _DDRB,  _DDRB};
66
67 static
68 uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
69
70 static
71 uint8_t *const row_pin[MATRIX_ROWS] = {_PINB,  _PINB,  _PINB,  _PINB,  _PINB,  _PINB};
72
73 static
74 const uint8_t row_bit[MATRIX_ROWS] = { _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5};
75
76 /* Specifies the ports and pin numbers for the columns */
77 static
78 uint8_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
82 static
83 uint8_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
87 static
88 const 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
92 static
93 inline void pull_column(int col) {
94   *col_port[col] &= ~col_bit[col];
95 }
96
97 static
98 inline 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
105 static
106 void 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). */
127 static
128 void 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
144 inline
145 uint8_t matrix_rows(void)
146 {
147     return MATRIX_ROWS;
148 }
149
150 inline
151 uint8_t matrix_cols(void)
152 {
153     return MATRIX_COLS;
154 }
155
156 void 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
173 uint8_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
205 bool matrix_is_modified(void)
206 {
207     // NOTE: no longer used
208     return true;
209 }
210
211 inline
212 bool matrix_has_ghost(void)
213 {
214     return false;
215 }
216
217 inline
218 bool matrix_is_on(uint8_t row, uint8_t col)
219 {
220     return (matrix_debounced[row] & ((matrix_row_t)1<<col));
221 }
222
223 inline
224 matrix_row_t matrix_get_row(uint8_t row)
225 {
226     return matrix_debounced[row];
227 }
228
229 void 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
239 uint8_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 }