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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/MacroLib.h>
  24. // Project Includes
  25. #include <cli.h>
  26. #include <led.h>
  27. #include <print.h>
  28. #include <scan_loop.h>
  29. #include <output_com.h>
  30. // Keymaps
  31. #include "usb_hid.h"
  32. #include <defaultMap.h>
  33. // Local Includes
  34. #include "macro.h"
  35. // ----- Function Declarations -----
  36. void cliFunc_capList ( char* args );
  37. void cliFunc_capSelect ( char* args );
  38. void cliFunc_lookComb ( char* args );
  39. void cliFunc_lookDefault( char* args );
  40. void cliFunc_lookPartial( char* args );
  41. void cliFunc_macroDebug ( char* args );
  42. // ----- Variables -----
  43. // Macro Module command dictionary
  44. char* macroCLIDictName = "Macro Module Commands (Not all commands fully work yet...)";
  45. CLIDictItem macroCLIDict[] = {
  46. { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
  47. { "capSelect", "Triggers the specified capability. U10 - USB Code 0x0A. K11 - Keyboard Capability 0x0B. S10 - Scancode 0x0A", cliFunc_capSelect },
  48. { "lookComb", "Do a lookup on the Combined map. S10 - Scancode 0x0A. U10 - USB Code 0x0A.", cliFunc_lookComb },
  49. { "lookDefault", "Do a lookup on the Default map. S10 - Scancode 0x0A.", cliFunc_lookDefault },
  50. { "lookPartial", "Do a lookup on the layered Partial maps. S10 - Scancode 0x0A. U10 - USB Code 0x0A.", cliFunc_lookPartial },
  51. { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
  52. { 0, 0, 0 } // Null entry for dictionary end
  53. };
  54. // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
  55. uint8_t macroDebugMode = 0;
  56. // ----- Functions -----
  57. inline void Macro_bufferAdd( uint8_t byte )
  58. {
  59. // Make sure we haven't overflowed the key buffer
  60. // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
  61. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
  62. {
  63. KeyIndex_Buffer[KeyIndex_BufferUsed++] = DefaultMap_Lookup[byte];
  64. }
  65. }
  66. inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
  67. {
  68. }
  69. inline void Macro_process()
  70. {
  71. // Only do one round of macro processing between Output Module timer sends
  72. if ( USBKeys_Sent != 0 )
  73. return;
  74. // Loop through input buffer
  75. for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
  76. {
  77. // Get the keycode from the buffer
  78. uint8_t key = KeyIndex_Buffer[index];
  79. // Set the modifier bit if this key is a modifier
  80. if ( key & KEY_LCTRL ) // AND with 0xE0
  81. {
  82. USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
  83. // Modifier processed, move on to the next key
  84. continue;
  85. }
  86. // Too many keys
  87. if ( USBKeys_Sent >= USBKeys_MaxSize )
  88. {
  89. warn_msg("USB Key limit reached");
  90. errorLED( 1 );
  91. break;
  92. }
  93. // Allow ignoring keys with 0's
  94. if ( key != 0 )
  95. {
  96. USBKeys_Array[USBKeys_Sent++] = key;
  97. }
  98. else
  99. {
  100. // Key was not mapped
  101. erro_msg( "Key not mapped... - " );
  102. printHex( key );
  103. errorLED( 1 );
  104. }
  105. }
  106. // Signal buffer that we've used it
  107. Scan_finishedWithBuffer( KeyIndex_BufferUsed );
  108. // If Macro debug mode is set, clear the USB Buffer
  109. if ( macroDebugMode )
  110. {
  111. USBKeys_Modifiers = 0;
  112. USBKeys_Sent = 0;
  113. }
  114. }
  115. inline void Macro_setup()
  116. {
  117. // Register Macro CLI dictionary
  118. CLI_registerDictionary( macroCLIDict, macroCLIDictName );
  119. // Disable Macro debug mode
  120. macroDebugMode = 0;
  121. }
  122. // ----- CLI Command Functions -----
  123. void cliFunc_capList( char* args )
  124. {
  125. // TODO
  126. }
  127. void cliFunc_capSelect( char* args )
  128. {
  129. // Parse code from argument
  130. // NOTE: Only first argument is used
  131. char* arg1Ptr;
  132. char* arg2Ptr;
  133. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  134. // Depending on the first character, the lookup changes
  135. switch ( arg1Ptr[0] )
  136. {
  137. // Keyboard Capability
  138. case 'K':
  139. // TODO
  140. break;
  141. // Scancode
  142. case 'S':
  143. // Add to the USB Buffer using the DefaultMap lookup
  144. Macro_bufferAdd( decToInt( &arg1Ptr[1] ) );
  145. break;
  146. // USB Code
  147. case 'U':
  148. // Just add the key to the USB Buffer
  149. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
  150. {
  151. KeyIndex_Buffer[KeyIndex_BufferUsed++] = decToInt( &arg1Ptr[1] );
  152. }
  153. break;
  154. }
  155. }
  156. void cliFunc_lookComb( char* args )
  157. {
  158. // Parse code from argument
  159. // NOTE: Only first argument is used
  160. char* arg1Ptr;
  161. char* arg2Ptr;
  162. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  163. // Depending on the first character, the lookup changes
  164. switch ( arg1Ptr[0] )
  165. {
  166. // Scancode
  167. case 'S':
  168. // TODO
  169. break;
  170. // USB Code
  171. case 'U':
  172. // TODO
  173. break;
  174. }
  175. }
  176. void cliFunc_lookDefault( char* args )
  177. {
  178. // Parse code from argument
  179. // NOTE: Only first argument is used
  180. char* arg1Ptr;
  181. char* arg2Ptr;
  182. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  183. // Depending on the first character, the lookup changes
  184. switch ( arg1Ptr[0] )
  185. {
  186. // Scancode
  187. case 'S':
  188. print( NL );
  189. printInt8( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
  190. print(" ");
  191. printHex( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
  192. break;
  193. }
  194. }
  195. void cliFunc_lookPartial( char* args )
  196. {
  197. // Parse code from argument
  198. // NOTE: Only first argument is used
  199. char* arg1Ptr;
  200. char* arg2Ptr;
  201. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  202. // Depending on the first character, the lookup changes
  203. switch ( arg1Ptr[0] )
  204. {
  205. // Scancode
  206. case 'S':
  207. // TODO
  208. break;
  209. // USB Code
  210. case 'U':
  211. // TODO
  212. break;
  213. }
  214. }
  215. void cliFunc_macroDebug( char* args )
  216. {
  217. // Toggle macro debug mode
  218. macroDebugMode = macroDebugMode ? 0 : 1;
  219. print( NL );
  220. info_msg("Macro Debug Mode: ");
  221. printInt8( macroDebugMode );
  222. }