matrix.c (2878B)
1 /* 2 3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com> 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 /* 20 * scan matrix 21 */ 22 #include <stdint.h> 23 #include <stdbool.h> 24 #include <avr/io.h> 25 #include <util/delay.h> 26 #include "action_layer.h" 27 #include "print.h" 28 #include "debug.h" 29 #include "util.h" 30 #include "matrix.h" 31 #include "timer.h" 32 #include <string.h> 33 34 /* matrix state(1:on, 0:off) */ 35 static matrix_row_t matrix[MATRIX_ROWS]; 36 static matrix_row_t matrix_stage[MATRIX_ROWS]; 37 static matrix_row_t matrix_debouncing[MATRIX_ROWS]; 38 39 static uint16_t debouncing_time; 40 static bool debouncing = false; 41 42 __attribute__ ((weak)) 43 void matrix_init_kb(void) { 44 matrix_init_user(); 45 } 46 47 __attribute__ ((weak)) 48 void matrix_scan_kb(void) { 49 matrix_scan_user(); 50 } 51 52 __attribute__ ((weak)) 53 void matrix_init_user(void) { 54 } 55 56 __attribute__ ((weak)) 57 void matrix_scan_user(void) { 58 } 59 60 inline 61 uint8_t matrix_rows(void) 62 { 63 return MATRIX_ROWS; 64 } 65 66 inline 67 uint8_t matrix_cols(void) 68 { 69 return MATRIX_COLS; 70 } 71 72 void matrix_init(void) 73 { 74 gpio_set_pin_input_high(C7); 75 gpio_set_pin_input_high(B5); 76 gpio_set_pin_input_high(B7); 77 gpio_set_pin_input_high(D1); 78 gpio_set_pin_input_high(D4); 79 gpio_set_pin_input_high(D6); 80 81 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 82 matrix[i] = 0; 83 matrix_debouncing[i] = 0; 84 matrix_stage[i] = 0; 85 } 86 87 matrix_init_kb(); 88 89 } 90 91 uint8_t matrix_scan(void) 92 { 93 matrix_stage[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2)); 94 matrix_stage[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2)); 95 96 if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) { 97 debouncing = true; 98 debouncing_time = timer_read(); 99 } 100 101 matrix_debouncing[0] = matrix_stage[0]; 102 matrix_debouncing[1] = matrix_stage[1]; 103 104 if (debouncing && (timer_elapsed(debouncing_time) > 20)) { 105 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 106 matrix[i] = matrix_debouncing[i]; 107 } 108 debouncing = false; 109 } 110 111 matrix_scan_kb(); 112 113 return 1; 114 } 115 116 inline 117 bool matrix_is_on(uint8_t row, uint8_t col) 118 { 119 return (matrix[row] & ((matrix_row_t)1<<col)); 120 } 121 122 inline 123 matrix_row_t matrix_get_row(uint8_t row) 124 { 125 return matrix[row]; 126 } 127 128 void matrix_print(void) 129 { 130 }