]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboard/hhkb/matrix.c
Add Ctrl + Up(SpaceFN) -> PgUp
[qmk_firmware.git] / keyboard / hhkb / matrix.c
index 350066b9040ece0a3f89c678f3d15f50575d0b04..d0731ef1f8f0406abccbc30a015746d07cd96cb2 100644 (file)
@@ -24,6 +24,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <avr/interrupt.h>
 #include <util/delay.h>
 #include "print.h"
+#include "debug.h"
 #include "util.h"
 #include "timer.h"
 #include "matrix.h"
@@ -34,31 +35,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #   error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
 #endif
 
-#if (MATRIX_COLS > 16)
-#   error "MATRIX_COLS must not exceed 16"
-#endif
-#if (MATRIX_ROWS > 255)
-#   error "MATRIX_ROWS must not exceed 255"
-#endif
-
 
 // matrix state buffer(1:on, 0:off)
-#if (MATRIX_COLS <= 8)
-static uint8_t *matrix;
-static uint8_t *matrix_prev;
-static uint8_t _matrix0[MATRIX_ROWS];
-static uint8_t _matrix1[MATRIX_ROWS];
-#else
-static uint16_t *matrix;
-static uint16_t *matrix_prev;
-static uint16_t _matrix0[MATRIX_ROWS];
-static uint16_t _matrix1[MATRIX_ROWS];
-#endif
-
-// HHKB has no ghost and no bounce.
-#ifdef MATRIX_HAS_GHOST
-static bool matrix_has_ghost_in_row(uint8_t row);
-#endif
+static matrix_row_t *matrix;
+static matrix_row_t *matrix_prev;
+static matrix_row_t _matrix0[MATRIX_ROWS];
+static matrix_row_t _matrix1[MATRIX_ROWS];
 
 
 // Matrix I/O ports
@@ -68,8 +50,8 @@ static bool matrix_has_ghost_in_row(uint8_t row);
 // key:     on: 0/off: 1
 // prev:    unknown: output previous key state(negated)?
 
-#ifdef HOST_PJRC
-// Ports for Teensy
+#if defined(__AVR_AT90USB1286__)
+// Ports for Teensy++
 // row:     PB0-2
 // col:     PB3-5,6
 // key:     PE6(pull-uped)
@@ -81,7 +63,7 @@ static bool matrix_has_ghost_in_row(uint8_t row);
     PORTE |= (1<<6);                    \
 } while (0)
 #define KEY_SELECT(ROW, COL)    (PORTB = (PORTB & 0xC0) |       \
-                                         (((COL) & 0x07)<<3) |    \
+                                         (((COL) & 0x07)<<3) |  \
                                          ((ROW) & 0x07))
 #define KEY_ENABLE()            (PORTB &= ~(1<<6))
 #define KEY_UNABLE()            (PORTB |=  (1<<6))
@@ -90,7 +72,52 @@ static bool matrix_has_ghost_in_row(uint8_t row);
 #define KEY_PREV_OFF()          (PORTE &= ~(1<<7))
 #define KEY_POWER_ON()
 #define KEY_POWER_OFF()
-#else
+
+#elif defined(__AVR_ATmega32U4__)
+// Ports for my designed Alt Controller PCB
+// row:     PB0-2
+// col:     PB3-5,6
+// key:     PD7(pull-uped)
+// prev:    PB7
+// power:   PD4(L:off/H:on)
+#define KEY_INIT()              do {    \
+    DDRB  = 0xFF;                       \
+    PORTB = 0x00;                       \
+    DDRD  &= ~0x80;                     \
+    PORTD |= 0x80;                      \
+    /* keyswitch board power on */      \
+    DDRD  |=  (1<<4);                   \
+    PORTD |=  (1<<4);                   \
+    KEY_UNABLE();                       \
+    KEY_PREV_OFF();                     \
+} while (0)
+#define KEY_SELECT(ROW, COL)    (PORTB = (PORTB & 0xC0) |       \
+                                         (((COL) & 0x07)<<3) |  \
+                                         ((ROW) & 0x07))
+#define KEY_ENABLE()            (PORTB &= ~(1<<6))
+#define KEY_UNABLE()            (PORTB |=  (1<<6))
+#define KEY_STATE()             (PIND & (1<<7))
+#define KEY_PREV_ON()           (PORTB |=  (1<<7))
+#define KEY_PREV_OFF()          (PORTB &= ~(1<<7))
+#define KEY_POWER_ON()
+#define KEY_POWER_OFF()
+/*
+#define KEY_POWER_ON()          do {    \
+    KEY_INIT();                         \
+    PORTD |=  (1<<4);                   \
+    _delay_ms(1);                       \
+} while (0)
+#define KEY_POWER_OFF()         do {    \
+    PORTD &= ~(1<<4);                   \
+    DDRB  &= ~0xFF;                     \
+    PORTB &= ~0xFF;                     \
+    DDRB  &= ~0x80;                     \
+    PORTB &= ~0x80;                     \
+} while (0)
+*/
+
+
+#elif defined(__AVR_ATmega328P__)
 // Ports for V-USB
 // key:     PB0(pull-uped)
 // prev:    PB1
@@ -118,7 +145,7 @@ static bool matrix_has_ghost_in_row(uint8_t row);
 #define KEY_POWER_ON()          do {    \
     KEY_INIT();                         \
     PORTB &= ~(1<<5);                   \
-    _delay_us(200);                     \
+    _delay_ms(1);                       \
 } while (0)
 #define KEY_POWER_OFF()         do {    \
     DDRB  &= ~0x3F;                     \
@@ -126,6 +153,9 @@ static bool matrix_has_ghost_in_row(uint8_t row);
     DDRC  &= ~0x0F;                     \
     PORTC &= ~0x0F;                     \
 } while (0)
+
+#else
+#   error "define code for matrix scan"
 #endif
 
 
@@ -143,6 +173,11 @@ uint8_t matrix_cols(void)
 
 void matrix_init(void)
 {
+#ifdef DEBUG
+    debug_enable = true;
+    debug_keyboard = true;
+#endif
+
     KEY_INIT();
 
     // initialize matrix state: all keys off
@@ -178,9 +213,21 @@ uint8_t matrix_scan(void)
             uint8_t last = TIMER_RAW;
 
             KEY_ENABLE();
+
             // Wait for KEY_STATE outputs its value.
             // 1us was ok on one HHKB, but not worked on another.
-            _delay_us(10);
+            // no   wait doesn't work on Teensy++ with pro(1us works)
+            // no   wait does    work on tmk PCB(8MHz) with pro2
+            // 1us  wait does    work on both of above
+            // 1us  wait doesn't work on tmk(16MHz)
+            // 5us  wait does    work on tmk(16MHz)
+            // 5us  wait does    work on tmk(16MHz/2)
+            // 5us  wait does    work on tmk(8MHz)
+            // 10us wait does    work on Teensy++ with pro
+            // 10us wait does    work on 328p+iwrap with pro
+            // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
+            _delay_us(5);
+
             if (KEY_STATE()) {
                 matrix[row] &= ~(1<<col);
             } else {
@@ -188,6 +235,8 @@ uint8_t matrix_scan(void)
             }
 
             // Ignore if this code region execution time elapses more than 20us.
+            // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
+            // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
             if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
                 matrix[row] = matrix_prev[row];
             }
@@ -215,12 +264,6 @@ bool matrix_is_modified(void)
 inline
 bool matrix_has_ghost(void)
 {
-#ifdef MATRIX_HAS_GHOST
-    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
-        if (matrix_has_ghost_in_row(i))
-            return true;
-    }
-#endif
     return false;
 }
 
@@ -231,64 +274,15 @@ bool matrix_is_on(uint8_t row, uint8_t col)
 }
 
 inline
-#if (MATRIX_COLS <= 8)
-uint8_t matrix_get_row(uint8_t row)
-#else
-uint16_t matrix_get_row(uint8_t row)
-#endif
+matrix_row_t matrix_get_row(uint8_t row)
 {
     return matrix[row];
 }
 
 void matrix_print(void)
 {
-#if (MATRIX_COLS <= 8)
     print("\nr/c 01234567\n");
-#else
-    print("\nr/c 0123456789ABCDEF\n");
-#endif
     for (uint8_t row = 0; row < matrix_rows(); row++) {
-        phex(row); print(": ");
-#if (MATRIX_COLS <= 8)
-        pbin_reverse(matrix_get_row(row));
-#else
-        pbin_reverse16(matrix_get_row(row));
-#endif
-#ifdef MATRIX_HAS_GHOST
-        if (matrix_has_ghost_in_row(row)) {
-            print(" <ghost");
-        }
-#endif
-        print("\n");
+        xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
     }
 }
-
-uint8_t matrix_key_count(void)
-{
-    uint8_t count = 0;
-    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
-#if (MATRIX_COLS <= 8)
-        count += bitpop(matrix[i]);
-#else
-        count += bitpop16(matrix[i]);
-#endif
-    }
-    return count;
-}
-
-#ifdef MATRIX_HAS_GHOST
-inline
-static bool matrix_has_ghost_in_row(uint8_t row)
-{
-    // no ghost exists in case less than 2 keys on
-    if (((matrix[row] - 1) & matrix[row]) == 0)
-        return false;
-
-    // ghost exists in case same state as other row
-    for (uint8_t i=0; i < MATRIX_ROWS; i++) {
-        if (i != row && (matrix[i] & matrix[row]) == matrix[row])
-            return true;
-    }
-    return false;
-}
-#endif