]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - keyboard/hid_liber/matrix.c
Merge branch 'rhaberkorn-serial-mouse'
[tmk_firmware.git] / keyboard / hid_liber / matrix.c
index de1790d7e9e40689f6e23a7befbe5b88f92946b2..adf6ac8c428573bfadb558a74a0683e7ffcff084 100644 (file)
 
 
 #ifndef DEBOUNCE
-#   define DEBOUNCE    5
+#   define DEBOUNCE 0
 #endif
 static uint8_t debouncing = DEBOUNCE;
 
 // bit array of key state(1:on, 0:off)
-static matrix_row_t *matrix;
-static matrix_row_t *matrix_debounced;
-static matrix_row_t _matrix0[MATRIX_ROWS];
-static matrix_row_t _matrix1[MATRIX_ROWS];
+static matrix_row_t matrix[MATRIX_ROWS];
+static matrix_row_t matrix_debouncing[MATRIX_ROWS];
 
 
-#define NROW 18
-#define NCOL 8
 #define _DDRA (uint8_t *const)&DDRA
 #define _DDRB (uint8_t *const)&DDRB
 #define _DDRC (uint8_t *const)&DDRC
@@ -64,35 +60,39 @@ static matrix_row_t _matrix1[MATRIX_ROWS];
 
 /* Specifies the ports and pin numbers for the rows */
 static
-uint8_t *const  row_ddr[NROW] = {                                 _DDRB,                  _DDRB,
-                                                                                 _DDRC,  _DDRC,
-                                 _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,
-                                 _DDRF,  _DDRF,                  _DDRF,  _DDRF,  _DDRF,  _DDRF};
+uint8_t *const row_ddr[MATRIX_ROWS] = {
+                                           _DDRB,                  _DDRB,
+                                                           _DDRC,  _DDRC,
+           _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,
+           _DDRF,  _DDRF,                  _DDRF,  _DDRF,  _DDRF,  _DDRF};
 
 static
-uint8_t *const row_port[NROW] = {                                _PORTB,                 _PORTB,
-                                _PORTC, _PORTC,
-                                _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
-                                _PORTF, _PORTF,                 _PORTF, _PORTF, _PORTF, _PORTF};
+uint8_t *const row_port[MATRIX_ROWS] = {
+                                          _PORTB,                 _PORTB,
+                                                          _PORTC, _PORTC,
+          _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
+          _PORTF, _PORTF,                 _PORTF, _PORTF, _PORTF, _PORTF};
 
 static
-uint8_t *const  row_pin[NROW] = {                                 _PINB,                  _PINB,
-                                 _PINC,  _PINC,
-                                 _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,
-                                 _PINF,  _PINF,                  _PINF,  _PINF,  _PINF,  _PINF};
+uint8_t *const row_pin[MATRIX_ROWS] = {
+                                           _PINB,                  _PINB,
+                                                           _PINC,  _PINC,
+           _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,
+           _PINF,  _PINF,                  _PINF,  _PINF,  _PINF,  _PINF};
 
 static
-const uint8_t   row_bit[NROW] = {                                 _BIT4,                  _BIT7,
-                                                                                 _BIT6,  _BIT7,
-                                 _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5,  _BIT6,  _BIT7,
-                                 _BIT0,  _BIT1,                  _BIT4,  _BIT5,  _BIT6,  _BIT7};
+const uint8_t row_bit[MATRIX_ROWS] = {
+                                           _BIT4,                  _BIT7,
+                                                           _BIT6,  _BIT7,
+           _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5,  _BIT6,  _BIT7,
+           _BIT0,  _BIT1,                  _BIT4,  _BIT5,  _BIT6,  _BIT7};
 
 static
 const uint8_t mask = 0x0E;
 
 /* Specifies the ports and pin numbers for the columns */
 static
-const uint8_t   col_bit[NCOL] = {  0x00,   0x02,   0x04,   0x06,   0x08,   0x0A,   0x0C,   0x0E};
+const uint8_t   col_bit[MATRIX_COLS] = {  0x00,   0x02,   0x04,   0x06,   0x08,   0x0A,   0x0C,   0x0E};
 
 static
 inline void pull_column(int col) {
@@ -110,7 +110,7 @@ void setup_io_pins(void) {
   uint8_t row;
   DDRB  |=  0x0E;
   PORTB &= ~0x0E;
-  for(row = 0; row < NROW; row++) {
+  for(row = 0; row < MATRIX_ROWS; row++) {
     *row_ddr[row]  &= ~row_bit[row];
     *row_port[row] &= ~row_bit[row];
   }
@@ -137,35 +137,33 @@ uint8_t matrix_cols(void)
 
 void matrix_init(void)
 {
+    // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
+    MCUCR |= (1<<JTD);
+    MCUCR |= (1<<JTD);
+
     // initialize row and col
     setup_io_pins();
     setup_leds();
 
     // initialize matrix state: all keys off
-    for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
-    for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
-    matrix = _matrix0;
-    matrix_debounced = _matrix1;
+    for (uint8_t i=0; i < MATRIX_ROWS; i++) {
+        matrix[i] = 0;
+        matrix_debouncing[i] = 0;
+    }
 }
 
 uint8_t matrix_scan(void)
 {
-    if (!debouncing) {
-        uint8_t *tmp = matrix_debounced;
-        matrix_debounced = matrix;
-        matrix = tmp;
-    }
-
-    for (uint8_t col = 0; col < NCOL; col++) {  // 0-7
+    for (uint8_t col = 0; col < MATRIX_COLS; col++) {  // 0-7
         pull_column(col);   // output hi on theline
-        _delay_us(1);       // without this wait it won't read stable value.
-        for (uint8_t row = 0; row < NROW; row++) {  // 0-17
-            bool prev_bit = matrix[row] & (1<<col);
+        _delay_us(5);       // without this wait it won't read stable value.
+        for (uint8_t row = 0; row < MATRIX_ROWS; row++) {  // 0-17
+            bool prev_bit = matrix_debouncing[row] & (1<<col);
             bool curr_bit = *row_pin[row] & row_bit[row];
             if (prev_bit != curr_bit) {
-                matrix[row] ^= (1<<col);
+                matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
                 if (debouncing) {
-                    debug("bounce!: "); debug_hex(debouncing); print("\n");
+                    dprintf("bounce!: %02X\n", debouncing);
                 }
                 debouncing = DEBOUNCE;
             }
@@ -174,7 +172,13 @@ uint8_t matrix_scan(void)
     }
 
     if (debouncing) {
-        debouncing--;
+        if (--debouncing) {
+            _delay_ms(1);
+        } else {
+            for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+                matrix[i] = matrix_debouncing[i];
+            }
+        }
     }
 
     return 1;
@@ -195,13 +199,13 @@ bool matrix_has_ghost(void)
 inline
 bool matrix_is_on(uint8_t row, uint8_t col)
 {
-    return (matrix_debounced[row] & (1<<col));
+    return (matrix[row] & ((matrix_row_t)1<<col));
 }
 
 inline
 matrix_row_t matrix_get_row(uint8_t row)
 {
-    return matrix_debounced[row];
+    return matrix[row];
 }
 
 void matrix_print(void)