2 Copyright 2012 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 static uint8_t debouncing = DEBOUNCE;
35 /* matrix state(1:on, 0:off) */
36 static matrix_row_t matrix[MATRIX_ROWS];
37 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39 static matrix_row_t read_cols(void);
40 static void init_cols(void);
41 static void unselect_rows(void);
42 static void select_row(uint8_t row);
46 uint8_t matrix_rows(void)
52 uint8_t matrix_cols(void)
57 /* generic STM32F103C8T6 board */
58 #ifdef BOARD_GENERIC_STM32_F103
59 #define LED_ON() do { palClearPad(GPIOC, GPIOC_LED) ;} while (0)
60 #define LED_OFF() do { palSetPad(GPIOC, GPIOC_LED); } while (0)
61 #define LED_TGL() do { palTogglePad(GPIOC, GPIOC_LED); } while (0)
65 #ifdef BOARD_MAPLEMINI_STM32_F103
66 #define LED_ON() do { palSetPad(GPIOB, 1) ;} while (0)
67 #define LED_OFF() do { palClearPad(GPIOB, 1); } while (0)
68 #define LED_TGL() do { palTogglePad(GPIOB, 1); } while (0)
71 void matrix_init(void)
73 // initialize row and col
77 // initialize matrix state: all keys off
78 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
80 matrix_debouncing[i] = 0;
90 uint8_t matrix_scan(void)
92 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
94 wait_us(30); // without this wait read unstable value.
95 matrix_row_t cols = read_cols();
96 if (matrix_debouncing[i] != cols) {
97 matrix_debouncing[i] = cols;
99 debug("bounce!: "); debug_hex(debouncing); debug("\n");
101 debouncing = DEBOUNCE;
110 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
111 matrix[i] = matrix_debouncing[i];
120 bool matrix_is_on(uint8_t row, uint8_t col)
122 return (matrix[row] & ((matrix_row_t)1<<col));
126 matrix_row_t matrix_get_row(uint8_t row)
131 void matrix_print(void)
133 print("\nr/c 0123456789ABCDEF\n");
134 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
135 phex(row); print(": ");
136 pbin_reverse16(matrix_get_row(row));
141 /* Column pin configuration
143 static void init_cols(void)
145 #ifdef BOARD_MAPLEMINI_STM32_F103
146 // don't need pullup/down, since it's pulled down in hardware
147 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
149 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_PULLDOWN);
153 /* Returns status of switches(1:on, 0:off) */
154 static matrix_row_t read_cols(void)
156 return ((palReadPad(GPIOB, 8)==PAL_LOW) ? 0 : (1<<0));
157 // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
160 /* Row pin configuration
162 static void unselect_rows(void)
164 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_INPUT); // hi-Z
167 static void select_row(uint8_t row)
170 // Output low to select
173 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_OUTPUT_PUSHPULL);
174 // palSetPad(GPIOA, GPIOA_PIN10, PAL_LOW);