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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // KLL Generated Defines
  20. #include <kll_defs.h>
  21. // Project Includes
  22. #include <print.h>
  23. #include <scan_loop.h>
  24. #include <macro.h>
  25. #include <output_com.h>
  26. // USB HID Keymap list
  27. #include <usb_hid.h>
  28. // ----- Types -----
  29. // - NOTE -
  30. // It is possible to change the maximum state and indexing positions of the state machine.
  31. // This usually affects the SRAM usage quite a bit, so it can be used to fit the code on smaller uCs
  32. // Or to allow for nearly infinite states.
  33. #if StateWordSize_define == 32
  34. typedef uint32_t var_uint_t;
  35. #elif StateWordSize_define == 16
  36. typedef uint16_t var_uint_t;
  37. #elif StateWordSize_define == 8
  38. typedef uint8_t var_uint_t;
  39. #else
  40. #error "Invalid StateWordSize, possible values: 32, 16 and 8."
  41. #endif
  42. // - NOTE -
  43. // Native pointer length
  44. // This needs to be defined per microcontroller
  45. // e.g. mk20s -> 32 bit
  46. // atmega -> 16 bit
  47. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  48. typedef uint32_t nat_ptr_t;
  49. #elif defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  50. typedef uint16_t nat_ptr_t;
  51. #endif
  52. // ----- Structs -----
  53. // -- Macro Type
  54. // Defines what type of Macro the struct defines
  55. // Always the first field
  56. typedef enum MacroType {
  57. MacroType_Normal, // Sequence of Combos
  58. MacroType_Simple, // Single combination (no state required)
  59. } MacroType;
  60. // -- Result Macro
  61. // Defines the sequence of combinations to as the Result of Trigger Macro
  62. // For RAM optimization reasons, ResultMacro has been split into ResultMacro and ResultMacroRecord structures
  63. //
  64. // Capability + args per USB send
  65. // Default Args (always sent): key state/analog of last key
  66. // Combo Length of 0 signifies end of sequence
  67. //
  68. // ResultMacro.type -> <Type of macro>
  69. // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
  70. //
  71. // ResultMacroRecord.pos -> <current combo position>
  72. // ResultMacroRecord.state -> <last key state>
  73. // ResultMacroRecord.stateType -> <last key state type>
  74. // ResultMacroRecord struct, one is created per ResultMacro, no duplicates, if a MacroType_Normal
  75. typedef struct ResultMacroRecord {
  76. var_uint_t pos;
  77. uint8_t state;
  78. uint8_t stateType;
  79. } ResultMacroRecord;
  80. // ResultMacro struct, one is created per ResultMacro, no duplicates
  81. typedef const struct ResultMacro {
  82. const MacroType type;
  83. const uint8_t *guide;
  84. ResultMacroRecord *record;
  85. } ResultMacro;
  86. // Guide, key element
  87. #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
  88. typedef struct ResultGuide {
  89. uint8_t index;
  90. uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
  91. } ResultGuide;
  92. // -- Trigger Macro
  93. // Defines the sequence of combinations to Trigger a Result Macro
  94. // For RAM optimization reasons TriggerMacro has been split into TriggerMacro and TriggerMacroRecord
  95. // Key Types:
  96. // * 0x00 Normal (Press/Hold/Release)
  97. // * 0x01 LED State (On/Off)
  98. // * 0x02 Analog (Threshold)
  99. // * 0x03-0xFE Reserved
  100. // * 0xFF Debug State
  101. //
  102. // Key State:
  103. // * Off - 0x00 (all flag states)
  104. // * On - 0x01
  105. // * Press/Hold/Release - 0x01/0x02/0x03
  106. // * Threshold (Range) - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
  107. // * Debug - 0xFF (Print capability name)
  108. //
  109. // Combo Length of 0 signifies end of sequence
  110. //
  111. // TriggerMacro.type -> <Type of macro>
  112. // TriggerMacro.guide -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
  113. // TriggerMacro.result -> <index to result macro>
  114. //
  115. // TriggerMacroRecord.pos -> <current combo position>
  116. // TriggerMacroRecord.state -> <status of the macro pos>
  117. // TriggerMacro states
  118. typedef enum TriggerMacroState {
  119. TriggerMacro_Press, // Combo in sequence is passing
  120. TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
  121. TriggerMacro_Waiting, // Awaiting user input
  122. } TriggerMacroState;
  123. // TriggerMacroRecord struct, one is created per TriggerMacro, no duplicates, if a MacroType_Normal
  124. typedef struct TriggerMacroRecord {
  125. var_uint_t pos;
  126. TriggerMacroState state;
  127. } TriggerMacroRecord;
  128. // TriggerMacro struct, one is created per TriggerMacro, no duplicates
  129. typedef const struct TriggerMacro {
  130. const MacroType type;
  131. const uint8_t *guide;
  132. const var_uint_t result;
  133. TriggerMacroRecord *record;
  134. } TriggerMacro;
  135. // Guide, key element
  136. #define TriggerGuideSize sizeof( TriggerGuide )
  137. typedef struct TriggerGuide {
  138. uint8_t type;
  139. uint8_t state;
  140. uint8_t scanCode;
  141. } TriggerGuide;
  142. // ----- Capabilities -----
  143. // Capability
  144. typedef struct Capability {
  145. const void *func;
  146. const uint8_t argCount;
  147. } Capability;
  148. // Total Number of Capabilities
  149. #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
  150. // -- Result Macros
  151. // Guide_RM / Record_RM / Define_RM
  152. // Guide_RM( index ) = result;
  153. // * index - Result Macro index number
  154. // * result - Result Macro guide (see ResultMacro)
  155. // Record_RM( index );
  156. // * index - Result Macro index number
  157. // Define_RM( index );
  158. // * index - Result Macro index number
  159. // Simple macros do not have a record
  160. // Must be used after Guide_RM and Record_RM
  161. #define Guide_RM( index ) const uint8_t rm##index##_guide[]
  162. #define Record_RM( index ) ResultMacroRecord rm##index##_record
  163. #define Define_RM_Normal( index ) { MacroType_Normal, rm##index##_guide, &rm##index##_record }
  164. #define Define_RM_Simple( index ) { MacroType_Simple, rm##index##_guide, 0 }
  165. // -- Result Macro List
  166. // Total number of result macros (rm's)
  167. // Used to create pending rm's table
  168. #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
  169. // -- Trigger Macros
  170. // Guide_TM / Record_TM / Define_TM Trigger Setup
  171. // Guide_TM( index ) = trigger;
  172. // * index - Trigger Macro index number
  173. // * trigger - Trigger Macro guide (see TriggerMacro)
  174. // Record_TM( index );
  175. // * index - Trigger Macro index number
  176. // Define_TM( index, result );
  177. // * index - Trigger Macro index number
  178. // * result - Result Macro index number which is triggered by this Trigger Macro
  179. #define Guide_TM( index ) const uint8_t tm##index##_guide[]
  180. #define Record_TM( index ) TriggerMacroRecord tm##index##_record
  181. #define Define_TM_Normal( index, result ) { MacroType_Normal, tm##index##_guide, result, &tm##index##_record }
  182. #define Define_TM_Simple( index, result ) { MacroType_Simple, tm##index##_guide, result, 0 }
  183. // -- Trigger Macro List
  184. // Total number of trigger macros (tm's)
  185. // Used to create pending tm's table
  186. #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
  187. // ----- Trigger Maps -----
  188. // Define_TL( layer, scanCode ) = triggerList;
  189. // * layer - basename of the layer
  190. // * scanCode - Hex value of the scanCode
  191. // * triggerList - Trigger List (see Trigger Lists)
  192. #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
  193. // ----- Layer Index -----
  194. // Defines each map of trigger macro lists
  195. // Layer 0 is always the default map
  196. // Layer States:
  197. // * Off - 0x00
  198. // * Shift - 0x01
  199. // * Latch - 0x02
  200. // * Lock - 0x04
  201. // Layer states are stored in the LayerState array
  202. //
  203. // Except for Off, all states an exist simultaneously for each layer
  204. // For example:
  205. // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
  206. //
  207. // First defines the first used scan code (most keyboards start at 0, some start higher e.g. 0x40)
  208. // - Compiler calculates this
  209. //
  210. // Last defines the last scan code used (helps reduce RAM usage)
  211. //
  212. // The name is defined for cli debugging purposes (Null terminated string)
  213. typedef struct Layer {
  214. const nat_ptr_t **triggerMap;
  215. const char *name;
  216. const uint8_t first;
  217. const uint8_t last;
  218. } Layer;
  219. // Layer_IN( map, name, first );
  220. // * map - Trigger map
  221. // * name - Name of the trigger map
  222. // * first - First scan code used (most keyboards start at 0, some start higher e.g. 0x40)
  223. #define Layer_IN( map, name, first ) { map, name, first, sizeof( map ) / sizeof( nat_ptr_t ) - 1 + first }
  224. // Total number of layers
  225. #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )
  226. #endif // __kll_h