]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Scan/MatrixARM/matrix_scan.c
Adding basic remote capabilities + UART Rx DMA buffers
[kiibohd-controller.git] / Scan / MatrixARM / matrix_scan.c
index 85f82050f33cb4ef83ce08086af369d85db0ccc0..a9018df9494a5d47ba5f3faee7347a207f5840c6 100644 (file)
@@ -26,7 +26,7 @@
 
 // Project Includes
 #include <cli.h>
-#include <kll.h>
+#include <kll_defs.h>
 #include <led.h>
 #include <print.h>
 #include <macro.h>
@@ -82,6 +82,9 @@ uint16_t matrixMaxScans  = 0;
 uint16_t matrixCurScans  = 0;
 uint16_t matrixPrevScans = 0;
 
+// System Timer used for delaying debounce decisions
+extern volatile uint32_t systick_millis_count;
+
 
 
 // ----- Functions -----
@@ -193,14 +196,16 @@ void Matrix_setup()
        print( NL );
        info_msg("Max Keys: ");
        printHex( Matrix_maxKeys );
+       print( NL );
 
        // Clear out Debounce Array
        for ( uint8_t item = 0; item < Matrix_maxKeys; item++ )
        {
-               Matrix_scanArray[ item ].prevState     = KeyState_Off;
-               Matrix_scanArray[ item ].curState      = KeyState_Off;
-               Matrix_scanArray[ item ].activeCount   = 0;
-               Matrix_scanArray[ item ].inactiveCount = DebounceDivThreshold_define; // Start at 'off' steady state
+               Matrix_scanArray[ item ].prevState        = KeyState_Off;
+               Matrix_scanArray[ item ].curState         = KeyState_Off;
+               Matrix_scanArray[ item ].activeCount      = 0;
+               Matrix_scanArray[ item ].inactiveCount    = DebounceDivThreshold_define; // Start at 'off' steady state
+               Matrix_scanArray[ item ].prevDecisionTime = 0;
        }
 
        // Clear scan stats counters
@@ -262,6 +267,9 @@ void Matrix_scan( uint16_t scanNum )
                matrixCurScans++;
        }
 
+       // Read systick for event scheduling
+       uint8_t currentTime = (uint8_t)systick_millis_count;
+
        // For each strobe, scan each of the sense pins
        for ( uint8_t strobe = 0; strobe < Matrix_colsNum; strobe++ )
        {
@@ -305,11 +313,16 @@ void Matrix_scan( uint16_t scanNum )
                        }
 
                        // Check for state change if it hasn't been set
+                       // But only if enough time has passed since last state change
                        // Only check if the minimum number of scans has been met
                        //   the current state is invalid
                        //   and either active or inactive count is over the debounce threshold
                        if ( state->curState == KeyState_Invalid )
                        {
+                               // Determine time since last decision
+                               uint8_t lastTransition = currentTime - state->prevDecisionTime;
+
+                               // Attempt state transition
                                switch ( state->prevState )
                                {
                                case KeyState_Press:
@@ -320,6 +333,15 @@ void Matrix_scan( uint16_t scanNum )
                                        }
                                        else
                                        {
+                                               // If not enough time has passed since Hold
+                                               // Keep previous state
+                                               if ( lastTransition < MinDebounceTime_define )
+                                               {
+                                                       //warn_print("FAST Release stopped");
+                                                       state->curState = state->prevState;
+                                                       continue;
+                                               }
+
                                                state->curState = KeyState_Release;
                                        }
                                        break;
@@ -328,6 +350,15 @@ void Matrix_scan( uint16_t scanNum )
                                case KeyState_Off:
                                        if ( state->activeCount > state->inactiveCount )
                                        {
+                                               // If not enough time has passed since Hold
+                                               // Keep previous state
+                                               if ( lastTransition < MinDebounceTime_define )
+                                               {
+                                                       //warn_print("FAST Press stopped");
+                                                       state->curState = state->prevState;
+                                                       continue;
+                                               }
+
                                                state->curState = KeyState_Press;
                                        }
                                        else
@@ -342,6 +373,9 @@ void Matrix_scan( uint16_t scanNum )
                                        break;
                                }
 
+                               // Update decision time
+                               state->prevDecisionTime = currentTime;
+
                                // Send keystate to macro module
                                Macro_keyState( key, state->curState );