Key | Key | ||||
|____ | |____ | ||||
| \ | | \ | ||||
| Key_LayeredKeys | |||||
| Key_LayeredKeysBase | |||||
| \____________________ | |||||
| / \ | |||||
| Key_LayeredKeys Key_LayeredKeys1 | |||||
| | | | ||||
|___________________________ | |___________________________ | ||||
| \ \ | | \ \ |
#include "Key_LayeredKeys.h" | #include "Key_LayeredKeys.h" | ||||
void Key_LayeredKeys::press() | |||||
uint8_t Key_LayeredKeys::getActiveLayer() | |||||
{ | { | ||||
layerId = refLayerState.getActiveLayer(); | |||||
ptrsKeys[layerId]->press(); | |||||
} | |||||
void Key_LayeredKeys::release() | |||||
{ | |||||
ptrsKeys[layerId]->release(); | |||||
return refLayerState.getActiveLayer(); | |||||
} | } |
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <LayerStateInterface.h> | #include <LayerStateInterface.h> | ||||
#include <Key.h> | #include <Key.h> | ||||
#include <Key_LayeredKeysBase.h> | |||||
/* 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. | |||||
When the key is pressed, active layerId is retreived from refLayerState and | |||||
the Key object of the active layerId is called. | |||||
/* Most of the Key_LayeredKeys functionality is in Key_LayeredKeysBase. | |||||
*/ | */ | ||||
class Key_LayeredKeys : public Key | |||||
class Key_LayeredKeys : public Key_LayeredKeysBase | |||||
{ | { | ||||
private: | private: | ||||
Key*const *const ptrsKeys; //array of Key pointers, one Key per layer | |||||
uint8_t layerId; //active layer when key was pressed | |||||
static LayerStateInterface& refLayerState; | static LayerStateInterface& refLayerState; | ||||
public: | public: | ||||
Key_LayeredKeys(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} | |||||
virtual void press(); | |||||
virtual void release(); | |||||
Key_LayeredKeys(Key* const ptrsKeys[]) : Key_LayeredKeysBase(ptrsKeys) {} | |||||
virtual uint8_t getActiveLayer(); //get active layer from refLayerState | |||||
}; | }; | ||||
#endif | #endif |
#include "Key_LayeredKeys1.h" | |||||
uint8_t Key_LayeredKeys1::getActiveLayer() | |||||
{ | |||||
return refLayerState.getActiveLayer(); | |||||
} |
#ifndef KEY_LAYEREDKEYS1_H | |||||
#define KEY_LAYEREDKEYS1_H | |||||
#include <Arduino.h> | |||||
#include <inttypes.h> | |||||
#include <LayerStateInterface.h> | |||||
#include <Key.h> | |||||
#include <Key_LayeredKeysBase.h> | |||||
/* Most of the Key_LayeredKeys1 functionality is in Key_LayeredKeysBase. | |||||
Key_LayeredKeys1 is just like Key_LayeredKeys, but with a distinct refLayerState. | |||||
Key_LayeredKeys1 and Key_LayeredKeys can have different size ptrsKeys[]. | |||||
*/ | |||||
class Key_LayeredKeys1 : public Key_LayeredKeysBase | |||||
{ | |||||
private: | |||||
static LayerStateInterface& refLayerState; | |||||
public: | |||||
Key_LayeredKeys1(Key* const ptrsKeys[]) : Key_LayeredKeysBase(ptrsKeys) {} | |||||
virtual uint8_t getActiveLayer(); //get active layer from refLayerState | |||||
}; | |||||
#endif |
#include "Key_LayeredKeysBase.h" | |||||
void Key_LayeredKeysBase::press() | |||||
{ | |||||
layerId = getActiveLayer(); | |||||
ptrsKeys[layerId]->press(); | |||||
} | |||||
void Key_LayeredKeysBase::release() | |||||
{ | |||||
ptrsKeys[layerId]->release(); | |||||
} |
#ifndef KEY_LAYEREDKEYSBASE_H | |||||
#define KEY_LAYEREDKEYSBASE_H | |||||
#include <Arduino.h> | |||||
#include <inttypes.h> | |||||
#include <LayerStateInterface.h> | |||||
#include <Key.h> | |||||
/* Class Key_LayeredKeysBase 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. | |||||
When the key is pressed, active layerId is retreived and the Key object of that layer is called. | |||||
Design note: | |||||
Child classes (Key_LayeredKeys and Key_LayeredKeys1) have distinct static refLayerState | |||||
and different size ptrsKeys[]. | |||||
That way Key_LayeredKeys instantiations don't need a LayerState argument, | |||||
which is significant for a sketch with 100 keys. | |||||
*/ | |||||
class Key_LayeredKeysBase : public Key | |||||
{ | |||||
private: | |||||
Key*const *const ptrsKeys; //array of Key pointers, one Key per layer | |||||
uint8_t layerId; //active layer when key was pressed | |||||
static LayerStateInterface& refLayerState; | |||||
public: | |||||
Key_LayeredKeysBase(Key* const ptrsKeys[]): ptrsKeys(ptrsKeys) {} | |||||
virtual uint8_t getActiveLayer()=0; | |||||
virtual void press(); | |||||
virtual void release(); | |||||
}; | |||||
#endif |