]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/deltasplit75/matrix.c
Normalize all line endings
[qmk_firmware.git] / keyboards / deltasplit75 / matrix.c
index c3bb0b058cde0c565fb770fc577d079234f06b97..13896900421f0654f29e75f46f9eeddc22ea1015 100644 (file)
-/*\r
-Copyright 2012 Jun Wako <wakojun@gmail.com>\r
-\r
-This program is free software: you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation, either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-This program is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
-*/\r
-\r
-/*\r
- * scan matrix\r
- */\r
-#include <stdint.h>\r
-#include <stdbool.h>\r
-#include <avr/io.h>\r
-#include <avr/wdt.h>\r
-#include <avr/interrupt.h>\r
-#include <util/delay.h>\r
-#include "print.h"\r
-#include "debug.h"\r
-#include "util.h"\r
-#include "matrix.h"\r
-#include "split_util.h"\r
-#include "pro_micro.h"\r
-#include "config.h"\r
-\r
-#ifdef USE_I2C\r
-#  include "i2c.h"\r
-#else // USE_SERIAL\r
-#  include "serial.h"\r
-#endif\r
-\r
-#ifndef DEBOUNCE\r
-#  define DEBOUNCE     5\r
-#endif\r
-\r
-#define ERROR_DISCONNECT_COUNT 5\r
-\r
-static uint8_t debouncing = DEBOUNCE;\r
-static const int ROWS_PER_HAND = MATRIX_ROWS/2;\r
-static uint8_t error_count = 0;\r
-\r
-static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;\r
-static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;\r
-\r
-/* matrix state(1:on, 0:off) */\r
-static matrix_row_t matrix[MATRIX_ROWS];\r
-static matrix_row_t matrix_debouncing[MATRIX_ROWS];\r
-\r
-static matrix_row_t read_cols(void);\r
-static void init_cols(void);\r
-static void unselect_rows(void);\r
-static void select_row(uint8_t row);\r
-\r
-__attribute__ ((weak))\r
-void matrix_init_quantum(void) {\r
-    matrix_init_kb();\r
-}\r
-\r
-__attribute__ ((weak))\r
-void matrix_scan_quantum(void) {\r
-    matrix_scan_kb();\r
-}\r
-\r
-__attribute__ ((weak))\r
-void matrix_init_kb(void) {\r
-    matrix_init_user();\r
-}\r
-\r
-__attribute__ ((weak))\r
-void matrix_scan_kb(void) {\r
-    matrix_scan_user();\r
-}\r
-\r
-__attribute__ ((weak))\r
-void matrix_init_user(void) {\r
-}\r
-\r
-__attribute__ ((weak))\r
-void matrix_scan_user(void) {\r
-}\r
-\r
-inline\r
-uint8_t matrix_rows(void)\r
-{\r
-    return MATRIX_ROWS;\r
-}\r
-\r
-inline\r
-uint8_t matrix_cols(void)\r
-{\r
-    return MATRIX_COLS;\r
-}\r
-\r
-void matrix_init(void)\r
-{\r
-    debug_enable = true;\r
-    debug_matrix = true;\r
-    debug_mouse = true;\r
-    // initialize row and col\r
-    unselect_rows();\r
-    init_cols();\r
-\r
-    TX_RX_LED_INIT;\r
-\r
-    // initialize matrix state: all keys off\r
-    for (uint8_t i=0; i < MATRIX_ROWS; i++) {\r
-        matrix[i] = 0;\r
-        matrix_debouncing[i] = 0;\r
-    }\r
-\r
-    matrix_init_quantum();\r
-}\r
-\r
-uint8_t _matrix_scan(void)\r
-{\r
-    // Right hand is stored after the left in the matirx so, we need to offset it\r
-    int offset = isLeftHand ? 0 : (ROWS_PER_HAND);\r
-\r
-    for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {\r
-        select_row(i);\r
-        _delay_us(30);  // without this wait read unstable value.\r
-        matrix_row_t cols = read_cols();\r
-        if (matrix_debouncing[i+offset] != cols) {\r
-            matrix_debouncing[i+offset] = cols;\r
-            debouncing = DEBOUNCE;\r
-        }\r
-        unselect_rows();\r
-    }\r
-\r
-    if (debouncing) {\r
-        if (--debouncing) {\r
-            _delay_ms(1);\r
-        } else {\r
-            for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {\r
-                matrix[i+offset] = matrix_debouncing[i+offset];\r
-            }\r
-        }\r
-    }\r
-\r
-    return 1;\r
-}\r
-\r
-#ifdef USE_I2C\r
-\r
-// Get rows from other half over i2c\r
-int i2c_transaction(void) {\r
-    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;\r
-\r
-    int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);\r
-    if (err) goto i2c_error;\r
-\r
-    // start of matrix stored at 0x00\r
-    err = i2c_master_write(0x00);\r
-    if (err) goto i2c_error;\r
-\r
-    // Start read\r
-    err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);\r
-    if (err) goto i2c_error;\r
-\r
-    if (!err) {\r
-        int i;\r
-        for (i = 0; i < ROWS_PER_HAND-1; ++i) {\r
-            matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);\r
-        }\r
-        matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);\r
-        i2c_master_stop();\r
-    } else {\r
-i2c_error: // the cable is disconnceted, or something else went wrong\r
-        i2c_reset_state();\r
-        return err;\r
-    }\r
-\r
-    return 0;\r
-}\r
-\r
-#else // USE_SERIAL\r
-\r
-int serial_transaction(void) {\r
-    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;\r
-\r
-    if (serial_update_buffers()) {\r
-        return 1;\r
-    }\r
-\r
-    for (int i = 0; i < ROWS_PER_HAND; ++i) {\r
-        matrix[slaveOffset+i] = serial_slave_buffer[i];\r
-    }\r
-    return 0;\r
-}\r
-#endif\r
-\r
-uint8_t matrix_scan(void)\r
-{\r
-    int ret = _matrix_scan();\r
-\r
-\r
-\r
-#ifdef USE_I2C\r
-    if( i2c_transaction() ) {\r
-#else // USE_SERIAL\r
-    if( serial_transaction() ) {\r
-#endif\r
-        // turn on the indicator led when halves are disconnected\r
-        TXLED1;\r
-\r
-        error_count++;\r
-\r
-        if (error_count > ERROR_DISCONNECT_COUNT) {\r
-            // reset other half if disconnected\r
-            int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;\r
-            for (int i = 0; i < ROWS_PER_HAND; ++i) {\r
-                matrix[slaveOffset+i] = 0;\r
-            }\r
-        }\r
-    } else {\r
-        // turn off the indicator led on no error\r
-        TXLED0;\r
-        error_count = 0;\r
-    }\r
-\r
-    matrix_scan_quantum();\r
-\r
-    return ret;\r
-}\r
-\r
-void matrix_slave_scan(void) {\r
-    _matrix_scan();\r
-\r
-    int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);\r
-\r
-#ifdef USE_I2C\r
-    for (int i = 0; i < ROWS_PER_HAND; ++i) {\r
-        /* i2c_slave_buffer[i] = matrix[offset+i]; */\r
-        i2c_slave_buffer[i] = matrix[offset+i];\r
-    }\r
-#else // USE_SERIAL\r
-    for (int i = 0; i < ROWS_PER_HAND; ++i) {\r
-        serial_slave_buffer[i] = matrix[offset+i];\r
-    }\r
-#endif\r
-}\r
-\r
-bool matrix_is_modified(void)\r
-{\r
-    if (debouncing) return false;\r
-    return true;\r
-}\r
-\r
-inline\r
-bool matrix_is_on(uint8_t row, uint8_t col)\r
-{\r
-    return (matrix[row] & ((matrix_row_t)1<<col));\r
-}\r
-\r
-inline\r
-matrix_row_t matrix_get_row(uint8_t row)\r
-{\r
-    return matrix[row];\r
-}\r
-\r
-void matrix_print(void)\r
-{\r
-    print("\nr/c 0123456789ABCDEF\n");\r
-    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {\r
-        phex(row); print(": ");\r
-        pbin_reverse16(matrix_get_row(row));\r
-        print("\n");\r
-    }\r
-}\r
-\r
-uint8_t matrix_key_count(void)\r
-{\r
-    uint8_t count = 0;\r
-    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {\r
-        count += bitpop16(matrix[i]);\r
-    }\r
-    return count;\r
-}\r
-\r
-static void  init_cols(void)\r
-{\r
-    for(int x = 0; x < MATRIX_COLS; x++) {\r
-        _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);\r
-        _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);\r
-    }\r
-}\r
-\r
-static matrix_row_t read_cols(void)\r
-{\r
-    matrix_row_t result = 0;\r
-    for(int x = 0; x < MATRIX_COLS; x++) {\r
-        result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);\r
-    }\r
-    return result;\r
-}\r
-\r
-static void unselect_rows(void)\r
-{\r
-    for(int x = 0; x < ROWS_PER_HAND; x++) {\r
-        _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);\r
-        _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);\r
-    }\r
-}\r
-\r
-static void select_row(uint8_t row)\r
-{\r
-    _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);\r
-    _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);\r
-}\r
+/*
+Copyright 2012 Jun Wako <wakojun@gmail.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ * scan matrix
+ */
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/io.h>
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+#include "print.h"
+#include "debug.h"
+#include "util.h"
+#include "matrix.h"
+#include "split_util.h"
+#include "pro_micro.h"
+#include "config.h"
+
+#ifdef USE_I2C
+#  include "i2c.h"
+#else // USE_SERIAL
+#  include "serial.h"
+#endif
+
+#ifndef DEBOUNCE
+#  define DEBOUNCE     5
+#endif
+
+#define ERROR_DISCONNECT_COUNT 5
+
+static uint8_t debouncing = DEBOUNCE;
+static const int ROWS_PER_HAND = MATRIX_ROWS/2;
+static uint8_t error_count = 0;
+
+static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
+static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
+
+/* 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 unselect_rows(void);
+static void select_row(uint8_t row);
+
+__attribute__ ((weak))
+void matrix_init_quantum(void) {
+    matrix_init_kb();
+}
+
+__attribute__ ((weak))
+void matrix_scan_quantum(void) {
+    matrix_scan_kb();
+}
+
+__attribute__ ((weak))
+void matrix_init_kb(void) {
+    matrix_init_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) {
+}
+
+inline
+uint8_t matrix_rows(void)
+{
+    return MATRIX_ROWS;
+}
+
+inline
+uint8_t matrix_cols(void)
+{
+    return MATRIX_COLS;
+}
+
+void matrix_init(void)
+{
+    debug_enable = true;
+    debug_matrix = true;
+    debug_mouse = true;
+    // initialize row and col
+    unselect_rows();
+    init_cols();
+
+    TX_RX_LED_INIT;
+
+    // initialize matrix state: all keys off
+    for (uint8_t i=0; i < MATRIX_ROWS; i++) {
+        matrix[i] = 0;
+        matrix_debouncing[i] = 0;
+    }
+
+    matrix_init_quantum();
+}
+
+uint8_t _matrix_scan(void)
+{
+    // Right hand is stored after the left in the matirx so, we need to offset it
+    int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
+
+    for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
+        select_row(i);
+        _delay_us(30);  // without this wait read unstable value.
+        matrix_row_t cols = read_cols();
+        if (matrix_debouncing[i+offset] != cols) {
+            matrix_debouncing[i+offset] = cols;
+            debouncing = DEBOUNCE;
+        }
+        unselect_rows();
+    }
+
+    if (debouncing) {
+        if (--debouncing) {
+            _delay_ms(1);
+        } else {
+            for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
+                matrix[i+offset] = matrix_debouncing[i+offset];
+            }
+        }
+    }
+
+    return 1;
+}
+
+#ifdef USE_I2C
+
+// Get rows from other half over i2c
+int i2c_transaction(void) {
+    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
+
+    int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
+    if (err) goto i2c_error;
+
+    // start of matrix stored at 0x00
+    err = i2c_master_write(0x00);
+    if (err) goto i2c_error;
+
+    // Start read
+    err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
+    if (err) goto i2c_error;
+
+    if (!err) {
+        int i;
+        for (i = 0; i < ROWS_PER_HAND-1; ++i) {
+            matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
+        }
+        matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
+        i2c_master_stop();
+    } else {
+i2c_error: // the cable is disconnceted, or something else went wrong
+        i2c_reset_state();
+        return err;
+    }
+
+    return 0;
+}
+
+#else // USE_SERIAL
+
+int serial_transaction(void) {
+    int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
+
+    if (serial_update_buffers()) {
+        return 1;
+    }
+
+    for (int i = 0; i < ROWS_PER_HAND; ++i) {
+        matrix[slaveOffset+i] = serial_slave_buffer[i];
+    }
+    return 0;
+}
+#endif
+
+uint8_t matrix_scan(void)
+{
+    int ret = _matrix_scan();
+
+
+
+#ifdef USE_I2C
+    if( i2c_transaction() ) {
+#else // USE_SERIAL
+    if( serial_transaction() ) {
+#endif
+        // turn on the indicator led when halves are disconnected
+        TXLED1;
+
+        error_count++;
+
+        if (error_count > ERROR_DISCONNECT_COUNT) {
+            // reset other half if disconnected
+            int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
+            for (int i = 0; i < ROWS_PER_HAND; ++i) {
+                matrix[slaveOffset+i] = 0;
+            }
+        }
+    } else {
+        // turn off the indicator led on no error
+        TXLED0;
+        error_count = 0;
+    }
+
+    matrix_scan_quantum();
+
+    return ret;
+}
+
+void matrix_slave_scan(void) {
+    _matrix_scan();
+
+    int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
+
+#ifdef USE_I2C
+    for (int i = 0; i < ROWS_PER_HAND; ++i) {
+        /* i2c_slave_buffer[i] = matrix[offset+i]; */
+        i2c_slave_buffer[i] = matrix[offset+i];
+    }
+#else // USE_SERIAL
+    for (int i = 0; i < ROWS_PER_HAND; ++i) {
+        serial_slave_buffer[i] = matrix[offset+i];
+    }
+#endif
+}
+
+bool matrix_is_modified(void)
+{
+    if (debouncing) return false;
+    return true;
+}
+
+inline
+bool matrix_is_on(uint8_t row, uint8_t col)
+{
+    return (matrix[row] & ((matrix_row_t)1<<col));
+}
+
+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++) {
+        phex(row); print(": ");
+        pbin_reverse16(matrix_get_row(row));
+        print("\n");
+    }
+}
+
+uint8_t matrix_key_count(void)
+{
+    uint8_t count = 0;
+    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+        count += bitpop16(matrix[i]);
+    }
+    return count;
+}
+
+static void  init_cols(void)
+{
+    for(int x = 0; x < MATRIX_COLS; x++) {
+        _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);
+        _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
+    }
+}
+
+static matrix_row_t read_cols(void)
+{
+    matrix_row_t result = 0;
+    for(int x = 0; x < MATRIX_COLS; x++) {
+        result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
+    }
+    return result;
+}
+
+static void unselect_rows(void)
+{
+    for(int x = 0; x < ROWS_PER_HAND; x++) {
+        _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);
+        _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
+    }
+}
+
+static void select_row(uint8_t row)
+{
+    _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);
+    _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
+}