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.

kll.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (C) 2014 by Jacob Alexander
  2. *
  3. * This file is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __kll_h
  17. #define __kll_h
  18. // ----- Includes -----
  19. // Project Includes
  20. #include <print.h>
  21. #include <scan_loop.h>
  22. #include <macro.h>
  23. #include <output_com.h>
  24. // USB HID Keymap list
  25. #include <usb_hid.h>
  26. // ----- Types -----
  27. // - NOTE -
  28. // It is possible to change the maximum state and indexing positions of the state machine.
  29. // This usually affects the SRAM usage quite a bit, so it can be used to fit the code on smaller uCs
  30. // Or to allow for nearly infinite states.
  31. // TODO Make selectable from layout variable
  32. //typedef uint32_t var_uint_t;
  33. //typedef uint16_t var_uint_t;
  34. typedef uint8_t var_uint_t;
  35. // - NOTE -
  36. // Native pointer length
  37. // This needs to be defined per microcontroller
  38. // e.g. mk20s -> 32 bit
  39. // atmega -> 16 bit
  40. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  41. typedef uint32_t nat_ptr_t;
  42. #elif defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  43. typedef uint16_t nat_ptr_t;
  44. #endif
  45. // ----- Structs -----
  46. // -- Result Macro
  47. // Defines the sequence of combinations to as the Result of Trigger Macro
  48. //
  49. // Capability + args per USB send
  50. // Default Args (always sent): key state/analog of last key
  51. // Combo Length of 0 signifies end of sequence
  52. //
  53. // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
  54. // ResultMacro.pos -> <current combo position>
  55. // ResultMacro.state -> <last key state>
  56. // ResultMacro.stateType -> <last key state type>
  57. // ResultMacro struct, one is created per ResultMacro, no duplicates
  58. typedef struct ResultMacro {
  59. const uint8_t *guide;
  60. var_uint_t pos;
  61. uint8_t state;
  62. uint8_t stateType;
  63. } ResultMacro;
  64. // Guide, key element
  65. #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
  66. typedef struct ResultGuide {
  67. uint8_t index;
  68. uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
  69. } ResultGuide;
  70. // -- Trigger Macro
  71. // Defines the sequence of combinations to Trigger a Result Macro
  72. // Key Types:
  73. // * 0x00 Normal (Press/Hold/Release)
  74. // * 0x01 LED State (On/Off)
  75. // * 0x02 Analog (Threshold)
  76. // * 0x03-0xFE Reserved
  77. // * 0xFF Debug State
  78. //
  79. // Key State:
  80. // * Off - 0x00 (all flag states)
  81. // * On - 0x01
  82. // * Press/Hold/Release - 0x01/0x02/0x03
  83. // * Threshold (Range) - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
  84. // * Debug - 0xFF (Print capability name)
  85. //
  86. // Combo Length of 0 signifies end of sequence
  87. //
  88. // TriggerMacro.guide -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
  89. // TriggerMacro.result -> <index to result macro>
  90. // TriggerMacro.pos -> <current combo position>
  91. // TriggerMacro.state -> <status of the macro pos>
  92. // TriggerMacro states
  93. typedef enum TriggerMacroState {
  94. TriggerMacro_Press, // Combo in sequence is passing
  95. TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
  96. TriggerMacro_Waiting, // Awaiting user input
  97. } TriggerMacroState;
  98. // TriggerMacro struct, one is created per TriggerMacro, no duplicates
  99. typedef struct TriggerMacro {
  100. const uint8_t *guide;
  101. var_uint_t result;
  102. var_uint_t pos;
  103. TriggerMacroState state;
  104. } TriggerMacro;
  105. // Guide, key element
  106. #define TriggerGuideSize sizeof( TriggerGuide )
  107. typedef struct TriggerGuide {
  108. uint8_t type;
  109. uint8_t state;
  110. uint8_t scanCode;
  111. } TriggerGuide;
  112. // ----- Capabilities -----
  113. // Capability
  114. typedef struct Capability {
  115. void *func;
  116. uint8_t argCount;
  117. } Capability;
  118. // Total Number of Capabilities
  119. #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
  120. // -- Result Macros
  121. // Guide_RM / Define_RM Pair
  122. // Guide_RM( index ) = result;
  123. // * index - Result Macro index number
  124. // * result - Result Macro guide (see ResultMacro)
  125. // Define_RM( index );
  126. // * index - Result Macro index number
  127. // Must be used after Guide_RM
  128. #define Guide_RM( index ) const uint8_t rm##index##_guide[]
  129. #define Define_RM( index ) { rm##index##_guide, 0, 0, 0 }
  130. // -- Result Macro List
  131. // Total number of result macros (rm's)
  132. // Used to create pending rm's table
  133. #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
  134. // -- Trigger Macros
  135. // Guide_TM / Define_TM Trigger Setup
  136. // Guide_TM( index ) = trigger;
  137. // * index - Trigger Macro index number
  138. // * trigger - Trigger Macro guide (see TriggerMacro)
  139. // Define_TM( index, result );
  140. // * index - Trigger Macro index number
  141. // * result - Result Macro index number which is triggered by this Trigger Macro
  142. #define Guide_TM( index ) const uint8_t tm##index##_guide[]
  143. #define Define_TM( index, result ) { tm##index##_guide, result, 0, TriggerMacro_Waiting }
  144. // -- Trigger Macro List
  145. // Total number of trigger macros (tm's)
  146. // Used to create pending tm's table
  147. #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
  148. // ----- Trigger Maps -----
  149. // Define_TL( layer, scanCode ) = triggerList;
  150. // * layer - basename of the layer
  151. // * scanCode - Hex value of the scanCode
  152. // * triggerList - Trigger List (see Trigger Lists)
  153. #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
  154. // ----- Layer Index -----
  155. // Defines each map of trigger macro lists
  156. // Layer 0 is always the default map
  157. // Layer States:
  158. // * Off - 0x00
  159. // * Shift - 0x01
  160. // * Latch - 0x02
  161. // * Lock - 0x04
  162. //
  163. // Except for Off, all states an exist simultaneously for each layer
  164. // For example:
  165. // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
  166. //
  167. // Max defines the maximum number of keys in the map, maximum of 0xFF
  168. // - Compiler calculates this
  169. //
  170. // The name is defined for cli debugging purposes (Null terminated string)
  171. typedef struct Layer {
  172. const nat_ptr_t **triggerMap;
  173. const char *name;
  174. const uint8_t max;
  175. uint8_t state;
  176. } Layer;
  177. // Layer_IN( map, name );
  178. // * map - Trigger map
  179. // * name - Name of the trigger map
  180. #define Layer_IN( map, name ) { map, name, sizeof( map ) / sizeof( nat_ptr_t ) - 1, 0 }
  181. // Total number of layers
  182. #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )
  183. #endif // __kll_h