Key | Key | ||||
|____ | |____ | ||||
| \ | | \ | ||||
| Key_LayeredKeysArray | |||||
| Key_LayeredKeys | |||||
| | | | ||||
|___________________________ | |___________________________ | ||||
| \ \ | | \ \ | ||||
*Key_Layered* class names start with "Key_Layered" and end with a descriptive name. | *Key_Layered* class names start with "Key_Layered" and end with a descriptive name. | ||||
Example Key_Layered class names: | Example Key_Layered class names: | ||||
* Key_LayeredScSc | * Key_LayeredScSc | ||||
* Key_LayeredKeysArray | |||||
* Key_LayeredKeys | |||||
Style guide | Style guide | ||||
----------- | ----------- |
#include "Key_LayeredKeys.h" | |||||
void Key_LayeredKeys::press() | |||||
{ | |||||
layer = refLayerState.getActiveLayer(); | |||||
ptrsKeys[layer]->press(); | |||||
} | |||||
void Key_LayeredKeys::release() | |||||
{ | |||||
ptrsKeys[layer]->release(); | |||||
} |
#ifndef KEY_LAYEREDKEYSARRAY_H | |||||
#define KEY_LAYEREDKEYSARRAY_H | |||||
#ifndef KEY_LAYEREDKEYS_H | |||||
#define KEY_LAYEREDKEYS_H | |||||
#include <Arduino.h> | #include <Arduino.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <LayerStateInterface.h> | #include <LayerStateInterface.h> | ||||
#include <Key.h> | #include <Key.h> | ||||
/* Class Key_LayeredKeysArray contains an array of Key pointers, one pointer per layer. | |||||
/* Class Key_LayeredKeys contains an array of Key pointers, one pointer per layer. | |||||
Codes are a kind of Key, so the Key pointers can point to Codes or Keys. | Codes are a kind of Key, so the Key pointers can point to Codes or Keys. | ||||
When the key is pressed, active layer is retreived from refLayerState and | When the key is pressed, active layer is retreived from refLayerState and | ||||
the Key object of the active layer is called. | the Key object of the active layer is called. | ||||
*/ | */ | ||||
class Key_LayeredKeysArray : public Key | |||||
class Key_LayeredKeys : public Key | |||||
{ | { | ||||
private: | private: | ||||
Key*const *const ptrsKeys; //array of Key pointers, one Key per layer | Key*const *const ptrsKeys; //array of Key pointers, one Key per layer | ||||
uint8_t layer; //active layer when key was pressed | uint8_t layer; //active layer when key was pressed | ||||
static LayerStateInterface& refLayerState; | static LayerStateInterface& refLayerState; | ||||
public: | public: | ||||
Key_LayeredKeysArray(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} | |||||
Key_LayeredKeys(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} | |||||
virtual void press(); | virtual void press(); | ||||
virtual void release(); | virtual void release(); | ||||
}; | }; |
#include "Key_LayeredKeysArray.h" | |||||
void Key_LayeredKeysArray::press() | |||||
{ | |||||
layer = refLayerState.getActiveLayer(); | |||||
ptrsKeys[layer]->press(); | |||||
} | |||||
void Key_LayeredKeysArray::release() | |||||
{ | |||||
ptrsKeys[layer]->release(); | |||||
} |
#include <Code_Sc.h> | #include <Code_Sc.h> | ||||
#include <LayerState.h> | #include <LayerState.h> | ||||
#include <Code_LayerHold.h> | #include <Code_LayerHold.h> | ||||
#include <Key_LayeredKeysArray.h> | |||||
#include <Key_LayeredKeys.h> | |||||
//Matrix | //Matrix | ||||
#include <Row.h> | #include <Row.h> | ||||
/* =================== KEYS ==================== | /* =================== KEYS ==================== | ||||
Here we pack Codes into keys. | Here we pack Codes into keys. | ||||
The Key_LayeredKeysArray constructor takes one array of Code pointers - one Code object per layer. | |||||
The Key_LayeredKeys constructor takes one array of Code pointers - one Code object per layer. | |||||
The Key object names in this sketch start with a "k_" followed by row-column coordinates. | The Key object names in this sketch start with a "k_" followed by row-column coordinates. | ||||
*/ | */ | ||||
Key* const ptrsCodes_01[] = { &s_a, &s_1 }; | Key* const ptrsCodes_01[] = { &s_a, &s_1 }; | ||||
Key_LayeredKeysArray k_01(ptrsCodes_01); | |||||
Key_LayeredKeys k_01(ptrsCodes_01); | |||||
Key* const ptrsCodes_11[] = { &s_b, &s_2 }; | Key* const ptrsCodes_11[] = { &s_b, &s_2 }; | ||||
Key_LayeredKeysArray k_11(ptrsCodes_11); | |||||
Key_LayeredKeys k_11(ptrsCodes_11); | |||||
/* Key_LayeredKeysArray has a reference to layerState. | |||||
Thus Key_LayeredKeysArray can call layerState to get the active layer id. | |||||
/* Key_LayeredKeys has a reference to layerState. | |||||
Thus Key_LayeredKeys can call layerState to get the active layer id. | |||||
*/ | */ | ||||
LayerStateInterface& Key_LayeredKeysArray::refLayerState = layerState; | |||||
LayerStateInterface& Key_LayeredKeys::refLayerState = layerState; | |||||
/* HOW LAYERED OBJECTS WORK | /* HOW LAYERED OBJECTS WORK | ||||
When a Key_LayeredKeysArray object is pressed, it gets the active layer id from layerState. | |||||
When a Key_LayeredKeys object is pressed, it gets the active layer id from layerState. | |||||
It then uses the layer id as an array index to call the Code of the active layer. | It then uses the layer id as an array index to call the Code of the active layer. | ||||
The Code object then sends its scancode over USB. | The Code object then sends its scancode over USB. | ||||
*/ | */ |
#include <Code_Shift.h> | #include <Code_Shift.h> | ||||
#include <LayerState.h> | #include <LayerState.h> | ||||
#include <Code_LayerHold.h> | #include <Code_LayerHold.h> | ||||
#include <Key_LayeredKeysArray.h> | |||||
#include <Key_LayeredKeys.h> | |||||
//Matrix | //Matrix | ||||
#include <Row_uC.h> | #include <Row_uC.h> | ||||
// =================== KEYS ==================== | // =================== KEYS ==================== | ||||
Key* const ptrsCodes_01[] = { &s_a, &s_exclamation }; | Key* const ptrsCodes_01[] = { &s_a, &s_exclamation }; | ||||
Key_LayeredKeysArray k_01(ptrsCodes_01); | |||||
Key_LayeredKeys k_01(ptrsCodes_01); | |||||
Key* const ptrsCodes_11[] = { &s_b, &s_at }; | Key* const ptrsCodes_11[] = { &s_b, &s_at }; | ||||
Key_LayeredKeysArray k_11(ptrsCodes_11); | |||||
Key_LayeredKeys k_11(ptrsCodes_11); | |||||
LayerStateInterface& Key_LayeredKeysArray::refLayerState = layerState; | |||||
LayerStateInterface& Key_LayeredKeys::refLayerState = layerState; | |||||
// =================== ROWS ==================== | // =================== ROWS ==================== | ||||
Key* const ptrsKeys_0[] = { &s_shift, &k_01 }; | Key* const ptrsKeys_0[] = { &s_shift, &k_01 }; |
#include <Code_LEDLock.h> | #include <Code_LEDLock.h> | ||||
#include <LayerState_LED.h> | #include <LayerState_LED.h> | ||||
#include <Code_LayerHold.h> | #include <Code_LayerHold.h> | ||||
#include <Key_LayeredKeysArray.h> | |||||
#include <Key_LayeredKeys.h> | |||||
#include <Row_uC.h> | #include <Row_uC.h> | ||||
#include <ScanDelay.h> | #include <ScanDelay.h> | ||||
// =================== KEYS ==================== | // =================== KEYS ==================== | ||||
Key* const ptrsCodes_01[] = { &s_a, &s_1 }; | Key* const ptrsCodes_01[] = { &s_a, &s_1 }; | ||||
Key_LayeredKeysArray k_01(ptrsCodes_01); | |||||
Key_LayeredKeys k_01(ptrsCodes_01); | |||||
Key* const ptrsCodes_11[] = { &s_b, &s_2 }; | Key* const ptrsCodes_11[] = { &s_b, &s_2 }; | ||||
Key_LayeredKeysArray k_11(ptrsCodes_11); | |||||
Key_LayeredKeys k_11(ptrsCodes_11); | |||||
LayerStateInterface& Key_LayeredKeysArray::refLayerState = layerState; | |||||
LayerStateInterface& Key_LayeredKeys::refLayerState = layerState; | |||||
// =================== ROWS ==================== | // =================== ROWS ==================== | ||||
Key* const ptrsKeys_0[] = { &o_capsLock, &k_01 }; | Key* const ptrsKeys_0[] = { &o_capsLock, &k_01 }; |
The sketch uses three layer-scheme classes: | The sketch uses three layer-scheme classes: | ||||
* LayerState | * LayerState | ||||
* Code_LayerHold | * Code_LayerHold | ||||
* Key_LayeredKeysArray | |||||
* Key_LayeredKeys | |||||
The internal workings of these three classes are revealed in the next section. | The internal workings of these three classes are revealed in the next section. | ||||
}; | }; | ||||
``` | ``` | ||||
**Key_LayeredKeysArray** objects contain an array of keys, one key for each layer. | |||||
Key_LayeredKeysArray objects use layer ids as Key_LayeredKeysArray indexes. | |||||
When a Key_LayeredKeysArray object is pressed, it gets the active layer from LayerState, and sends the corresponding key. | |||||
**Key_LayeredKeys** objects contain an array of keys, one key for each layer. | |||||
Key_LayeredKeys objects use layer ids as Key_LayeredKeys indexes. | |||||
When a Key_LayeredKeys object is pressed, it gets the active layer from LayerState, and sends the corresponding key. | |||||
``` | ``` | ||||
class Key_LayeredKeysArray | |||||
class Key_LayeredKeys | |||||
{ | { | ||||
Key** ptrsKeys; //array of Key pointers, one Key pointer per layer | Key** ptrsKeys; //array of Key pointers, one Key pointer per layer | ||||
LayerState& refLayerState; | LayerState& refLayerState; | ||||
|getActiveLayer() | |getActiveLayer() | ||||
| | | | ||||
+----------------------+ | +----------------------+ | ||||
| Key_LayeredKeysArray | | |||||
| Key_LayeredKeys | | |||||
+----------------------+ | +----------------------+ | ||||
``` | ``` | ||||
Layer-scheme classes | Layer-scheme classes | ||||
* LayerState | * LayerState | ||||
Key_Layered classes include: | Key_Layered classes include: | ||||
* Key_LayeredKeysArray | |||||
* Key_LayeredKeys | |||||
* Key_LayeredScSc | * Key_LayeredScSc | ||||
* Key_LayeredCodeSc | * Key_LayeredCodeSc | ||||