]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - macway/matrix.c
add debouncing into macway/matrix.c.
[max/tmk_keyboard.git] / macway / matrix.c
index f8c0640fd74d734e40cf2c4d01fcea7066152bc2..56fb85896025311ad85cfac7aa485d94a623a239 100644 (file)
@@ -1,3 +1,20 @@
+/*
+Copyright 2011 Jun Wako <wakojun@gmail.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 /*
  * scan matrix
  */
@@ -6,8 +23,9 @@
 #include <avr/io.h>
 #include <util/delay.h>
 #include "print.h"
+#include "debug.h"
 #include "util.h"
-#include "matrix_skel.h"
+#include "matrix.h"
 
 
 #if (MATRIX_COLS > 16)
 #endif
 
 
+#ifndef DEBOUNCE
+#   define DEBOUNCE    0
+#endif
+static uint8_t debouncing = DEBOUNCE;
+
 // matrix state buffer(1:on, 0:off)
 #if (MATRIX_COLS <= 8)
 static uint8_t *matrix;
@@ -68,27 +91,40 @@ void matrix_init(void)
 
 uint8_t matrix_scan(void)
 {
-    uint8_t *tmp;
-
-    tmp = matrix_prev;
-    matrix_prev = matrix;
-    matrix = tmp;
+    if (!debouncing) {
+        uint8_t *tmp = matrix_prev;
+        matrix_prev = matrix;
+        matrix = tmp;
+    }
 
     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
         unselect_rows();
         select_row(i);
         _delay_us(30);  // without this wait read unstable value.
-        matrix[i] = ~read_col();
+        if (matrix[i] != (uint8_t)~read_col()) {
+            matrix[i] = (uint8_t)~read_col();
+            if (debouncing) {
+                debug("bounce!: "); debug_hex(debouncing); print("\n");
+            }
+            debouncing = DEBOUNCE;
+        }
     }
     unselect_rows();
+
+    if (debouncing) {
+        debouncing--;
+    }
+
     return 1;
 }
 
 bool matrix_is_modified(void)
 {
+    if (debouncing) return false;
     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
-        if (matrix[i] != matrix_prev[i])
+        if (matrix[i] != matrix_prev[i]) {
             return true;
+        }
     }
     return false;
 }