keybrd library is an open source library for creating custom-keyboard firmware.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

7 лет назад
7 лет назад
8 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
8 лет назад
8 лет назад
7 лет назад
7 лет назад
7 лет назад
8 лет назад
7 лет назад
8 лет назад
8 лет назад
7 лет назад
7 лет назад
7 лет назад
8 лет назад
7 лет назад
8 лет назад
8 лет назад
7 лет назад
7 лет назад
7 лет назад
8 лет назад
8 лет назад
7 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
7 лет назад
7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Tutorial 3a - multi-layer keyboard
  2. ==================================
  3. When you finish this tutorial you will be able to be able to modify a multi-layer keybrd sketch to write your very own multi-layer keyboard firmware.
  4. Multi-layer nomenclature
  5. ------------------------
  6. **[layers](http://deskthority.net/wiki/Layer)** - are key bindings provided by the keyboard firmware. For example,
  7. * The classic [IBM Model M keyboard](http://en.wikipedia.org/wiki/IBM_PC_keyboard) has one layer.
  8. * Many compact keyboards have an additional [Fn layer](http://en.wikipedia.org/wiki/Fn_key).
  9. * The [Neo layout](http://neo-layout.org/index_en.html) has 6 layers.
  10. **layer id** - is an integer used to identify a layer.
  11. **active layer** - is the layer currently used by the keyboard.
  12. **default layer** - is the active layer when the keyboard starts up.
  13. **layer scheme** - is a system for changing the active layer while typing (a single-layer scheme does not change layers).
  14. Code classes
  15. ------------
  16. Code objects only have one scancode or one layer code.
  17. Example Code classes include:
  18. * Code_Sc
  19. * Code_ScS
  20. * Code_ScNS
  21. * Code_Shift
  22. * Code_LayerHold
  23. * Code_LayerLock
  24. Single-layer keys contain one Code object.
  25. Multi-layer keys contain multiple Code objects, one code for each layer.
  26. A simple multi-layer keybrd sketch
  27. ----------------------------------
  28. The [keybrd_3a_multi-layerHold.ino](keybrd_3a_multi-layerHold/keybrd_3a_multi-layerHold.ino) sketch is for a simple two-layer keyboard.
  29. It will run on the basic breadboard keyboard described in [tutorial_1_breadboard_keyboard.md](tutorial_1_breadboard_keyboard.md).
  30. ![basic breadboard keyboard](keybrd_1_breadboard/basic_breadboard_keyboard_front.JPG "basic breadboard keyboard")
  31. The sketch annotations explain how multi-layer keyboards work.
  32. The sketch uses three layer-scheme classes:
  33. * LayerState
  34. * Code_LayerHold
  35. * Key_LayeredKeys
  36. The internal workings of these three classes are revealed in the next section.
  37. Pseudo code for simple layer scheme
  38. -----------------------------------
  39. The following pseudo code is of three keybrd library classes.
  40. It has just enough detail to show the internal workings of layer schemes.
  41. **Code_Layer** objects change the active layer.
  42. When a Code_Layer object is pressed, it tells LayerState to update the active layer.
  43. ```
  44. class Code_Layer
  45. {
  46. const int layerId;
  47. LayerState& refLayerState;
  48. press() { refLayerState.setActiveLayer(layerId); }
  49. };
  50. ```
  51. **LayerState** objects keep track of the activeLayer.
  52. A LayerState's activeLayer is always up to date.
  53. ```
  54. class LayerState
  55. {
  56. int activeLayer;
  57. setActiveLayer(int layerId) { activeLayer = layerId; }
  58. getActiveLayer() { return activeLayer; }
  59. };
  60. ```
  61. **Key_LayeredKeys** objects contain arrays of keys, one key for each layer.
  62. Key_LayeredKeys objects use layerIds as array indexes.
  63. When a Key_LayeredKeys object is pressed, it gets the active layerId from LayerState, and sends the corresponding key.
  64. ```
  65. class Key_LayeredKeys
  66. {
  67. Key** ptrsKeys; //array of Key pointers, one Key pointer per layer
  68. LayerState& refLayerState;
  69. press() { layerId = refLayerState.getActiveLayer();
  70. ptrsKeys[layerId]->press(); }
  71. };
  72. ```
  73. Dependency diagram
  74. ```
  75. +------------+
  76. | Code_Layer |
  77. +------------+
  78. |
  79. |setActiveLayer()
  80. |
  81. v
  82. +------------+
  83. | LayerState |
  84. +------------+
  85. ^
  86. |
  87. |getActiveLayer()
  88. |
  89. +-----------------+
  90. | Key_LayeredKeys |
  91. +-----------------+
  92. ```
  93. Layer-scheme classes
  94. --------------------
  95. There are several layer scheme-classes to choose from.
  96. You can view all the class definitions in the [keybrd library](../src/).
  97. Code_Layer classes include:
  98. * Code_LayerHold
  99. * Code_LayerLock
  100. A basic LayerState class is:
  101. * LayerState
  102. Key_Layered classes include:
  103. * Key_LayeredKeys
  104. * Key_LayeredScSc (covered in next tutorial)
  105. * Key_LayeredCodeSc
  106. The association between Codes, Keys, and Rows
  107. ---------------------------------------------
  108. Codes, Keys, and Rows are associated by class compositions:
  109. ```
  110. Each Code object contains one scancode or one layercode.
  111. Each Key contains either
  112. * one Code object (single-layer)
  113. * multiple Code objects (multi-layer)
  114. * Key object (key nested in key)
  115. Each Row contains Key objects.
  116. ```
  117. You may have been wondering why Code pointers are in Key pointers arrays.
  118. You don't need to know the reasons to write a sketch.
  119. For the curious, two reasons are explained below.
  120. 1) Single-layer keys is the first reason you see Code pointers in a Key pointers array.
  121. Rows contain keys. The keys can be Single-layer or Multi-layer.
  122. Wrapping a code in a single-layer key before placing it a row is tedious.
  123. It is more convenient to place a code directly in a row.
  124. Codes are a kind of Key (polymorphism), so that rows can contain codes and keys.
  125. From keybrd_3a_multi-layerHold.ino:
  126. ```
  127. Key* const ptrsKeys_0[] = { &k_00, &k_01 };
  128. Row row_0(scanner, 0, ptrsKeys_0, keyCount_0);
  129. Key* const ptrsKeys_1[] = { &l_fn, &s_shift };
  130. Row row_1(scanner, 1, ptrsKeys_1, keyCount_1);
  131. ```
  132. row0's ptrsKeys_0[] array contains pointers to Keys.
  133. row1's ptrsKeys_1[] array contains pointers to Codes.
  134. 2) Sublayers (nested keys) is the second reason you see Code pointers in a Key pointers array.
  135. Layered keys usually contain just codes. When nesting keys, layered keys contain keys.
  136. Codes are a kind of Key (polymorphism), so that layered keys can contain both codes and keys.
  137. From keybrd_3d_sublayerNested.ino:
  138. ```
  139. Key* const ptrsKeys_sub00[] = { &s_minus, &s_1 };
  140. Key_LayeredKeys1 k_sub00(ptrsKeys_sub00);
  141. Key* const ptrsKeys_00[] = { &s_a, &k_sub00 };
  142. Key_LayeredKeys k_00(ptrsKeys_00);
  143. ```
  144. k_00's ptrsKeys_00[] array contains pointers to code s_a and key k_sub00.
  145. Exercises
  146. ---------
  147. 1) Compile and run [keybrd_3a_multi-layerHold.ino](keybrd_3a_multi-layerHold/keybrd_3a_multi-layerHold.ino)
  148. and [keybrd_3b_multi-layerLock.ino](keybrd_3b_multi-layerLock/keybrd_3b_multi-layerLock.ino).
  149. Notice how Code_LayerHold and Code_LayerLock objects behave.
  150. 2) Modify the keybrd_3a_multi-layerHold.ino sketch to make a 3-layer keyboard with two Code_LayerHold keys.
  151. | Layout | **0** | **1** |
  152. |:------:|:-----:|:-----:|
  153. | **0** | a - 1 | b = 2 |
  154. | **1** | sym | num |
  155. <br>
  156. <a rel="license" href="https://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://licensebuttons.net/l/by/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">keybrd tutorial</span> by <a xmlns:cc="https://creativecommons.org/ns" href="https://github.com/wolfv6/keybrd" property="cc:attributionName" rel="cc:attributionURL">Wolfram Volpi</a> is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.<br />Permissions beyond the scope of this license may be available at <a xmlns:cc="https://creativecommons.org/ns" href="https://github.com/wolfv6/keybrd/issues/new" rel="cc:morePermissions">https://github.com/wolfv6/keybrd/issues/new</a>.