7 #include <avr/interrupt.h>
8 #include <util/delay.h>
11 // Timer resolution check
12 #if (1000000/TIMER_RAW_FREQ > 20)
13 # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
20 * row: HC4051[A,B,C] selects scan row0-7
21 * row-ext: [En0,En1] row extention for JP
22 * col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
24 * prev: hysteresis control: assert(1) when previous key state is on
28 #if defined(__AVR_ATmega32U4__)
30 * For TMK HHKB alt controller(ATMega32U4)
36 * power: PD4(L:off/H:on)
37 * row-ext: PC6,7 for HHKB JP(active low)
39 static inline void KEY_ENABLE(void) { (PORTB &= ~(1<<6)); }
40 static inline void KEY_UNABLE(void) { (PORTB |= (1<<6)); }
41 static inline bool KEY_STATE(void) { return (PIND & (1<<7)); }
42 static inline void KEY_PREV_ON(void) { (PORTB |= (1<<7)); }
43 static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); }
44 #ifdef HHKB_POWER_SAVING
45 static inline void KEY_POWER_ON(void) {
46 _delay_ms(10); // TODO: sleep to save power
47 DDRB = 0xFF; PORTB = 0x40; // change pins output
48 DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on
49 /* Without this wait you will miss or get false key events. */
50 _delay_ms(1); // wait for powering up
52 static inline void KEY_POWER_OFF(void) {
53 /* input with pull-up consumes less than without it when pin is open. */
54 DDRB = 0x00; PORTB = 0xFF; // change pins input with pull-up
55 DDRD |= (1<<4); PORTD &= ~(1<<4); // MOS FET switch off
58 static inline void KEY_POWER_ON(void) {}
59 static inline void KEY_POWER_OFF(void) {}
61 static inline void KEY_INIT(void)
63 /* row,col,prev: output */
65 PORTB = 0x40; // unable
66 /* key: input with pull-up */
70 /* row extention for HHKB JP */
79 static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
81 PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
83 if ((ROW) & 0x08) PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<6);
84 else PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<7);
89 #elif defined(__AVR_AT90USB1286__)
91 * For Teensy++(AT90USB1286)
94 * row: PB0-2 (6-8) (5-7)
95 * col: PB3-5,6 (9-12) (8-11)
96 * key: PE6(pull-uped) (4) (3)
99 * TODO: convert into 'staitc inline' function
101 #define KEY_INIT() do { \
107 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
108 (((COL) & 0x07)<<3) | \
110 #define KEY_ENABLE() (PORTB &= ~(1<<6))
111 #define KEY_UNABLE() (PORTB |= (1<<6))
112 #define KEY_STATE() (PINE & (1<<6))
113 #define KEY_PREV_ON() (PORTE |= (1<<7))
114 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
115 #define KEY_POWER_ON()
116 #define KEY_POWER_OFF()
120 # error "define code for matrix scan"
125 // For ATMega328P with V-USB
127 // #elif defined(__AVR_ATmega328P__)
129 // key: PB0(pull-uped)
133 // power: PB5(Low:on/Hi-z:off)
134 #define KEY_INIT() do { \
142 #define KEY_SELECT(ROW, COL) do { \
143 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
144 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
146 #define KEY_ENABLE() (PORTC &= ~(1<<3))
147 #define KEY_UNABLE() (PORTC |= (1<<3))
148 #define KEY_STATE() (PINB & (1<<0))
149 #define KEY_PREV_ON() (PORTB |= (1<<1))
150 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
151 // Power supply switching
152 #define KEY_POWER_ON() do { \
157 #define KEY_POWER_OFF() do { \