diff options
Diffstat (limited to 'x68k_usb/matrix.c')
| -rw-r--r-- | x68k_usb/matrix.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/x68k_usb/matrix.c b/x68k_usb/matrix.c new file mode 100644 index 000000000..954e7ef12 --- /dev/null +++ b/x68k_usb/matrix.c | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 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. | ||
| 8 | |||
| 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. | ||
| 13 | |||
| 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/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <stdint.h> | ||
| 19 | #include <stdbool.h> | ||
| 20 | #include <avr/io.h> | ||
| 21 | #include <util/delay.h> | ||
| 22 | #include "print.h" | ||
| 23 | #include "util.h" | ||
| 24 | #include "x68k.h" | ||
| 25 | #include "matrix.h" | ||
| 26 | |||
| 27 | |||
| 28 | /* | ||
| 29 | * Matrix Array usage: | ||
| 30 | * | ||
| 31 | * ROW: 16(4bits) | ||
| 32 | * COL: 8(3bits) | ||
| 33 | * | ||
| 34 | * 8bit wide | ||
| 35 | * +---------+ | ||
| 36 | * 0|00 ... 07| | ||
| 37 | * 1|08 ... 0F| | ||
| 38 | * :| ... | | ||
| 39 | * :| ... | | ||
| 40 | * E|70 ... 77| | ||
| 41 | * F|78 ... 7F| | ||
| 42 | * +---------+ | ||
| 43 | * | ||
| 44 | */ | ||
| 45 | static uint8_t matrix[MATRIX_ROWS]; | ||
| 46 | #define ROW(code) ((code>>3)&0xF) | ||
| 47 | #define COL(code) (code&0x07) | ||
| 48 | |||
| 49 | static bool is_modified = false; | ||
| 50 | |||
| 51 | |||
| 52 | inline | ||
| 53 | uint8_t matrix_rows(void) | ||
| 54 | { | ||
| 55 | return MATRIX_ROWS; | ||
| 56 | } | ||
| 57 | |||
| 58 | inline | ||
| 59 | uint8_t matrix_cols(void) | ||
| 60 | { | ||
| 61 | return MATRIX_COLS; | ||
| 62 | } | ||
| 63 | |||
| 64 | void matrix_init(void) | ||
| 65 | { | ||
| 66 | x68k_init(); | ||
| 67 | |||
| 68 | // initialize matrix state: all keys off | ||
| 69 | for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; | ||
| 70 | |||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | uint8_t matrix_scan(void) | ||
| 75 | { | ||
| 76 | is_modified = false; | ||
| 77 | |||
| 78 | uint8_t code; | ||
| 79 | code = x68k_recv(); | ||
| 80 | if (code == 0) { | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | phex(code); print(" "); | ||
| 85 | if (code&0x80) { | ||
| 86 | // break code | ||
| 87 | if (matrix_is_on(ROW(code), COL(code))) { | ||
| 88 | matrix[ROW(code)] &= ~(1<<COL(code)); | ||
| 89 | is_modified = true; | ||
| 90 | } | ||
| 91 | } else { | ||
| 92 | // make code | ||
| 93 | if (!matrix_is_on(ROW(code), COL(code))) { | ||
| 94 | matrix[ROW(code)] |= (1<<COL(code)); | ||
| 95 | is_modified = true; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | return code; | ||
| 99 | } | ||
| 100 | |||
| 101 | bool matrix_is_modified(void) | ||
| 102 | { | ||
| 103 | return is_modified; | ||
| 104 | } | ||
| 105 | |||
| 106 | inline | ||
| 107 | bool matrix_has_ghost(void) | ||
| 108 | { | ||
| 109 | return false; | ||
| 110 | } | ||
| 111 | |||
| 112 | inline | ||
| 113 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 114 | { | ||
| 115 | return (matrix[row] & (1<<col)); | ||
| 116 | } | ||
| 117 | |||
| 118 | inline | ||
| 119 | uint8_t matrix_get_row(uint8_t row) | ||
| 120 | { | ||
| 121 | return matrix[row]; | ||
| 122 | } | ||
| 123 | |||
| 124 | void matrix_print(void) | ||
| 125 | { | ||
| 126 | print("\nr/c 01234567\n"); | ||
| 127 | for (uint8_t row = 0; row < matrix_rows(); row++) { | ||
| 128 | phex(row); print(": "); | ||
| 129 | pbin_reverse(matrix_get_row(row)); | ||
| 130 | print("\n"); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | uint8_t matrix_key_count(void) | ||
| 135 | { | ||
| 136 | uint8_t count = 0; | ||
| 137 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 138 | count += bitpop(matrix[i]); | ||
| 139 | } | ||
| 140 | return count; | ||
| 141 | } | ||
