aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ymdk/sp64/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/ymdk/sp64/matrix.c')
-rw-r--r--keyboards/ymdk/sp64/matrix.c169
1 files changed, 83 insertions, 86 deletions
diff --git a/keyboards/ymdk/sp64/matrix.c b/keyboards/ymdk/sp64/matrix.c
index 6f8c7962eb..97fdb87ecb 100644
--- a/keyboards/ymdk/sp64/matrix.c
+++ b/keyboards/ymdk/sp64/matrix.c
@@ -40,132 +40,129 @@ static uint8_t mcp23018_reset_loop = 0;
// user-defined overridable functions
-__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
+__attribute__((weak)) void matrix_init_kb(void) {
+ matrix_init_user();
+}
-__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
+__attribute__((weak)) void matrix_scan_kb(void) {
+ matrix_scan_user();
+}
__attribute__((weak)) void matrix_init_user(void) {}
__attribute__((weak)) void matrix_scan_user(void) {}
// helper functions
-void matrix_init(void)
-{
- // all outputs for rows high
- DDRB = 0xFF;
- PORTB = 0xFF;
- // all inputs for columns
- DDRA = 0x00;
- DDRC &= ~(0x111111<<2);
- DDRD &= ~(1<<PIND7);
- // all columns are pulled-up
- PORTA = 0xFF;
- PORTC |= (0b111111<<2);
- PORTD |= (1<<PIND7);
+void matrix_init(void) {
+ // all outputs for rows high
+ DDRB = 0xFF;
+ PORTB = 0xFF;
+ // all inputs for columns
+ DDRA = 0x00;
+ DDRC &= ~(0x111111 << 2);
+ DDRD &= ~(1 << PIND7);
+ // all columns are pulled-up
+ PORTA = 0xFF;
+ PORTC |= (0b111111 << 2);
+ PORTD |= (1 << PIND7);
#ifdef RIGHT_HALF
- // initialize row and col
- mcp23018_status = init_mcp23018();
+ // initialize row and col
+ mcp23018_status = init_mcp23018();
#endif
- // initialize matrix state: all keys off
- for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
- matrix[row] = 0;
- matrix_debouncing[row] = 0;
- }
- debounce_init(MATRIX_ROWS);
- matrix_init_kb();
+ // initialize matrix state: all keys off
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ matrix[row] = 0;
+ matrix_debouncing[row] = 0;
+ }
+ debounce_init();
+ matrix_init_kb();
}
-uint8_t matrix_scan(void)
-{
+uint8_t matrix_scan(void) {
#ifdef RIGHT_HALF
- // Then the keyboard
- if (mcp23018_status != I2C_STATUS_SUCCESS) {
- if (++mcp23018_reset_loop == 0) {
- // if (++mcp23018_reset_loop >= 1300) {
- // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
- // this will be approx bit more frequent than once per second
- print("trying to reset mcp23018\n");
- mcp23018_status = init_mcp23018();
- if (mcp23018_status) {
- print("left side not responding\n");
- } else {
- print("left side attached\n");
- }
+ // Then the keyboard
+ if (mcp23018_status != I2C_STATUS_SUCCESS) {
+ if (++mcp23018_reset_loop == 0) {
+ // if (++mcp23018_reset_loop >= 1300) {
+ // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
+ // this will be approx bit more frequent than once per second
+ print("trying to reset mcp23018\n");
+ mcp23018_status = init_mcp23018();
+ if (mcp23018_status) {
+ print("left side not responding\n");
+ } else {
+ print("left side attached\n");
+ }
+ }
}
- }
#endif
- bool changed = false;
- for (uint8_t row = 0; row < MATRIX_ROWS; row++)
- {
- matrix_row_t cols;
+ bool changed = false;
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ matrix_row_t cols;
- matrix_select_row(row);
+ matrix_select_row(row);
#ifndef RIGHT_HALF
- _delay_us(5);
+ _delay_us(5);
#endif
- cols = (
- // cols 0..7, PORTA 0 -> 7
- (~PINA) & 0xFF
- );
+ cols = (
+ // cols 0..7, PORTA 0 -> 7
+ (~PINA) & 0xFF);
#ifdef RIGHT_HALF
- uint8_t data = 0x7F;
- // Receive the columns from right half
- i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT);
- cols |= ((~(data) & 0x7F) << 7);
+ uint8_t data = 0x7F;
+ // Receive the columns from right half
+ i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT);
+ cols |= ((~(data) & 0x7F) << 7);
#endif
- if (matrix_debouncing[row] != cols) {
- matrix_debouncing[row] = cols;
- //debouncing = DEBOUNCE;
- changed = true;
+ if (matrix_debouncing[row] != cols) {
+ matrix_debouncing[row] = cols;
+ // debouncing = DEBOUNCE;
+ changed = true;
+ }
}
- }
- debounce(matrix_debouncing, matrix, MATRIX_ROWS, changed);
+ debounce(matrix_debouncing, matrix, changed);
- matrix_scan_kb();
+ matrix_scan_kb();
#ifdef DEBUG_MATRIX
- for (uint8_t c = 0; c < MATRIX_COLS; c++)
- for (uint8_t r = 0; r < MATRIX_ROWS; r++)
- if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
+ for (uint8_t c = 0; c < MATRIX_COLS; c++)
+ for (uint8_t r = 0; r < MATRIX_ROWS; r++)
+ if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
#endif
- return (uint8_t)changed;
+ return (uint8_t)changed;
}
-inline
-matrix_row_t matrix_get_row(uint8_t row)
-{
+inline matrix_row_t matrix_get_row(uint8_t row) {
return matrix[row];
}
-void matrix_print(void)
-{
- print("\nr/c 0123456789ABCDEF\n");
- for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
- print_hex8(row); print(": ");
- print_bin_reverse16(matrix_get_row(row));
- print("\n");
- }
+void matrix_print(void) {
+ print("\nr/c 0123456789ABCDEF\n");
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ print_hex8(row);
+ print(": ");
+ print_bin_reverse16(matrix_get_row(row));
+ print("\n");
+ }
}
-static void matrix_select_row(uint8_t row)
-{
+static void matrix_select_row(uint8_t row) {
#ifdef RIGHT_HALF
- uint8_t txdata[3];
+ uint8_t txdata[3];
- //Set the remote row on port A
- txdata[0] = GPIOA;
- txdata[1] = 0xFF & ~(1<<row);
- mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT);
+ // Set the remote row on port A
+ txdata[0] = GPIOA;
+ txdata[1] = 0xFF & ~(1 << row);
+ mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT);
#endif
- // select other half
- DDRB = (1 << row);
- PORTB = ~(1 << row);
+ // select other half
+ DDRB = (1 << row);
+ PORTB = ~(1 << row);
}