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_3a_multi-layer_keyboard.md 5.2KB

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
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. **layer scheme** - is a system for changing the active layer while typing (a single-layer scheme does not change layers).
  13. A simple multi-layer keybrd sketch
  14. ----------------------------------
  15. The [keybrd_3a_multi-layer.ino](keybrd_3a_multi-layer/keybrd_3a_multi-layer.ino) sketch is for a simple two-layer keyboard.
  16. It will run on the basic breadboard keyboard described in [tutorial_1_breadboard_keyboard.md](tutorial_1_breadboard_keyboard.md).
  17. ![basic breadboard keyboard](keybrd_1_breadboard/breadboard_keyboard_2x2.JPG "basic breadboard keyboard")
  18. Read the sketch annotations to understand how multi-layer keyboards work.
  19. The sketch uses three layer-scheme classes:
  20. * LayerState
  21. * Code_LayerHold
  22. * Key_LayeredKeysArray
  23. The internal workings of these three classes are revealed in the next section.
  24. Pseudo code for simple layer scheme
  25. -----------------------------------
  26. The following pseudo code is of three keybrd library classes.
  27. It has just enough detail to show the internal workings of layer schemes.
  28. **Key_Layer** objects change the active layer when pressed.
  29. The "layer" variable is a layer id number.
  30. When a Key_Layer object is pressed, it tells LayerState to update the active layer.
  31. ```
  32. class Key_Layer
  33. {
  34. int layer;
  35. LayerState& refLayerState;
  36. press() { refLayerState.setActiveLayer(layer); }
  37. };
  38. ```
  39. **LayerState** objects keep track of the active layer.
  40. A LayerState's activeLayer is always up to date.
  41. ```
  42. class LayerState
  43. {
  44. int activeLayer;
  45. setActiveLayer(int layer) { activeLayer = layer; }
  46. getActiveLayer() { return activeLayer; }
  47. };
  48. ```
  49. **Key_LayeredKeysArray** objects contain an array of keys, one key for each layer.
  50. Key_LayeredKeysArray use layer ids as array indexes.
  51. When a Key_LayeredKeysArray object is pressed, it gets the active layer from LayerState, and sends the corresponding key.
  52. ```
  53. class Key_LayeredKeysArray
  54. {
  55. Key** ptrsKeys; //array of Key pointers, one Key pointer per layer
  56. LayerState& refLayerState;
  57. press() { layer = refLayerState.getActiveLayer();
  58. ptrsKeys[layer]->press(); }
  59. };
  60. ```
  61. Dependency diagram
  62. ```
  63. +-----------+
  64. | Key_Layer |
  65. +-----------+
  66. |
  67. |setActiveLayer()
  68. |
  69. v
  70. +------------+
  71. | LayerState |
  72. +------------+
  73. ^
  74. |
  75. |getActiveLayer()
  76. |
  77. +----------------------+
  78. | Key_LayeredKeysArray |
  79. +----------------------+
  80. ```
  81. Layer-scheme classes
  82. --------------------
  83. There are several layer scheme-classes to choose from.
  84. You can view all the class definitions in the [keybrd library](../src/).
  85. Key_Layer classes include:
  86. * Code_LayerHold
  87. * Code_LayerLock
  88. A basic LayerState class is:
  89. * LayerState
  90. Key_Layered classes include:
  91. * Key_LayeredKeysArray
  92. * Code_LayeredScSc
  93. * Code_LayeredCodeSc
  94. * Code_LayeredCodeCode
  95. The basic LayerState provided by the keybrd library is sufficient for implementing ordinary layer schemes.
  96. For experimental layer schemes, you would need to create a custom LayerState class, and possibly Key_Layer and Key_Layered custom layer classes as well.
  97. Single-layer Codes
  98. ------------------
  99. Most Code objects only have one scancode or code.
  100. Example single-layer Code classes include:
  101. * Code_Sc
  102. * Code_ScS
  103. * Code_ScNS
  104. * Code_Shift
  105. * Code_LayerHold
  106. * Code_LayerLock
  107. Exercises
  108. ---------
  109. 1) Modify the keybrd_3_multi-layer.ino sketch to use two Code_LayerLock objects.
  110. | Layout | **0** | **1** |
  111. |:------:|:------:|:------:|
  112. | **0** | a 1 | b 2 |
  113. | **1** | layer0 | layer1 |
  114. <br>
  115. <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>.