comparison Scan/CK3/scan_loop.c @ 415:293154e4aafe

Added support for ghosting matrices and code for elimination. To use define GHOST in matrix.h, see example in Scan/CK3
author CryHam <cryham@gmail.com>
date Fri, 19 Feb 2016 18:10:25 +0100
parents
children 971e88d4420a
comparison
equal deleted inserted replaced
414:06b8d295518e 415:293154e4aafe
1 /* Copyright (C) 2014 by Jacob Alexander
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/ScanLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <led.h>
30 #include <print.h>
31 #include <matrix_scan.h>
32 #include <macro.h>
33
34 // Local Includes
35 #include "scan_loop.h"
36
37
38
39 // ----- Function Declarations -----
40
41 // CLI Functions
42 void cliFunc_echo( char* args );
43
44
45
46 // ----- Variables -----
47
48 // Scan Module command dictionary
49 CLIDict_Entry( echo, "Example command, echos the arguments." );
50
51 CLIDict_Def( scanCLIDict, "Scan Module Commands" ) = {
52 CLIDict_Item( echo ),
53 { 0, 0, 0 } // Null entry for dictionary end
54 };
55
56 // Number of scans since the last USB send
57 uint16_t Scan_scanCount = 0;
58
59
60
61 // ----- Functions -----
62
63 // Setup
64 inline void Scan_setup()
65 {
66 // Register Scan CLI dictionary
67 CLI_registerDictionary( scanCLIDict, scanCLIDictName );
68
69 // Setup GPIO pins for matrix scanning
70 Matrix_setup();
71
72 // Reset scan count
73 Scan_scanCount = 0;
74 }
75
76
77 // Main Detection Loop
78 inline uint8_t Scan_loop()
79 {
80 Matrix_scan( Scan_scanCount++ );
81
82 return 0;
83 }
84
85
86 // Signal from Macro Module that all keys have been processed (that it knows about)
87 inline void Scan_finishedWithMacro( uint8_t sentKeys )
88 {
89 }
90
91
92 // Signal from Output Module that all keys have been processed (that it knows about)
93 inline void Scan_finishedWithOutput( uint8_t sentKeys )
94 {
95 // Reset scan loop indicator (resets each key debounce state)
96 // TODO should this occur after USB send or Macro processing?
97 Scan_scanCount = 0;
98 }
99
100
101
102 // ----- Capabilities -----
103
104 // Custom capability examples
105 // Refer to kll.h in Macros/PartialMap for state and stateType information
106 void CustomAction_action1_capability( uint8_t state, uint8_t stateType, uint8_t *args )
107 {
108 // Display capability name
109 // XXX This is required for debug cli to give you a list of capabilities
110 if ( stateType == 0xFF && state == 0xFF )
111 {
112 print("CustomAction_action1_capability()");
113 return;
114 }
115
116 // Prints Action1 info message to the debug cli
117 info_print("Action1");
118 }
119
120 uint8_t CustomAction_blockHold_storage = 0;
121 void CustomAction_blockHold_capability( uint8_t state, uint8_t stateType, uint8_t *args )
122 {
123 // Display capability name
124 if ( stateType == 0xFF && state == 0xFF )
125 {
126 print("CustomAction_blockHold_capability(usbCode)");
127 return;
128 }
129
130 // Retrieve 8-bit argument
131 uint8_t key = args[0];
132
133 // We only care about normal keys
134 if ( stateType == 0x00 )
135 {
136 // Block given key if we're in the "Press" or "Hold" state
137 if ( ( state == 0x01 || state == 0x02 )
138 && CustomAction_blockHold_storage == 0 )
139 {
140 CustomAction_blockHold_storage = key;
141 info_msg("Blocking Key: ");
142 printHex( key );
143 print( NL );
144 }
145 // Release if in the "Off" or "Release" state and we're blocking
146 else if ( ( state == 0x00 || state == 0x03 )
147 && key == CustomAction_blockHold_storage )
148 {
149 info_msg("Unblocking Key: ");
150 printHex( CustomAction_blockHold_storage );
151 print( NL );
152 CustomAction_blockHold_storage = 0;
153 }
154 }
155 }
156
157 void CustomAction_blockKey_capability( uint8_t state, uint8_t stateType, uint8_t *args )
158 {
159 // Display capability name
160 if ( stateType == 0xFF && state == 0xFF )
161 {
162 print("CustomAction_blockKey_capability(usbCode)");
163 return;
164 }
165
166 // Retrieve 8-bit argument
167 uint8_t key = args[0];
168
169 // If key is not blocked, process
170 if ( key != CustomAction_blockHold_storage )
171 {
172 extern void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args );
173 Output_usbCodeSend_capability( state, stateType, &key );
174 }
175 }
176
177
178
179 // ----- CLI Command Functions -----
180
181 // XXX Just an example command showing how to parse arguments (more complex than generally needed)
182 void cliFunc_echo( char* args )
183 {
184 char* curArgs;
185 char* arg1Ptr;
186 char* arg2Ptr = args;
187
188 // Parse args until a \0 is found
189 while ( 1 )
190 {
191 print( NL ); // No \r\n by default after the command is entered
192
193 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
194 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
195
196 // Stop processing args if no more are found
197 if ( *arg1Ptr == '\0' )
198 break;
199
200 // Print out the arg
201 dPrint( arg1Ptr );
202 }
203 }
204