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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <led_scan.h>
  28. #include <print.h>
  29. #include <matrix_scan.h>
  30. #include <macro.h>
  31. #include <output_com.h>
  32. // Local Includes
  33. #include "scan_loop.h"
  34. // ----- Function Declarations -----
  35. // ----- Variables -----
  36. // Number of scans since the last USB send
  37. uint16_t Scan_scanCount = 0;
  38. // ----- Functions -----
  39. // Setup
  40. inline void Scan_setup()
  41. {
  42. // Setup GPIO pins for matrix scanning
  43. Matrix_setup();
  44. // Setup ISSI chip to control the leds
  45. LED_setup();
  46. // Reset scan count
  47. Scan_scanCount = 0;
  48. }
  49. // Main Detection Loop
  50. inline uint8_t Scan_loop()
  51. {
  52. Matrix_scan( Scan_scanCount++ );
  53. // Process any LED events
  54. LED_scan();
  55. return 0;
  56. }
  57. // Signal from Macro Module that all keys have been processed (that it knows about)
  58. inline void Scan_finishedWithMacro( uint8_t sentKeys )
  59. {
  60. }
  61. // Signal from Output Module that all keys have been processed (that it knows about)
  62. inline void Scan_finishedWithOutput( uint8_t sentKeys )
  63. {
  64. // Reset scan loop indicator (resets each key debounce state)
  65. // TODO should this occur after USB send or Macro processing?
  66. Scan_scanCount = 0;
  67. }
  68. // Signal from the Output Module that the available current has changed
  69. // current - mA
  70. void Scan_currentChange( unsigned int current )
  71. {
  72. // Indicate to all submodules current change
  73. Matrix_currentChange( current );
  74. LED_currentChange( current );
  75. }
  76. // ----- Capabilities -----
  77. // Custom capability examples
  78. // Refer to kll.h in Macros/PartialMap for state and stateType information
  79. void CustomAction_action1_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  80. {
  81. // Display capability name
  82. // XXX This is required for debug cli to give you a list of capabilities
  83. if ( stateType == 0xFF && state == 0xFF )
  84. {
  85. print("CustomAction_action1_capability()");
  86. return;
  87. }
  88. // Prints Action1 info message to the debug cli
  89. info_print("Action1");
  90. }
  91. uint8_t CustomAction_blockHold_storage = 0;
  92. void CustomAction_blockHold_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  93. {
  94. // Display capability name
  95. if ( stateType == 0xFF && state == 0xFF )
  96. {
  97. print("CustomAction_blockHold_capability(usbCode)");
  98. return;
  99. }
  100. // Retrieve 8-bit argument
  101. uint8_t key = args[0];
  102. // We only care about normal keys
  103. if ( stateType == 0x00 )
  104. {
  105. // Block given key if we're in the "Press" or "Hold" state
  106. if ( ( state == 0x01 || state == 0x02 )
  107. && CustomAction_blockHold_storage == 0 )
  108. {
  109. CustomAction_blockHold_storage = key;
  110. info_msg("Blocking Key: ");
  111. printHex( key );
  112. print( NL );
  113. }
  114. // Release if in the "Off" or "Release" state and we're blocking
  115. else if ( ( state == 0x00 || state == 0x03 )
  116. && key == CustomAction_blockHold_storage )
  117. {
  118. info_msg("Unblocking Key: ");
  119. printHex( CustomAction_blockHold_storage );
  120. print( NL );
  121. CustomAction_blockHold_storage = 0;
  122. }
  123. }
  124. }
  125. void CustomAction_blockKey_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  126. {
  127. // Display capability name
  128. if ( stateType == 0xFF && state == 0xFF )
  129. {
  130. print("CustomAction_blockKey_capability(usbCode)");
  131. return;
  132. }
  133. // Retrieve 8-bit argument
  134. uint8_t key = args[0];
  135. // If key is not blocked, process
  136. if ( key != CustomAction_blockHold_storage )
  137. {
  138. extern void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args );
  139. Output_usbCodeSend_capability( state, stateType, &key );
  140. }
  141. }