From 5f530b524845293c6b8a4e553e98bf75b2b1ad57 Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Mon, 8 Feb 2016 08:28:38 -0800 Subject: [PATCH 1/4] Update README.markdown --- README.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/README.markdown b/README.markdown index 9e0d5c4..a7802f9 100644 --- a/README.markdown +++ b/README.markdown @@ -84,6 +84,7 @@ Contact If you really need to get a hold of HaaTa, email is best: `haata@kiibohd.com` IRC is likely faster though. +`#input.club@irc.freenode.net` `#geekhack@irc.freenode.net` `#deskthority@irc.freenode.net` From 1fd19c48eac0d7fd8834aa91783385c2844dca9a Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Thu, 11 Feb 2016 22:56:25 -0800 Subject: [PATCH 2/4] Changed name from MD1_1 to MD1.1 --- Scan/{MD1_1 => MD1.1}/defaultMap.kll | 2 +- Scan/{MD1_1 => MD1.1}/matrix.h | 0 Scan/{MD1_1 => MD1.1}/pinout | 0 Scan/{MD1_1 => MD1.1}/scan_loop.c | 0 Scan/{MD1_1 => MD1.1}/scan_loop.h | 0 Scan/{MD1_1 => MD1.1}/setup.cmake | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename Scan/{MD1_1 => MD1.1}/defaultMap.kll (99%) rename Scan/{MD1_1 => MD1.1}/matrix.h (100%) rename Scan/{MD1_1 => MD1.1}/pinout (100%) rename Scan/{MD1_1 => MD1.1}/scan_loop.c (100%) rename Scan/{MD1_1 => MD1.1}/scan_loop.h (100%) rename Scan/{MD1_1 => MD1.1}/setup.cmake (100%) diff --git a/Scan/MD1_1/defaultMap.kll b/Scan/MD1.1/defaultMap.kll similarity index 99% rename from Scan/MD1_1/defaultMap.kll rename to Scan/MD1.1/defaultMap.kll index df51d82..f60a4ed 100644 --- a/Scan/MD1_1/defaultMap.kll +++ b/Scan/MD1.1/defaultMap.kll @@ -1,4 +1,4 @@ -Name = MD1_1; +Name = MD1.1; Version = 0.3d; Author = "HaaTa (Jacob Alexander) 2014-2016"; KLL = 0.3c; diff --git a/Scan/MD1_1/matrix.h b/Scan/MD1.1/matrix.h similarity index 100% rename from Scan/MD1_1/matrix.h rename to Scan/MD1.1/matrix.h diff --git a/Scan/MD1_1/pinout b/Scan/MD1.1/pinout similarity index 100% rename from Scan/MD1_1/pinout rename to Scan/MD1.1/pinout diff --git a/Scan/MD1_1/scan_loop.c b/Scan/MD1.1/scan_loop.c similarity index 100% rename from Scan/MD1_1/scan_loop.c rename to Scan/MD1.1/scan_loop.c diff --git a/Scan/MD1_1/scan_loop.h b/Scan/MD1.1/scan_loop.h similarity index 100% rename from Scan/MD1_1/scan_loop.h rename to Scan/MD1.1/scan_loop.h diff --git a/Scan/MD1_1/setup.cmake b/Scan/MD1.1/setup.cmake similarity index 100% rename from Scan/MD1_1/setup.cmake rename to Scan/MD1.1/setup.cmake From 6687470ae7f9163353991d93136eb2c1da9b8e86 Mon Sep 17 00:00:00 2001 From: CryHam Date: Fri, 19 Feb 2016 18:10:25 +0100 Subject: [PATCH 3/4] Added support for ghosting matrices and code for elimination. To use define GHOST in matrix.h, see example in Scan/CK3 --- Scan/CK3/defaultMap.kll | 85 +++++++++++++++ Scan/CK3/matrix.h | 53 +++++++++ Scan/CK3/pinout | 88 +++++++++++++++ Scan/CK3/prototype.kll | 73 +++++++++++++ Scan/CK3/scan_loop.c | 204 +++++++++++++++++++++++++++++++++++ Scan/CK3/scan_loop.h | 48 +++++++++ Scan/CK3/setup.cmake | 32 ++++++ Scan/MatrixARM/matrix_scan.c | 120 ++++++++++++++++++++- Scan/MatrixARM/matrix_scan.h | 10 ++ 9 files changed, 712 insertions(+), 1 deletion(-) create mode 100644 Scan/CK3/defaultMap.kll create mode 100644 Scan/CK3/matrix.h create mode 100644 Scan/CK3/pinout create mode 100644 Scan/CK3/prototype.kll create mode 100644 Scan/CK3/scan_loop.c create mode 100644 Scan/CK3/scan_loop.h create mode 100644 Scan/CK3/setup.cmake diff --git a/Scan/CK3/defaultMap.kll b/Scan/CK3/defaultMap.kll new file mode 100644 index 0000000..d821d95 --- /dev/null +++ b/Scan/CK3/defaultMap.kll @@ -0,0 +1,85 @@ +Name = CK3; +Version = 0.3; +Author = "Crystal Hammer 2016"; +KLL = 0.3c; + +# Modified Date +Date = 2016-02-19; + + +S0x00 : U"Esc"; +S0x01 : U"1"; +S0x02 : U"2"; +S0x03 : U"3"; +S0x04 : U"4"; +S0x05 : U"5"; +S0x06 : U"6"; +S0x07 : U"7"; +S0x08 : U"8"; +S0x09 : U"9"; +S0x0A : U"0"; +S0x0B : U"Minus"; +S0x0C : U"Equal"; +S0x0D : U"Backslash"; +S0x0E : U"Backtick"; +S0x0F : U"Tab"; +S0x10 : U"Q"; +S0x11 : U"W"; +S0x12 : U"E"; +S0x13 : U"R"; +S0x14 : U"T"; +S0x15 : U"Y"; +S0x16 : U"U"; +S0x17 : U"I"; +S0x18 : U"O"; +S0x19 : U"P"; +S0x1A : U"LBrace"; +S0x1B : U"RBrace"; +S0x1C : U"Backspace"; +S0x1D : U"Ctrl"; +S0x1E : U"A"; +S0x1F : U"S"; +S0x20 : U"D"; +S0x21 : U"F"; +S0x22 : U"G"; +S0x23 : U"H"; +S0x24 : U"J"; +S0x25 : U"K"; +S0x26 : U"L"; +S0x27 : U"Semicolon"; +S0x28 : U"Quote"; +S0x29 : U"Enter"; +S0x2A : U"LShift"; +S0x2B : U"Z"; +S0x2C : U"X"; +S0x2D : U"C"; +S0x2E : U"V"; +S0x2F : U"B"; +S0x30 : U"N"; +S0x31 : U"M"; +S0x32 : U"Comma"; +S0x33 : U"Period"; +S0x34 : U"Slash"; +S0x35 : U"RShift"; +S0x36 : U"Function1"; # Fun key +S0x37 : U"Function2"; # Left Blank Key +S0x38 : U"LAlt"; +S0x39 : U"LGui"; +S0x3A : U"Space"; +S0x3B : U"RGui"; +S0x3C : U"RAlt"; +S0x3D : U"Function3"; # Right Blank Key 1 +S0x3E : U"Function4"; # Right Blank Key 2 + + +# Custom Action Examples + +# Example capability, prints to cli +action1 => CustomAction_action1_capability(); # No arguments + +# Blocks given USB Code, must be used with blockLink +# Simple example, supports only blocking a single key at a time +# Keys must be specified using numbers see Macro/PartialMap/usb_hid.h +blockHold => CustomAction_blockHold_capability( usbCode : 1 ); # Single 8-bit argument +blockKey => CustomAction_blockKey_capability( usbCode : 1 ); + diff --git a/Scan/CK3/matrix.h b/Scan/CK3/matrix.h new file mode 100644 index 0000000..28bd3b0 --- /dev/null +++ b/Scan/CK3/matrix.h @@ -0,0 +1,53 @@ +/* Copyright (C) 2014-2015 by Jacob Alexander + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +// ----- Includes ----- + +// Project Includes +#include + + + +// ----- Matrix Definition ----- + +// Freescale ARM MK20's support GPIO PTA, PTB, PTC, PTD and PTE 0..31 +// Not all chips have access to all of these pins (most don't have 160 pins :P) +// +// NOTE: +// Before using a pin, make sure it supports being a GPIO *and* doesn't have a default pull-up/pull-down +// Checking this is completely on the ownness of the user + +// +// Columns (Strobe) // PTB0..3,16,17 PTC4,5 PTD0 +// Rows (Sense) // PTD1..7 + +// Define Rows (Sense) and Columns (Strobes) +GPIO_Pin Matrix_cols[] = { gpio(B,0), gpio(B,1), gpio(B,2), gpio(B,3), gpio(B,16), gpio(B,17), gpio(C,4), gpio(C,6), gpio(D,0) }; +GPIO_Pin Matrix_rows[] = { gpio(D,1), gpio(D,2), gpio(D,3), gpio(D,4), gpio(D,5), gpio(D,6), gpio(D,7) }; + +// Define type of scan matrix +Config Matrix_type = Config_Pulldown; + + +// define this if your matrix has ghosting (i.e. regular keyboard without diodes) +#define GHOST diff --git a/Scan/CK3/pinout b/Scan/CK3/pinout new file mode 100644 index 0000000..a72c0e6 --- /dev/null +++ b/Scan/CK3/pinout @@ -0,0 +1,88 @@ +Pin Usage +========= + +mk20dx128vlf5 + + ---- +|Keys| + ---- + +* Strobe (Columns) + +PTB0 +PTB1 +PTB2 +PTB3 +PTB16 +PTB17 +PTC4 +PTC5 +PTD0 + + +* Sense (Rows) + +PTD1 +PTD2 +PTD3 +PTD4 +PTD5 +PTD6 +PTD7 + + + ----- +|Debug| + ----- + +* SWD + +PTA0 (Pull-down) +PTA3 (Pull-up) + +* LEDs + +PTA19 (LED only for PCB, not McHCK) (XTAL) + +* UARTs + +PTA1 - RX0 +PTA2 - TX0 + +* Tag Connect + +1 - Vdd +5 +2 - PTA3 / SWD_IO +3 - Vss / Gnd +4 - PTA0 / SWD_CLK +5 - +5V +6 - PTA2 / TRACE_SWO +7 - N/C +8 - PTA1 / JTAG_TDI +9 - N/C +10 - Reset_b + + + ------ +|Unused| + ------ + +* GPIO + +PTA1 (Not broken out on PCB, available on McHCK) (Pull-up) +PTA2 (") +PTA4 (Pull-up) +PTA18 (EXTAL) + +PTC0 +PTC1 +PTC2 +PTC3 +PTC6 +PTC7 + +* Analog + +ADC0_DP0 +ADC0_DM0 + diff --git a/Scan/CK3/prototype.kll b/Scan/CK3/prototype.kll new file mode 100644 index 0000000..64fe862 --- /dev/null +++ b/Scan/CK3/prototype.kll @@ -0,0 +1,73 @@ +Name = MD1; +Version = 0.2; +Author = "HaaTa (Jacob Alexander) 2014"; +KLL = 0.3; + +# Modified Date +Date = 2014-09-14; + + +S0x00 : U"Esc"; +S0x01 : U"1"; +S0x02 : U"2"; +S0x03 : U"3"; +S0x04 : U"4"; +S0x05 : U"5"; +S0x06 : U"6"; +S0x07 : U"7"; +S0x08 : U"8"; +S0x09 : U"9"; +S0x0A : U"0"; +S0x0B : U"Minus"; +S0x0C : U"Equal"; +S0x0D : U"Backslash"; +S0x0E : U"Tab"; +S0x0F : U"Q"; +S0x10 : U"W"; +S0x11 : U"E"; +S0x12 : U"R"; +S0x13 : U"T"; +S0x14 : U"Y"; +S0x15 : U"U"; +S0x16 : U"I"; +S0x17 : U"O"; +S0x18 : U"P"; +S0x19 : U"LBrace"; +S0x1A : U"RBrace"; +S0x1B : U"Backspace"; +S0x1C : U"Ctrl"; +S0x1D : U"A"; +S0x1E : U"S"; +S0x1F : U"D"; +S0x20 : U"F"; +S0x21 : U"G"; +S0x22 : U"H"; +S0x23 : U"J"; +S0x24 : U"K"; +S0x25 : U"L"; +S0x26 : U"Semicolon"; +S0x27 : U"Quote"; +S0x28 : U"Enter"; +S0x29 : U"LShift"; +S0x2A : U"Z"; +S0x2B : U"X"; +S0x2C : U"C"; +S0x2D : U"V"; +S0x2E : U"B"; +S0x2F : U"N"; +S0x30 : U"M"; +S0x31 : U"Comma"; +S0x32 : U"Period"; +S0x33 : U"Slash"; +S0x34 : U"RShift"; +S0x35 : U"Function1"; # Fun key +S0x36 : U"Function2"; # Left Blank Key +S0x37 : U"LAlt"; +S0x38 : U"LGui"; +S0x39 : U"Space"; +S0x3A : U"RGui"; +S0x3B : U"RAlt"; +S0x3C : U"Function3"; # Right Blank Key 1 +S0x3D : U"Function4"; # Right Blank Key 2 +S0x3E : U"BackTick"; + diff --git a/Scan/CK3/scan_loop.c b/Scan/CK3/scan_loop.c new file mode 100644 index 0000000..22d43c2 --- /dev/null +++ b/Scan/CK3/scan_loop.c @@ -0,0 +1,204 @@ +/* Copyright (C) 2014 by Jacob Alexander + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// ----- Includes ----- + +// Compiler Includes +#include + +// Project Includes +#include +#include +#include +#include +#include + +// Local Includes +#include "scan_loop.h" + + + +// ----- Function Declarations ----- + +// CLI Functions +void cliFunc_echo( char* args ); + + + +// ----- Variables ----- + +// Scan Module command dictionary +CLIDict_Entry( echo, "Example command, echos the arguments." ); + +CLIDict_Def( scanCLIDict, "Scan Module Commands" ) = { + CLIDict_Item( echo ), + { 0, 0, 0 } // Null entry for dictionary end +}; + +// Number of scans since the last USB send +uint16_t Scan_scanCount = 0; + + + +// ----- Functions ----- + +// Setup +inline void Scan_setup() +{ + // Register Scan CLI dictionary + CLI_registerDictionary( scanCLIDict, scanCLIDictName ); + + // Setup GPIO pins for matrix scanning + Matrix_setup(); + + // Reset scan count + Scan_scanCount = 0; +} + + +// Main Detection Loop +inline uint8_t Scan_loop() +{ + Matrix_scan( Scan_scanCount++ ); + + return 0; +} + + +// Signal from Macro Module that all keys have been processed (that it knows about) +inline void Scan_finishedWithMacro( uint8_t sentKeys ) +{ +} + + +// Signal from Output Module that all keys have been processed (that it knows about) +inline void Scan_finishedWithOutput( uint8_t sentKeys ) +{ + // Reset scan loop indicator (resets each key debounce state) + // TODO should this occur after USB send or Macro processing? + Scan_scanCount = 0; +} + + + +// ----- Capabilities ----- + +// Custom capability examples +// Refer to kll.h in Macros/PartialMap for state and stateType information +void CustomAction_action1_capability( uint8_t state, uint8_t stateType, uint8_t *args ) +{ + // Display capability name + // XXX This is required for debug cli to give you a list of capabilities + if ( stateType == 0xFF && state == 0xFF ) + { + print("CustomAction_action1_capability()"); + return; + } + + // Prints Action1 info message to the debug cli + info_print("Action1"); +} + +uint8_t CustomAction_blockHold_storage = 0; +void CustomAction_blockHold_capability( uint8_t state, uint8_t stateType, uint8_t *args ) +{ + // Display capability name + if ( stateType == 0xFF && state == 0xFF ) + { + print("CustomAction_blockHold_capability(usbCode)"); + return; + } + + // Retrieve 8-bit argument + uint8_t key = args[0]; + + // We only care about normal keys + if ( stateType == 0x00 ) + { + // Block given key if we're in the "Press" or "Hold" state + if ( ( state == 0x01 || state == 0x02 ) + && CustomAction_blockHold_storage == 0 ) + { + CustomAction_blockHold_storage = key; + info_msg("Blocking Key: "); + printHex( key ); + print( NL ); + } + // Release if in the "Off" or "Release" state and we're blocking + else if ( ( state == 0x00 || state == 0x03 ) + && key == CustomAction_blockHold_storage ) + { + info_msg("Unblocking Key: "); + printHex( CustomAction_blockHold_storage ); + print( NL ); + CustomAction_blockHold_storage = 0; + } + } +} + +void CustomAction_blockKey_capability( uint8_t state, uint8_t stateType, uint8_t *args ) +{ + // Display capability name + if ( stateType == 0xFF && state == 0xFF ) + { + print("CustomAction_blockKey_capability(usbCode)"); + return; + } + + // Retrieve 8-bit argument + uint8_t key = args[0]; + + // If key is not blocked, process + if ( key != CustomAction_blockHold_storage ) + { + extern void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args ); + Output_usbCodeSend_capability( state, stateType, &key ); + } +} + + + +// ----- CLI Command Functions ----- + +// XXX Just an example command showing how to parse arguments (more complex than generally needed) +void cliFunc_echo( char* args ) +{ + char* curArgs; + char* arg1Ptr; + char* arg2Ptr = args; + + // Parse args until a \0 is found + while ( 1 ) + { + print( NL ); // No \r\n by default after the command is entered + + curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + break; + + // Print out the arg + dPrint( arg1Ptr ); + } +} + diff --git a/Scan/CK3/scan_loop.h b/Scan/CK3/scan_loop.h new file mode 100644 index 0000000..0c89838 --- /dev/null +++ b/Scan/CK3/scan_loop.h @@ -0,0 +1,48 @@ +/* Copyright (C) 2014-2015 by Jacob Alexander + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +// ----- Includes ----- + +// Compiler Includes +#include + + + +// ----- Functions ----- + +// Functions to be called by main.c +void Scan_setup( void ); +uint8_t Scan_loop( void ); + +// Call-backs +void Scan_finishedWithMacro( uint8_t sentKeys ); // Called by Macro Module +void Scan_finishedWithOutput( uint8_t sentKeys ); // Called by Output Module + + +// ----- Capabilities ----- + +// Example capabilities +void CustomAction_action1_capability( uint8_t state, uint8_t stateType, uint8_t *args ); +void CustomAction_blockHold_capability( uint8_t state, uint8_t stateType, uint8_t *args ); +void CustomAction_blockKey_capability( uint8_t state, uint8_t stateType, uint8_t *args ); + diff --git a/Scan/CK3/setup.cmake b/Scan/CK3/setup.cmake new file mode 100644 index 0000000..d40a338 --- /dev/null +++ b/Scan/CK3/setup.cmake @@ -0,0 +1,32 @@ +###| CMake Kiibohd Controller Scan Module |### +# +# Written by Jacob Alexander in 2014 for the Kiibohd Controller +# +# Released into the Public Domain +# +### + + +### +# Required Submodules +# + +AddModule ( Scan MatrixARM ) + + +### +# Module C files +# + +set ( Module_SRCS + scan_loop.c +) + + +### +# Compiler Family Compatibility +# +set ( ModuleCompatibility + arm +) + diff --git a/Scan/MatrixARM/matrix_scan.c b/Scan/MatrixARM/matrix_scan.c index a9018df..03a4148 100644 --- a/Scan/MatrixARM/matrix_scan.c +++ b/Scan/MatrixARM/matrix_scan.c @@ -70,6 +70,15 @@ CLIDict_Def( matrixCLIDict, "Matrix Module Commands" ) = { // Debounce Array KeyState Matrix_scanArray[ Matrix_colsNum * Matrix_rowsNum ]; +// Ghost Arrays +#ifdef GHOST +KeyGhost Matrix_ghostArray[ Matrix_colsNum * Matrix_rowsNum ]; + +uint8_t col_use[Matrix_colsNum], row_use[Matrix_rowsNum]; // used count +uint8_t col_ghost[Matrix_colsNum], row_ghost[Matrix_rowsNum]; // marked as having ghost if 1 +#endif + + // Matrix debug flag - If set to 1, for each keypress the scan code is displayed in hex // If set to 2, for each key state change, the scan code is displayed along with the state uint8_t matrixDebugMode = 0; @@ -111,10 +120,17 @@ uint8_t Matrix_pin( GPIO_Pin gpio, Type type ) { case Type_StrobeOn: *GPIO_PSOR |= (1 << gpio.pin); + #ifdef GHOST + *GPIO_PDDR |= (1 << gpio.pin); // output + #endif break; case Type_StrobeOff: *GPIO_PCOR |= (1 << gpio.pin); + #ifdef GHOST + // Ghosting martix needs to put not used (off) strobes in high impedance state + *GPIO_PDDR &= ~(1 << gpio.pin); // input, high Z state + #endif break; case Type_StrobeSetup: @@ -206,6 +222,11 @@ void Matrix_setup() Matrix_scanArray[ item ].activeCount = 0; Matrix_scanArray[ item ].inactiveCount = DebounceDivThreshold_define; // Start at 'off' steady state Matrix_scanArray[ item ].prevDecisionTime = 0; + #ifdef GHOST + Matrix_ghostArray[ item ].prev = KeyState_Off; + Matrix_ghostArray[ item ].cur = KeyState_Off; + Matrix_ghostArray[ item ].saved = KeyState_Off; + #endif } // Clear scan stats counters @@ -372,12 +393,14 @@ void Matrix_scan( uint16_t scanNum ) erro_print("Matrix scan bug!! Report me!"); break; } - + // Update decision time state->prevDecisionTime = currentTime; // Send keystate to macro module + #ifndef GHOST Macro_keyState( key, state->curState ); + #endif // Matrix Debug, only if there is a state change if ( matrixDebugMode && state->curState != state->prevState ) @@ -403,6 +426,101 @@ void Matrix_scan( uint16_t scanNum ) Matrix_pin( Matrix_cols[ strobe ], Type_StrobeOff ); } + + // Matrix ghosting check and elimination + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +#ifdef GHOST + // strobe = column, sense = row + + // Count (rows) use for columns + //print("C "); + for ( uint8_t col = 0; col < Matrix_colsNum; col++ ) + { + uint8_t used = 0; + for ( uint8_t row = 0; row < Matrix_rowsNum; row++ ) + { + uint8_t key = Matrix_colsNum * row + col; + KeyState *state = &Matrix_scanArray[ key ]; + if ( keyOn(state->curState) ) + used++; + } + //printInt8(used); + col_use[col] = used; + col_ghost[col] = 0; // clear + } + + // Count (columns) use for rows + //print(" R "); + for ( uint8_t row = 0; row < Matrix_rowsNum; row++ ) + { + uint8_t used = 0; + for ( uint8_t col = 0; col < Matrix_colsNum; col++ ) + { + uint8_t key = Matrix_colsNum * row + col; + KeyState *state = &Matrix_scanArray[ key ]; + if ( keyOn(state->curState) ) + used++; + } + //printInt8(used); + row_use[row] = used; + row_ghost[row] = 0; // clear + } + + // Check if matrix has ghost + // Happens when key is pressed and some other key is pressed in same row and another in same column + //print(" G "); + for ( uint8_t col = 0; col < Matrix_colsNum; col++ ) + { + for ( uint8_t row = 0; row < Matrix_rowsNum; row++ ) + { + uint8_t key = Matrix_colsNum * row + col; + KeyState *state = &Matrix_scanArray[ key ]; + if ( keyOn(state->curState) && col_use[col] >= 2 && row_use[row] >= 2 ) + { + // mark col and row as having ghost + col_ghost[col] = 1; + row_ghost[row] = 1; + //print(" "); printInt8(col); print(","); printInt8(row); + } + } + } + //print( NL ); + + // Send keys + for ( uint8_t col = 0; col < Matrix_colsNum; col++ ) + { + for ( uint8_t row = 0; row < Matrix_rowsNum; row++ ) + { + uint8_t key = Matrix_colsNum * row + col; + KeyState *state = &Matrix_scanArray[ key ]; + KeyGhost *st = &Matrix_ghostArray[ key ]; + + // col or row is ghosting (crossed) + uint8_t ghost = (col_ghost[col] > 0 || row_ghost[row] > 0) ? 1 : 0; + + st->prev = st->cur; // previous + // save state if no ghost or outside ghosted area + if ( ghost == 0 ) + st->saved = state->curState; // save state if no ghost + // final + // use saved state if ghosting, or current if not + st->cur = ghost > 0 ? st->saved : state->curState; + + // Send keystate to macro module + KeyPosition k = !st->cur + ? (!st->prev ? KeyState_Off : KeyState_Release) + : ( st->prev ? KeyState_Hold : KeyState_Press); + //if (!st->cur && !st->prev) k = KeyState_Off; else + //if ( st->cur && st->prev) k = KeyState_Hold; else + //if ( st->cur && !st->prev) k = KeyState_Press; else + //if (!st->cur && st->prev) k = KeyState_Release; + Macro_keyState( key, k ); + } + } +#endif + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + // State Table Output Debug if ( matrixDebugStateCounter > 0 ) { diff --git a/Scan/MatrixARM/matrix_scan.h b/Scan/MatrixARM/matrix_scan.h index 810e6d9..ce09bf1 100644 --- a/Scan/MatrixARM/matrix_scan.h +++ b/Scan/MatrixARM/matrix_scan.h @@ -139,6 +139,16 @@ typedef struct KeyState { uint8_t prevDecisionTime; } __attribute__((packed)) KeyState; +// Ghost Element, after ghost detection/cancelation +typedef struct KeyGhost { + KeyPosition prev; + KeyPosition cur; + KeyPosition saved; // state before ghosting +} __attribute__((packed)) KeyGhost; + +// utility +inline uint8_t keyOn(/*KeyPosition*/uint8_t st) +{ return (st == KeyState_Press || st == KeyState_Hold) ? 1 : 0; } // ----- Functions ----- From 57a75851e29756ed36251b114e2b3b3630bb8121 Mon Sep 17 00:00:00 2001 From: CryHam Date: Sat, 20 Feb 2016 08:33:39 +0100 Subject: [PATCH 4/4] Minor styling fixes. Renamed define to GHOSTING_MATRIX. Deleted unused files from CK3. --- Scan/CK3/matrix.h | 5 +- Scan/CK3/pinout | 88 ------------------------------------ Scan/CK3/prototype.kll | 73 ------------------------------ Scan/MatrixARM/matrix_scan.c | 14 +++--- Scan/MatrixARM/matrix_scan.h | 6 ++- 5 files changed, 14 insertions(+), 172 deletions(-) delete mode 100644 Scan/CK3/pinout delete mode 100644 Scan/CK3/prototype.kll diff --git a/Scan/CK3/matrix.h b/Scan/CK3/matrix.h index 28bd3b0..1a2be2e 100644 --- a/Scan/CK3/matrix.h +++ b/Scan/CK3/matrix.h @@ -49,5 +49,6 @@ GPIO_Pin Matrix_rows[] = { gpio(D,1), gpio(D,2), gpio(D,3), gpio(D,4), gpio(D,5) Config Matrix_type = Config_Pulldown; -// define this if your matrix has ghosting (i.e. regular keyboard without diodes) -#define GHOST +// Define this if your matrix has ghosting (i.e. regular keyboard without diodes) +// this will enable the anti-ghosting code +#define GHOSTING_MATRIX diff --git a/Scan/CK3/pinout b/Scan/CK3/pinout deleted file mode 100644 index a72c0e6..0000000 --- a/Scan/CK3/pinout +++ /dev/null @@ -1,88 +0,0 @@ -Pin Usage -========= - -mk20dx128vlf5 - - ---- -|Keys| - ---- - -* Strobe (Columns) - -PTB0 -PTB1 -PTB2 -PTB3 -PTB16 -PTB17 -PTC4 -PTC5 -PTD0 - - -* Sense (Rows) - -PTD1 -PTD2 -PTD3 -PTD4 -PTD5 -PTD6 -PTD7 - - - ----- -|Debug| - ----- - -* SWD - -PTA0 (Pull-down) -PTA3 (Pull-up) - -* LEDs - -PTA19 (LED only for PCB, not McHCK) (XTAL) - -* UARTs - -PTA1 - RX0 -PTA2 - TX0 - -* Tag Connect - -1 - Vdd +5 -2 - PTA3 / SWD_IO -3 - Vss / Gnd -4 - PTA0 / SWD_CLK -5 - +5V -6 - PTA2 / TRACE_SWO -7 - N/C -8 - PTA1 / JTAG_TDI -9 - N/C -10 - Reset_b - - - ------ -|Unused| - ------ - -* GPIO - -PTA1 (Not broken out on PCB, available on McHCK) (Pull-up) -PTA2 (") -PTA4 (Pull-up) -PTA18 (EXTAL) - -PTC0 -PTC1 -PTC2 -PTC3 -PTC6 -PTC7 - -* Analog - -ADC0_DP0 -ADC0_DM0 - diff --git a/Scan/CK3/prototype.kll b/Scan/CK3/prototype.kll deleted file mode 100644 index 64fe862..0000000 --- a/Scan/CK3/prototype.kll +++ /dev/null @@ -1,73 +0,0 @@ -Name = MD1; -Version = 0.2; -Author = "HaaTa (Jacob Alexander) 2014"; -KLL = 0.3; - -# Modified Date -Date = 2014-09-14; - - -S0x00 : U"Esc"; -S0x01 : U"1"; -S0x02 : U"2"; -S0x03 : U"3"; -S0x04 : U"4"; -S0x05 : U"5"; -S0x06 : U"6"; -S0x07 : U"7"; -S0x08 : U"8"; -S0x09 : U"9"; -S0x0A : U"0"; -S0x0B : U"Minus"; -S0x0C : U"Equal"; -S0x0D : U"Backslash"; -S0x0E : U"Tab"; -S0x0F : U"Q"; -S0x10 : U"W"; -S0x11 : U"E"; -S0x12 : U"R"; -S0x13 : U"T"; -S0x14 : U"Y"; -S0x15 : U"U"; -S0x16 : U"I"; -S0x17 : U"O"; -S0x18 : U"P"; -S0x19 : U"LBrace"; -S0x1A : U"RBrace"; -S0x1B : U"Backspace"; -S0x1C : U"Ctrl"; -S0x1D : U"A"; -S0x1E : U"S"; -S0x1F : U"D"; -S0x20 : U"F"; -S0x21 : U"G"; -S0x22 : U"H"; -S0x23 : U"J"; -S0x24 : U"K"; -S0x25 : U"L"; -S0x26 : U"Semicolon"; -S0x27 : U"Quote"; -S0x28 : U"Enter"; -S0x29 : U"LShift"; -S0x2A : U"Z"; -S0x2B : U"X"; -S0x2C : U"C"; -S0x2D : U"V"; -S0x2E : U"B"; -S0x2F : U"N"; -S0x30 : U"M"; -S0x31 : U"Comma"; -S0x32 : U"Period"; -S0x33 : U"Slash"; -S0x34 : U"RShift"; -S0x35 : U"Function1"; # Fun key -S0x36 : U"Function2"; # Left Blank Key -S0x37 : U"LAlt"; -S0x38 : U"LGui"; -S0x39 : U"Space"; -S0x3A : U"RGui"; -S0x3B : U"RAlt"; -S0x3C : U"Function3"; # Right Blank Key 1 -S0x3D : U"Function4"; # Right Blank Key 2 -S0x3E : U"BackTick"; - diff --git a/Scan/MatrixARM/matrix_scan.c b/Scan/MatrixARM/matrix_scan.c index 03a4148..90343fe 100644 --- a/Scan/MatrixARM/matrix_scan.c +++ b/Scan/MatrixARM/matrix_scan.c @@ -71,7 +71,7 @@ CLIDict_Def( matrixCLIDict, "Matrix Module Commands" ) = { KeyState Matrix_scanArray[ Matrix_colsNum * Matrix_rowsNum ]; // Ghost Arrays -#ifdef GHOST +#ifdef GHOSTING_MATRIX KeyGhost Matrix_ghostArray[ Matrix_colsNum * Matrix_rowsNum ]; uint8_t col_use[Matrix_colsNum], row_use[Matrix_rowsNum]; // used count @@ -120,14 +120,14 @@ uint8_t Matrix_pin( GPIO_Pin gpio, Type type ) { case Type_StrobeOn: *GPIO_PSOR |= (1 << gpio.pin); - #ifdef GHOST + #ifdef GHOSTING_MATRIX *GPIO_PDDR |= (1 << gpio.pin); // output #endif break; case Type_StrobeOff: *GPIO_PCOR |= (1 << gpio.pin); - #ifdef GHOST + #ifdef GHOSTING_MATRIX // Ghosting martix needs to put not used (off) strobes in high impedance state *GPIO_PDDR &= ~(1 << gpio.pin); // input, high Z state #endif @@ -222,7 +222,7 @@ void Matrix_setup() Matrix_scanArray[ item ].activeCount = 0; Matrix_scanArray[ item ].inactiveCount = DebounceDivThreshold_define; // Start at 'off' steady state Matrix_scanArray[ item ].prevDecisionTime = 0; - #ifdef GHOST + #ifdef GHOSTING_MATRIX Matrix_ghostArray[ item ].prev = KeyState_Off; Matrix_ghostArray[ item ].cur = KeyState_Off; Matrix_ghostArray[ item ].saved = KeyState_Off; @@ -393,12 +393,12 @@ void Matrix_scan( uint16_t scanNum ) erro_print("Matrix scan bug!! Report me!"); break; } - + // Update decision time state->prevDecisionTime = currentTime; // Send keystate to macro module - #ifndef GHOST + #ifndef GHOSTING_MATRIX Macro_keyState( key, state->curState ); #endif @@ -429,7 +429,7 @@ void Matrix_scan( uint16_t scanNum ) // Matrix ghosting check and elimination // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . -#ifdef GHOST +#ifdef GHOSTING_MATRIX // strobe = column, sense = row // Count (rows) use for columns diff --git a/Scan/MatrixARM/matrix_scan.h b/Scan/MatrixARM/matrix_scan.h index ce09bf1..8b9cee7 100644 --- a/Scan/MatrixARM/matrix_scan.h +++ b/Scan/MatrixARM/matrix_scan.h @@ -146,9 +146,11 @@ typedef struct KeyGhost { KeyPosition saved; // state before ghosting } __attribute__((packed)) KeyGhost; -// utility +// utility inline uint8_t keyOn(/*KeyPosition*/uint8_t st) -{ return (st == KeyState_Press || st == KeyState_Hold) ? 1 : 0; } +{ + return (st == KeyState_Press || st == KeyState_Hold) ? 1 : 0; +} // ----- Functions -----