diff --git a/keyboards/cheapino/config.h b/keyboards/cheapino/config.h index f315973307..7149673ef9 100644 --- a/keyboards/cheapino/config.h +++ b/keyboards/cheapino/config.h @@ -35,3 +35,5 @@ #define MAX_DEFERRED_EXECUTORS 32 + +// #define DEBUG_MATRIX_SCAN_RATE diff --git a/keyboards/cheapino/ghosting.c b/keyboards/cheapino/ghosting.c new file mode 100644 index 0000000000..a1d123181d --- /dev/null +++ b/keyboards/cheapino/ghosting.c @@ -0,0 +1,96 @@ +// +// Created by Thomas Haukland on 2024-05-05. +// + +#include "matrix.h" +#include "quantum.h" +#include "print.h" + +// This is just to be able to declare constants as they appear in the qmk console +#define rev(b) \ + ((b & 1) << 15) | \ + ((b & (1 << 1)) << 13) | \ + ((b & (1 << 2)) << 11) | \ + ((b & (1 << 3)) << 9) | \ + ((b & (1 << 4)) << 7) | \ + ((b & (1 << 5)) << 5) | \ + ((b & (1 << 6)) << 3) | \ + ((b & (1 << 7)) << 1) | \ + ((b & (1 << 8)) >> 1) | \ + ((b & (1 << 9)) >> 3) | \ + ((b & (1 << 10)) >> 5) | \ + ((b & (1 << 11)) >> 7) | \ + ((b & (1 << 12)) >> 9) | \ + ((b & (1 << 13)) >> 11) | \ + ((b & (1 << 14)) >> 13) | \ + b >> 15 + +/* This is for debugging the matrix rows +void printBits(uint16_t n) +{ + long i; + for (i = 15; i >= 0; i--) { + if ((n & (1 << i)) != 0) { + printf("1"); + } + else { + printf("0"); + } + } + printf("\n"); +} +*/ + +bool bit_pattern_set(uint16_t number, uint16_t bitPattern) { + return !(~number & bitPattern); +} + +void fix_ghosting_instance( + matrix_row_t current_matrix[], + ushort row_num_with_possible_error_cause, + uint16_t possible_error_cause, + ushort row_num_with_possible_error, + uint16_t possible_error, + uint16_t error_fix) { + if (bit_pattern_set(current_matrix[row_num_with_possible_error_cause], possible_error_cause)) { + if (bit_pattern_set(current_matrix[row_num_with_possible_error], possible_error)) { + current_matrix[row_num_with_possible_error] = current_matrix[row_num_with_possible_error] ^ error_fix; + } + } +} + +void fix_ghosting_column( + matrix_row_t matrix[], + uint16_t possible_error_cause, + uint16_t possible_error, + uint16_t error_fix) { + // First the right side + for (short i = 0; i<3; i++) { + fix_ghosting_instance(matrix, i, possible_error_cause, (i+1)%3, possible_error, error_fix); + fix_ghosting_instance(matrix, i, possible_error_cause, (i+2)%3, possible_error, error_fix); + } + + // Then exactly same procedure on the left side + for (short i = 0; i<3; i++) { + fix_ghosting_instance(matrix, i+4, possible_error_cause<<6, 4+((i+1)%3), possible_error<<6, error_fix<<6); + fix_ghosting_instance(matrix, i+4, possible_error_cause<<6, 4+((i+2)%3), possible_error<<6, error_fix<<6); + } +} + +// For QWERTY layout, key combo a+s+e also outputs q. This suppresses the q, and other similar ghosts +// These are observed ghosts(following a pattern). TODO: need to fix this for v3 +// Might need to add 2 diodes(one in each direction) for every row, to increase voltage drop. +void fix_ghosting(matrix_row_t matrix[]) { + fix_ghosting_column(matrix, rev(0B0110000000000000), rev(0B1010000000000000), rev(0B1000000000000000)); + fix_ghosting_column(matrix, rev(0B0110000000000000), rev(0B0101000000000000), rev(0B0001000000000000)); + + fix_ghosting_column(matrix, rev(0B0001100000000000), rev(0B0010100000000000), rev(0B0010000000000000)); + fix_ghosting_column(matrix, rev(0B0001100000000000), rev(0B0001010000000000), rev(0B0000010000000000)); + + fix_ghosting_column(matrix, rev(0B1000010000000000), rev(0B1000100000000000), rev(0B1000000000000000)); + fix_ghosting_column(matrix, rev(0B1000010000000000), rev(0B0100010000000000), rev(0B0000010000000000)); + + fix_ghosting_column(matrix, rev(0B1001000000000000), rev(0B0101000000000000), rev(0B0001000000000000)); + + fix_ghosting_column(matrix, rev(0B0100100000000000), rev(0B0100010000000000), rev(0B0100000000000000)); +} diff --git a/keyboards/cheapino/ghosting.h b/keyboards/cheapino/ghosting.h new file mode 100644 index 0000000000..1dfef2af3f --- /dev/null +++ b/keyboards/cheapino/ghosting.h @@ -0,0 +1,5 @@ +// +// Created by Thomas Haukland on 2024-05-05. +// + +void fix_ghosting(matrix_row_t current_matrix[]); \ No newline at end of file diff --git a/keyboards/cheapino/matrix.c b/keyboards/cheapino/matrix.c index 21f9150088..f6f518318b 100644 --- a/keyboards/cheapino/matrix.c +++ b/keyboards/cheapino/matrix.c @@ -26,10 +26,13 @@ along with this program. If not, see . #include "quantum.h" #include "debounce.h" #include "encoder.h" +#include "ghosting.h" #include "print.h" // How long the scanning code waits for changed io to settle. -#define MATRIX_IO_DELAY 30 +// Adjust from default 30 to weigh up for increased time spent ghost-hunting. +// (the rp2040 does not seem to have any problems with this value...) +#define MATRIX_IO_DELAY 25 #define COL_SHIFTER ((uint16_t)1) @@ -142,6 +145,8 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) { fix_encoder_action(current_matrix); + fix_ghosting(current_matrix); + return has_matrix_changed(current_matrix); } diff --git a/keyboards/cheapino/rules.mk b/keyboards/cheapino/rules.mk index 3123102472..70ed45868b 100644 --- a/keyboards/cheapino/rules.mk +++ b/keyboards/cheapino/rules.mk @@ -4,4 +4,5 @@ WS2812_DRIVER = vendor RGBLIGHT_ENABLE = yes DEFERRED_EXEC_ENABLE = yes SRC += encoder.c +SRC += ghosting.c SRC += matrix.c \ No newline at end of file