Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

scan_loop.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (C) 2014-2016 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. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/ScanLib.h>
  24. // Project Includes
  25. #include <cli.h>
  26. #include <led.h>
  27. #include <print.h>
  28. #include <matrix_scan.h>
  29. #include <macro.h>
  30. // Local Includes
  31. #include "scan_loop.h"
  32. // ----- Function Declarations -----
  33. // CLI Functions
  34. void cliFunc_echo( char* args );
  35. // ----- Variables -----
  36. // Scan Module command dictionary
  37. CLIDict_Entry( echo, "Example command, echos the arguments." );
  38. CLIDict_Def( scanCLIDict, "Scan Module Commands" ) = {
  39. CLIDict_Item( echo ),
  40. { 0, 0, 0 } // Null entry for dictionary end
  41. };
  42. // Number of scans since the last USB send
  43. uint16_t Scan_scanCount = 0;
  44. // ----- Functions -----
  45. // Setup
  46. inline void Scan_setup()
  47. {
  48. // Register Scan CLI dictionary
  49. CLI_registerDictionary( scanCLIDict, scanCLIDictName );
  50. // Setup GPIO pins for matrix scanning
  51. Matrix_setup();
  52. // Reset scan count
  53. Scan_scanCount = 0;
  54. }
  55. // Main Detection Loop
  56. inline uint8_t Scan_loop()
  57. {
  58. Matrix_scan( Scan_scanCount++ );
  59. return 0;
  60. }
  61. // Signal from Macro Module that all keys have been processed (that it knows about)
  62. inline void Scan_finishedWithMacro( uint8_t sentKeys )
  63. {
  64. }
  65. // Signal from Output Module that all keys have been processed (that it knows about)
  66. inline void Scan_finishedWithOutput( uint8_t sentKeys )
  67. {
  68. // Reset scan loop indicator (resets each key debounce state)
  69. // TODO should this occur after USB send or Macro processing?
  70. Scan_scanCount = 0;
  71. }
  72. // ----- Capabilities -----
  73. // Custom capability examples
  74. // Refer to kll.h in Macros/PartialMap for state and stateType information
  75. void CustomAction_action1_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  76. {
  77. // Display capability name
  78. // XXX This is required for debug cli to give you a list of capabilities
  79. if ( stateType == 0xFF && state == 0xFF )
  80. {
  81. print("CustomAction_action1_capability()");
  82. return;
  83. }
  84. // Prints Action1 info message to the debug cli
  85. info_print("Action1");
  86. }
  87. uint8_t CustomAction_blockHold_storage = 0;
  88. void CustomAction_blockHold_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  89. {
  90. // Display capability name
  91. if ( stateType == 0xFF && state == 0xFF )
  92. {
  93. print("CustomAction_blockHold_capability(usbCode)");
  94. return;
  95. }
  96. // Retrieve 8-bit argument
  97. uint8_t key = args[0];
  98. // We only care about normal keys
  99. if ( stateType == 0x00 )
  100. {
  101. // Block given key if we're in the "Press" or "Hold" state
  102. if ( ( state == 0x01 || state == 0x02 )
  103. && CustomAction_blockHold_storage == 0 )
  104. {
  105. CustomAction_blockHold_storage = key;
  106. info_msg("Blocking Key: ");
  107. printHex( key );
  108. print( NL );
  109. }
  110. // Release if in the "Off" or "Release" state and we're blocking
  111. else if ( ( state == 0x00 || state == 0x03 )
  112. && key == CustomAction_blockHold_storage )
  113. {
  114. info_msg("Unblocking Key: ");
  115. printHex( CustomAction_blockHold_storage );
  116. print( NL );
  117. CustomAction_blockHold_storage = 0;
  118. }
  119. }
  120. }
  121. void CustomAction_blockKey_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_blockKey_capability(usbCode)");
  127. return;
  128. }
  129. // Retrieve 8-bit argument
  130. uint8_t key = args[0];
  131. // If key is not blocked, process
  132. if ( key != CustomAction_blockHold_storage )
  133. {
  134. extern void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args );
  135. Output_usbCodeSend_capability( state, stateType, &key );
  136. }
  137. }
  138. // Signal from the Output Module that the available current has changed
  139. // current - mA
  140. void Scan_currentChange( unsigned int current )
  141. {
  142. // Indicate to all submodules current change
  143. Matrix_currentChange( current );
  144. }
  145. // ----- CLI Command Functions -----
  146. // XXX Just an example command showing how to parse arguments (more complex than generally needed)
  147. void cliFunc_echo( char* args )
  148. {
  149. char* curArgs;
  150. char* arg1Ptr;
  151. char* arg2Ptr = args;
  152. // Parse args until a \0 is found
  153. while ( 1 )
  154. {
  155. print( NL ); // No \r\n by default after the command is entered
  156. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  157. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  158. // Stop processing args if no more are found
  159. if ( *arg1Ptr == '\0' )
  160. break;
  161. // Print out the arg
  162. dPrint( arg1Ptr );
  163. }
  164. }