diff options
Diffstat (limited to 'adb/matrix.c')
| -rw-r--r-- | adb/matrix.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/adb/matrix.c b/adb/matrix.c new file mode 100644 index 000000000..d0643371e --- /dev/null +++ b/adb/matrix.c | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | /* | ||
| 2 | * scan matrix | ||
| 3 | */ | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <avr/io.h> | ||
| 7 | #include <util/delay.h> | ||
| 8 | #include "print.h" | ||
| 9 | #include "util.h" | ||
| 10 | #include "debug.h" | ||
| 11 | #include "adb.h" | ||
| 12 | #include "matrix_skel.h" | ||
| 13 | |||
| 14 | |||
| 15 | #if (MATRIX_COLS > 16) | ||
| 16 | # error "MATRIX_COLS must not exceed 16" | ||
| 17 | #endif | ||
| 18 | #if (MATRIX_ROWS > 255) | ||
| 19 | # error "MATRIX_ROWS must not exceed 255" | ||
| 20 | #endif | ||
| 21 | |||
| 22 | |||
| 23 | static bool _matrix_is_modified = false; | ||
| 24 | |||
| 25 | // matrix state buffer(1:on, 0:off) | ||
| 26 | #if (MATRIX_COLS <= 8) | ||
| 27 | static uint8_t *matrix; | ||
| 28 | static uint8_t _matrix0[MATRIX_ROWS]; | ||
| 29 | #else | ||
| 30 | static uint16_t *matrix; | ||
| 31 | static uint16_t _matrix0[MATRIX_ROWS]; | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #ifdef MATRIX_HAS_GHOST | ||
| 35 | static bool matrix_has_ghost_in_row(uint8_t row); | ||
| 36 | #endif | ||
| 37 | static void _register_key(uint8_t key); | ||
| 38 | |||
| 39 | |||
| 40 | inline | ||
| 41 | uint8_t matrix_rows(void) | ||
| 42 | { | ||
| 43 | return MATRIX_ROWS; | ||
| 44 | } | ||
| 45 | |||
| 46 | inline | ||
| 47 | uint8_t matrix_cols(void) | ||
| 48 | { | ||
| 49 | return MATRIX_COLS; | ||
| 50 | } | ||
| 51 | |||
| 52 | void matrix_init(void) | ||
| 53 | { | ||
| 54 | adb_host_init(); | ||
| 55 | |||
| 56 | // initialize matrix state: all keys off | ||
| 57 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; | ||
| 58 | matrix = _matrix0; | ||
| 59 | |||
| 60 | print_enable = true; | ||
| 61 | debug_enable = true; | ||
| 62 | debug_matrix = true; | ||
| 63 | debug_keyboard = true; | ||
| 64 | debug_mouse = true; | ||
| 65 | print("debug enabled.\n"); | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | uint8_t matrix_scan(void) | ||
| 70 | { | ||
| 71 | uint16_t codes; | ||
| 72 | uint8_t key0, key1; | ||
| 73 | |||
| 74 | _matrix_is_modified = false; | ||
| 75 | |||
| 76 | codes = adb_host_kbd_recv(); | ||
| 77 | key0 = codes>>8; | ||
| 78 | key1 = codes&0xFF; | ||
| 79 | if (debug_matrix) { | ||
| 80 | //print("adb_host_kbd_recv: "); phex16(codes); print("\n"); | ||
| 81 | } | ||
| 82 | |||
| 83 | if (codes == 0) { // no keys | ||
| 84 | return 0; | ||
| 85 | } else if (key0 == 0xFF && key1 != 0xFF) { // error | ||
| 86 | return codes&0xFF; | ||
| 87 | } else { | ||
| 88 | _matrix_is_modified = true; | ||
| 89 | _register_key(key0); | ||
| 90 | if (key1 != 0xFF) // key1 is 0xFF when no second key. | ||
| 91 | _register_key(key1); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | bool matrix_is_modified(void) | ||
| 98 | { | ||
| 99 | return _matrix_is_modified; | ||
| 100 | } | ||
| 101 | |||
| 102 | inline | ||
| 103 | bool matrix_has_ghost(void) | ||
| 104 | { | ||
| 105 | #ifdef MATRIX_HAS_GHOST | ||
| 106 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 107 | if (matrix_has_ghost_in_row(i)) | ||
| 108 | return true; | ||
| 109 | } | ||
| 110 | #endif | ||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | inline | ||
| 115 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 116 | { | ||
| 117 | return (matrix[row] & (1<<col)); | ||
| 118 | } | ||
| 119 | |||
| 120 | inline | ||
| 121 | #if (MATRIX_COLS <= 8) | ||
| 122 | uint8_t matrix_get_row(uint8_t row) | ||
| 123 | #else | ||
| 124 | uint16_t matrix_get_row(uint8_t row) | ||
| 125 | #endif | ||
| 126 | { | ||
| 127 | return matrix[row]; | ||
| 128 | } | ||
| 129 | |||
| 130 | void matrix_print(void) | ||
| 131 | { | ||
| 132 | #if (MATRIX_COLS <= 8) | ||
| 133 | print("\nr/c 01234567\n"); | ||
| 134 | #else | ||
| 135 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 136 | #endif | ||
| 137 | for (uint8_t row = 0; row < matrix_rows(); row++) { | ||
| 138 | phex(row); print(": "); | ||
| 139 | #if (MATRIX_COLS <= 8) | ||
| 140 | pbin_reverse(matrix_get_row(row)); | ||
| 141 | #else | ||
| 142 | pbin_reverse16(matrix_get_row(row)); | ||
| 143 | #endif | ||
| 144 | #ifdef MATRIX_HAS_GHOST | ||
| 145 | if (matrix_has_ghost_in_row(row)) { | ||
| 146 | print(" <ghost"); | ||
| 147 | } | ||
| 148 | #endif | ||
| 149 | print("\n"); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | uint8_t matrix_key_count(void) | ||
| 154 | { | ||
| 155 | uint8_t count = 0; | ||
| 156 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 157 | #if (MATRIX_COLS <= 8) | ||
| 158 | count += bitpop(matrix[i]); | ||
| 159 | #else | ||
| 160 | count += bitpop16(matrix[i]); | ||
| 161 | #endif | ||
| 162 | } | ||
| 163 | return count; | ||
| 164 | } | ||
| 165 | |||
| 166 | #ifdef MATRIX_HAS_GHOST | ||
| 167 | inline | ||
| 168 | static bool matrix_has_ghost_in_row(uint8_t row) | ||
| 169 | { | ||
| 170 | // no ghost exists in case less than 2 keys on | ||
| 171 | if (((matrix[row] - 1) & matrix[row]) == 0) | ||
| 172 | return false; | ||
| 173 | |||
| 174 | // ghost exists in case same state as other row | ||
| 175 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 176 | if (i != row && (matrix[i] & matrix[row]) == matrix[row]) | ||
| 177 | return true; | ||
| 178 | } | ||
| 179 | return false; | ||
| 180 | } | ||
| 181 | #endif | ||
| 182 | |||
| 183 | inline | ||
| 184 | static void _register_key(uint8_t key) | ||
| 185 | { | ||
| 186 | uint8_t col, row; | ||
| 187 | col = key&0x07; | ||
| 188 | row = (key>>3)&0x0F; | ||
| 189 | if (key&0x80) { | ||
| 190 | matrix[row] &= ~(1<<col); | ||
| 191 | } else { | ||
| 192 | matrix[row] |= (1<<col); | ||
| 193 | } | ||
| 194 | } | ||
