/* Copyright 2014 Warren Janssens 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 . */ /* * scan matrix */ #include #include #include #include #include "action_layer.h" #include "print.h" #include "debug.h" #include "util.h" #include "matrix.h" #include "led.h" #ifndef DEBOUNCE # define DEBOUNCE 5 #endif static uint8_t debouncing = DEBOUNCE; /* matrix state(1:on, 0:off) */ static uint8_t matrix[MATRIX_ROWS]; static uint8_t matrix_debouncing[MATRIX_ROWS]; static matrix_row_t read_row(uint8_t row); static void unselect_rows(void); static void select_rows(uint8_t row); __attribute__ ((weak)) void matrix_init_kb(void) { matrix_init_user(); } __attribute__ ((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } __attribute__ ((weak)) void matrix_init_user(void) { } __attribute__ ((weak)) void matrix_scan_user(void) { } inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } inline uint8_t matrix_cols(void) { return MATRIX_COLS; } void matrix_init(void) { //debug_enable = true; //dprint("matrix_init"); dprintln(); // output high (leds) DDRD = 0xFF; PORTD = 0xFF; // output low (multiplexers) DDRF = 0xFF; PORTF = 0x00; // input with pullup (matrix) DDRB = 0x00; PORTB = 0xFF; // input with pullup (program and keypad buttons) DDRC = 0x00; PORTC = 0xFF; // initialize row and col unselect_rows(); // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) { matrix[i] = 0; matrix_debouncing[i] = 0; } } uint8_t matrix_scan(void) { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { select_rows(i); uint8_t row = read_row(i); if (matrix_debouncing[i] != row) { matrix_debouncing[i] = row; if (debouncing) { dprintf("bounce!: %02X\n", debouncing); } debouncing = DEBOUNCE; } unselect_rows(); } if (debouncing) { if (--debouncing) { _delay_ms(1); } else { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { matrix[i] = matrix_debouncing[i]; } } } matrix_scan_kb(); return 1; } inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1< /? RS */