tldr: viewtopic.php?p=456023#p456023
Code: Select all
/*
* scan matrix
*/
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
#include "print.h"
#include "debug.h"
#include "util.h"
#include "matrix.h"
#ifndef DEBOUNCE
# define DEBOUNCE 4
#endif
static uint8_t debouncing = DEBOUNCE;
/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
static matrix_row_t read_cols(void);
static void init_cols(void);
static void init_rows(void);
static uint8_t select_row(uint8_t row);
static uint8_t index;
static uint8_t row_to_select;
void matrix_init(void)
{
// initialize row and col
init_cols();
init_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++) {
uint8_t allow_select = select_row(i);
//_delay_us(30);
if (allow_select) {
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
}
}
if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
}
}
return 1;
}
inline
matrix_row_t matrix_get_row(uint8_t row)
{
return matrix[row];
}
/* Column pin configuration
* col: 0 1 2 3 4 5 6 7 8 9 10 11
* pin: B7 D0 D1 D2 D3 D4 D5 D7 E0 E1 C0 C1
*/
static void init_cols(void)
{
DDRB &= ~(1<<0);
PORTB |= (1<<0);
DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5| 1<<7);
PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5| 1<<7);
DDRE &= ~(1<<0 | 1<<1);
PORTE |= (1<<0 | 1<<1);
DDRC &= ~(1<<0 | 1<<1);
PORTC |= (1<<0 | 1<<1);
}
static matrix_row_t read_cols(void)
{
return (PINB&(1<<0) ? 0 : (1<<0)) |
(PIND&(1<<0) ? 0 : (1<<1)) |
(PIND&(1<<1) ? 0 : (1<<2)) |
(PIND&(1<<2) ? 0 : (1<<3)) |
(PIND&(1<<3) ? 0 : (1<<4)) |
(PIND&(1<<4) ? 0 : (1<<5)) |
(PIND&(1<<5) ? 0 : (1<<6)) |
(PIND&(1<<7) ? 0 : (1<<7)) |
(PINE&(1<<0) ? 0 : (1<<8)) |
(PINE&(1<<1) ? 0 : (1<<9)) |
(PINC&(1<<0) ? 0 : (1<<10)) |
(PINC&(1<<1) ? 0 : (1<<11));
}
/* Row pin configuration
* row: 0 1 2 3 4 5 6 7 8 9 10 11
* pin: B6 B5 B4 B3 B2 B1 B0 E7 E6 F0 F1 F2
*/
static void init_rows(void)
{
DDRB &= ~(1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
PORTB |= (1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
DDRE &= ~(1<<7 | 1<<6);
PORTE |= (1<<7 | 1<<6);
DDRF &= ~(1<<2 | 1<<1 | 1<<0);
PORTF |= (1<<2 | 1<<1 | 1<<0);
}
static uint8_t select_row(uint8_t row)
{
uint8_t allow_select = 0;
switch (row) {
case 0:
allow_select = (PINB&(1<<3) ? 0 : 1);
break;
case 1:
allow_select = (PINB&(1<<5) ? 0 : 1);
break;
case 2:
allow_select = (PINB&(1<<4) ? 0 : 1);
break;
case 3:
allow_select = (PINB&(1<<3) ? 0 : 1);
break;
case 4:
allow_select = (PINB&(1<<2) ? 0 : 1);
break;
case 5:
allow_select = (PINB&(1<<1) ? 0 : 1);
break;
case 6:
allow_select = (PINB&(1<<0) ? 0 : 1);
break;
case 7:
allow_select = (PINE&(1<<7) ? 0 : 1);
break;
case 8:
allow_select = (PINE&(1<<6) ? 0 : 1);
break;
case 9:
allow_select = (PINF&(1<<0) ? 0 : 1);
break;
case 10:
allow_select = (PINF&(1<<1) ? 0 : 1);
break;
case 11:
allow_select = (PINF&(1<<2) ? 0 : 1);
break;
}
return allow_select;
}
I got this keyboard yesterday and I was very excited so I wanted to do a video opening it up and checking things out.
Didn't find anything really crazy but I did discover something yesterday which I'll talk about later (re: conversion). What I'll say now is that there are some definite signs of life. I do not know if there's a mechanism for NKRO -- this is one piece of the puzzle that I'm not seeing. But I feel this keyboard is absolutely convertable. Definitely going to need lots of help, though, as I wade through these new waters.