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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. typedef uint32_t nat_ptr_t;
  41. //typedef uint16_t nat_ptr_t;
  42. // ----- Structs -----
  43. // -- Result Macro
  44. // Defines the sequence of combinations to as the Result of Trigger Macro
  45. //
  46. // Capability + args per USB send
  47. // Default Args (always sent): key state/analog of last key
  48. // Combo Length of 0 signifies end of sequence
  49. //
  50. // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
  51. // ResultMacro.pos -> <current combo position>
  52. // ResultMacro.state -> <last key state>
  53. // ResultMacro.stateType -> <last key state type>
  54. // ResultMacro struct, one is created per ResultMacro, no duplicates
  55. typedef struct ResultMacro {
  56. const uint8_t *guide;
  57. var_uint_t pos;
  58. uint8_t state;
  59. uint8_t stateType;
  60. } ResultMacro;
  61. // Guide, key element
  62. #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
  63. typedef struct ResultGuide {
  64. uint8_t index;
  65. uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
  66. } ResultGuide;
  67. // -- Trigger Macro
  68. // Defines the sequence of combinations to Trigger a Result Macro
  69. // Key Types:
  70. // * 0x00 Normal (Press/Hold/Release)
  71. // * 0x01 LED State (On/Off)
  72. // * 0x02 Analog (Threshold)
  73. // * 0x03-0xFE Reserved
  74. // * 0xFF Debug State
  75. //
  76. // Key State:
  77. // * Off - 0x00 (all flag states)
  78. // * On - 0x01
  79. // * Press/Hold/Release - 0x01/0x02/0x03
  80. // * Threshold (Range) - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
  81. // * Debug - 0xFF (Print capability name)
  82. //
  83. // Combo Length of 0 signifies end of sequence
  84. //
  85. // TriggerMacro.guide -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
  86. // TriggerMacro.result -> <index to result macro>
  87. // TriggerMacro.pos -> <current combo position>
  88. // TriggerMacro.state -> <status of the macro pos>
  89. // TriggerMacro states
  90. typedef enum TriggerMacroState {
  91. TriggerMacro_Press, // Combo in sequence is passing
  92. TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
  93. TriggerMacro_Waiting, // Awaiting user input
  94. } TriggerMacroState;
  95. // TriggerMacro struct, one is created per TriggerMacro, no duplicates
  96. typedef struct TriggerMacro {
  97. const uint8_t *guide;
  98. var_uint_t result;
  99. var_uint_t pos;
  100. TriggerMacroState state;
  101. } TriggerMacro;
  102. // Guide, key element
  103. #define TriggerGuideSize sizeof( TriggerGuide )
  104. typedef struct TriggerGuide {
  105. uint8_t type;
  106. uint8_t state;
  107. uint8_t scanCode;
  108. } TriggerGuide;
  109. // ----- Capabilities -----
  110. // Capability
  111. typedef struct Capability {
  112. void *func;
  113. uint8_t argCount;
  114. } Capability;
  115. // Total Number of Capabilities
  116. #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
  117. // -- Result Macros
  118. // Guide_RM / Define_RM Pair
  119. // Guide_RM( index ) = result;
  120. // * index - Result Macro index number
  121. // * result - Result Macro guide (see ResultMacro)
  122. // Define_RM( index );
  123. // * index - Result Macro index number
  124. // Must be used after Guide_RM
  125. #define Guide_RM( index ) const uint8_t rm##index##_guide[]
  126. #define Define_RM( index ) { rm##index##_guide, 0, 0, 0 }
  127. // -- Result Macro List
  128. // Total number of result macros (rm's)
  129. // Used to create pending rm's table
  130. #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
  131. // -- Trigger Macros
  132. // Guide_TM / Define_TM Trigger Setup
  133. // Guide_TM( index ) = trigger;
  134. // * index - Trigger Macro index number
  135. // * trigger - Trigger Macro guide (see TriggerMacro)
  136. // Define_TM( index, result );
  137. // * index - Trigger Macro index number
  138. // * result - Result Macro index number which is triggered by this Trigger Macro
  139. #define Guide_TM( index ) const uint8_t tm##index##_guide[]
  140. #define Define_TM( index, result ) { tm##index##_guide, result, 0, TriggerMacro_Waiting }
  141. // -- Trigger Macro List
  142. // Total number of trigger macros (tm's)
  143. // Used to create pending tm's table
  144. #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
  145. // ----- Trigger Maps -----
  146. // Define_TL( layer, scanCode ) = triggerList;
  147. // * layer - basename of the layer
  148. // * scanCode - Hex value of the scanCode
  149. // * triggerList - Trigger List (see Trigger Lists)
  150. #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
  151. // ----- Layer Index -----
  152. // Defines each map of trigger macro lists
  153. // Layer 0 is always the default map
  154. // Layer States:
  155. // * Off - 0x00
  156. // * Shift - 0x01
  157. // * Latch - 0x02
  158. // * Lock - 0x04
  159. //
  160. // Except for Off, all states an exist simultaneously for each layer
  161. // For example:
  162. // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
  163. //
  164. // Max defines the maximum number of keys in the map, maximum of 0xFF
  165. // - Compiler calculates this
  166. //
  167. // The name is defined for cli debugging purposes (Null terminated string)
  168. typedef struct Layer {
  169. const nat_ptr_t **triggerMap;
  170. const char *name;
  171. const uint8_t max;
  172. uint8_t state;
  173. } Layer;
  174. // Layer_IN( map, name );
  175. // * map - Trigger map
  176. // * name - Name of the trigger map
  177. #define Layer_IN( map, name ) { map, name, sizeof( map ) / 4 - 1, 0 }
  178. // Total number of layers
  179. #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )
  180. #endif // __kll_h