F256k matrix keyboard code producing scan codes
This commit is contained in:
parent
33ffdb09c3
commit
e85ab376f9
|
@ -56,33 +56,22 @@ static const uint8_t kbd_scan_codes[KBD_ROWS][KBD_COLUMNS] = {
|
||||||
// Driver variables
|
// Driver variables
|
||||||
//
|
//
|
||||||
|
|
||||||
static uint8_t kbd_stat[KBD_MATRIX_SIZE];
|
static uint16_t kbd_stat[KBD_MATRIX_SIZE];
|
||||||
static short counter = 0;
|
static short counter = 0;
|
||||||
static uint8_t last_press = 0;
|
static uint8_t last_press = 0;
|
||||||
static t_word_ring scan_code_buffer;
|
static t_word_ring scan_code_buffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the keys selected in a given column
|
* @brief Get the keys selected in a given row
|
||||||
*
|
*
|
||||||
* @param column the number of the column (0 - 8)
|
* @param row the number of the row to turn on (0 - 7)
|
||||||
* @return uint8_t a bitfield representing the keys in that column (0 = released, 1 = pressed)
|
* @return uint8_t a bitfield representing the keys in that row (0 = released, 1 = pressed)
|
||||||
*/
|
*/
|
||||||
static uint8_t kbd_get_rows(short column) {
|
static uint16_t kbd_get_columns(uint8_t row) {
|
||||||
uint8_t result = 0x00;
|
uint16_t result = 0;
|
||||||
|
|
||||||
if (column > 7) {
|
via1->pa = ~(0x01 << row);
|
||||||
via0->pb = 0x00;
|
result = ((uint16_t)via1->pb | (((uint16_t)(via0->pb) & 0x0080) << 1)) ^ 0x1ff;
|
||||||
result = via1->pa;
|
|
||||||
via0->pb = 0x80;
|
|
||||||
} else {
|
|
||||||
via1->pb = ~(0x01 << column);
|
|
||||||
result = via1->pa;
|
|
||||||
via1->pb = 0xff;
|
|
||||||
}
|
|
||||||
|
|
||||||
char message[80];
|
|
||||||
sprintf(message, "Row: %02X, %02X", result, column);
|
|
||||||
uart_writeln(0, message);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -96,8 +85,9 @@ static uint8_t kbd_get_rows(short column) {
|
||||||
*/
|
*/
|
||||||
static void kbd_process_key(short column, short row, bool is_pressed) {
|
static void kbd_process_key(short column, short row, bool is_pressed) {
|
||||||
uint8_t scan_code = kbd_scan_codes[row][column];
|
uint8_t scan_code = kbd_scan_codes[row][column];
|
||||||
|
|
||||||
if (scan_code != 0) {
|
if (scan_code != 0) {
|
||||||
if (!is_pressed) {
|
if (is_pressed == 0) {
|
||||||
if (last_press == scan_code) {
|
if (last_press == scan_code) {
|
||||||
// If we released the last key pressed, remove it from the typematic variables
|
// If we released the last key pressed, remove it from the typematic variables
|
||||||
last_press = 0;
|
last_press = 0;
|
||||||
|
@ -112,7 +102,7 @@ static void kbd_process_key(short column, short row, bool is_pressed) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rb_word_full(&scan_code_buffer)) {
|
if (!rb_word_full(&scan_code_buffer)) {
|
||||||
rb_word_put(&scan_code_buffer, (unsigned short)scan_code);
|
rb_word_put(&scan_code_buffer, scan_code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,23 +126,23 @@ unsigned short kbd_get_scancode() {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void kbd_handle_irq() {
|
void kbd_handle_irq() {
|
||||||
for (short column = 0; column < 8; column++) {
|
for (uint8_t row = 0; row < 8; row++) {
|
||||||
// Check each column to see if any key is pressed
|
// Check each column to see if any key is pressed
|
||||||
uint8_t rows_stat = kbd_get_rows(column);
|
uint16_t columns_stat = kbd_get_columns(row);
|
||||||
uint8_t rows_eor = kbd_stat[column] ^ rows_stat;
|
uint16_t columns_eor = kbd_stat[row] ^ columns_stat;
|
||||||
if (rows_eor != 0) {
|
if (columns_eor != 0) {
|
||||||
short row = 0;
|
short column = 0;
|
||||||
|
|
||||||
kbd_stat[column] = rows_stat;
|
kbd_stat[row] = columns_stat;
|
||||||
while (rows_eor != 0) {
|
while (columns_eor != 0) {
|
||||||
if (rows_eor & 0x01) {
|
if (columns_eor & 0x01) {
|
||||||
// Current key changed
|
// Current key changed
|
||||||
kbd_process_key(column, row, rows_stat && 0x01);
|
kbd_process_key(column, row, columns_stat && 0x01);
|
||||||
}
|
}
|
||||||
|
|
||||||
rows_stat = rows_stat >> 1;
|
columns_stat = columns_stat >> 1;
|
||||||
rows_eor = rows_eor >> 1;
|
columns_eor = columns_eor >> 1;
|
||||||
row++;
|
column++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,16 +153,16 @@ void kbd_handle_irq() {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
short kbd_init() {
|
short kbd_init() {
|
||||||
// Initialize VIA0 -- we'll just write to PB7
|
// Initialize VIA0 -- we'll just read from PB7
|
||||||
via0->ddra = 0x00;
|
via0->ddra = 0x00;
|
||||||
via0->ddrb = 0x80;
|
via0->ddrb = 0x00;
|
||||||
via0->acr = 0x00;
|
via0->acr = 0x00;
|
||||||
via0->pcr = 0x00;
|
via0->pcr = 0x00;
|
||||||
via0->ier = 0x00;
|
via0->ier = 0x00;
|
||||||
|
|
||||||
// Initialize VIA1 -- we'll write to all of PB
|
// Initialize VIA1 -- we'll write to all of PB
|
||||||
via1->ddra = 0x00;
|
via1->ddra = 0xff;
|
||||||
via1->ddrb = 0xff;
|
via1->ddrb = 0x00;
|
||||||
via1->acr = 0x00;
|
via1->acr = 0x00;
|
||||||
via1->pcr = 0x00;
|
via1->pcr = 0x00;
|
||||||
via1->ier = 0x00;
|
via1->ier = 0x00;
|
||||||
|
|
|
@ -285,15 +285,14 @@ int main(int argc, char * argv[]) {
|
||||||
|
|
||||||
initialize();
|
initialize();
|
||||||
|
|
||||||
// while (1) {
|
kbd_init();
|
||||||
// kbd_handle_irq();
|
while (1) {
|
||||||
// unsigned short scan_code = kbd_get_scancode();
|
kbd_handle_irq();
|
||||||
// if (scan_code != 0) {
|
unsigned short scan_code = kbd_get_scancode();
|
||||||
// tvky_text_matrix[0] = dec2hex((scan_code & 0xf0) >> 4);
|
if (scan_code != 0) {
|
||||||
// tvky_text_matrix[1] = dec2hex(scan_code & 0x0f);
|
INFO1("0x%02X ", scan_code);
|
||||||
// tvky_text_matrix[2] += 1;
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// Attempt to start up the user code
|
// Attempt to start up the user code
|
||||||
// log(LOG_INFO, "Looking for user startup code:");
|
// log(LOG_INFO, "Looking for user startup code:");
|
||||||
|
|
Loading…
Reference in a new issue