keybrd library is an open source library for creating custom-keyboard firmware.
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.

tutorial_3ab_multi-layer_keyboard.md 5.4KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 PC 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 (in class LayerState, default layerId=0).
  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 code.
  17. Example single-layer Code classes include:
  18. * Code_Sc (used in keybrd_2_single-layer.ino)
  19. * Code_ScS
  20. * Code_ScNS
  21. * Code_Shift
  22. * Code_LayerHold
  23. * Code_LayerLock
  24. Single-layer keybrd sketches have one Code object per key.
  25. Multi-layer keybrd sketches have multiple Code objects per key, 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/breadboard_keyboard_2x2.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. 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 an array of keys, one key for each layer.
  62. Key_LayeredKeys objects use layerIds as Key_LayeredKeys 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. Exercises
  107. ---------
  108. 1) Compile and run [keybrd_3a_multi-layerHold.ino](keybrd_3a_multi-layerHold/keybrd_3a_multi-layerHold.ino)
  109. and [keybrd_3b_multi-layerLock.ino](keybrd_3b_multi-layerLock/keybrd_3b_multi-layerLock.ino).
  110. Notice how Code_LayerHold and Code_LayerLock objects behave.
  111. 2) Modify the keybrd_3a_multi-layerHold.ino sketch to make a 3-layer keyboard with two Code_LayerHold keys.
  112. | Layout | **0** | **1** |
  113. |:------:|:-----:|:-----:|
  114. | **0** | a - 1 | b = 2 |
  115. | **1** | sym | num |
  116. <br>
  117. <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>.