From 76b3490f108a1813420fdfeb426f38c5c0e10b23 Mon Sep 17 00:00:00 2001 From: Thomas Haukland Date: Sat, 25 Mar 2023 15:29:34 +0100 Subject: [PATCH] Cheapino: encoder works --- keyboards/cheapino/encoder.c | 57 ++++++++++++++ keyboards/cheapino/encoder.h | 5 ++ keyboards/cheapino/keymaps/tompi/keymap.json | 8 +- keyboards/cheapino/matrix.c | 79 ++++++++++---------- keyboards/cheapino/rules.mk | 3 +- 5 files changed, 106 insertions(+), 46 deletions(-) create mode 100644 keyboards/cheapino/encoder.c create mode 100644 keyboards/cheapino/encoder.h diff --git a/keyboards/cheapino/encoder.c b/keyboards/cheapino/encoder.c new file mode 100644 index 0000000000..e382c418e3 --- /dev/null +++ b/keyboards/cheapino/encoder.c @@ -0,0 +1,57 @@ +#include "matrix.h" +#include "quantum.h" + +#define COL_SHIFTER ((uint16_t)1) + +static bool colABpressed = false; + +void blank_column(matrix_row_t current_matrix[], uint8_t col) { + uint16_t column_index_bitmask = COL_SHIFTER << col; + for (uint8_t row_index = 0; row_index < MATRIX_ROWS-1; row_index++) { + current_matrix[row_index] &= ~column_index_bitmask; + } +} + +bool is_entire_column_held(matrix_row_t current_matrix[], uint8_t col) { + uint16_t column_index_bitmask = COL_SHIFTER << col; + for (uint8_t row_index = 0; row_index < MATRIX_ROWS-1; row_index++) { + if (!(current_matrix[row_index] & column_index_bitmask)) return false; + } + return true; +} + +// Because of a bug in the routing of the cheapino, encoder action +// triggers entire columns... fix it in software here, assumption is that +// you never press an entire column, sounds safe? +void fix_encoder_action(matrix_row_t current_matrix[]) { + + // 7th column means encoder was pressed + if (is_entire_column_held(current_matrix, 7)) { + blank_column(current_matrix, 7); + current_matrix[3] |= COL_SHIFTER; + } else { + current_matrix[3] &= ~COL_SHIFTER; + } + + // Now check rotary action: + bool colA = is_entire_column_held(current_matrix, 9); + bool colB = is_entire_column_held(current_matrix, 11); + + if (colA && colB) { + colABpressed = true; + blank_column(current_matrix, 9); + blank_column(current_matrix, 11); + } else if (colA) { + if (colABpressed) { + colABpressed = false; + tap_code(KC_U); + } + blank_column(current_matrix, 9); + } else if (colB) { + if (colABpressed) { + colABpressed = false; + tap_code(KC_D); + } + blank_column(current_matrix, 11); + } +} diff --git a/keyboards/cheapino/encoder.h b/keyboards/cheapino/encoder.h new file mode 100644 index 0000000000..e393e5da2d --- /dev/null +++ b/keyboards/cheapino/encoder.h @@ -0,0 +1,5 @@ +// +// Created by Thomas Haukland on 25/03/2023. +// + +void fix_encoder_action(matrix_row_t current_matrix[]); \ No newline at end of file diff --git a/keyboards/cheapino/keymaps/tompi/keymap.json b/keyboards/cheapino/keymaps/tompi/keymap.json index 39dbb24a82..03527fcb33 100644 --- a/keyboards/cheapino/keymaps/tompi/keymap.json +++ b/keyboards/cheapino/keymaps/tompi/keymap.json @@ -2,7 +2,7 @@ "version": 1, "notes": "My awesome keymap", "documentation": "\"This file is a QMK Configurator export. You can import this at . It can also be used directly with QMK's source code.\n\nTo setup your QMK environment check out the tutorial: \n\nYou can convert this file to a keymap.c using this command: `qmk json2c {keymap}`\n\nYou can compile this keymap using this command: `qmk compile {keymap}`\"\n", - "keyboard": "cheapino", + "keyboard": "bastardkb/skeletyl/blackpill", "keymap": "default", "layout": "LAYOUT_split_3x5_3", "layers": [ @@ -129,7 +129,7 @@ "KC_AT", "KC_UNDS", "KC_PIPE", - "KC_QUOT", + "KC_GRV", "KC_PERC", "KC_TRNS", "KC_TRNS", @@ -160,7 +160,7 @@ ], [ "KC_EQL", - "KC_COLN", + "KC_CIRC", "KC_LT", "KC_GT", "KC_SCLN", @@ -274,4 +274,4 @@ ] ], "author": "thomas.haukland@gmail.com" -} +} \ No newline at end of file diff --git a/keyboards/cheapino/matrix.c b/keyboards/cheapino/matrix.c index 56dcf75613..d4aa8ee247 100644 --- a/keyboards/cheapino/matrix.c +++ b/keyboards/cheapino/matrix.c @@ -25,11 +25,21 @@ along with this program. If not, see . #include "config.h" #include "quantum.h" #include "debounce.h" +#include "encoder.h" -#define ROW_SHIFTER ((uint16_t)1) +#define COL_SHIFTER ((uint16_t)1) static const pin_t row_pins[] = MATRIX_ROW_PINS; static const pin_t col_pins[] = MATRIX_COL_PINS; +static matrix_row_t previous_matrix[MATRIX_ROWS]; + +void keyboard_post_init_user(void) { + // Customise these values to desired behaviour + debug_enable=true; + debug_matrix=true; + debug_keyboard=true; + //debug_mouse=true; +} static void select_row(uint8_t row) { setPinOutput(row_pins[row]); @@ -59,25 +69,14 @@ static void unselect_cols(void) { } } -static void init_pins(void) { - unselect_cols(); - unselect_rows(); -} - -static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { - // Store last value of row prior to reading - matrix_row_t last_row_value = current_matrix[current_row]; - - // Clear data in matrix row - //current_matrix[current_row] = 0; - +static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { // Select row and wait for row selection to stabilize select_row(current_row); wait_us(30); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) { - uint16_t column_index_bitmask = ROW_SHIFTER << ((col_index * 2) + 1); + uint16_t column_index_bitmask = COL_SHIFTER << ((col_index * 2) + 1); // Check row pin state if (readPin(col_pins[col_index*2])) { // Pin HI, clear col bit @@ -90,23 +89,16 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); - - return (last_row_value != current_matrix[current_row]); } -static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { - bool matrix_changed = false; - +static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { // Select col and wait for col selection to stabilize select_col(current_col*2); wait_us(30); + uint16_t column_index_bitmask = COL_SHIFTER << (current_col * 2); // For each row... for (uint8_t row_index = 0; row_index < MATRIX_ROWS-1; row_index++) { - // Store last value of row prior to reading - matrix_row_t last_row_value = current_matrix[row_index]; - - uint16_t column_index_bitmask = ROW_SHIFTER << (current_col * 2); // Check row pin state if (readPin(row_pins[row_index])) { // Pin HI, clear col bit @@ -115,41 +107,46 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Pin LO, set col bit current_matrix[row_index] |= column_index_bitmask; } - - // Determine if the matrix changed state - if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { - matrix_changed = true; - } } - // Unselect col unselect_col(current_col*2); - - return matrix_changed; } void matrix_init_custom(void) { // initialize key pins - init_pins(); - + unselect_cols(); + unselect_rows(); + setPinInput(row_pins[MATRIX_ROWS-1]); + writePinHigh(row_pins[MATRIX_ROWS-1]); debounce_init(MATRIX_ROWS); } +void store_old_matrix(matrix_row_t current_matrix[]) { + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + previous_matrix[i] = current_matrix[i]; + } +} + +bool has_matrix_changed(matrix_row_t current_matrix[]) { + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + if (previous_matrix[i] != current_matrix[i]) return true; + } + return false; +} + bool matrix_scan_custom(matrix_row_t current_matrix[]) { - - bool changed = false; - + store_old_matrix(current_matrix); // Set row, read cols for (uint8_t current_row = 0; current_row < MATRIX_ROWS-1; current_row++) { - changed |= read_cols_on_row(current_matrix, current_row); + read_cols_on_row(current_matrix, current_row); } // Set col, read rows for (uint8_t current_col = 0; current_col < MATRIX_COLS/2; current_col++) { - changed |= read_rows_on_col(current_matrix, current_col); + read_rows_on_col(current_matrix, current_col); } - //debounce(raw_matrix, matrix, MATRIX_ROWS, changed); + fix_encoder_action(current_matrix); - return changed; -} \ No newline at end of file + return has_matrix_changed(current_matrix); +} diff --git a/keyboards/cheapino/rules.mk b/keyboards/cheapino/rules.mk index 7cf4eebb42..569d5c56d8 100644 --- a/keyboards/cheapino/rules.mk +++ b/keyboards/cheapino/rules.mk @@ -1,2 +1,3 @@ CUSTOM_MATRIX = lite -SRC += matrix.c \ No newline at end of file +SRC += encoder.c +SRC += matrix.c \ No newline at end of file