2016-05-09 14:05:08 +00:00
Tutorial 3a - multi-layer keyboard
==================================
2016-07-18 02:03:03 +00:00
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 design.
2016-05-09 14:05:08 +00:00
## Multi-layer nomenclature
2016-07-18 02:03:03 +00:00
**[layers](http://deskthority.net/wiki/Layer)** - are key bindings provided by the keyboard firmware. For example,
2016-05-28 18:37:40 +00:00
* The classic [IBM PC keyboard ](http://en.wikipedia.org/wiki/IBM_PC_keyboard ) has one layer.
2016-05-09 14:05:08 +00:00
* Many compact keyboards have an additional [Fn layer ](http://en.wikipedia.org/wiki/Fn_key ).
* The [Neo layout ](http://neo-layout.org/index_en.html ) has 6 layers.
2016-05-28 18:37:40 +00:00
**layer id** - is an integer used to identify a layer.
2016-05-09 14:05:08 +00:00
**active layer** - is the layer currently used by the keyboard.
2016-07-18 02:03:03 +00:00
**layer scheme** - is a system for changing the active layer while typing (a single-layer scheme does not change layers).
## A simple multi-layer keybrd sketch
The [keybrd_3a_multi-layer.ino ](keybrd_3a_multi-layer/keybrd_3a_multi-layer.ino ) sketch is for a simple two-layer keyboard.
It will run on the basic breadboard keyboard described in [tutorial_1_breadboard_keyboard.md ](tutorial_1_breadboard_keyboard.md )
![basic breadboard keyboard ](keybrd_1_breadboard_images/breadboard_keyboard_2x2.JPG "basic breadboard keyboard" )
Read the sketch annotations to understand how multi-layer keyboards work.
The sketch uses three layer-scheme classes:
* LayerState
* Code_LayerHold
* Key_LayeredKeysArray
The internal workings of these three classes are revealed in the next section.
2016-05-09 14:05:08 +00:00
2016-05-10 15:05:25 +00:00
## Pseudo code for simple layer scheme
2016-07-18 02:03:03 +00:00
The following is pseudo code of three keybrd library classes.
It has just enough detail to show the internal workings of layer schemes.
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
**Key_Layer** objects change the active layer when pressed.
2016-05-28 18:37:40 +00:00
The "layer" variable is a layer id number.
2016-05-28 21:16:32 +00:00
When a Key_Layer object is pressed, it tells LayerState to update the active layer.
2016-05-10 15:05:25 +00:00
```
class Key_Layer
{
2016-07-18 02:03:03 +00:00
int layer;
LayerState& refLayerState;
press() { refLayerState.setActiveLayer(layer); }
};
2016-05-10 15:05:25 +00:00
```
2016-05-09 14:05:08 +00:00
2016-05-28 21:16:32 +00:00
**LayerState** objects keep track of the active layer.
A LayerState's activeLayer is always up to date.
2016-05-10 15:05:25 +00:00
```
2016-05-28 21:16:32 +00:00
class LayerState
2016-05-10 15:05:25 +00:00
{
2016-07-18 02:03:03 +00:00
int activeLayer;
setActiveLayer(int layer) { activeLayer = layer; }
getActiveLayer() { return activeLayer; }
};
2016-05-10 15:05:25 +00:00
```
2016-05-09 14:05:08 +00:00
2016-07-18 02:03:03 +00:00
**Key_LayeredKeysArray** objects contain an array of keys, one key for each layer.
Key_LayeredKeysArray use layer ids as array indexes to send the appropriate key.
When a Key_LayeredKeysArray object is pressed, it gets the active layer from LayerState, and sends the corresponding key.
2016-05-10 15:05:25 +00:00
```
2016-07-18 02:03:03 +00:00
class Key_LayeredKeysArray
2016-05-10 15:05:25 +00:00
{
2016-07-18 02:03:03 +00:00
Key** ptrsKeys; //array of Key pointers, one Key pointer per layer
LayerState& refLayerState;
press() { layer = refLayerState.getActiveLayer();
ptrsKeys[layer]->press(); }
};
2016-05-10 15:05:25 +00:00
```
2016-05-11 15:25:48 +00:00
Dependency diagram
```
2016-07-18 02:03:03 +00:00
+-----------+
| Key_Layer |
+-----------+
|
|setActiveLayer()
|
v
+------------+
| LayerState |
+------------+
^
|
|getActiveLayer()
|
+----------------------+
| Key_LayeredKeysArray |
+----------------------+
2016-05-11 15:25:48 +00:00
```
2016-05-10 15:05:25 +00:00
## Layer-scheme classes
There are several layer scheme-classes to choose from.
You can view all the class definitions in the [keybrd library ](../src/ ).
2016-05-28 18:37:40 +00:00
Key_Layer classes include:
2016-05-09 14:05:08 +00:00
* Code_LayerHold
* Code_LayerLock
2016-05-28 21:16:32 +00:00
A basic LayerState class is:
* LayerState
2016-05-10 15:05:25 +00:00
2016-05-28 18:37:40 +00:00
Key_Layered classes include:
2016-07-18 02:03:03 +00:00
* Key_LayeredKeysArray
2016-05-09 14:05:08 +00:00
* Code_LayeredScSc
* Code_LayeredCodeSc
* Code_LayeredCodeCode
## Single-layer Codes
2016-05-10 15:05:25 +00:00
Most Code objects only have one scancode or code.
2016-05-09 14:05:08 +00:00
Example single-layer Code classes include:
* Code_Sc
* Code_ScS
* Code_ScNS
* Code_Shift
* Code_LayerHold
* Code_LayerLock
## Exercises
2016-07-18 02:03:03 +00:00
1) Modify the keybrd_3_multi-layer.ino sketch to use two Code_LayerLock objects.
2016-05-09 14:05:08 +00:00
| Layout | **0** | **1** |
|:------:|--------|--------|
| **0** | a 1 | b 2 |
| **1** | layer0 | layer1 |
2016-05-11 15:25:48 +00:00
< a rel = "license" href = "http://creativecommons.org/licenses/by/4.0/" > < img alt = "Creative Commons License" style = "border-width:0" src = "https://i.creativecommons.org/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 = "http://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 = "http://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 = "http://creativecommons.org/ns#" href = "https://github.com/wolfv6/keybrd/issues/new" rel = "cc:morePermissions" > https://github.com/wolfv6/keybrd/issues/new< / a > .
2016-07-18 02:03:03 +00:00